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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392// The MCP store's write policy, against the real store compiled with only// its HTTP calls stubbed. A daemon E2E cannot schedule a token refresh// between a read and a PATCH, so the 412 paths (spec §6.6) are tested here.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, message) { super(message); this.status = status; } } export const calls = { patches: [], reads: 0 }; export const replies = { patches: [], reads: [] }; export function reset() { calls.patches = []; calls.reads = 0; replies.patches = []; replies.reads = []; } export async function patchSettingsGroup(group, etag, body) { calls.patches.push({ etag, body }); const reply = replies.patches.shift(); if (typeof reply === 'function') return reply(); if (reply instanceof Error) throw reply; return reply; } export async function readSettingsGroup() { calls.reads++; const reply = replies.reads.shift(); return typeof reply === 'function' ? reply() : reply; } export async function mcpExtensionEnabled() { return true; } export function startLogin() { return new Promise(() => {}); } export function readLogin(id) { return Promise.reject(new Error('404 not_found')); } export async function cancelLogin() {} export async function answerLogin() {}`);const api = await import(apiURL);const markdownURL = moduleURL('export const safeHref = (href) => href;');const source = (file) => readFile(new URL(`../src/lib/settings/${file}`, import.meta.url), 'utf8');const resolveImports = (code, imports) => code.replace( /from (['"])(.*?)\1/g, (_, _quote, path) => `from ${JSON.stringify(imports.get(path) ?? import.meta.resolve(path))}`, );const compile = async (file) => compileModule( stripTypeScriptTypes(await source(file), { mode: 'transform' }), { filename: file.replace('.ts', '.js'), generate: 'client', }, ).js.code;const mcpURL = moduleURL( stripTypeScriptTypes(await source('mcp.ts'), { mode: 'transform' }),);const flowURL = moduleURL( resolveImports( await compile('sign-in-flow.svelte.ts'), new Map([ ['../api', apiURL], ['../markdown', markdownURL], ]), ),);const storage = new Map();globalThis.sessionStorage = { getItem: (key) => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, String(value)), removeItem: (key) => storage.delete(key),};const { mcp, RESUME_KEY } = await import( moduleURL( resolveImports( await compile('mcp.svelte.ts'), new Map([ ['../api', apiURL], ['./mcp', mcpURL], ['./sign-in-flow.svelte', flowURL], ]), ), ));const { draftFor } = await import(mcpURL);
const definition = (extra = {}) => ({ enabled: true, transport: 'http', url: 'https://x.example/mcp', command: null, arguments: [], cwd: null, bearer_token_env_var: null, headers: {}, environment: {}, enabled_tools: null, disabled_tools: [], startup_timeout_ms: 20000, call_timeout_ms: 60000, secret_presence: { bearer_token: false, headers: [], environment: [] }, auth: { state: 'none', account: null, expires_at: null, reason: null, step_up: null, client: null, }, ...extra,});const signedIn = (extra = {}) => definition({ auth: { ...definition().auth, state: 'signed_in', expires_at: '2026-11-13T00:00:00Z', }, ...extra, });const read = (definitions, etag) => ({ etag, value: { definitions } });const saved = (definitions, etag, compositionChanged = true) => ({ ...read(definitions, etag), warnings: [], compositionChanged, validation: 'passed',});const stale = () => new api.ApiError(412, 'settings changed');
/** Puts the store at a known list and ETag, as if just read. */function start(definitions, etag = 'e1') { api.reset(); mcp.dismiss(); mcp.servers = Object.entries(definitions).map(([name, d]) => ({ ...d, name, })); mcp.etag = etag; mcp.lastCheck = {};}
test('add retries once when a 412 was only auth churn elsewhere', async () => { start({ other: definition() }); api.replies.patches.push( stale(), saved({ other: signedIn(), linear: definition() }, 'e3'), ); api.replies.reads.push(read({ other: signedIn() }, 'e2')); const draft = { ...draftFor(), url: 'https://mcp.linear.app/mcp', name: 'linear', }; assert.equal(await mcp.add(draft), true); assert.deepEqual( api.calls.patches.map((p) => p.etag), ['e1', 'e2'], ); assert.equal(mcp.problem, null); assert.match(mcp.notice, /Added linear/);});
test('add says the name is taken when the 412 was that very name', async () => { start({}); api.replies.patches.push(stale()); api.replies.reads.push(read({ linear: definition() }, 'e2')); const draft = { ...draftFor(), url: 'https://mcp.linear.app/mcp', name: 'linear', }; assert.equal(await mcp.add(draft), false); assert.equal( api.calls.patches.length, 1, 'never overwrites the other server', ); assert.equal(mcp.problem.owner, 'new'); assert.match(mcp.problem.text, /just added somewhere else/); assert.equal( mcp.problem.probe, false, 'no "add without checking" for a conflict', );});
test('turning on retries an auth-only 412 (its own probe can cause one)', async () => { start({ s: definition({ enabled: false }) }); api.replies.patches.push(stale(), saved({ s: definition() }, 'e3')); api.replies.reads.push( read( { s: definition({ enabled: false, auth: { ...definition().auth, state: 'needs_sign_in' }, }), }, 'e2', ), ); await mcp.setEnabled('s', true); assert.equal(api.calls.patches.length, 2); assert.equal(mcp.problem, null); assert.equal(mcp.forceEnable, ''); assert.match(mcp.notice, /Turned s on/);});
test('turning off does not overwrite a real edit made elsewhere', async () => { start({ s: definition() }); api.replies.patches.push(stale()); api.replies.reads.push( read({ s: definition({ url: 'https://moved.example/mcp' }) }, 'e2'), ); await mcp.setEnabled('s', false); assert.equal(api.calls.patches.length, 1); assert.match(mcp.problem.text, /changed somewhere else/); assert.equal( mcp.server('s').url, 'https://moved.example/mcp', 'the card shows it as it is', );});
test('removing a server someone else already removed is just done', async () => { start({ s: definition() }); api.replies.patches.push(stale()); api.replies.reads.push(read({}, 'e2')); await mcp.remove('s'); assert.equal(api.calls.patches.length, 1, 'nothing re-created'); assert.equal(mcp.problem, null); assert.equal(mcp.notice, 'Removed s.');});
test('remove retries an auth-only 412', async () => { start({ s: definition() }); api.replies.patches.push(stale(), saved({}, 'e3')); api.replies.reads.push(read({ s: signedIn() }, 'e2')); await mcp.remove('s'); assert.equal(api.calls.patches.length, 2); assert.equal(mcp.notice, 'Removed s.');});
test('a second 412 is a conflict, never "could not connect"', async () => { const base = { ...definition(), name: 's' }; start({ s: definition() }); api.replies.patches.push(stale(), stale()); api.replies.reads.push( read({ s: signedIn() }, 'e2'), read({ s: signedIn() }, 'e3'), ); assert.equal(await mcp.save(draftFor(base), base), false); assert.equal(api.calls.patches.length, 2, 'retries once, then stops'); assert.match( mcp.problem.text, /changed somewhere else, so nothing was saved/, ); assert.equal(mcp.problem.probe, false);});
test('a failed probe on save offers saving without checking', async () => { const base = { ...definition(), name: 's' }; start({ s: definition() }); api.replies.patches.push( new api.ApiError(503, 'settings could not be published'), ); assert.equal(await mcp.save(draftFor(base), base), false); assert.equal(mcp.problem.owner, 's'); assert.equal(mcp.problem.probe, true);});
test('a save for a server removed elsewhere closes the form and says so', async () => { const base = { ...definition(), name: 's' }; start({ s: definition() }); api.replies.patches.push(stale()); api.replies.reads.push(read({}, 'e2')); assert.equal(await mcp.save(draftFor(base), base), true); assert.equal(mcp.problem.owner, ''); assert.match(mcp.problem.text, /removed somewhere else/);});
test('a disabled server is never "checked" (the daemon would not probe it)', async () => { start({ off: definition({ enabled: false }) }); await mcp.check('off'); assert.equal(api.calls.patches.length, 0); assert.equal(mcp.lastCheck.off, undefined);});
test('the no-op check retries a 412 and records the result', async () => { start({ s: definition() }); api.replies.patches.push(stale(), saved({ s: signedIn() }, 'e3', false)); api.replies.reads.push(read({ s: signedIn() }, 'e2')); await mcp.check('s'); assert.deepEqual( api.calls.patches.map((p) => p.body), [{ definitions: { s: {} } }, { definitions: { s: {} } }], ); assert.equal(mcp.lastCheck.s.ok, true);});
test('a refresh that started before a write does not undo it', async () => { start({ s: definition() }); let releaseRead; api.replies.reads.push( () => new Promise( (done) => (releaseRead = () => done(read({ s: definition() }, 'e1'))), ), ); const refreshing = mcp.refresh(); api.replies.patches.push(saved({}, 'e2')); await mcp.remove('s'); releaseRead(); await refreshing; assert.equal(mcp.etag, 'e2'); assert.equal( mcp.server('s'), undefined, 'the removed server does not come back', );});
test('two cards can write at once without clearing each other', async () => { start({ a: definition(), b: definition() }); let releaseA; api.replies.patches.push( () => new Promise( (done) => (releaseA = () => done( saved( { a: definition({ enabled: false }), b: definition() }, 'e2', ), )), ), saved( { a: definition({ enabled: false }), b: definition({ enabled: false }) }, 'e3', ), ); const a = mcp.setEnabled('a', false); const b = mcp.setEnabled('b', false); await b; assert.equal( mcp.isBusy('a'), true, "b finishing does not re-enable a's buttons", ); assert.equal(mcp.isBusy('b'), false); releaseA(); await a; assert.equal(mcp.isBusy('a'), false);});
test('notices clear on the next action', async () => { start({ s: definition() }); api.replies.patches.push(saved({ s: definition({ enabled: false }) }, 'e2')); await mcp.setEnabled('s', false); assert.match(mcp.notice, /Turned s off/); api.replies.patches.push(new api.ApiError(400, 'invalid settings candidate')); await mcp.setEnabled('s', true, true); assert.equal(mcp.notice, '');});
test('a notice drops the reload tail when nothing a session loads changed', async () => { start({ s: definition() }); api.replies.patches.push(saved({ s: definition() }, 'e2', false)); const base = { ...definition(), name: 's' }; await mcp.save(draftFor(base), base); assert.equal(mcp.notice, 'Saved s.');});
test('a corrupt resume entry is dropped, not thrown', () => { storage.set(RESUME_KEY, 'null'); assert.doesNotThrow(() => mcp.resume()); assert.equal(storage.has(RESUME_KEY), false);});
test('a resumed login that already ended is dropped with its entry', async () => { start({ s: definition() }); storage.set(RESUME_KEY, JSON.stringify({ name: 's', id: 'gone', url: null })); mcp.resume(); assert.equal(mcp.signIns.has('s'), true); // the first poll (after 1 s) finds the login gone await new Promise((done) => setTimeout(done, 1200)); assert.equal(mcp.signIns.has('s'), false, 'no "signing in" left behind'); assert.equal(storage.has(RESUME_KEY), false);});