Skip to content

Commit 3590864

Browse files
committed
Move more tests into node
1 parent 8b8ff41 commit 3590864

File tree

2 files changed

+103
-122
lines changed

2 files changed

+103
-122
lines changed

packages/node/tests/sync.test.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,109 @@ function defineSyncTests(impl: SyncClientImplementation) {
492492
rows = (await query.next()).value.rows._array;
493493
expect(rows).toStrictEqual([{ name: 'from server' }]);
494494
});
495+
496+
mockSyncServiceTest('should update sync state incrementally', async ({ syncService }) => {
497+
const powersync = await syncService.createDatabase();
498+
powersync.connect(new TestConnector(), options);
499+
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
500+
501+
const buckets: BucketChecksum[] = [];
502+
for (let prio = 0; prio <= 3; prio++) {
503+
buckets.push({ bucket: `prio${prio}`, priority: prio, checksum: 10 + prio });
504+
}
505+
syncService.pushLine({
506+
checkpoint: {
507+
last_op_id: '4',
508+
buckets
509+
}
510+
});
511+
512+
let operationId = 1;
513+
const addRow = (prio: number) => {
514+
syncService.pushLine({
515+
data: {
516+
bucket: `prio${prio}`,
517+
data: [
518+
{
519+
checksum: prio + 10,
520+
data: JSON.stringify({ name: 'row' }),
521+
op: 'PUT',
522+
op_id: (operationId++).toString(),
523+
object_id: `prio${prio}`,
524+
object_type: 'lists'
525+
}
526+
]
527+
}
528+
});
529+
};
530+
531+
const syncCompleted = vi.fn();
532+
powersync.waitForFirstSync().then(syncCompleted);
533+
534+
// Emit partial sync complete for each priority but the last.
535+
for (var prio = 0; prio < 3; prio++) {
536+
const partialSyncCompleted = vi.fn();
537+
powersync.waitForFirstSync({ priority: prio }).then(partialSyncCompleted);
538+
expect(powersync.currentStatus.statusForPriority(prio).hasSynced).toBe(false);
539+
expect(partialSyncCompleted).not.toHaveBeenCalled();
540+
expect(syncCompleted).not.toHaveBeenCalled();
541+
542+
addRow(prio);
543+
syncService.pushLine({
544+
partial_checkpoint_complete: {
545+
last_op_id: operationId.toString(),
546+
priority: prio
547+
}
548+
});
549+
550+
await powersync.syncStreamImplementation!.waitUntilStatusMatches((status) => {
551+
return status.statusForPriority(prio).hasSynced === true;
552+
});
553+
await new Promise((r) => setTimeout(r));
554+
expect(partialSyncCompleted).toHaveBeenCalledOnce();
555+
556+
expect(await powersync.getAll('select * from lists')).toHaveLength(prio + 1);
557+
}
558+
559+
// Then, complete the sync.
560+
addRow(3);
561+
syncService.pushLine({ checkpoint_complete: { last_op_id: operationId.toString() } });
562+
await vi.waitFor(() => expect(syncCompleted).toHaveBeenCalledOnce(), 500);
563+
expect(await powersync.getAll('select * from lists')).toHaveLength(4);
564+
});
565+
566+
mockSyncServiceTest('Should remember sync state', async ({ syncService }) => {
567+
const powersync = await syncService.createDatabase();
568+
powersync.connect(new TestConnector(), options);
569+
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
570+
571+
const buckets: BucketChecksum[] = [];
572+
for (let prio = 0; prio <= 3; prio++) {
573+
buckets.push({ bucket: `prio${prio}`, priority: prio, checksum: 0 });
574+
}
575+
syncService.pushLine({
576+
checkpoint: {
577+
last_op_id: '0',
578+
buckets
579+
}
580+
});
581+
syncService.pushLine({
582+
partial_checkpoint_complete: {
583+
last_op_id: '0',
584+
priority: 0
585+
}
586+
});
587+
588+
await powersync.waitForFirstSync({ priority: 0 });
589+
590+
// Open another database instance.
591+
const another = await syncService.createDatabase();
592+
await another.init();
593+
594+
expect(another.currentStatus.priorityStatusEntries).toHaveLength(1);
595+
expect(another.currentStatus.statusForPriority(0).hasSynced).toBeTruthy();
596+
await another.waitForFirstSync({ priority: 0 });
597+
});
495598
}
496599

