Open History to see the last commit
1// Fixed examples of the persisted-readiness contract: an adapter-contract
2// violation, markReady() after fail(), a Collection without initialRender, the
3// default deadline, and option validation, asserted against literal values. The
4// readiness law, its model, and its generated histories are owned by
5// persisted-readiness-oracle.test.ts.
6import { createCollection } from '@tanstack/db'
7import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
8import { persistedCollectionOptions } from '../src/index.js'
9import {
10 DISK_ROW,
11 REMOTE_FAILURE,
12 TIMEOUT,
13 controlledAdapter,
14 networkFirst,
15} from './persisted-readiness-support.js'
16import type { Row } from './persisted-readiness-support.js'
17
18beforeEach(() => {
19 vi.useFakeTimers()
20})
21afterEach(() => {
22 vi.useRealTimers()
23})
24
25describe(`persisted readiness examples`, () => {
26 // The wrapper delivers the adapter's calls after startup, so a contract
27 // violation cannot throw back at the adapter. It crashes the Collection
28 // with the violation as its cause, as a throwing sync function does.
29 it.each([
30 {
31 violation: `reportError() before markReady()`,
32 calls: [`report`] as const,
33 cause: /reportError/,
34 },
35 ])(`crashes on $violation`, async ({ violation, calls, cause }) => {
36 const source = networkFirst(`contract-violation ${violation}`, false)
37 const consoleError = vi.spyOn(console, `error`).mockImplementation(() => {})
38 try {
39 void source.collection.preload().catch(() => undefined)
40 source.restore(`empty`)
41 await vi.advanceTimersByTimeAsync(0)
42 calls.forEach(source.call)
43 await vi.advanceTimersByTimeAsync(0)
44 expect(source.collection.status).toBe(`crashed`)
45 expect(source.collection.health.error?.message).toMatch(cause)
46 } finally {
47 consoleError.mockRestore()
48 await source.collection.cleanup({ dependents: true })
49 }
50 })
51
52 // A run's markReady() after its fail() does nothing (core API decision
53 // F18/F6): the stopped Collection stays ready with its error.
54 it(`ignores markReady() after fail() on a ready Collection`, async () => {
55 const source = networkFirst(`mark-ready-after-fail`, false)
56 try {
57 void source.collection.preload().catch(() => undefined)
58 source.restore(`empty`)
59 await vi.advanceTimersByTimeAsync(0)
60 for (const call of [`ready`, `fail`, `ready`] as const) source.call(call)
61 await vi.advanceTimersByTimeAsync(0)
62 expect({
63 status: source.collection.status,
64 error: source.collection.health.error?.message,
65 }).toEqual({ status: `ready`, error: REMOTE_FAILURE })
66 } finally {
67 await source.collection.cleanup({ dependents: true })
68 }
69 })
70
71 it(`keeps a Collection without initialRender on the network boundary`, async () => {
72 const source = networkFirst(`no-initial-render`, false)
73 try {
74 void source.collection.preload().catch(() => undefined)
75 source.restore(`rows`)
76 await vi.advanceTimersByTimeAsync(TIMEOUT * 10)
77 expect(source.collection.status).toBe(`loading`)
78 source.call(`ready`)
79 await vi.advanceTimersByTimeAsync(0)
80 expect(source.collection.status).toBe(`ready`)
81 } finally {
82 await source.collection.cleanup({ dependents: true })
83 }
84 })
85
86 it(`defaults the network-first deadline to three seconds`, async () => {
87 const fixture = controlledAdapter()
88 const collection = createCollection(
89 persistedCollectionOptions<Row, string>({
90 id: `default-network-deadline`,
91 getKey: (row) => row.id,
92 persistence: { adapter: fixture.adapter },
93 initialRender: { strategy: `network-first` },
94 sync: { sync: () => {} },
95 }),
96 )
97 try {
98 void collection.preload().catch(() => undefined)
99 fixture.load.resolve([DISK_ROW])
100 await vi.advanceTimersByTimeAsync(2_999)
101 expect(collection.status).toBe(`loading`)
102 await vi.advanceTimersByTimeAsync(1)
103 expect(collection.status).toBe(`ready`)
104 } finally {
105 await collection.cleanup({ dependents: true })
106 }
107 })
108
109 it(`rejects network-first initial rendering for on-demand persistence`, () => {
110 const fixture = controlledAdapter()
111 expect(() =>
112 persistedCollectionOptions<Row, string>({
113 id: `local-on-demand`,
114 getKey: (row) => row.id,
115 syncMode: `on-demand`,
116 initialRender: { strategy: `network-first`, networkTimeoutMs: 100 },
117 persistence: { adapter: fixture.adapter },
118 }),
119 ).toThrow(/network-first.*eager/i)
120 })
121
122 it.each([-1, Number.NaN, Number.POSITIVE_INFINITY, 2_147_483_648])(
123 `rejects an invalid network timeout (%s)`,
124 (networkTimeoutMs) => {
125 const fixture = controlledAdapter()
126 expect(() =>
127 persistedCollectionOptions<Row, string>({
128 id: `invalid-network-deadline`,
129 getKey: (row) => row.id,
130 persistence: { adapter: fixture.adapter },
131 initialRender: { strategy: `network-first`, networkTimeoutMs },
132 }),
133 ).toThrow(/networkTimeoutMs/)
134 },
135 )
136})