import test from 'node:test'; import assert from 'node:assert/strict'; import { createAnalysisSessionClient } from '../src/agent/sessionClient.ts'; const tick = () => new Promise(resolve => setImmediate(resolve)); const deferred = () => { let resolve, reject; const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); return { promise, resolve, reject }; }; function restoreFixture({ readList = async () => ({ items: [] }), readTurn } = {}) { const opened = []; const authorizationChecks = []; const client = createAnalysisSessionClient({ userId: 'restore-fixture', onUnauthorized: error => { authorizationChecks.push(error); return false; }, transport: { analysisSessions: readList, analysisTurn: readTurn, analysisSessionFollow: async (id, { onEvent }) => { opened.push(id); onEvent({ event: 'snapshot', data: { id, turns: [{ id: 'requested-turn', status: 'COMPLETED' }] } }); }, }, }); return { client, opened, authorizationChecks }; } test('replaying mount discards a stale turn lookup even before the replacement resolves', async t => { const oldTurn = deferred(); const currentTurn = deferred(); let requests = 0; const { client, opened, authorizationChecks } = restoreFixture({ readTurn: () => (++requests === 1 ? oldTurn.promise : currentTurn.promise), }); t.after(client.dispose); client.resume(); const first = client.openInitial({ turnId: 'requested-turn' }); client.suspend(); client.resume(); await tick(); assert.equal(requests, 2); oldTurn.reject(Object.assign(new Error('old request unavailable'), { status: 503 })); assert.equal(await first, false); assert.equal(client.getSnapshot().historyLoading, true); assert.equal(client.getSnapshot().historyError, ''); assert.deepEqual(authorizationChecks, []); currentTurn.resolve({ sessionId: 'current-session' }); await tick(); assert.equal(client.getSnapshot().historyLoading, false); assert.equal(client.getSnapshot().focusTurnId, 'requested-turn'); assert.deepEqual(opened, ['current-session']); }); test('replaying mount shares a list read and finishes loading an empty history', async t => { const list = deferred(); let requests = 0; const { client, opened } = restoreFixture({ readList: () => { requests++; return list.promise; } }); t.after(client.dispose); client.resume(); const first = client.openInitial(); client.suspend(); client.resume(); assert.equal(client.getSnapshot().historyLoading, true); assert.equal(await client.submit('must wait for selection'), false); list.resolve({ items: [] }); assert.equal(await first, false); await tick(); assert.equal(requests, 1); assert.equal(client.getSnapshot().historyLoading, false); assert.equal(client.getSnapshot().historyError, ''); assert.deepEqual(opened, []); }); test('a failed recent-session lookup can be retried without creating a conversation', async t => { let requests = 0; const { client, opened } = restoreFixture({ readList: async () => { if (++requests === 1) throw new Error('session list unavailable'); return { items: [{ id: 'recent' }] }; }, }); t.after(client.dispose); assert.equal(await client.openInitial(), false); assert.equal(client.getSnapshot().historyLoading, false); assert.equal(client.getSnapshot().historyError, 'session list unavailable'); assert.equal(await client.openInitial(), true); assert.equal(client.getSnapshot().historyError, ''); assert.equal(client.getSnapshot().sessionId, 'recent'); assert.deepEqual(opened, ['recent']); }); test('starting a new conversation invalidates a pending automatic selection', async t => { const list = deferred(); const { client, opened } = restoreFixture({ readList: () => list.promise }); t.after(client.dispose); const opening = client.openInitial(); client.newSession(); list.resolve({ items: [{ id: 'recent' }] }); assert.equal(await opening, false); assert.equal(client.getSnapshot().historyLoading, false); assert.equal(client.getSnapshot().sessionId, ''); assert.deepEqual(opened, []); });