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// 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: [], 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.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 }; } 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 }; } 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, ...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 and keeps the exchange', async () => { const store = new SideQuestions('s1'); store.applyReset(exchange('one', 'running')); await store.dismiss(); assert.equal(store.visible, false); assert.equal(store.exchange.status, 'cancelled'); assert.deepEqual(api.calls.cancel, [{ session: 's1', id: 'one' }]); assert.equal(store.reopen(), true); assert.equal(store.visible, true);});
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')); await store.dismiss(); store.applyEvent(exchange('one', 'cancelled')); assert.equal(store.visible, false);});