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

seed-world.e2e.test.ts

300 lines9.4 KB

1import { expect, it } from 'vitest'
2import {
3 captureSeedData,
4 generateSeedData,
5} from '../../db-collection-e2e/src/fixtures/seed-data'
6import type { SeedDataResult } from '../../db-collection-e2e/src/types'
7import type { FixtureValue } from '../../db-collection-e2e/src/fixtures/fixture-artifact'
8​
9function literalFixture(): SeedDataResult {
10 return {
11 users: [
12 {
13 id: 'user-a',
14 name: 'A',
15 email: null,
16 age: -5,
17 isActive: true,
18 createdAt: new Date(1_710_381_566_535),
19 metadata: { nested: { label: 'kept', count: 9007199254740993n } },
20 deletedAt: null,
21 },
22 ],
23 posts: [
24 {
25 id: 'post-a',
26 userId: 'user-a',
27 title: 'Owned',
28 content: null,
29 viewCount: 0,
30 largeViewCount: 9007199254740993n,
31 publishedAt: new Date(1_710_381_566_536),
32 deletedAt: null,
33 },
34 ],
35 comments: [
36 {
37 id: 'comment-a',
38 postId: 'post-a',
39 userId: 'user-a',
40 text: 'Literal',
41 createdAt: new Date(1_710_381_566_537),
42 deletedAt: new Date(1_710_381_566_538),
43 },
44 ],
45 userIds: ['user-a'],
46 postIds: ['post-a'],
47 commentIds: ['comment-a'],
48 }
50​
51it('retains the exact literal seed independently of input, driver, and readers', () => {
52 const input = literalFixture()
53 const fixture = captureSeedData(input)
54 const archived = fixture()
55 const driver = fixture()
56 input.users[0]!.name = 'input changed'
57 input.users[0]!.createdAt.setTime(0)
58 input.posts[0]!.largeViewCount = 1n
59 input.userIds[0] = 'input-id'
60 driver.users[0]!.metadata!.nested = { label: 'driver changed' }
61 driver.posts[0]!.publishedAt!.setTime(0)
62 driver.comments[0]!.deletedAt!.setTime(0)
63 driver.posts[0]!.userId = 'wrong-edge'
64 driver.commentIds.pop()
65​
66 const reader = fixture()
67 reader.comments[0]!.text = 'reader changed'
68 reader.postIds.push('reader-id')
69 expect(fixture()).toEqual(literalFixture())
70 expect(archived).toEqual(literalFixture())
71 expect(fixture().posts[0]!.largeViewCount).toBe(9007199254740993n)
72 expect(fixture().users[0]!.createdAt.getTime()).toBe(1_710_381_566_535)
73​
74 const wrongKey = fixture()
75 wrongKey.users[0]!.id = 'other-user'
76 expect(() => expect(wrongKey).toEqual(literalFixture())).toThrow()
77 const wrongValue = fixture()
78 wrongValue.posts[0]!.largeViewCount = 9007199254740992n
79 expect(() => expect(wrongValue).toEqual(literalFixture())).toThrow()
80})
81​
82it('preserves unknown own data fields and native values without a field whitelist', () => {
83 const input = literalFixture()
84 const extension = {
85 extra: { time: new Date(1_710_381_566_539), number: 9007199254740995n },
86 absent: undefined,
87 }
88 Object.assign(input.users[0]!, extension)
89 const fixture = captureSeedData(input)
90 extension.extra.time.setTime(0)
91 extension.extra.number = 0n
92 const first = fixture()
93 expect(first.users[0]).toEqual({
94 ...literalFixture().users[0],
95 extra: { time: new Date(1_710_381_566_539), number: 9007199254740995n },
96 absent: undefined,
97 })
98 expect(Object.hasOwn(first.users[0]!, 'absent')).toBe(true)
99 Object.assign(first.users[0]!, { extra: null, absent: 'changed' })
100 expect(fixture().users[0]).toEqual({
101 ...literalFixture().users[0],
102 extra: { time: new Date(1_710_381_566_539), number: 9007199254740995n },
103 absent: undefined,
104 })
105})
106​
107it('captures one generated world without regenerating its random dates', () => {
108 const input = generateSeedData()
109 const expected = structuredClone(input)
110 const fixture = captureSeedData(input)
111 const archived = fixture()
112 expect(input.users).toHaveLength(100)
113 expect(input.posts).toHaveLength(100)
114 expect(input.comments).toHaveLength(100)
115 for (const user of input.users) {
116 user.createdAt.setTime(0)
117 if (user.deletedAt) user.deletedAt.setTime(0)
118 user.name = 'driver replacement'
119 }
120 input.posts.splice(0, input.posts.length)
121 input.comments.reverse()
122 input.userIds.reverse()
123 const delivered = fixture()
124 delivered.postIds.reverse()
125 delivered.comments[0]!.createdAt.setTime(0)
126 expect(fixture()).toEqual(expected)
127 expect(archived).toEqual(expected)
128})
129​
130it('emits one fixture record at capture rather than on later reader access', () => {
131 const records: Array<string> = []
132 const input = literalFixture()
133 const fixture: () => SeedDataResult = Reflect.apply(
134 captureSeedData,
135 undefined,
136 [
137 input,
138 {
139 registration: 'literal capture',
140 provider: 'test sink',
141 emit: (line: string) => records.push(line),
142 },
143 ],
144 )
145 expect(records).toHaveLength(1)
146 const original = records[0]
147 input.users[0]!.createdAt.setTime(0)
148 fixture().posts[0]!.largeViewCount = 0n
149 fixture()
150 expect(records).toEqual([original])
151})
152​
153function readFixtureRecord(line: string) {
154 const prefix = '[db-e2e-fixture] '
155 expect(line.startsWith(prefix)).toBe(true)
156 return JSON.parse(line.slice(prefix.length)) as {
157 version: number
158 status: 'complete' | 'unsupported'
159 registration: string
160 provider: string
161 capturedAt: number
162 runtime: null
163 providerVersion: null
164 fixture: FixtureValue
165 reason?: string
166 }
168​
169// Test-local interpreter: the native decoded values are compared with an
170// independently retained input, never with encoder-computed expectations.
171function decodeFixtureValue(value: FixtureValue): unknown {
172 switch (value[0]) {
173 case 'null':
174 return null
175 case 'undefined':
176 return undefined
177 case 'boolean':
178 case 'string':
179 return value[1]
180 case 'number':
181 return Number(value[1])
182 case 'bigint':
183 return BigInt(value[1])
184 case 'date':
185 return new Date(Number(value[1]))
186 case 'array':
187 return value[1].map(decodeFixtureValue)
188 case 'object':
189 return Object.fromEntries(
190 value[1].map(([key, item]) => [key, decodeFixtureValue(item)]),
191 )
192 }
194​
195it('records the exact native fixture and capture context before driver mutation', () => {
196 const input = literalFixture()
197 input.users[0]!.metadata = {
198 tagShaped: ['date', '0'],
199 objectTag: { type: 'bigint', value: '1' },
200 native: { time: new Date(123), integer: 9007199254740999n },
201 absent: undefined,
202 numbers: [-0, NaN, Infinity, -Infinity],
203 }
204 const expected = structuredClone(input)
205 const records: Array<string> = []
206 const before = Date.now()
207 const fixture = captureSeedData(input, {
208 registration: 'literal',
209 provider: 'recording test sink',
210 emit: (line) => records.push(line),
211 })
212 const after = Date.now()
213 expect(records).toHaveLength(1)
214 input.users[0]!.metadata.native = 'driver changed'
215 input.posts[0]!.largeViewCount = 0n
216 fixture().comments[0]!.createdAt.setTime(0)
217 const record = readFixtureRecord(records[0]!)
218 expect(record).toMatchObject({
219 version: 1,
220 registration: 'literal',
221 provider: 'recording test sink',
222 runtime: null,
223 providerVersion: null,
224 status: 'complete',
225 })
226 expect(record.capturedAt).toBeGreaterThanOrEqual(before)
227 expect(record.capturedAt).toBeLessThanOrEqual(after)
228 const decoded = decodeFixtureValue(record.fixture) as SeedDataResult
229 expect(decoded).toStrictEqual(expected)
230 expect(decoded.users[0]!.createdAt).toBeInstanceOf(Date)
231 expect(decoded.posts[0]!.largeViewCount).toBe(9007199254740993n)
232 expect(Object.hasOwn(decoded.users[0]!.metadata!, 'absent')).toBe(true)
233 const numbers = decoded.users[0]!.metadata!.numbers as Array<number>
234 expect(Object.is(numbers[0], -0)).toBe(true)
235 expect(Number.isNaN(numbers[1])).toBe(true)
236 expect(numbers.slice(2)).toEqual([Infinity, -Infinity])
237 expect(records).toHaveLength(1)
238})
239​
240it.each(['empty', 'generated'] as const)(
241 'logs the complete %s world without regenerating input',
242 (kind) => {
243 const input =
244 kind === 'generated'
245 ? generateSeedData()
246 : {
247 users: [],
248 posts: [],
249 comments: [],
250 userIds: [],
251 postIds: [],
252 commentIds: [],
253 }
254 const expected = structuredClone(input)
255 const records: Array<string> = []
256 const fixture = captureSeedData(input, {
257 registration: kind,
258 provider: 'recording test sink',
259 emit: (line) => records.push(line),
260 })
261 input.users.splice(0)
262 input.comments.reverse()
263 const record = readFixtureRecord(records[0]!)
264 expect(record.status).toBe('complete')
265 expect(decodeFixtureValue(record.fixture)).toStrictEqual(expected)
266 expect(fixture()).toStrictEqual(expected)
267 expect(records).toHaveLength(1)
268 },
270​
271it.each(['map', 'set', 'shared', 'cycle', 'sparse'] as const)(
272 'reports unsupported %s diagnostics without narrowing the original fixture getter',
273 (kind) => {
274 const input = literalFixture()
275 const shared = { value: 1 }
276 const cycle: Record<string, unknown> = {}
277 cycle.self = cycle
278 const values = {
279 map: new Map([['key', 1]]),
280 set: new Set([1]),
281 shared: { left: shared, right: shared },
282 cycle,
283 sparse: new Array(2),
284 }
285 input.users[0]!.metadata = { extension: values[kind] }
286 const expected = structuredClone(input)
287 const records: Array<string> = []
288 const fixture = captureSeedData(input, {
289 registration: kind,
290 provider: 'recording test sink',
291 emit: (line) => records.push(line),
292 })
293 expect(records).toHaveLength(1)
294 const record = readFixtureRecord(records[0]!)
295 expect(record.status).toBe('unsupported')
296 expect(record.reason).toEqual(expect.any(String))
297 expect(Object.hasOwn(record, 'fixture')).toBe(false)
298 expect(fixture()).toStrictEqual(expected)
299 },