497600
function bucket(name: string, count: number, options: { priority: number } = { priority: 3 }): BucketChecksum {

packages/web/tests/stream.test.ts

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -303,127 +303,5 @@ function describeStreamingTests(createConnectedDatabase: () => Promise<Connected
303303
}
304304
);
305305
});
306-
307-
it('Should upload after reconnecting', async () => {
308-
const { powersync, connect, uploadSpy } = await createConnectedDatabase();
309-
expect(powersync.connected).toBe(true);
310-
311-
await powersync.disconnect();
312-
313-
// Status should update after uploads are completed
314-
await vi.waitFor(
315-
() => {
316-
// to-have-been-called seems to not work after failing a check
317-
expect(powersync.currentStatus.dataFlowStatus.uploading).false;
318-
},
319-
{
320-
timeout: UPLOAD_TIMEOUT_MS
321-
}
322-
);
323-
});
324-
325-
it('Should update sync state incrementally', async () => {
326-
const { powersync, remote } = await createConnectedDatabase();
327-
expect(powersync.currentStatus.dataFlowStatus.downloading).toBe(false);
328-
329-
const buckets: BucketChecksum[] = [];
330-
for (let prio = 0; prio <= 3; prio++) {
331-
buckets.push({ bucket: `prio${prio}`, priority: prio, checksum: 10 + prio });
332-
}
333-
remote.enqueueLine({
334-
checkpoint: {
335-
last_op_id: '4',
336-
buckets
337-
}
338-
});
339-
340-
let operationId = 1;
341-
const addRow = (prio: number) => {
342-
remote.enqueueLine({
343-
data: {
344-
bucket: `prio${prio}`,
345-
data: [
346-
{
347-
checksum: prio + 10,
348-
data: JSON.stringify({ name: 'row' }),
349-
op: 'PUT',
350-
op_id: (operationId++).toString(),
351-
object_id: `prio${prio}`,
352-
object_type: 'users'
353-
}
354-
]
355-
}
356-
});
357-
};
358-
359-
const syncCompleted = vi.fn();
360-
powersync.waitForFirstSync().then(syncCompleted);
361-
362-
// Emit partial sync complete for each priority but the last.
363-
for (var prio = 0; prio < 3; prio++) {
364-
const partialSyncCompleted = vi.fn();
365-
powersync.waitForFirstSync({ priority: prio }).then(partialSyncCompleted);
366-
expect(powersync.currentStatus.statusForPriority(prio).hasSynced).toBe(false);
367-
expect(partialSyncCompleted).not.toHaveBeenCalled();
368-
expect(syncCompleted).not.toHaveBeenCalled();
369-
370-
addRow(prio);
371-
remote.enqueueLine({
372-
partial_checkpoint_complete: {
373-
last_op_id: operationId.toString(),
374-
priority: prio
375-
}
376-
});
377-
378-
await powersync.syncStreamImplementation!.waitUntilStatusMatches((status) => {
379-
return status.statusForPriority(prio).hasSynced === true;
380-
});
381-
await new Promise((r) => setTimeout(r));
382-
expect(partialSyncCompleted).toHaveBeenCalledOnce();
383-
384-
expect(await powersync.getAll('select * from users')).toHaveLength(prio + 1);
385-
}
386-
387-
// Then, complete the sync.
388-
addRow(3);
389-
remote.enqueueLine({ checkpoint_complete: { last_op_id: operationId.toString() } });
390-
await vi.waitFor(() => expect(syncCompleted).toHaveBeenCalledOnce(), 500);
391-
expect(await powersync.getAll('select * from users')).toHaveLength(4);
392-
});
393-
394-
it('Should remember sync state', async () => {
395-
const { powersync, remote, openAnother } = await createConnectedDatabase();
396-
expect(powersync.currentStatus.dataFlowStatus.downloading).toBe(false);
397-
398-
const buckets: BucketChecksum[] = [];
399-
for (let prio = 0; prio <= 3; prio++) {
400-
buckets.push({ bucket: `prio${prio}`, priority: prio, checksum: 0 });
401-
}
402-
remote.enqueueLine({
403-
checkpoint: {
404-
last_op_id: '0',
405-
buckets
406-
}
407-
});
408-
remote.enqueueLine({
409-
partial_checkpoint_complete: {
410-
last_op_id: '0',
411-
priority: 0
412-
}
413-
});
414-
415-
await powersync.waitForFirstSync({ priority: 0 });
416-
417-
// Open another database instance.
418-
const another = openAnother();
419-
onTestFinished(async () => {
420-
await another.close();
421-
});
422-
await another.init();
423-
424-
expect(another.currentStatus.priorityStatusEntries).toHaveLength(1);
425-
expect(another.currentStatus.statusForPriority(0).hasSynced).toBeTruthy();
426-
await another.waitForFirstSync({ priority: 0 });
427-
});
428306
};
429307
}

0 commit comments

Comments
 (0)