// A daemon E2E cannot catch the browser reading a nonexistent top-level value // and silently ignoring the confirmed configuration after a successful save. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = ( await readFile(new URL('../src/lib/api.ts', import.meta.url), 'utf8') ).replace(/^import type .*;\n/gm, ''); const javascript = stripTypeScriptTypes(source, { mode: 'transform' }); const api = await import( `data:text/javascript;base64,${Buffer.from(javascript).toString('base64')}` ); test('a configuration save returns the confirmed embedded value', async (t) => { const originalFetch = globalThis.fetch; t.after(() => { globalThis.fetch = originalFetch; }); const saved = { id: 'session', preferences: { pinned: true, archived: false }, }; const requests = []; globalThis.fetch = async (url, options) => { assert.equal(url, '/api/sessions/session/configuration'); requests.push(options.method ?? 'GET'); if (options.method === 'PATCH') { assert.equal(options.headers['if-match'], '"observed"'); assert.deepEqual(JSON.parse(options.body), { preferences: { pinned: true }, }); return Response.json({ resource: { etag: '"saved"', value: saved }, application: { desired_revision: 'desired', loaded_revision: null, needs_reload: true, }, }); } return Response.json({}, { headers: { etag: '"observed"' } }); }; assert.deepEqual( await api.configureSession( 'session', { preferences: { pinned: true } }, '"observed"', ), { etag: '"saved"', value: saved }, ); assert.deepEqual(requests, ['PATCH']); }); test('an observed stale edit is neither refreshed nor replayed', async (t) => { const originalFetch = globalThis.fetch; t.after(() => { globalThis.fetch = originalFetch; }); let requests = 0; globalThis.fetch = async (_url, options) => { requests++; assert.equal(options.method, 'PATCH'); assert.equal(options.headers['if-match'], '"stale"'); return Response.json( { code: 'precondition_failed', detail: 'changed' }, { status: 412 }, ); }; await assert.rejects( api.configureSession('session', { effort: 'high' }, '"stale"'), { status: 412 }, ); assert.equal(requests, 1); }); test('a lost compaction reply recovers the same identity and partial result', async (t) => { const originalFetch = globalThis.fetch; t.after(() => { globalThis.fetch = originalFetch; }); const calls = []; const result = { state: 'failed', selection_applied: true, effective_strategy: 'rolling', observation: null, failure: { code: 'compaction_failed', detail: 'provider unavailable' }, }; globalThis.fetch = async (url, options) => { calls.push({ url, method: options.method }); if (options.method === 'PUT') throw new TypeError('connection closed'); return Response.json({ id: url.split('/').at(-1), session_id: 'lost-action', state: 'completed', result, }); }; assert.deepEqual(await api.compactSession('lost-action', 'rolling'), result); assert.deepEqual( calls.map((call) => call.method), ['PUT', 'GET'], ); assert.equal(calls[0].url, calls[1].url); }); test('overlapping compaction callers share admission and retain mismatched receipts for recovery', async (t) => { const original = globalThis.fetch; t.after(() => { globalThis.fetch = original; }); const paths = []; globalThis.fetch = async (url) => { paths.push(url); return Response.json({ id: 'other', session_id: 'overlap', state: 'interrupted', result: null, }); }; const outcomes = await Promise.allSettled([ api.compactSession('overlap'), api.compactSession('overlap'), ]); assert.ok( outcomes.every( (result) => result.status === 'rejected' && result.reason.message.includes('Invalid compaction receipt'), ), ); assert.equal(paths.length, 1); globalThis.fetch = async (url) => { assert.equal(url, paths[0]); return Response.json({ code: 'identity_expired' }, { status: 409 }); }; await assert.rejects(api.compactSession('overlap'), /unknown outcome/); await assert.rejects(api.compactSession('overlap'), /unknown outcome/); });