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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257// The store's replacement rules are client-only state: a daemon E2E sees the// events, not which exchange a late reply may overwrite. Compile the real// store and stub only its HTTP calls.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 class ApiError extends Error { constructor(status, code, message) { super(message); this.status = status; this.code = code; } } export const calls = { ask: [], cancel: [], patch: [], create: [] }; let next = 0; const pending = new Map(); export function uuidv7() { return 'id-' + (++next); } export function resetIds() { next = 0; pending.clear(); calls.ask = []; calls.cancel = []; calls.patch = []; calls.create = []; } export function hold(id) { let release; pending.set(id, new Promise((resolve) => (release = resolve))); return release; } export async function askSideQuestion(session, id, question) { calls.ask.push({ session, id, question }); if (pending.has(id)) return pending.get(id); return { id, question, status: 'running', answer: null, error: null, asked_at: 1, continuation_session_id: null, dismissed: false }; } export async function cancelSideQuestion(session, id) { calls.cancel.push({ session, id }); return { id, question: 'q', status: 'cancelled', answer: null, error: null, asked_at: 1, continuation_session_id: null, dismissed: false }; } const patchErrors = []; export function failNextPatch(error) { patchErrors.push(error); } export async function patchSideQuestion(session, id, dismissed) { calls.patch.push({ session, id, dismissed }); if (patchErrors.length) throw patchErrors.shift(); if (pending.has('patch:' + id + ':' + dismissed)) return pending.get('patch:' + id + ':' + dismissed); if (pending.has('patch:' + id)) return pending.get('patch:' + id); return { id, question: 'q', status: 'answered', answer: 'a', error: null, asked_at: 1, continuation_session_id: null, dismissed }; } export async function continueSideQuestion(source, question, created) { calls.create.push({ source, question, created }); if (calls.create.length === 1) throw new ApiError(503, 'side_question_unavailable', 'daemon busy'); return { id: created }; }`);const api = await import(apiURL);const source = await readFile( new URL('../src/lib/side-questions.svelte.ts', import.meta.url), 'utf8',);let code = compileModule(stripTypeScriptTypes(source, { mode: 'transform' }), { filename: 'side-questions.svelte.js', generate: 'client',}).js.code;code = code.replace( /from (['"])(.*?)\1/g, (_, _quote, path) => `from ${JSON.stringify(path === './api' ? apiURL : import.meta.resolve(path))}`,);const { SideQuestions } = await import(moduleURL(code));
const exchange = (id, status = 'answered', extra = {}) => ({ id, question: `q-${id}`, status, answer: status === 'answered' ? 'a' : null, error: null, asked_at: 1, continuation_session_id: null, dismissed: false, ...extra,});
test('reset then events, a replaced id, and a reset to null', () => { const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'running')); assert.equal(store.exchange.id, 'one'); assert.equal(store.visible, true); store.applyEvent(exchange('two')); assert.equal(store.exchange.id, 'two'); store.applyEvent({ nope: true }); assert.equal(store.exchange.id, 'two'); store.applyReset(null); assert.equal(store.exchange, null); assert.equal(store.reopen(), false);});
test('a late ask reply does not replace a newer exchange', async () => { api.resetIds(); const store = new SideQuestions('s1'); const release = api.hold('id-1'); const asking = store.ask('first'); store.applyEvent(exchange('other')); release(exchange('id-1', 'running')); await asking; assert.equal(store.exchange.id, 'other');});
test('dismissing a running question cancels it, patches dismissed, and reopen clears it', async () => { api.resetIds(); const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'running')); await store.dismiss(); assert.equal(store.visible, false); assert.equal(store.exchange.dismissed, true); assert.deepEqual(api.calls.cancel, [{ session: 's1', id: 'one' }]); assert.deepEqual(api.calls.patch, [ { session: 's1', id: 'one', dismissed: true }, ]); assert.equal(store.reopen(), true); assert.equal(store.visible, true); await Promise.resolve(); assert.deepEqual(api.calls.patch, [ { session: 's1', id: 'one', dismissed: true }, { session: 's1', id: 'one', dismissed: false }, ]);});
test('a repeated continue press retries the same created session id', async () => { api.resetIds(); const store = new SideQuestions('s1'); store.applyReset(exchange('one')); const opened = []; await store.continueInNewSession((id) => opened.push(id)); assert.equal(store.continueError, 'daemon busy'); assert.deepEqual(opened, []); await store.continueInNewSession((id) => opened.push(id)); assert.equal(api.calls.create.length, 2); assert.equal(api.calls.create[0].created, api.calls.create[1].created); assert.deepEqual(opened, [api.calls.create[0].created]); assert.equal( store.exchange.continuation_session_id, api.calls.create[0].created, );});
test('a terminal stream event wins over the HTTP admission response', async () => { api.resetIds(); const store = new SideQuestions('s1'); const release = api.hold('id-1'); const asking = store.ask('q'); store.applyEvent(exchange('id-1', 'answered')); release(exchange('id-1', 'running')); await asking; assert.equal(store.exchange.status, 'answered');});
test('a reset to null invalidates a pending admission response', async () => { api.resetIds(); const store = new SideQuestions('s1'); const release = api.hold('id-1'); const asking = store.ask('q'); store.applyReset(null); release(exchange('id-1', 'running')); await asking; assert.equal(store.exchange, null);});
test('a cancellation event keeps the dismissed card hidden', async () => { api.resetIds(); const store = new SideQuestions('s1'); store.applyEvent(exchange('one', 'running')); const releasePatch = api.hold('patch:one'); const dismissing = store.dismiss(); store.applyEvent(exchange('one', 'cancelled', { dismissed: false })); assert.equal(store.visible, false); releasePatch(exchange('one', 'cancelled', { dismissed: true })); await dismissing; assert.equal(store.visible, false);});
test('reset with dismissed true stays hidden on a newly mounted store', () => { const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'answered', { dismissed: true })); assert.equal(store.exchange.id, 'one'); assert.equal(store.visible, false);});
test('dismissing an answered card patches without cancelling and syncs via stream events', async () => { api.resetIds(); const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'answered')); await store.dismiss(); assert.deepEqual(api.calls.cancel, []); assert.deepEqual(api.calls.patch, [ { session: 's1', id: 'one', dismissed: true }, ]); const peer = new SideQuestions('s1'); peer.applyReset(exchange('one', 'answered')); assert.equal(peer.visible, true); peer.applyEvent(exchange('one', 'answered', { dismissed: true })); assert.equal(peer.visible, false); peer.applyEvent(exchange('one', 'answered', { dismissed: false })); assert.equal(peer.visible, true);});
test('dismissing before ask settles works both before and after SSE arrives without duplicate patches', async () => { api.resetIds(); const store = new SideQuestions('s1'); const release = api.hold('id-1'); const asking = store.ask('q'); await store.dismiss(); release(exchange('id-1', 'running')); await asking; assert.equal(store.visible, false); assert.equal(api.calls.cancel.length, 1); assert.equal(api.calls.patch.length, 1);
api.resetIds(); const sseFirst = new SideQuestions('s1'); const releaseSecond = api.hold('id-1'); const askingSecond = sseFirst.ask('q'); sseFirst.applyEvent(exchange('id-1', 'running')); await sseFirst.dismiss(); releaseSecond(exchange('id-1', 'running')); await askingSecond; assert.equal(sseFirst.visible, false); assert.equal(api.calls.cancel.length, 1); assert.equal(api.calls.patch.length, 1);});
test('racing stream event and immediate reopen after dismiss preserve order and server state', async () => { api.resetIds(); const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'running')); const releaseTrue = api.hold('patch:one:true'); const dismissing = store.dismiss(); store.applyEvent(exchange('one', 'answered', { dismissed: false })); assert.equal(store.visible, false); assert.equal(store.reopen(), true); assert.equal(store.visible, true); // Only the dismiss PATCH was dispatched so far; reopen queues behind it. assert.deepEqual(api.calls.patch, [ { session: 's1', id: 'one', dismissed: true }, ]); releaseTrue(exchange('one', 'answered', { dismissed: true })); await dismissing; await new Promise((resolve) => setTimeout(resolve, 0)); assert.equal(store.visible, true); assert.equal(store.exchange.dismissed, false); assert.deepEqual(api.calls.patch, [ { session: 's1', id: 'one', dismissed: true }, { session: 's1', id: 'one', dismissed: false }, ]);});
test('a failed dismiss PATCH keeps the card hidden and lets reopen recover', async () => { api.resetIds(); api.failNextPatch(new Error('offline')); const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'answered')); await store.dismiss(); assert.equal(store.visible, false); assert.equal(store.reopen(), true); assert.equal(store.visible, true); await new Promise((resolve) => setTimeout(resolve, 0)); assert.equal(api.calls.patch.length, 2);});