| 1 | import assert from "node:assert/strict"; |
| 2 | import test from "node:test"; |
| 3 | import { TranscriptStore } from "../lib/transcriptStore"; |
| 4 | import { initialState, reducer } from "../lib/useController"; |
| 5 | |
| 6 | test("durable user handoff survives actual store reclaim and reload", () => { |
| 7 | const store = new TranscriptStore({ HistorySliceForTab: async () => { throw new Error("unexpected slice read"); }, HistoryContentForTab: async () => { throw new Error("unexpected content read"); } }, { windowMaxPages: 1, windowPageEntries: 2 }); |
| 8 | const durable = { |
| 9 | entryId: "m:first", |
| 10 | turn: 1, |
| 11 | order: 0, |
| 12 | message: { role: "user" as const, content: "question", messageId: "first", submissionId: "submit-first" }, |
| 13 | refs: [], |
| 14 | }; |
| 15 | const installed = store.installSlice("tab-handoff", "/s/handoff.jsonl", { |
| 16 | entries: [durable], nextCursor: "", newerCursor: "", hasOlder: false, hasNewer: false, |
| 17 | startTurn: 1, endTurn: 1, totalTurns: 1, revision: 1, digest: "handoff-1", stale: false, |
| 18 | }); |
| 19 | let state = reducer({ ...initialState, transcriptProtocol: 2 }, { |
| 20 | type: "user", text: "question", seq: 0, submissionId: "submit-first", |
| 21 | }); |
| 22 | state = reducer(state, { type: "transcript_records", confirmedUsers: [], projection: { ...installed, removeIds: [] } }); |
| 23 | assert.equal(state.localSubmissionOrder.length, 0, "durable install retires the local submission echo"); |
| 24 | assert.equal(state.items.filter(item => item.kind === "user" && item.id === "m:first").length, 1, "durable install uses the canonical message id once"); |
| 25 | |
| 26 | const reclaimed = store.appendEntries("tab-handoff", "/s/handoff.jsonl", [ |
| 27 | { entryId: "m:next", turn: 2, order: 1, message: { role: "user", content: "next", messageId: "next" }, refs: [] }, |
| 28 | { entryId: "m:answer", turn: 2, order: 2, message: { role: "assistant", content: "answer", messageId: "answer" }, refs: [] }, |
| 29 | ])!; |
| 30 | assert.ok(reclaimed.removeIds.includes("m:first"), "the real bounded store reclaims the durable user row"); |
| 31 | state = reducer(state, { type: "transcript_records", confirmedUsers: [], projection: reclaimed }); |
| 32 | assert.equal(state.items.some(item => item.id === "m:first"), false, "store reclaim removes the canonical row without reviving its echo"); |
| 33 | assert.equal(state.localSubmissionOrder.length, 0, "store reclaim cannot restore a retired local echo"); |
| 34 | |
| 35 | const reloaded = store.installSlice("tab-handoff", "/s/handoff.jsonl", { |
| 36 | entries: [durable], nextCursor: "", newerCursor: "", hasOlder: false, hasNewer: false, |
| 37 | startTurn: 1, endTurn: 1, totalTurns: 1, revision: 2, digest: "handoff-2", stale: false, |
| 38 | }); |
| 39 | state = reducer(state, { type: "transcript_records", confirmedUsers: [], projection: { ...reloaded, removeIds: reclaimed.items.map(item => item.id) } }); |
| 40 | assert.equal(state.items.filter(item => item.kind === "user" && item.id === "m:first").length, 1, "reloading the reclaimed page restores exactly one durable user row"); |
| 41 | }); |
| 42 |