import type { Meta, StoryObj } from '@storybook/react-vite'; import { expect } from 'storybook/test'; import { auth } from '../../src/api/client'; import { createRunEventSource } from '../../src/hooks/useRunNotifications'; function TransportExample() { return

Browser auth uses HttpOnly cookies for API and run-event requests.

; } const meta: Meta = { title: 'Authentication/Cookie transport', component: TransportExample, tags: ['autodocs'], }; export default meta; type Story = StoryObj; export const CookieCredentials: Story = { play: async () => { const originalFetch = globalThis.fetch; const localStorageLength = localStorage.length; let requestInit: RequestInit | undefined; globalThis.fetch = async (_input, init) => { requestInit = init; return new Response( JSON.stringify({ user: { id: 'synthetic-user-id', username: 'synthetic-operator', createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z', }, }), { status: 200, headers: { 'Content-Type': 'application/json' } }, ); }; try { const user = await auth.login('synthetic-operator', 'synthetic-password'); await expect(requestInit?.credentials).toBe('include'); await expect(user.username).toBe('synthetic-operator'); await expect(new Headers(requestInit?.headers).has('Authorization')).toBe(false); await expect(localStorage.length).toBe(localStorageLength); } finally { globalThis.fetch = originalFetch; } }, }; export const EventStreamIncludesCookieCredentials: Story = { play: async () => { const previous = Object.getOwnPropertyDescriptor(globalThis, 'EventSource'); let options: EventSourceInit | undefined; class StubEventSource { constructor(_url: string | URL, init?: EventSourceInit) { options = init; } close() {} } Object.defineProperty(globalThis, 'EventSource', { configurable: true, value: StubEventSource, }); try { createRunEventSource().close(); await expect(options?.withCredentials).toBe(true); } finally { if (previous) Object.defineProperty(globalThis, 'EventSource', previous); else Reflect.deleteProperty(globalThis, 'EventSource'); } }, };