| 1 | import assert from "node:assert/strict"; |
| 2 | import { JSDOM } from "jsdom"; |
| 3 | import { installDesktopHostStub } from "./desktopHostStub"; |
| 4 | import { getTranscriptStore } from "../lib/transcriptStore"; |
| 5 | import { loadHistoryWindow, type HistoryWindowAction } from "../lib/historyWindowController"; |
| 6 | import { initialState, reducer, type State } from "../lib/useController"; |
| 7 | |
| 8 | const dom = new JSDOM("", { url: "http://localhost/" }); |
| 9 | globalThis.window = dom.window as unknown as Window & typeof globalThis; |
| 10 | |
| 11 | let revision = 1; |
| 12 | const stub = installDesktopHostStub({ |
| 13 | SessionHistoryWindowForTab: async () => ({ |
| 14 | status: "ready", |
| 15 | snapshotSequence: revision, |
| 16 | generation: `generation-${revision}`, |
| 17 | totalTurns: 30, |
| 18 | hasOlder: true, |
| 19 | olderCursor: "older-cursor", |
| 20 | messages: [{ messageId: `user-${revision}`, position: 30, version: 1, role: "user", eventSequence: 30, |
| 21 | visibleTurn: 30, preview: `revision ${revision}`, inline: { id: `user-${revision}`, role: "user", content: `revision ${revision}` } }], |
| 22 | }), |
| 23 | }); |
| 24 | |
| 25 | try { |
| 26 | const store = getTranscriptStore(); |
| 27 | for (const protocol of [1, 2] as const) { |
| 28 | revision = 1; |
| 29 | const tabId = `history-reload-tab-${protocol}`; |
| 30 | const sessionPath = `/fixture/history-reload-${protocol}.jsonl`; |
| 31 | const initial = await store.loadLatest(tabId, sessionPath); |
| 32 | assert.equal(initial?.revision, 1); |
| 33 | store.evictTab(tabId); |
| 34 | revision = 2; |
| 35 | let state: State = { |
| 36 | ...initialState, |
| 37 | transcriptProtocol: protocol, |
| 38 | historyStartTurn: initial!.startTurn, |
| 39 | historyTotalTurns: initial!.totalTurns, |
| 40 | historyHasOlder: true, |
| 41 | historyHasNewer: false, |
| 42 | historyOlderLoading: false, |
| 43 | historyNewerLoading: false, |
| 44 | historyRevision: 1, |
| 45 | historyDigest: "generation-1", |
| 46 | running: false, |
| 47 | meta: { label: "fixture", ready: true, eventChannel: "fixture", cwd: "/fixture", sessionPath, |
| 48 | sessionRevision: 1, sessionDigest: "generation-1" }, |
| 49 | }; |
| 50 | const actions: HistoryWindowAction[] = []; |
| 51 | const outcome = await loadHistoryWindow({ |
| 52 | tabId, direction: "older", trigger: "retry", state, requestSeq: 1, |
| 53 | isCurrent: () => true, |
| 54 | currentState: () => state, |
| 55 | dispatch: action => { |
| 56 | actions.push(action); |
| 57 | state = reducer(state, action); |
| 58 | }, |
| 59 | }); |
| 60 | assert.equal(outcome, "loaded", `protocol ${protocol}: refreshed page should replace an evicted old window: ${JSON.stringify(actions)}`); |
| 61 | assert.equal(actions[actions.length - 1]?.type, protocol === 2 ? "transcript_records" : "history_replace"); |
| 62 | assert.equal(actions.find(action => action.type === "history_older_error"), undefined); |
| 63 | assert.equal(state.historyRevision, 2); |
| 64 | assert.equal(state.historyDigest, "generation-2"); |
| 65 | assert.equal(state.historyOlderLoading, false); |
| 66 | assert.ok(state.items.some(item => item.kind === "user" && item.text === "revision 2")); |
| 67 | } |
| 68 | } finally { |
| 69 | stub.uninstall(); |
| 70 | dom.window.close(); |
| 71 | } |
| 72 |