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

runtime-restart-checker.test.ts

60 lines2.0 KB

1import { describe, expect, it } from 'vitest'
2import { assertRuntimeRestartHistory } from './contracts/runtime-bridge-e2e-contract'
3import type {
4 RuntimeBridgeE2EContractHarness,
5 RuntimeBridgeE2EContractTodo,
6} from './contracts/runtime-bridge-e2e-contract'
7​
8describe(`runtime restart history checker`, () => {
9 it.each([`none`, `score`, `id`, `extra-row`, `drop-next-write`] as const)(
10 `distinguishes %s from a faithful restart`,
11 async (fault) => {
12 const rows = new Map<string, RuntimeBridgeE2EContractTodo>()
13 let restarted = false
14 let faultHits = 0
15 const harness: RuntimeBridgeE2EContractHarness = {
16 writeTodoFromClient: (todo) => {
17 if (restarted && fault === `drop-next-write`) {
18 faultHits++
19 return Promise.resolve()
20 }
21 rows.set(todo.id, { ...todo })
22 return Promise.resolve()
23 },
24 loadTodosFromClient: () =>
25 Promise.resolve(
26 [...rows].map(([key, value]) => ({ key, value: { ...value } })),
27 ),
28 restartHost: () => {
29 restarted = true
30 const row = rows.get(`restart-1`)!
31 if (fault === `score`) {
32 faultHits++
33 row.score++
34 } else if (fault === `id`) {
35 faultHits++
36 row.id = `wrong-id`
37 } else if (fault === `extra-row`) {
38 faultHits++
39 rows.set(`phantom`, { id: `phantom`, title: `Extra`, score: 0 })
40 }
41 return Promise.resolve()
42 },
43 loadUnknownCollectionErrorFromClient: () =>
44 Promise.reject(new Error(`not part of the restart history`)),
45 cleanup: () => {},
46 }
47 if (fault === `none`) {
48 await assertRuntimeRestartHistory(harness)
49 expect(faultHits).toBe(0)
50 } else {
51 await expect(
52 assertRuntimeRestartHistory(harness),
53 ).rejects.toMatchObject({
54 name: `AssertionError`,
55 })
56 expect(faultHits).toBe(1)
57 }
58 },
59 )
60})