diff --git a/.changeset/tall-dolphins-sleep.md b/.changeset/tall-dolphins-sleep.md new file mode 100644 index 000000000..d6abd48d2 --- /dev/null +++ b/.changeset/tall-dolphins-sleep.md @@ -0,0 +1,8 @@ +--- +'@powersync/common': patch +'@powersync/node': patch +'@powersync/web': patch +'@powersync/react-native': patch +--- + +Rust sync client: Fix reported `lastSyncedAt` values in sync status. diff --git a/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts b/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts index f24e72e7a..e8aac6aaf 100644 --- a/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +++ b/packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts @@ -923,7 +923,7 @@ The next upload iteration will be delayed.`); return { priority: status.priority, hasSynced: status.has_synced ?? undefined, - lastSyncedAt: status?.last_synced_at != null ? new Date(status!.last_synced_at!) : undefined + lastSyncedAt: status?.last_synced_at != null ? new Date(status!.last_synced_at! * 1000) : undefined }; } diff --git a/packages/node/tests/sync.test.ts b/packages/node/tests/sync.test.ts index 14e123497..ad1efb255 100644 --- a/packages/node/tests/sync.test.ts +++ b/packages/node/tests/sync.test.ts @@ -112,6 +112,29 @@ function defineSyncTests(impl: SyncClientImplementation) { connectionMethod: SyncStreamConnectionMethod.HTTP }; + mockSyncServiceTest('sets last sync time', async ({ syncService }) => { + const db = await syncService.createDatabase(); + db.connect(new TestConnector(), options); + await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1)); + + syncService.pushLine({ + checkpoint: { + last_op_id: '0', + buckets: [] + } + }); + syncService.pushLine({ checkpoint_complete: { last_op_id: '0' } }); + const now = Date.now(); + + await db.waitForFirstSync(); + const status = db.currentStatus; + const lastSyncedAt = status.lastSyncedAt!.getTime(); + + // The reported time of the last sync should be close to the current time (5s is very generous already, but we've + // had an issue where dates weren't parsed correctly and we were off by decades). + expect(Math.abs(lastSyncedAt - now)).toBeLessThan(5000); + }); + describe('reports progress', () => { let lastOpId = 0;