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

cloudflare-do-runtime-bridge.e2e.test.ts

214 lines6.1 KB

1import { mkdtempSync, rmSync } from 'node:fs'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import { describe, expect, it } from 'vitest'
5import { runRuntimeBridgeE2EContractSuite } from '../../db-sqlite-persistence-core/tests/contracts/runtime-bridge-e2e-contract'
6import { postJson, startWranglerRuntime } from './fixtures/wrangler-runtime'
7import type {
8 RuntimeBridgeE2EContractError,
9 RuntimeBridgeE2EContractHarness,
10 RuntimeBridgeE2EContractHarnessFactory,
11 RuntimeBridgeE2EContractTodo,
12} from '../../db-sqlite-persistence-core/tests/contracts/runtime-bridge-e2e-contract'
13import type { WranglerRuntimeResponse } from './fixtures/wrangler-runtime'
14​
15function assertRuntimeError(
16 response: WranglerRuntimeResponse<unknown>,
17): RuntimeBridgeE2EContractError {
18 if (!response.ok) {
19 return response.error
20 }
21​
22 throw new Error(`Expected runtime call to fail, but it succeeded`)
24​
25function assertRuntimeSuccess<TPayload>(
26 response: WranglerRuntimeResponse<TPayload>,
27): TPayload | undefined {
28 if (response.ok) {
29 return response.rows
30 }
31​
32 throw new Error(`${response.error.name}: ${response.error.message}`)
34​
35const createHarness: RuntimeBridgeE2EContractHarnessFactory = () => {
36 const tempDirectory = mkdtempSync(join(tmpdir(), `db-cloudflare-do-e2e-`))
37 const persistPath = join(tempDirectory, `wrangler-state`)
38 const collectionId = `todos`
39 let nextSequence = 1
40 const runtimePromise = startWranglerRuntime({
41 persistPath,
42 })
43​
44 const harness: RuntimeBridgeE2EContractHarness = {
45 writeTodoFromClient: async (todo: RuntimeBridgeE2EContractTodo) => {
46 const runtime = await runtimePromise
47 const result = await postJson<void>(runtime, `/write-todo`, {
48 collectionId,
49 todo,
50 txId: `tx-${nextSequence}`,
51 seq: nextSequence,
52 rowVersion: nextSequence,
53 })
54 nextSequence++
55​
56 if (!result.ok) {
57 throw new Error(`${result.error.name}: ${result.error.message}`)
58 }
59 },
60 loadTodosFromClient: async (targetCollectionId?: string) => {
61 const runtime = await runtimePromise
62 const result = await postJson<
63 Array<{ key: string; value: RuntimeBridgeE2EContractTodo }>
64 >(runtime, `/load-todos`, {
65 collectionId: targetCollectionId ?? collectionId,
66 })
67 if (!result.ok) {
68 throw new Error(`${result.error.name}: ${result.error.message}`)
69 }
70 return result.rows ?? []
71 },
72 loadUnknownCollectionErrorFromClient:
73 async (): Promise<RuntimeBridgeE2EContractError> => {
74 const runtime = await runtimePromise
75 const result = await postJson<never>(
76 runtime,
77 `/load-unknown-collection-error`,
78 {
79 collectionId: `missing`,
80 },
81 )
82 if (result.ok) {
83 throw new Error(
84 `Expected unknown collection request to fail, but it succeeded`,
85 )
86 }
87 return result.error
88 },
89 restartHost: async () => {
90 const runtime = await runtimePromise
91 await runtime.restart()
92 },
93 cleanup: async () => {
94 try {
95 const runtime = await runtimePromise
96 await runtime.stop()
97 } finally {
98 rmSync(tempDirectory, { recursive: true, force: true })
99 }
100 },
101 }
102​
103 return harness
105​
106runRuntimeBridgeE2EContractSuite(
107 `cloudflare durable object runtime bridge e2e (wrangler local)`,
108 createHarness,
109 {
110 testTimeoutMs: 90_000,
111 },
113​
114describe(`cloudflare durable object schema mismatch behavior (wrangler local)`, () => {
115 it(`throws on schema mismatch in sync-absent mode`, async () => {
116 const tempDirectory = mkdtempSync(
117 join(tmpdir(), `db-cloudflare-do-local-mismatch-e2e-`),
118 )
119 const persistPath = join(tempDirectory, `wrangler-state`)
120 const collectionId = `todos`
121 let runtime = await startWranglerRuntime({
122 persistPath,
123 syncEnabled: false,
124 schemaVersion: 1,
125 collectionId,
126 })
127​
128 try {
129 const writeResult = await postJson<void>(runtime, `/write-todo`, {
130 collectionId,
131 txId: `tx-1`,
132 seq: 1,
133 rowVersion: 1,
134 todo: {
135 id: `local-1`,
136 title: `Local mode row`,
137 score: 10,
138 },
139 })
140 assertRuntimeSuccess(writeResult)
141 } finally {
142 await runtime.stop()
143 }
144​
145 runtime = await startWranglerRuntime({
146 persistPath,
147 syncEnabled: false,
148 schemaVersion: 2,
149 collectionId,
150 })
151 try {
152 const loadResult = await postJson<
153 Array<{ key: string; value: RuntimeBridgeE2EContractTodo }>
154 >(runtime, `/load-todos`, {
155 collectionId,
156 })
157 const runtimeError = assertRuntimeError(loadResult)
158 expect(runtimeError.message).toContain(`Schema version mismatch`)
159 } finally {
160 await runtime.stop()
161 rmSync(tempDirectory, { recursive: true, force: true })
162 }
163 }, 90_000)
164​
165 it(`resets collection on schema mismatch in sync-present mode`, async () => {
166 const tempDirectory = mkdtempSync(
167 join(tmpdir(), `db-cloudflare-do-sync-mismatch-e2e-`),
168 )
169 const persistPath = join(tempDirectory, `wrangler-state`)
170 const collectionId = `todos`
171 let runtime = await startWranglerRuntime({
172 persistPath,
173 syncEnabled: true,
174 schemaVersion: 1,
175 collectionId,
176 })
177​
178 try {
179 const writeResult = await postJson<void>(runtime, `/write-todo`, {
180 collectionId,
181 txId: `tx-1`,
182 seq: 1,
183 rowVersion: 1,
184 todo: {
185 id: `sync-1`,
186 title: `Sync mode row`,
187 score: 20,
188 },
189 })
190 assertRuntimeSuccess(writeResult)
191 } finally {
192 await runtime.stop()
193 }
194​
195 runtime = await startWranglerRuntime({
196 persistPath,
197 syncEnabled: true,
198 schemaVersion: 2,
199 collectionId,
200 })
201 try {
202 const loadResult = await postJson<
203 Array<{ key: string; value: RuntimeBridgeE2EContractTodo }>
204 >(runtime, `/load-todos`, {
205 collectionId,
206 })
207 const rows = assertRuntimeSuccess(loadResult) ?? []
208 expect(rows).toEqual([])
209 } finally {
210 await runtime.stop()
211 rmSync(tempDirectory, { recursive: true, force: true })
212 }
213 }, 90_000)
214})