| 1 | // Native shell + bundled Go host; every home is disposable and synthetic. |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
| 4 | import { createHash } from "node:crypto"; |
| 5 | import { tmpdir } from "node:os"; |
| 6 | import path from "node:path"; |
| 7 | import { fileURLToPath } from "node:url"; |
| 8 | import { _electron as electron } from "playwright"; |
| 9 | |
| 10 | const executablePath = process.argv[2]; |
| 11 | assert(executablePath, "usage: node tagged-history-smoke.mjs <packaged executable> [versions...]"); |
| 12 | const versions = process.argv.slice(3); |
| 13 | if (!versions.length) for (let n = 3; n <= 11; n++) versions.push(`1.38.${n}`); |
| 14 | const fixtures = fileURLToPath(new URL("../../testdata/session-history-upgrade/", import.meta.url)); |
| 15 | const evidence = process.env.REASONIX_HISTORY_EVIDENCE || "/tmp/reasonix-tagged-package-evidence"; |
| 16 | mkdirSync(evidence, { recursive: true }); |
| 17 | const results = []; |
| 18 | const rpc = (page, command, ...args) => page.evaluate(({ command, args }) => window.reasonixDesktop.invoke(command, args), { command, args }); |
| 19 | const pause = () => new Promise(resolve => setTimeout(resolve, 100)); |
| 20 | |
| 21 | for (const version of versions) { |
| 22 | const fixture = path.join(fixtures, version); |
| 23 | const source = JSON.parse(readFileSync(path.join(fixture, "SOURCE.json"), "utf8")); |
| 24 | const home = mkdtempSync(path.join(tmpdir(), `reasonix-tagged-${version}-`)); |
| 25 | cpSync(path.join(fixture, "legacy"), path.join(home, "sessions"), { recursive: true }); |
| 26 | if (existsSync(path.join(fixture, "canonical"))) cpSync(path.join(fixture, "canonical"), path.join(home, "sessions-v4"), { recursive: true }); |
| 27 | const env = { ...process.env, REASONIX_HOME: home, REASONIX_STATE_HOME: home, REASONIX_CACHE_HOME: path.join(home, "cache") }; |
| 28 | delete env.REASONIX_DEV; |
| 29 | delete env.REASONIX_DESKTOP_SERVICE; |
| 30 | let shell; |
| 31 | async function launch() { |
| 32 | shell = await electron.launch({ executablePath, env, timeout: 60000 }); |
| 33 | const page = await shell.firstWindow(); |
| 34 | await page.waitForURL("reasonix://app/index.html", { timeout: 60000 }); |
| 35 | await page.locator(".app").waitFor({ state: "visible", timeout: 60000 }); |
| 36 | await page.addLocatorHandler(page.locator(".management-screen__back:visible"), async button => button.click()); |
| 37 | await page.locator(".sidebar__quick-action:visible").first().click({ trial: true, timeout: 60000 }); |
| 38 | return page; |
| 39 | } |
| 40 | try { |
| 41 | let page = await launch(); |
| 42 | const initial = await rpc(page, "ListHistoricalSessions"); |
| 43 | const count = Number(version.split(".")[2]) >= 8 ? 6 : 3; |
| 44 | assert.equal(initial.items.length, count, JSON.stringify(initial)); |
| 45 | assert(initial.items.every(item => !item.session), "startup must preserve explicit historical import"); |
| 46 | await rpc(page, "StartHistoricalImport", initial.items.map(item => item.id)); |
| 47 | let status; |
| 48 | const deadline = Date.now() + 120000; |
| 49 | do { await pause(); status = await rpc(page, "ListHistoricalSessions"); } while (status.running && Date.now() < deadline); |
| 50 | assert.equal(status.running, false, "historical import did not settle"); |
| 51 | assert.equal(status.failed, 0, JSON.stringify(status)); |
| 52 | assert.equal(status.blocked, 0, JSON.stringify(status)); |
| 53 | const refs = status.items.map(item => item.session); |
| 54 | assert(refs.every(Boolean), JSON.stringify(status)); |
| 55 | assert.equal(new Set(refs.map(ref => ref.sessionId)).size, count); |
| 56 | for (const ref of refs) { |
| 57 | const history = await rpc(page, "ReadSessionHistory", ref, "", 100); |
| 58 | assert(history.messages.some(message => /Answer 69|Independent branch work/.test(message.content)), "historical tail missing"); |
| 59 | const state = await rpc(page, "GetSessionComposerState", ref); |
| 60 | await rpc(page, "SaveSessionComposerState", { ref, expectedRevision: state.revision, contentVersion: 1, contentJson: JSON.stringify({ text: `unsent ${version} ${ref.sessionId}` }) }); |
| 61 | } |
| 62 | await shell.close(); shell = undefined; |
| 63 | page = await launch(); |
| 64 | for (const ref of refs) { |
| 65 | const restored = await rpc(page, "GetSessionComposerState", ref); |
| 66 | assert.equal(JSON.parse(restored.contentJson).text, `unsent ${version} ${ref.sessionId}`); |
| 67 | assert(!restored.historyChanged, "unchanged history should not require stale-input review"); |
| 68 | } |
| 69 | const snapshot = await rpc(page, "GetWorkspaceSnapshot"); |
| 70 | assert.deepEqual([...new Set(snapshot.workspaces.flatMap(workspace => workspace.sessionIds))].sort(), refs.map(ref => ref.sessionId).sort(), "restart must not create another session"); |
| 71 | assert.deepEqual(await rpc(page, "ListSessionDraftSummaries"), []); |
| 72 | for (const [name, expected] of Object.entries(source.files)) { |
| 73 | // Startup may refresh rebuildable display/index timestamps. The durable |
| 74 | // transcript, event log, metadata and content objects remain immutable. |
| 75 | if (name.endsWith(".display-index.json") || name.endsWith(".event-index.json")) continue; |
| 76 | const relative = name.startsWith("legacy/") ? name.replace("legacy/", "sessions/") : name.startsWith("canonical/") ? name.replace("canonical/", "sessions-v4/") : undefined; |
| 77 | if (!relative) continue; |
| 78 | assert.equal(createHash("sha256").update(readFileSync(path.join(home, relative))).digest("hex"), expected, `original source modified: ${name}`); |
| 79 | } |
| 80 | await page.screenshot({ path: path.join(evidence, `${version}.png`) }); |
| 81 | results.push({ version, commit: source.commit, home, sessions: count, refs, status: "passed" }); |
| 82 | writeFileSync(path.join(evidence, "results.json"), JSON.stringify({ executablePath, results }, null, 2)); |
| 83 | console.log(`PASS packaged ${version}: ${count} historical sessions, restart, unsent input, unchanged source`); |
| 84 | } catch (error) { |
| 85 | if (shell) await (await shell.firstWindow()).screenshot({ path: path.join(evidence, `${version}-failure.png`) }); |
| 86 | throw error; |
| 87 | } finally { if (shell) await shell.close(); } |
| 88 | } |
| 89 |