Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

node-persisted-on-demand-coverage.test.ts

83 lines3.9 KB

1/**
2 * # Receiving witness: persisted demand coverage over better-sqlite3
3 *
4 * Law and authority: the receiving contract
5 * (`packages/db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract.ts`)
6 * states the law, owns the histories and their expected checkpoints, and runs
7 * the sessions. The law is `docs/contributing/oracle-reviews/2026-10-20-on-demand-network-first-law.md`;
8 * 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 better-sqlite3 host. It checks
10 * the same law over the real better-sqlite3 driver and
11 * `createNodeSQLitePersistence`, which the primary oracle does not exercise
12 * (it runs `node:sqlite` through a test driver).
13 *
14 * Histories: every history of `RECEIVING_HISTORIES`, each with its own
15 * database file in a temp directory. Session 1 is online and takes the
16 * history's steps. Session 2 is offline and opens a new connection to the same
17 * file. Its upstream load never answers, or rejects at once, as the history says.
18 *
19 * Production path: each session builds a `persistedCollectionOptions`
20 * Collection over `createNodeSQLitePersistence({ database })` and runs the
21 * query's preload. Session 1 closes its database before session 2 opens.
22 *
23 * Observations and checkpoints (session 2 only, read by the contract):
24 * - `beforeDeadline`: 100 ms after the upstream load is forwarded.
25 * - `afterDeadline`: after polling up to 2 500 ms for the query to leave
26 * `loading`.
27 * Each history's `expected` holds the outcome the law gives at both
28 * checkpoints. The positive witness is `upstreamCalls > 0`: the offline source
29 * was asked before any settlement.
30 *
31 * Mutant evidence (run in a temporary copy, see the coverage map): with
32 * `SESSION_2_WAIT_MS = 60_000` in the contract, the covered histories stay
33 * loading at `afterDeadline` and fail. With `covered-upstream-fails-at-once`
34 * changed to `never-answers`, that history stays loading and fails.
35 *
36 * Limits: one driver (better-sqlite3, file database, single process
37 * coordinator). It does not cover the browser OPFS driver (see the browser
38 * witness), a second process, or multi-tab coordination. The bookkeeping is
39 * observed only through the public query status and rows.
40 */
41import { mkdtempSync, rmSync } from 'node:fs'
42import { tmpdir } from 'node:os'
43import { join } from 'node:path'
44import { afterEach, describe, expect, it } from 'vitest'
45import BetterSqlite3 from 'better-sqlite3'
46import { createNodeSQLitePersistence } from '../src'
47import {
48 RECEIVING_HISTORIES,
49 coverageRecords,
50 recordReceivingHistory,
51} from '../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
52import { coverageViolations } from '../../db-sqlite-persistence-core/tests/persisted-demand-coverage-model-oracle'
53​
54const dirs: Array<string> = []
55​
56afterEach(() => {
57 for (const dir of dirs.splice(0))
58 rmSync(dir, { recursive: true, force: true })
59})
60​
61describe(`node better-sqlite3 persisted demand coverage (receiving witness)`, () => {
62 it.each(RECEIVING_HISTORIES)(`$description`, async (history) => {
63 const dir = mkdtempSync(join(tmpdir(), `node-pod-`))
64 dirs.push(dir)
65 const path = join(dir, `db.sqlite`)
66​
67 const { session1, session2 } = await recordReceivingHistory(history, () => {
68 const database = new BetterSqlite3(path)
69 return Promise.resolve({
70 persistence: createNodeSQLitePersistence({ database }),
71 close: () => {
72 database.close()
73 return Promise.resolve()
74 },
75 })
76 })
77 // Positive witness: session 2 asked the offline source for the demand.
78 expect(session2.sent.length).toBeGreaterThan(0)
79 const records = coverageRecords(history, session1, session2)
80 expect(coverageViolations(records.beforeDeadline)).toEqual([])
81 expect(coverageViolations(records.afterDeadline)).toEqual([])
82 })
83})