Open History to see the last commit
1import { mkdtempSync, rmSync } from 'node:fs'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import { runRuntimeBridgeE2EContractSuite } from '../../db-sqlite-persistence-core/tests/contracts/runtime-bridge-e2e-contract'
5import { runElectronRuntimeBridgeScenario } from './e2e/electron-process-client'
6import type {
7 RuntimeBridgeE2EContractError,
8 RuntimeBridgeE2EContractHarness,
9 RuntimeBridgeE2EContractHarnessFactory,
10 RuntimeBridgeE2EContractTodo,
11} from '../../db-sqlite-persistence-core/tests/contracts/runtime-bridge-e2e-contract'
12import type {
13 ElectronRuntimeBridgeInput,
14 ElectronRuntimeBridgeScenarioResult,
15} from './e2e/fixtures/runtime-bridge-types'
16
17const createHarness: RuntimeBridgeE2EContractHarnessFactory = () => {
18 const tempDirectory = mkdtempSync(
19 join(tmpdir(), `db-electron-runtime-bridge-`),
20 )
21 const dbPath = join(tempDirectory, `state.sqlite`)
22 const collectionId = `todos`
23 let nextSequence = 1
24
25 const runScenario = async (
26 scenario: ElectronRuntimeBridgeInput[`scenario`],
27 ): Promise<ElectronRuntimeBridgeScenarioResult> =>
28 runElectronRuntimeBridgeScenario({
29 dbPath,
30 collectionId,
31 timeoutMs: 4_000,
32 scenario,
33 })
34
35 const harness: RuntimeBridgeE2EContractHarness = {
36 writeTodoFromClient: async (todo: RuntimeBridgeE2EContractTodo) => {
37 const result = await runScenario({
38 type: `writeTodo`,
39 todo,
40 txId: `tx-${nextSequence}`,
41 seq: nextSequence,
42 rowVersion: nextSequence,
43 })
44 nextSequence++
45
46 if (result.type !== `writeTodo`) {
47 throw new Error(`Unexpected write scenario result: ${result.type}`)
48 }
49 },
50 loadTodosFromClient: async (targetCollectionId?: string) => {
51 const result = await runScenario({
52 type: `loadTodos`,
53 collectionId: targetCollectionId,
54 })
55 if (result.type !== `loadTodos`) {
56 throw new Error(`Unexpected load scenario result: ${result.type}`)
57 }
58 return result.rows
59 },
60 loadUnknownCollectionErrorFromClient:
61 async (): Promise<RuntimeBridgeE2EContractError> => {
62 const result = await runScenario({
63 type: `loadUnknownCollectionError`,
64 collectionId: `missing`,
65 })
66 if (result.type !== `loadUnknownCollectionError`) {
67 throw new Error(`Unexpected error scenario result: ${result.type}`)
68 }
69 return result.error
70 },
71 restartHost: async () => {
72 const result = await runScenario({
73 type: `noop`,
74 })
75 if (result.type !== `noop`) {
76 throw new Error(`Unexpected restart scenario result: ${result.type}`)
77 }
78 },
79 cleanup: () => {
80 rmSync(tempDirectory, { recursive: true, force: true })
81 },
82 }
83
84 return harness
85}
86
87runRuntimeBridgeE2EContractSuite(
88 `electron runtime bridge e2e (real main/renderer IPC)`,
89 createHarness,
90 {
91 testTimeoutMs: 45_000,
92 },
93)