| 1 | import assert from "node:assert/strict"; |
| 2 | import React, { act } from "react"; |
| 3 | import { createRoot } from "react-dom/client"; |
| 4 | import { JSDOM } from "jsdom"; |
| 5 | import { useSessionExportCommands, type SessionExportFormat } from "../app-runtime/useSessionExportCommands"; |
| 6 | import { t } from "../lib/i18n"; |
| 7 | import { installDesktopHostStub } from "./desktopHostStub"; |
| 8 | |
| 9 | const dom = new JSDOM("<!doctype html><div id='root'></div>", { url: "http://localhost", pretendToBeVisual: true }); |
| 10 | Object.assign(globalThis, { |
| 11 | window: dom.window, |
| 12 | document: dom.window.document, |
| 13 | Element: dom.window.Element, |
| 14 | IS_REACT_ACT_ENVIRONMENT: true, |
| 15 | }); |
| 16 | |
| 17 | const hostExports: Array<{ tabId: string; format: string; title: string }> = []; |
| 18 | const finishes: string[] = []; |
| 19 | const stub = installDesktopHostStub({ |
| 20 | BeginSessionExportForTarget: async (_selector: unknown, tabId: string, format: string, title: string) => { |
| 21 | hostExports.push({ tabId, format, title }); |
| 22 | return { exportId: `export-${hostExports.length}`, format, snapshot: { title } }; |
| 23 | }, |
| 24 | FinishSessionExport: async (id: string) => { finishes.push(id); return { paths: ["/tmp/full.md"], records: 137, pages: 0 }; }, |
| 25 | CancelSessionExport: async () => {}, |
| 26 | SaveExportFile: async () => { throw new Error("resident exports are forbidden"); }, |
| 27 | }); |
| 28 | |
| 29 | let exportSession!: (format: SessionExportFormat) => Promise<void> | undefined; |
| 30 | function Probe({ remote }: { remote: boolean }) { |
| 31 | exportSession = useSessionExportCommands({ |
| 32 | tabId: "tab-export", |
| 33 | remote, |
| 34 | sessionTitle: "Bounded session", |
| 35 | items: [{ kind: "user", id: "u1", text: "resident projection" }], |
| 36 | live: undefined, |
| 37 | hasContent: true, |
| 38 | t, |
| 39 | showToast: () => {}, |
| 40 | }).exportSession; |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | const root = createRoot(document.getElementById("root")!); |
| 45 | try { |
| 46 | await act(async () => root.render(<Probe remote={false} />)); |
| 47 | await act(async () => { await exportSession("markdown"); }); |
| 48 | assert.deepEqual(hostExports, [{ tabId: "tab-export", format: "markdown", title: "Bounded session" }]); |
| 49 | assert.deepEqual(finishes, ["export-1"]); |
| 50 | await act(async () => root.render(<Probe remote />)); |
| 51 | await act(async () => { await exportSession("markdown"); }); |
| 52 | assert.equal(hostExports.length, 2, "remote export uses the same authoritative host API"); |
| 53 | assert.deepEqual(finishes, ["export-1", "export-2"]); |
| 54 | console.log("session export routing: both sources use fixed host snapshots"); |
| 55 | } finally { |
| 56 | await act(async () => root.unmount()); |
| 57 | stub.uninstall(); |
| 58 | dom.window.close(); |
| 59 | } |
| 60 |