import { describe, expect, test, afterEach, mock } from 'bun:test' import { createOrganization, getSession, OtpRateLimitError, pollDeviceToken, requestDeviceCode, sendVerificationOtp, setActiveOrganization, verifyOtp, } from './api-client' const BASE_URL = 'https://console-api.example.com' describe('requestDeviceCode', () => { const originalFetch = globalThis.fetch afterEach(() => { globalThis.fetch = originalFetch }) test('sends POST with client_id and returns device code response', async () => { const mockResponse = { device_code: 'dc-123', user_code: 'ABCD-EFGH', verification_uri: 'https://console.example.com/device', verification_uri_complete: 'https://console.example.com/device?code=ABCD-EFGH', expires_in: 600, interval: 5, } globalThis.fetch = mock(async () => new Response(JSON.stringify(mockResponse), { status: 200 }) ) as typeof fetch const result = await requestDeviceCode(BASE_URL) expect(result).toEqual(mockResponse) expect(globalThis.fetch).toHaveBeenCalledTimes(1) }) test('throws on non-OK response', async () => { globalThis.fetch = mock(async () => new Response('Bad Request', { status: 400 }) ) as typeof fetch await expect(requestDeviceCode(BASE_URL)).rejects.toThrow('Failed to request device code: 400') }) }) describe('pollDeviceToken', () => { const originalFetch = globalThis.fetch afterEach(() => { globalThis.fetch = originalFetch }) test('polls through authorization_pending then returns token', async () => { let callCount = 0 globalThis.fetch = mock(async () => { callCount++ if (callCount < 3) { return new Response(JSON.stringify({ error: 'authorization_pending' }), { status: 200 }) } return new Response(JSON.stringify({ access_token: 'tok-final' }), { status: 200 }) }) as typeof fetch // Use very short interval for fast test const token = await pollDeviceToken(BASE_URL, 'dc-123', 0.01, 10) expect(token).toBe('tok-final') expect(callCount).toBe(3) }) // Note: slow_down increases interval by 5s per RFC 8628. Testing the timing // would require waiting 5+ seconds which isn't practical in unit tests. // The behavior is verified structurally by the other polling tests. test('throws on access_denied', async () => { globalThis.fetch = mock(async () => new Response(JSON.stringify({ error: 'access_denied' }), { status: 200 }) ) as typeof fetch await expect(pollDeviceToken(BASE_URL, 'dc-123', 0.01, 10)).rejects.toThrow( 'Authorization denied by user.' ) }) test('throws on expired_token', async () => { globalThis.fetch = mock(async () => new Response(JSON.stringify({ error: 'expired_token' }), { status: 200 }) ) as typeof fetch await expect(pollDeviceToken(BASE_URL, 'dc-123', 0.01, 10)).rejects.toThrow( 'Device code expired' ) }) }) describe('getSession', () => { const originalFetch = globalThis.fetch afterEach(() => { globalThis.fetch = originalFetch }) test('returns session with user info', async () => { const session = { user: { id: 'u1', name: 'Test User', email: 'test@example.com' } } globalThis.fetch = mock(async () => new Response(JSON.stringify(session), { status: 200 }) ) as typeof fetch const result = await getSession(BASE_URL, 'tok-123') expect(result).toEqual(session) }) test('throws on 401', async () => { globalThis.fetch = mock(async () => new Response('Unauthorized', { status: 401 }) ) as typeof fetch await expect(getSession(BASE_URL, 'bad-token')).rejects.toThrow('Failed to get session: 401') }) }) describe('sendVerificationOtp', () => { const originalFetch = globalThis.fetch afterEach(() => { globalThis.fetch = originalFetch }) test('posts email + sign-in type', async () => { let captured: RequestInit | undefined globalThis.fetch = mock(async (_url: string, init: RequestInit) => { captured = init return new Response('{}', { status: 200 }) }) as unknown as typeof fetch await sendVerificationOtp(BASE_URL, 'new@example.com') expect(JSON.parse(String(captured?.body))).toEqual({ email: 'new@example.com', type: 'sign-in' }) }) test('throws OtpRateLimitError on 429', async () => { globalThis.fetch = mock(async () => new Response('Too Many Requests', { status: 429 })) as typeof fetch await expect(sendVerificationOtp(BASE_URL, 'new@example.com')).rejects.toBeInstanceOf(OtpRateLimitError) }) }) describe('verifyOtp', () => { const originalFetch = globalThis.fetch afterEach(() => { globalThis.fetch = originalFetch }) test('reads the bearer token from the set-auth-token header', async () => { globalThis.fetch = mock(async () => new Response(JSON.stringify({ user: { id: 'u1', email: 'new@example.com' } }), { status: 200, headers: { 'set-auth-token': 'bearer-xyz' }, }) ) as typeof fetch const result = await verifyOtp(BASE_URL, 'new@example.com', '123456') expect(result.token).toBe('bearer-xyz') expect(result.user.email).toBe('new@example.com') }) test('throws when no token header is returned', async () => { globalThis.fetch = mock(async () => new Response(JSON.stringify({ user: { id: 'u1', email: 'new@example.com' } }), { status: 200 }) ) as typeof fetch await expect(verifyOtp(BASE_URL, 'new@example.com', '123456')).rejects.toThrow('no auth token') }) }) describe('createOrganization / setActiveOrganization', () => { const originalFetch = globalThis.fetch afterEach(() => { globalThis.fetch = originalFetch }) test('createOrganization returns the new id', async () => { globalThis.fetch = mock(async () => new Response(JSON.stringify({ id: 'org-1' }), { status: 200 })) as typeof fetch const result = await createOrganization(BASE_URL, 'tok', { name: 'marc', slug: 'marc-ab12cd' }) expect(result.id).toBe('org-1') }) test('setActiveOrganization resolves on 200', async () => { globalThis.fetch = mock(async () => new Response('{}', { status: 200 })) as typeof fetch await expect(setActiveOrganization(BASE_URL, 'tok', 'org-1')).resolves.toBeUndefined() }) })