Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144// 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: [] }; export function reset() { calls.starts = []; calls.reads = []; calls.cancels = []; } 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() {}`);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', );});