// A daemon E2E cannot pause the browser between a sign-in request and its // reply, so it cannot see a stale reply open a popup, finish a cancelled // wizard step, or leave a login running. Compile the real sign-in flow and // stub only its HTTP calls, holding each reply until the test releases it. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; import { compileModule } from 'svelte/compiler'; const moduleURL = (code) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; const apiURL = moduleURL(` export const calls = { starts: [], reads: [], cancels: [], answers: [] }; export function reset() { calls.starts = []; calls.reads = []; calls.cancels = []; calls.answers = []; } function held(list, args) { let resolve, reject; const reply = new Promise((ok, fail) => { resolve = ok; reject = fail; }); list.push({ args, resolve, reject }); return reply; } export function startLogin(provider, flow) { return held(calls.starts, { provider, flow }); } export function readLogin(id) { return held(calls.reads, { id }); } export async function cancelLogin(id) { calls.cancels.push(id); } export async function answerLogin(id, response) { calls.answers.push({ id, response }); } `); const api = await import(apiURL); const markdownURL = moduleURL('export const safeHref = (href) => href;'); const imports = new Map([ ['../api', apiURL], ['../markdown', markdownURL], ]); const source = await readFile( new URL('../src/lib/settings/sign-in-flow.svelte.ts', import.meta.url), 'utf8', ); let code = compileModule(stripTypeScriptTypes(source, { mode: 'transform' }), { filename: 'sign-in-flow.svelte.js', generate: 'client', }).js.code; code = code.replace( /from (['"])(.*?)\1/g, (_, _quote, path) => `from ${JSON.stringify(imports.get(path) ?? import.meta.resolve(path))}`, ); const { SignInFlow } = await import(moduleURL(code)); const opened = []; globalThis.window = { open: (url) => opened.push(url) }; const realSetTimeout = globalThis.setTimeout; async function settle() { for (let i = 0; i < 12; i++) await new Promise((resolve) => setImmediate(resolve)); } function flow() { api.reset(); opened.length = 0; const done = []; const signIn = new SignInFlow( () => ({ id: 'claude', flows: ['browser', 'manual'] }), (accounts) => done.push(accounts), ); return { signIn, done }; } const login = (id) => ({ id, state: 'waiting', url: `https://sign.in/${id}`, instructions: '', }); // The poll interval is not under test; let it pass at once. test.beforeEach(() => { globalThis.setTimeout = (fn) => setImmediate(fn); }); test.afterEach(() => { globalThis.setTimeout = realSetTimeout; }); test('a login that starts after the panel closed is cancelled without opening its page', async () => { const { signIn, done } = flow(); signIn.start(); await settle(); signIn.stop(); api.calls.starts[0].resolve(login('late')); await settle(); assert.deepEqual(api.calls.cancels, ['late']); assert.deepEqual(opened, []); assert.equal( api.calls.reads.length, 0, 'a closed panel must not keep polling', ); assert.deepEqual(done, []); }); test('a sign-in that completes after Cancel does not finish the wizard', async () => { const { signIn, done } = flow(); signIn.start(); await settle(); api.calls.starts[0].resolve(login('first')); await settle(); assert.deepEqual(opened, ['https://sign.in/first']); signIn.stop(); api.calls.reads[0].resolve({ ...login('first'), state: 'complete', accounts: [{ id: 'account' }], }); await settle(); assert.deepEqual(done, []); assert.deepEqual(api.calls.cancels, ['first']); }); test('Try again after a lost poll cancels the earlier login before starting the next', async () => { const { signIn, done } = flow(); signIn.start(); await settle(); api.calls.starts[0].resolve(login('first')); await settle(); api.calls.reads[0].reject(new Error('network down')); await settle(); assert.equal(signIn.problem, 'network down'); signIn.start(); await settle(); assert.deepEqual(api.calls.cancels, ['first']); api.calls.starts[1].resolve(login('second')); await settle(); api.calls.reads[1].resolve({ ...login('second'), state: 'complete', accounts: [{ id: 'account' }], }); await settle(); assert.deepEqual(done, [[{ id: 'account' }]]); signIn.stop(); assert.deepEqual( api.calls.cancels, ['first'], 'a completed login is not cancelled on teardown', ); }); // resume(): a page reload mid-sign-in picks the login back up without // starting a new one, and a login that has since ended becomes a problem. test('resume follows an existing login to completion without starting one', async () => { const { signIn, done } = flow(); const running = signIn.resume('mid', 'https://sign.in/mid'); assert.deepEqual(api.calls.starts, [], 'resume must not start a login'); await settle(); assert.deepEqual( api.calls.reads.map((r) => r.args), [{ id: 'mid' }], ); api.calls.reads[0].resolve({ ...login('mid'), state: 'complete', accounts: [{ id: 'account' }], }); await settle(); assert.deepEqual(done, [[{ id: 'account' }]]); assert.equal(signIn.url, 'https://sign.in/mid'); await running; }); test('a resumed login takes a pasted address and is cancelled on Cancel', async () => { const { signIn } = flow(); void signIn.resume('mid', 'https://sign.in/mid'); await settle(); await signIn.answer('http://127.0.0.1:33418/callback?code=abc'); assert.deepEqual(api.calls.answers, [ { id: 'mid', response: 'http://127.0.0.1:33418/callback?code=abc' }, ]); signIn.stop(); assert.deepEqual(api.calls.cancels, ['mid'], 'the daemon listener is closed'); }); test('resume surfaces a login that already ended', async () => { const { signIn, done } = flow(); const running = signIn.resume('gone', null); await settle(); api.calls.reads[0].resolve({ ...login('gone'), state: 'expired', failure: { code: 'expired', detail: 'the sign-in expired' }, }); await settle(); assert.match(signIn.problem, /expired/); assert.deepEqual(done, []); await running; }); test('resume survives a lost login (404) as a problem, not a crash', async () => { const { signIn, done } = flow(); const running = signIn.resume('missing', null); await settle(); api.calls.reads[0].reject(new Error('404 not_found')); await settle(); assert.match(signIn.problem, /404/); assert.deepEqual(done, []); await running; }); test('device instructions stay visible when polling reports progress', async () => { const { signIn } = flow(); const started = { ...login('device'), instructions: 'enter code ZY42' }; signIn.start(); await settle(); api.calls.starts[0].resolve(started); await settle(); api.calls.reads[0].resolve({ ...login('device'), progress: 'waiting for authorization', }); await settle(); assert.equal(signIn.instructions, started.instructions); assert.equal(signIn.progress, 'waiting for authorization'); signIn.stop(); });