Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from "vitest"
2import { Collection } from "@tanstack/optimistic"
3import { createElectricSync } from "../src/electric"
4import type { PendingMutation, Transaction } from "@tanstack/optimistic"
5import type { Message, Row } from "@electric-sql/client"
6import type { ElectricSync } from "../src/electric"
7import "fake-indexeddb/auto"
8
9// Mock the ShapeStream module
10const mockSubscribe = vi.fn()
11const mockStream = {
12 subscribe: mockSubscribe,
13}
14
15vi.mock(`@electric-sql/client`, async () => {
16 const actual = await vi.importActual(`@electric-sql/client`)
17 return {
18 ...actual,
19 ShapeStream: vi.fn(() => mockStream),
20 }
21})
22
23describe(`Electric Integration`, () => {
24 let collection: Collection
25 let subscriber: (messages: Array<Message<Row>>) => void
26 let electricSync: ElectricSync
27
28 beforeEach(() => {
29 vi.clearAllMocks()
30
31 // Reset mock subscriber
32 mockSubscribe.mockImplementation((callback) => {
33 subscriber = callback
34 return () => {}
35 })
36
37 // Create Electric sync config with primary key
38 electricSync = createElectricSync(
39 {
40 url: `http://test-url`,
41 params: {
42 table: `test_table`,
43 },
44 },
45 { primaryKey: [`id`] }
46 ) // Using 'id' as the primary key column
47
48 // Create collection with Electric sync
49 collection = new Collection({
50 id: `test`,
51 sync: electricSync,
52 mutationFn: {
53 persist: vi.fn().mockResolvedValue(undefined),
54 awaitSync: async () => {},
55 },
56 })
57 })
58
59 it(`should handle incoming insert messages and commit on up-to-date`, () => {
60 // Simulate incoming insert message
61 subscriber([
62 {
63 key: `1`,
64 value: { id: 1, name: `Test User` },
65 headers: { operation: `insert` },
66 },
67 ])
68
69 // Send up-to-date control message to commit transaction
70 subscriber([
71 {
72 headers: { control: `up-to-date` },
73 },
74 ])
75
76 expect(collection.state).toEqual(
77 new Map([[`1`, { id: 1, name: `Test User` }]])
78 )
79 })
80
81 it(`should handle multiple changes before committing`, () => {
82 // First batch of changes
83 subscriber([
84 {
85 key: `1`,
86 value: { id: 1, name: `Test User` },
87 headers: { operation: `insert` },
88 },
89 ])
90
91 // Second batch of changes
92 subscriber([
93 {
94 key: `2`,
95 value: { id: 2, name: `Another User` },
96 headers: { operation: `insert` },
97 },
98 ])
99
100 // Send up-to-date to commit all changes
101 subscriber([
102 {
103 headers: { control: `up-to-date` },
104 },
105 ])
106
107 expect(collection.state).toEqual(
108 new Map([
109 [`1`, { id: 1, name: `Test User` }],
110 [`2`, { id: 2, name: `Another User` }],
111 ])
112 )
113 })
114
115 it(`should handle updates across multiple messages`, () => {
116 // First insert
117 subscriber([
118 {
119 key: `1`,
120 value: { id: 1, name: `Test User` },
121 headers: { operation: `insert` },
122 },
123 ])
124
125 // Update in a separate message
126 subscriber([
127 {
128 key: `1`,
129 value: { id: 1, name: `Updated User` },
130 headers: { operation: `update` },
131 },
132 ])
133
134 // Commit with up-to-date
135 subscriber([
136 {
137 headers: { control: `up-to-date` },
138 },
139 ])
140
141 expect(collection.state).toEqual(
142 new Map([[`1`, { id: 1, name: `Updated User` }]])
143 )
144 })
145
146 it(`should handle delete operations`, () => {
147 // Insert and commit
148 subscriber([
149 {
150 key: `1`,
151 value: { id: 1, name: `Test User` },
152 headers: { operation: `insert` },
153 },
154 {
155 headers: { control: `up-to-date` },
156 },
157 ])
158
159 // Delete in new transaction
160 subscriber([
161 {
162 key: `1`,
163 value: {},
164 headers: { operation: `delete` },
165 },
166 {
167 headers: { control: `up-to-date` },
168 },
169 ])
170
171 expect(collection.state).toEqual(new Map())
172 })
173
174 it(`should not commit changes without up-to-date message`, () => {
175 // Send changes without up-to-date
176 subscriber([
177 {
178 key: `1`,
179 value: { id: 1, name: `Test User` },
180 headers: { operation: `insert` },
181 },
182 ])
183
184 // Send must-refetch control message
185 subscriber([
186 {
187 headers: { control: `must-refetch` },
188 },
189 ])
190
191 // Changes should still be pending until up-to-date is received
192 expect(collection.state).toEqual(new Map())
193 })
194
195 // Tests for txid tracking functionality
196 describe(`txid tracking`, () => {
197 it(`should track txids from incoming messages`, async () => {
198 const testTxid = 123
199
200 // Send a message with a txid
201 subscriber([
202 {
203 key: `1`,
204 value: { id: 1, name: `Test User` },
205 headers: {
206 operation: `insert`,
207 txids: [testTxid],
208 },
209 },
210 {
211 headers: { control: `up-to-date` },
212 },
213 ])
214
215 // The txid should be tracked and awaitTxid should resolve immediately
216 await expect(electricSync.awaitTxid(testTxid)).resolves.toBe(true)
217 })
218
219 it(`should handle multiple txids in a single message`, async () => {
220 const txid1 = 1
221 const txid2 = 2
222
223 // Send a message with multiple txids
224 subscriber([
225 {
226 key: `1`,
227 value: { id: 1, name: `Test User` },
228 headers: {
229 operation: `insert`,
230 txids: [txid1, txid2],
231 },
232 },
233 {
234 headers: { control: `up-to-date` },
235 },
236 ])
237
238 // Both txids should be tracked
239 await expect(electricSync.awaitTxid(txid1)).resolves.toBe(true)
240 await expect(electricSync.awaitTxid(txid2)).resolves.toBe(true)
241 })
242
243 it(`should reject with timeout when waiting for unknown txid`, async () => {
244 // Set a short timeout for the test
245 const unknownTxid = 0
246 const shortTimeout = 100
247
248 // Attempt to await a txid that hasn't been seen with a short timeout
249 const promise = electricSync.awaitTxid(unknownTxid, shortTimeout)
250
251 // The promise should reject with a timeout error
252 await expect(promise).rejects.toThrow(
253 `Timeout waiting for txid: ${unknownTxid}`
254 )
255 })
256
257 it(`should resolve when a txid arrives after awaitTxid is called`, async () => {
258 const laterTxid = 1000
259
260 // Start waiting for a txid that hasn't arrived yet
261 const promise = electricSync.awaitTxid(laterTxid, 1000)
262
263 // Send the txid after a short delay
264 setTimeout(() => {
265 subscriber([
266 {
267 key: `foo`,
268 value: { id: 1, bar: true },
269 headers: {
270 operation: `insert`,
271 },
272 },
273 {
274 headers: {
275 control: `up-to-date`,
276 txids: [laterTxid],
277 },
278 },
279 {
280 headers: {
281 control: `up-to-date`,
282 },
283 },
284 ])
285 }, 50)
286
287 // The promise should resolve when the txid arrives
288 await expect(promise).resolves.toBe(true)
289 })
290
291 // Test the complete flow from persist to awaitSync
292 it(`should simulate the complete flow from persist to awaitSync`, async () => {
293 // Create a fake backend store to simulate server-side storage
294 const fakeBackend = {
295 data: new Map<string, { txid: number; value: unknown }>(),
296 // Simulates persisting data to a backend and returning a txid
297 persist: (mutations: Array<PendingMutation>): Promise<number> => {
298 const txid = Date.now()
299
300 // Store the changes with the txid
301 mutations.forEach((mutation) => {
302 fakeBackend.data.set(mutation.key, {
303 value: mutation.changes,
304 txid,
305 })
306 })
307
308 return Promise.resolve(txid)
309 },
310 // Simulates the server sending sync messages with txids
311 simulateSyncMessage: (txid: number) => {
312 // Create messages for each item in the store that has this txid
313 const messages: Array<Message<Row>> = []
314
315 fakeBackend.data.forEach((value, key) => {
316 if (value.txid === txid) {
317 messages.push({
318 key,
319 value: value.value as Row,
320 headers: {
321 operation: `insert`,
322 txids: [txid],
323 },
324 })
325 }
326 })
327
328 // Add an up-to-date message to complete the sync
329 messages.push({
330 headers: {
331 control: `up-to-date`,
332 },
333 })
334
335 // Send the messages to the subscriber
336 subscriber(messages)
337 },
338 }
339
340 // Create a test mutation function that uses our fake backend
341 const testMutationFn = {
342 persist: vi.fn(
343 async ({ transaction }: { transaction: Transaction }) => {
344 // Persist to fake backend and get txid
345 const txid = await fakeBackend.persist(transaction.mutations)
346
347 // Return the txid (which will be passed to awaitSync)
348 return Promise.resolve({ txid })
349 }
350 ),
351
352 awaitSync: vi.fn(
353 async ({ persistResult }: { persistResult: { txid: number } }) => {
354 // Get the txid from the transaction metadata
355 const txid = persistResult.txid
356
357 if (!txid) {
358 throw new Error(`No txid found`)
359 }
360
361 // Start waiting for the txid
362 const awaitPromise = electricSync.awaitTxid(txid, 1000)
363
364 // Simulate the server sending sync messages after a delay
365 setTimeout(() => {
366 fakeBackend.simulateSyncMessage(txid)
367 }, 50)
368
369 // Wait for the txid to be seen
370 await awaitPromise
371
372 return Promise.resolve()
373 }
374 ),
375 }
376
377 // Create a new collection with our test mutation function
378 const testCollection = new Collection({
379 id: `ofo`,
380 sync: electricSync,
381 mutationFn: testMutationFn,
382 })
383
384 let transaction = testCollection.insert(
385 { id: 1, name: `Test item 1` },
386 { key: `item1` }
387 )
388
389 await transaction.isPersisted?.promise
390
391 transaction = testCollection.transactions.get(transaction.id)!
392
393 await transaction.isSynced?.promise
394
395 // Verify the mutation function was called correctly
396 expect(testMutationFn.persist).toHaveBeenCalledTimes(1)
397 expect(testMutationFn.awaitSync).toHaveBeenCalledTimes(1)
398
399 // Check that the data was added to the collection
400 // Note: In a real implementation, the collection would be updated by the sync process
401 // This is just verifying our test setup worked correctly
402 expect(fakeBackend.data.has(`item1`)).toBe(true)
403 expect(testCollection.state.has(`item1`)).toBe(true)
404 })
405 })
406
407 it(`should include primaryKey in the metadata`, () => {
408 // Simulate incoming insert message
409 subscriber([
410 {
411 key: `1`,
412 value: { id: 1, name: `Test User` },
413 headers: { operation: `insert` },
414 },
415 ])
416
417 // Send up-to-date control message to commit transaction
418 subscriber([
419 {
420 headers: { control: `up-to-date` },
421 },
422 ])
423
424 // Get the metadata for the inserted item
425 const metadata = collection.syncedMetadata.state.get(`1`) as {
426 primaryKey: string
427 }
428
429 // Verify that the primaryKey is included in the metadata
430 expect(metadata).toHaveProperty(`primaryKey`)
431 expect(metadata.primaryKey).toEqual([`id`])
432 })
433
434 it(`Transaction proxy with toObject method works correctly`, () => {
435 // Create a collection with a simple mutation function
436 const testCollection = new Collection({
437 id: `foo`,
438 sync: createElectricSync({ url: `foo` }, { primaryKey: [`id`] }), // Add primary key
439 mutationFn: {
440 persist: async () => {},
441 awaitSync: async () => {},
442 },
443 })
444
445 // Create a transaction
446 const transaction = testCollection.transactionManager.applyTransaction(
447 [], // No mutations
448 { type: `ordered` } // Simple strategy
449 )
450
451 // Test that we can access properties
452 expect(transaction.id).toBeDefined()
453 expect(transaction.state).toBe(`pending`)
454
455 // Test the toObject method
456 const transactionObj = transaction.toObject()
457 expect(transactionObj).toBeDefined()
458 expect(transactionObj.id).toBe(transaction.id)
459 expect(transactionObj.state).toBe(transaction.state)
460
461 // Test that we can create a modified clone
462 const modifiedTransaction = {
463 ...transaction.toObject(),
464 metadata: {
465 ...transaction.toObject().metadata,
466 testKey: `testValue`,
467 },
468 }
469
470 expect(modifiedTransaction.id).toBe(transaction.id)
471 expect(modifiedTransaction.metadata.testKey).toBe(`testValue`)
472 })
473})