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

shared-driver-fairness.opfs.spec.ts

140 lines5.3 KB

1/**
2 * Browser checkpoint assertions for the OPFS refinement. Expected public rows
3 * are rebuilt from the scenario IDs rather than from production output. The
4 * neutral case proves cold-query reach; the storm case checks K=1 at each
5 * hydrate completion and full FIFO completion of logical persists. This is the
6 * receiving witness for the Node fixture's held-BEGIN, cold-hydrate premise:
7 * the same public preload/core-adapter/driver path reaches a Chromium OPFS
8 * worker, then public rows and logical completions are checked after all work
9 * settles. Failure-before-checkpoint, semantic mismatch, driver cleanup, and
10 * OPFS cleanup remain distinct outcomes so setup or teardown cannot satisfy
11 * the scheduling laws.
12 */
13import { expect, test } from '@playwright/test'
14import type { Page } from '@playwright/test'
15import type { OPFSOracleResult } from './shared-driver-fairness.opfs'
16​
17async function readOracleResult(
18 page: Page,
19 mode: `neutral` | `storm`,
20): Promise<OPFSOracleResult> {
21 let rejectPageError!: (error: Error) => void
22 const pageError = new Promise<never>((_resolve, reject) => {
23 rejectPageError = reject
24 })
25 void pageError.catch(() => undefined)
26 const onPageError = (error: Error) => {
27 rejectPageError(
28 new Error(
29 `OPFS fairness page failed before publishing a result: ${error.message}`,
30 ),
31 )
32 }
33 page.on(`pageerror`, onPageError)
34​
35 try {
36 await page.goto(`/e2e/shared-driver-fairness.opfs.html?mode=${mode}`)
37 await Promise.race([
38 page.waitForFunction(
39 () => window.__tanstackDriverFairnessOracle !== undefined,
40 ),
41 pageError,
42 ])
43 return page.evaluate(() => window.__tanstackDriverFairnessOracle!)
44 } finally {
45 page.off(`pageerror`, onPageError)
46 }
48​
49function expectedHydratedCollections(scenarioId: string, count: number) {
50 return Array.from({ length: count }, (_, index) => ({
51 collectionId: `${scenarioId}-hydrate-${index}`,
52 rows: [
53 { id: `row-${index}-0`, value: index * 10 },
54 { id: `row-${index}-1`, value: index * 10 + 1 },
55 ],
56 }))
58​
59test(`real Chromium OPFS fixture reaches and cleans up cold hydration`, async ({
60 page,
61}) => {
62 const result = await readOracleResult(page, `neutral`)
63​
64 if (result.status !== `complete`) throw new Error(result.primaryFailure)
65 expect(result.provider).toBe(`Chromium OPFSCoopSyncVFS worker`)
66 expect(result.observation.admittedHydrateIds).toHaveLength(2)
67 // These are actual public Collection rows captured after preload, compared
68 // with seed values built independently by this browser assertion.
69 expect(result.observation.hydratedCollections).toEqual(
70 expectedHydratedCollections(`opfs-neutral-reach`, 2),
71 )
72 expect(
73 result.observation.rawDequeues.some((entry) =>
74 entry.sql.startsWith(`SELECT key, value, metadata, row_version FROM`),
75 ),
76 ).toBe(true)
77 expect(result.observation.cleanupFailures).toEqual([])
78 expect(result.opfsCleanupFailures).toEqual([])
79})
80​
81test(`real Chromium OPFS fixture bounds cold hydration and completes persists in FIFO order`, async ({
82 page,
83}) => {
84 const result = await readOracleResult(page, `storm`)
85​
86 if (result.status !== `complete`) throw new Error(result.primaryFailure)
87 expect(result.provider).toBe(`Chromium OPFSCoopSyncVFS worker`)
88 expect(result.observation.admittedHydrateIds).toHaveLength(4)
89 expect(result.observation.hydratedCollections).toEqual(
90 expectedHydratedCollections(`opfs-fixed-persist-storm`, 4),
91 )
92 expect(
93 result.observation.rawDequeues.some((entry) =>
94 entry.sql.startsWith(`SELECT key, value, metadata, row_version FROM`),
95 ),
96 ).toBe(true)
97 expect(
98 result.observation.hydrationCompletions.map(
99 (checkpoint) => checkpoint.collectionId,
100 ),
101 ).toEqual(
102 Array.from(
103 { length: 4 },
104 (_, index) => `opfs-fixed-persist-storm-hydrate-${index}`,
105 ),
106 )
107 expect(
108 result.observation.hydrationCompletions[0]?.completedPersistIds,
109 ).toEqual([`opfs-fixed-persist-storm-persist-0`])
110 expect(
111 result.observation.hydrationCompletions[1]?.pendingPersistCount,
112 ).toBeGreaterThanOrEqual(3)
113 // This is the semantic RED checkpoint. Setup, wall time, and cleanup are
114 // reported independently and cannot satisfy this assertion.
115 if (result.violation !== undefined) {
116 throw new Error(
117 `real OPFS fairness mismatch: ${JSON.stringify(result.violation)}; ` +
118 `logical completion order: ${JSON.stringify(result.observation.logicalCompletionOrder)}; ` +
119 `driver admissions: ${result.observation.driverAdmissions.length}; ` +
120 `raw dequeues: ${result.observation.rawDequeues.length}; ` +
121 `driver cleanup diagnostics: ${JSON.stringify(result.observation.cleanupFailures)}; ` +
122 `OPFS cleanup diagnostics: ${JSON.stringify(result.opfsCleanupFailures)}`,
123 )
124 }
125 // The Node oracle rejects a swapped second/third completion. At this real
126 // provider boundary, the public adapter's logical persist promises must
127 // complete in the admitted persist-lane order after all work settles.
128 expect(
129 result.observation.logicalCompletionOrder.filter((entry) =>
130 entry.startsWith(`persist:`),
131 ),
132 ).toEqual(
133 Array.from(
134 { length: 5 },
135 (_, index) => `persist:opfs-fixed-persist-storm-persist-${index}`,
136 ),
137 )
138 expect(result.observation.cleanupFailures).toEqual([])
139 expect(result.opfsCleanupFailures).toEqual([])
140})