|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { buildApprovalUrl, createAuthRequest, pollForKey } from './device-flow.js' |
| 3 | + |
| 4 | +const ENDPOINT = 'https://sim.test' |
| 5 | + |
| 6 | +function reply(status: number, body: unknown): Response { |
| 7 | + return new Response(JSON.stringify(body), { status }) as Response |
| 8 | +} |
| 9 | + |
| 10 | +const COMPLETE = { |
| 11 | + status: 'complete', |
| 12 | + key: { id: 'k1', apiKey: 'sim_abc' }, |
| 13 | + scope: 'platform', |
| 14 | + workspaceId: 'ws_1', |
| 15 | + workspaceBound: true, |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + vi.restoreAllMocks() |
| 20 | + vi.useRealTimers() |
| 21 | +}) |
| 22 | + |
| 23 | +/** Drives the poll loop without waiting out its real 2s interval. */ |
| 24 | +async function poll(responses: Array<() => Response>) { |
| 25 | + let call = 0 |
| 26 | + vi.spyOn(globalThis, 'fetch').mockImplementation(async () => responses[call++]()) |
| 27 | + vi.spyOn(globalThis, 'setTimeout').mockImplementation(((fn: () => void) => { |
| 28 | + fn() |
| 29 | + return 0 as unknown as NodeJS.Timeout |
| 30 | + }) as never) |
| 31 | + |
| 32 | + const auth = createAuthRequest() |
| 33 | + return { result: await pollForKey(ENDPOINT, auth), calls: () => call } |
| 34 | +} |
| 35 | + |
| 36 | +describe('pollForKey', () => { |
| 37 | + it('returns the key once the approval completes', async () => { |
| 38 | + const { result } = await poll([() => reply(200, COMPLETE)]) |
| 39 | + expect(result).toMatchObject({ apiKey: 'sim_abc', scope: 'platform', workspaceBound: true }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('keeps polling while the approval is pending', async () => { |
| 43 | + const { result, calls } = await poll([ |
| 44 | + () => reply(200, { status: 'pending' }), |
| 45 | + () => reply(200, { status: 'pending' }), |
| 46 | + () => reply(200, COMPLETE), |
| 47 | + ]) |
| 48 | + expect(calls()).toBe(3) |
| 49 | + expect(result.apiKey).toBe('sim_abc') |
| 50 | + }) |
| 51 | + |
| 52 | + it('retries a 5xx, because the server released the approval for a later poll', async () => { |
| 53 | + // The regression: treating every non-429 as terminal threw away an approval |
| 54 | + // the user had already granted in the browser. |
| 55 | + const { result } = await poll([ |
| 56 | + () => reply(500, { error: 'Failed to generate API key' }), |
| 57 | + () => reply(200, COMPLETE), |
| 58 | + ]) |
| 59 | + expect(result.apiKey).toBe('sim_abc') |
| 60 | + }) |
| 61 | + |
| 62 | + it('retries a same-second name conflict', async () => { |
| 63 | + const { result } = await poll([ |
| 64 | + () => reply(409, { error: 'A personal API key named "CLI (…)" already exists.' }), |
| 65 | + () => reply(200, COMPLETE), |
| 66 | + ]) |
| 67 | + expect(result.apiKey).toBe('sim_abc') |
| 68 | + }) |
| 69 | + |
| 70 | + it('retries a rate-limited poll', async () => { |
| 71 | + const { result } = await poll([() => reply(429, {}), () => reply(200, COMPLETE)]) |
| 72 | + expect(result.apiKey).toBe('sim_abc') |
| 73 | + }) |
| 74 | + |
| 75 | + it('survives a transport failure without ending the login', async () => { |
| 76 | + let call = 0 |
| 77 | + vi.spyOn(globalThis, 'fetch').mockImplementation(async () => { |
| 78 | + if (call++ === 0) throw new Error('ECONNRESET') |
| 79 | + return reply(200, COMPLETE) |
| 80 | + }) |
| 81 | + vi.spyOn(globalThis, 'setTimeout').mockImplementation(((fn: () => void) => { |
| 82 | + fn() |
| 83 | + return 0 as unknown as NodeJS.Timeout |
| 84 | + }) as never) |
| 85 | + |
| 86 | + const result = await pollForKey(ENDPOINT, createAuthRequest()) |
| 87 | + expect(result.apiKey).toBe('sim_abc') |
| 88 | + }) |
| 89 | + |
| 90 | + it('gives up on a deliberate refusal rather than spinning to the timeout', async () => { |
| 91 | + await expect( |
| 92 | + poll([() => reply(400, { error: 'verifier must be a base64url secret' })]) |
| 93 | + ).rejects.toThrow('verifier must be a base64url secret') |
| 94 | + }) |
| 95 | + |
| 96 | + it('gives up on a 403', async () => { |
| 97 | + await expect(poll([() => reply(403, { error: 'Forbidden' })])).rejects.toThrow('Forbidden') |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +describe('createAuthRequest', () => { |
| 102 | + it('mints a 43-character base64url request id, challenge, and secret', () => { |
| 103 | + const auth = createAuthRequest() |
| 104 | + for (const value of [auth.request, auth.challenge, auth.pollSecret]) { |
| 105 | + expect(value).toMatch(/^[A-Za-z0-9\-_]{43}$/) |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + it('uses a pairing alphabet with no look-alike characters', () => { |
| 110 | + // The code is compared across two screens; O/0 and I/1 would defeat that. |
| 111 | + for (let i = 0; i < 50; i++) { |
| 112 | + expect(createAuthRequest().pairing).toMatch( |
| 113 | + /^[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{4}$/ |
| 114 | + ) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + it('never puts the poll secret in the browser URL', () => { |
| 119 | + const auth = createAuthRequest() |
| 120 | + const url = buildApprovalUrl(ENDPOINT, auth, 'platform', 'ws_1') |
| 121 | + expect(url).toContain(encodeURIComponent(auth.challenge)) |
| 122 | + expect(url).not.toContain(auth.pollSecret) |
| 123 | + }) |
| 124 | +}) |
0 commit comments