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

hydration-commit-oracle.opfs.spec.ts

151 lines6.2 KB

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