Open History to see the last commit
1/**
2 * Renderer driver for the persisted demand coverage witness in
3 * `tests/persisted-demand-coverage.electron.e2e.test.ts`. It runs in a real
4 * Electron renderer and calls the real main process over real IPC through
5 * `createElectronSQLitePersistence`. The test runs the app twice: phase `1`
6 * is an online session that takes each history's steps, then changes the
7 * source. Phase `2` is a later, separate app launch over the same database
8 * file, with its upstream load answered as the history says.
9 *
10 * The histories, the expected checkpoints, and the two session runners are
11 * the receiving contract's
12 * (`packages/db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract.ts`).
13 * This driver only opens each history's main-process session and records what
14 * the runners observe; the test owns the assertions.
15 */
16import { createElectronSQLitePersistence } from '../../../src/renderer'
17import {
18 RECEIVING_HISTORIES,
19 recordSession1,
20 recordSession2,
21} from '../../../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
22import type {
23 ReceivingSession,
24 Session1Record,
25 Session2Record,
26} from '../../../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
27import type {
28 ElectronPersistenceInvoke,
29 ElectronPersistenceResponseEnvelope,
30} from '../../../src/protocol'
31
32type HistoryResult = {
33 name: string
34 error?: string
35 session1?: Session1Record
36 session2?: Session2Record
37}
38
39type Bridge = {
40 invoke: (channel: string, ...args: Array<unknown>) => Promise<unknown>
41}
42
43declare global {
44 interface Window {
45 demandBridge: Bridge
46 }
47}
48
49const bridge = window.demandBridge
50const invoke: ElectronPersistenceInvoke = (channel, request) =>
51 bridge.invoke(
52 channel,
53 request,
54 ) as Promise<ElectronPersistenceResponseEnvelope>
55
56// Opens the main-process session over one history's database file. The
57// renderer's persistence then reaches that file only through IPC.
58async function openSession(filename: string): Promise<ReceivingSession> {
59 await bridge.invoke(`demand:session`, filename)
60 return {
61 persistence: createElectronSQLitePersistence({ invoke }),
62 close: async () => {
63 await bridge.invoke(`demand:close-session`)
64 },
65 }
66}
67
68async function run(): Promise<void> {
69 const params = new URLSearchParams(location.search)
70 const phase = params.get(`phase`)
71 const dir = params.get(`dir`)!
72 const results: Array<HistoryResult> = []
73 for (const [index, history] of RECEIVING_HISTORIES.entries()) {
74 const filename = `${dir}/h${index}.sqlite`
75 try {
76 const { persistence, close } = await openSession(filename)
77 try {
78 if (phase === `1`) {
79 results.push({
80 name: history.id,
81 session1: await recordSession1(persistence, history),
82 })
83 } else {
84 results.push({
85 name: history.id,
86 session2: await recordSession2(persistence, history),
87 })
88 }
89 } finally {
90 await close()
91 }
92 } catch (error) {
93 results.push({
94 name: history.id,
95 error: error instanceof Error ? error.message : String(error),
96 })
97 }
98 }
99 await bridge.invoke(`demand:result`, JSON.stringify({ phase, results }))
100}
101
102void run()