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

hydration-commit.opfs.spec.ts

165 lines6.7 KB

1/**
2 * Harness self-test for the hydration-commit checkpoint: the fixture's cleanup
3 * failures must not replace the primary observation. The real-provider matrix below
4 * is in this file too.
5 */
6import { expect, test } from '@playwright/test'
7import type { HydrationCommitObservation } from './hydration-commit.opfs'
8​
9// ORC-010 calibration: two teardown failures cannot erase the independently
10// captured observation, mutate its receipt errors, or hide one another.
11test(`preserves the primary observation through multiple cleanup failures`, async ({
12 page,
13}) => {
14 await page.goto(
15 `/e2e/hydration-commit.opfs.html?phase=after-ready&baseline=false&coordinator=browser&rich=false&cleanupFault=true`,
16 )
17 await page.waitForFunction(() => window.__hydrationCommitDone === true)
18 const result = await page.evaluate(() => ({
19 observation: window.__hydrationCommitResult,
20 error: window.__hydrationCommitError,
21 cleanup: window.__hydrationCommitCleanupErrors,
22 }))
23 expect(result.error).toBeUndefined()
24 expect(result.observation?.cycleCalls).toBe(0)
25 expect(result.observation?.errors).toEqual([])
26 expect(result.observation?.reopenedRows).toEqual([
27 { id: `r1`, value: 1 },
28 { id: `tail`, value: 9 },
29 ])
30 expect(result.cleanup).toEqual([
31 `Error: injected first cleanup failure`,
32 `Error: injected last cleanup failure`,
33 ])
34})
35​
36/**
37 * Real-provider refinement of the law/driver in hydration-commit.opfs.ts.
38 * The insert history retains baseline+r1. The rich history replaces all rows,
39 * ends with r2=3, cursor=8 and r2 ownership metadata. Both append an ordinary
40 * tail after their receipt cut. Held histories additionally require scope exit. These literal expectations are a
41 * second formulation of the core edit-log oracle, not derived from its model.
42 * Peer B's schema/data and C's ordinary write must survive A's nested work.
43 * A cycle is an assertion at a reached causal boundary, never a timeout waiver.
44 * For on-demand A, the fixture follows its claimed physical storage ID. Its
45 * post-cleanup fresh claim checks durable rows, not offline Collection restore;
46 * a partial on-demand cache has no authority to restore public rows alone.
47 */
48// The edit-log model's rows after the receipt cut and the ordinary tail: the rich
49// history replaces all rows with r2; the insert history keeps r1 and an optional baseline.
50function expectedHydrationCommitRows(
51 baseline: boolean,
52 rich: boolean,
53): Array<{ id: string; value: number }> {
54 return rich
55 ? [
56 { id: `r2`, value: 3 },
57 { id: `tail`, value: 9 },
58 ]
59 : [
60 ...(baseline ? [{ id: `baseline`, value: 0 }] : []),
61 { id: `r1`, value: 1 },
62 { id: `tail`, value: 9 },
63 ]
65​
66for (const phase of [`startup`, `subscription`, `after-ready`] as const) {
67 for (const baseline of [false, true]) {
68 for (const coordinator of [`default`, `browser`] as const) {
69 for (const rich of [false, true]) {
70 test(`${phase}, baseline=${baseline}, coordinator=${coordinator}, rich=${rich}: preserves source and peer durability`, async ({
71 page,
72 }) => {
73 await page.goto(
74 `/e2e/hydration-commit.opfs.html?phase=${phase}&baseline=${baseline}&coordinator=${coordinator}&rich=${rich}`,
75 )
76 await page.waitForFunction(
77 () => window.__hydrationCommitDone === true,
78 )
79 const result = await page.evaluate(() => ({
80 observation: window.__hydrationCommitResult,
81 error: window.__hydrationCommitError,
82 cleanupErrors: window.__hydrationCommitCleanupErrors,
83 }))
84 const observed: HydrationCommitObservation | undefined =
85 result.observation
86 expect(
87 observed,
88 `fixture must reach its observation cut: ${JSON.stringify(result)}`,
89 ).toBeDefined()
90 if (!observed) return
91 expect(observed.rowReads).toBeGreaterThan(0)
92 if (phase === `subscription`) {
93 expect(observed.sourceStorageId).not.toBe(`a`)
94 } else {
95 expect(observed.sourceStorageId).toBe(`a`)
96 }
97 if (phase !== `after-ready`)
98 expect(observed.held).toEqual({
99 receipts: Array.from({ length: rich ? 6 : 1 }, () => `pending`),
100 rows: [],
101 ordinary: `pending`,
102 interleavedSql: [],
103 peerHydrationAdmitted: true,
104 ordinaryAdmitted: true,
105 })
106 expect(observed.cycleCalls, JSON.stringify(result)).toBe(0)
107 expect(observed.errors).toEqual([])
108 expect(
109 observed.ordinaryCommitSqlCalls,
110 `the ordinary SQL observer must be reached`,
111 ).toBe(1)
112 expect(
113 observed.tailScheduling,
114 `the tail must reenter ordinary scheduling exactly once`,
115 ).toEqual([
116 coordinator === `browser` ? `regular-scope` : `public-apply`,
117 ])
118 expect(
119 observed.peerWorkBeforeScopeExit,
120 `peer work cannot preempt the whole owning callback`,
121 ).toEqual([])
122 expect({
123 receipts: observed.receipts,
124 source: observed.sourceStatus,
125 peer: observed.peerStatus,
126 ordinary: observed.ordinaryReceipt,
127 exited: observed.owningScopeExited,
128 }).toEqual({
129 receipts: Array.from({ length: rich ? 7 : 2 }, () => `fulfilled`),
130 source: `ready`,
131 peer: `ready`,
132 ordinary: `fulfilled`,
133 exited: phase === `after-ready` ? null : true,
134 })
135 const expected = expectedHydrationCommitRows(baseline, rich)
136 expect(observed.rows).toEqual(expected)
137 expect(observed.durableRows).toEqual(expected)
138 expect(observed.reopenedRows).toEqual(expected)
139 expect(observed.durableMetadata).toEqual(
140 rich ? [{ key: `cursor`, value: 8 }] : [],
141 )
142 expect(observed.durableRowMetadata).toEqual(
143 rich ? [{ key: `r2`, metadata: { owner: `source` } }] : [],
144 )
145 expect(observed.peerRows).toEqual([
146 { id: `peer-baseline`, value: 22 },
147 ])
148 expect(observed.ordinaryRows).toEqual([{ id: `ordinary`, value: 33 }])
149 expect(observed.schemas).toEqual(
150 [
151 {
152 collection_id: observed.sourceStorageId,
153 schema_version: 11,
154 },
155 { collection_id: `b`, schema_version: 22 },
156 { collection_id: `c`, schema_version: 33 },
157 ].sort((a, b) => a.collection_id.localeCompare(b.collection_id)),
158 )
159 expect(result.error).toBeUndefined()
160 expect(result.cleanupErrors).toEqual([])
161 })
162 }
163 }
164 }