Open History to see the last commit
1/**
2 * # Receiving witness: persisted demand coverage over the Capacitor driver
3 *
4 * Law and authority: the receiving contract
5 * (`packages/db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract.ts`)
6 * holds the histories and records the sessions. The law is
7 * `docs/contributing/oracle-reviews/2026-10-20-on-demand-network-first-law.md`,
8 * and the primary oracle `packages/db-sqlite-persistence-core/tests/persisted-demand-coverage-oracle.test.ts`
9 * owns it across its cells. This file is the Capacitor host. It checks the same law
10 * through the wrapper `createCapacitorSQLitePersistence`, over the better-sqlite3 stand-in in `tests/helpers/capacitor-sqlite-test-db.ts`.
11 *
12 * Histories: every history of `RECEIVING_HISTORIES`, each with its own database
13 * file. Session 1 is online and takes the history's steps. Session 2 is offline
14 * and opens a new driver on the same file, with its upstream as the history says.
15 *
16 * Production path: each session builds a `persistedCollectionOptions`
17 * Collection over `createCapacitorSQLitePersistence` and runs the query's preload. Session 1 closes
18 * its driver before session 2 opens.
19 *
20 * Observations: the records of both sessions, judged by the shared judge
21 * (`persisted-demand-coverage-model-oracle.ts`) at each session 2 checkpoint.
22 *
23 * Limits: It runs in this package's existing unit suite, which already exercises the wrapper over that stand-in. It does not run the Capacitor runtime on a device or simulator; the device e2e (`e2e/`, run with `test:e2e:ios` or `test:e2e:android`) needs an iOS simulator or an Android emulator, and `adb` is not installed here, so that path is not covered. The sessions are two driver instances in one process over one file, not an app restart.
24 */
25import { mkdtempSync, rmSync } from 'node:fs'
26import { tmpdir } from 'node:os'
27import { join } from 'node:path'
28import { afterEach, describe, expect, it } from 'vitest'
29import { createCapacitorSQLitePersistence } from '../src'
30import {
31 RECEIVING_HISTORIES,
32 coverageRecords,
33 recordReceivingHistory,
34} from '../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
35import { coverageViolations } from '../../db-sqlite-persistence-core/tests/persisted-demand-coverage-model-oracle'
36import { createCapacitorSQLiteTestDatabase } from './helpers/capacitor-sqlite-test-db'
37
38const dirs: Array<string> = []
39
40afterEach(() => {
41 for (const dir of dirs.splice(0))
42 rmSync(dir, { recursive: true, force: true })
43})
44
45const DESCRIBE = `capacitor persisted demand coverage (receiving witness)`
46
47describe(DESCRIBE, () => {
48 it.each(RECEIVING_HISTORIES)(
49 `$description`,
50 async (history) => {
51 const dir = mkdtempSync(join(tmpdir(), `capacitor-demand-coverage-`))
52 dirs.push(dir)
53 const filename = join(dir, `state.sqlite`)
54
55 const { session1, session2 } = await recordReceivingHistory(
56 history,
57 () => {
58 const database = createCapacitorSQLiteTestDatabase({ filename })
59 return Promise.resolve({
60 persistence: createCapacitorSQLitePersistence({ database }),
61 close: async () => {
62 await database.close()
63 },
64 })
65 },
66 )
67 // Positive witness: session 2 asked the offline source for the demand.
68 expect(session2.sent.length).toBeGreaterThan(0)
69 const records = coverageRecords(history, session1, session2)
70 expect(coverageViolations(records.beforeDeadline)).toEqual([])
71 expect(coverageViolations(records.afterDeadline)).toEqual([])
72 },
73 60_000,
74 )
75})