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

react-native-persisted-demand-coverage.e2e.test.ts

81 lines3.7 KB

1/**
2 * # Receiving witness: persisted demand coverage over the React Native 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 React Native host. It checks the same law
10 * through the wrapper `createReactNativeSQLitePersistence`, over the op-sqlite-shaped driver in `tests/helpers/op-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 `createReactNativeSQLitePersistence` 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: No device runs here. The helper backs the driver with better-sqlite3 unless a runtime factory is supplied (`TANSTACK_DB_MOBILE_SQLITE_FACTORY_MODULE`), in which case this witness uses that factory like the conformance suite. It does not run on a device or simulator, and it does not cover the native OP-SQLite binding unless a runtime factory is supplied. 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 { createReactNativeSQLitePersistence } from '../src'
30import { createOpSQLiteTestDatabase } from '../tests/helpers/op-sqlite-test-db'
31import {
32 RECEIVING_HISTORIES,
33 coverageRecords,
34 recordReceivingHistory,
35} from '../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
36import { coverageViolations } from '../../db-sqlite-persistence-core/tests/persisted-demand-coverage-model-oracle'
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 = `react native persisted demand coverage (receiving witness)`
46​
47describe(DESCRIBE, () => {
48 it.each(RECEIVING_HISTORIES)(
49 `$description`,
50 async (history) => {
51 const dir = mkdtempSync(join(tmpdir(), `rn-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 = createOpSQLiteTestDatabase({
59 filename,
60 resultShape: `statement-array`,
61 })
62 return Promise.resolve({
63 persistence: createReactNativeSQLitePersistence({
64 database,
65 arrayResultMode: `statement-results`,
66 }),
67 close: async () => {
68 await database.close()
69 },
70 })
71 },
72 )
73 // Positive witness: session 2 asked the offline source for the demand.
74 expect(session2.sent.length).toBeGreaterThan(0)
75 const records = coverageRecords(history, session1, session2)
76 expect(coverageViolations(records.beforeDeadline)).toEqual([])
77 expect(coverageViolations(records.afterDeadline)).toEqual([])
78 },
79 60_000,
80 )
81})