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

cloudflare-persisted-demand-coverage.e2e.test.ts

101 lines4.4 KB

1/**
2 * # Receiving witness: persisted demand coverage over Cloudflare Durable Objects
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 Cloudflare host: it checks the
10 * same law through the Durable Object persistence
11 * (`createCloudflareDOSQLitePersistence`) inside a real workerd runtime started
12 * by `wrangler dev --local --persist-to`. The primary oracle does not run workerd.
13 *
14 * Histories: every history of `RECEIVING_HISTORIES`. Each history has its own
15 * wrangler persist directory.
16 *
17 * Production path: the test starts wrangler and posts session 1 to the Durable
18 * Object (`/demand-session`, phase `session1`). The Durable Object runs the
19 * contract's recorder in `e2e/fixtures/demand-coverage-session.mjs` over its
20 * own storage. The test stops wrangler, starts it again over the same persist
21 * directory, and posts session 2 (phase `session2`).
22 *
23 * Observations: the session 1 source log, and the session 2 loads and two
24 * checkpoints, read inside the Durable Object.
25 *
26 * Limits: local workerd with the Durable Object's SQLite storage, one Durable
27 * Object name, and a process restart between the sessions. It does not cover
28 * deployed Cloudflare, the browser or React adapter, or multi-object
29 * coordination.
30 */
31import { mkdtempSync, rmSync } from 'node:fs'
32import { tmpdir } from 'node:os'
33import { join } from 'node:path'
34import { describe, expect, it } from 'vitest'
35import {
36 RECEIVING_HISTORIES,
37 coverageRecords,
38} from '../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
39import { coverageViolations } from '../../db-sqlite-persistence-core/tests/persisted-demand-coverage-model-oracle'
40import { postJson, startWranglerRuntime } from './fixtures/wrangler-runtime'
41import type {
42 Session1Record,
43 Session2Record,
44} from '../../db-sqlite-persistence-core/tests/contracts/persisted-demand-coverage-receiving-contract'
45​
46describe(`cloudflare durable object persisted demand coverage (wrangler local, receiving witness)`, () => {
47 it.each(RECEIVING_HISTORIES)(
48 `$description`,
49 async (history) => {
50 const tempDirectory = mkdtempSync(join(tmpdir(), `db-cloudflare-demand-`))
51 const persistPath = join(tempDirectory, `wrangler-state`)
52 try {
53 // Session 1 is online. It runs in one wrangler process, then stops.
54 const first = await startWranglerRuntime({ persistPath })
55 let session1Record: Session1Record | undefined
56 try {
57 const session1 = await postJson<{ record: Session1Record }>(
58 first,
59 `/demand-session`,
60 { phase: `session1`, history: history.id },
61 )
62 if (!session1.ok) {
63 throw new Error(`${session1.error.name}: ${session1.error.message}`)
64 }
65 session1Record = session1.rows?.record
66 } finally {
67 await first.stop()
68 }
69​
70 // Session 2 is a later process over the same persist directory.
71 const second = await startWranglerRuntime({ persistPath })
72 try {
73 const session2 = await postJson<{ record: Session2Record }>(
74 second,
75 `/demand-session`,
76 { phase: `session2`, history: history.id },
77 )
78 if (!session2.ok) {
79 throw new Error(`${session2.error.name}: ${session2.error.message}`)
80 }
81 const session2Record = session2.rows?.record
82 // Positive witness: session 2 asked the offline source.
83 expect(session2Record?.sent.length ?? 0).toBeGreaterThan(0)
84 expect(session1Record).toBeDefined()
85 const records = coverageRecords(
86 history,
87 session1Record as Session1Record,
88 session2Record as Session2Record,
89 )
90 expect(coverageViolations(records.beforeDeadline)).toEqual([])
91 expect(coverageViolations(records.afterDeadline)).toEqual([])
92 } finally {
93 await second.stop()
94 }
95 } finally {
96 rmSync(tempDirectory, { recursive: true, force: true })
97 }
98 },
99 90_000,
100 )
101})