Skip to content

Commit 00227a1

Browse files
committed
test: add failing test for array txid bug in electric collection
This test demonstrates issue #793 where returning an array of txids from a collection handler (e.g., `return { txid: [txid1, txid2] }`) causes a TimeoutWaitingForTxIdError instead of properly waiting for all txids to be seen in the sync stream. The test shows that when a handler returns multiple txids, the processMatchingStrategy function doesn't correctly handle the array, even though it has code that appears to support arrays.
1 parent 21aad24 commit 00227a1

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

packages/electric-db-collection/tests/electric.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,77 @@ describe(`Electric Integration`, () => {
667667
expect(onInsert).toHaveBeenCalled()
668668
})
669669

670+
it(`should handle array of txids returned from handler`, async () => {
671+
// Create a fake backend that returns multiple txids
672+
const fakeBackend = {
673+
persist: (
674+
mutations: Array<PendingMutation<Row>>
675+
): Promise<Array<number>> => {
676+
// Simulate multiple items being persisted and each getting a txid
677+
const txids = mutations.map(() => Math.floor(Math.random() * 10000))
678+
return Promise.resolve(txids)
679+
},
680+
}
681+
682+
// Create handler that returns array of txids
683+
const onInsert = vi.fn(async (params: MutationFnParams<Row>) => {
684+
const txids = await fakeBackend.persist(params.transaction.mutations)
685+
686+
// Simulate server sending sync messages after a delay
687+
setTimeout(() => {
688+
subscriber([
689+
{
690+
key: `1`,
691+
value: { id: 1, name: `Item 1` },
692+
headers: {
693+
operation: `insert`,
694+
txids: [txids[0]],
695+
},
696+
},
697+
{
698+
key: `2`,
699+
value: { id: 2, name: `Item 2` },
700+
headers: {
701+
operation: `insert`,
702+
txids: [txids[1]],
703+
},
704+
},
705+
{ headers: { control: `up-to-date` } },
706+
])
707+
}, 50)
708+
709+
// Return array of txids - this is the pattern that's failing
710+
return { txid: txids }
711+
})
712+
713+
const config = {
714+
id: `test-array-txids`,
715+
shapeOptions: {
716+
url: `http://test-url`,
717+
params: { table: `test_table` },
718+
},
719+
startSync: true,
720+
getKey: (item: Row) => item.id as number,
721+
onInsert,
722+
}
723+
724+
const testCollection = createCollection(electricCollectionOptions(config))
725+
726+
// Insert multiple items
727+
const tx = testCollection.insert([
728+
{ id: 1, name: `Item 1` },
729+
{ id: 2, name: `Item 2` },
730+
])
731+
732+
// This should resolve when all txids are seen
733+
await expect(tx.isPersisted.promise).resolves.toBeDefined()
734+
expect(onInsert).toHaveBeenCalled()
735+
736+
// Verify both items were added
737+
expect(testCollection.has(1)).toBe(true)
738+
expect(testCollection.has(2)).toBe(true)
739+
})
740+
670741
it(`should support custom match function using awaitMatch utility`, async () => {
671742
let resolveCustomMatch: () => void
672743
const customMatchPromise = new Promise<void>((resolve) => {

0 commit comments

Comments
 (0)