Skip to content

Commit

Permalink
add comparable tests for MongoLSN
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Feb 4, 2025
1 parent 44099a0 commit 0da4c50
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions modules/module-mongodb/test/src/resume.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { MongoLSN, ZERO_LSN } from '@module/common/MongoLSN.js';
import { mongo } from '@powersync/lib-service-mongodb';
import { describe, expect, test } from 'vitest';

describe('mongo lsn resume tokens', () => {
test('LSN with resume tokens should be comparable', () => {
// Values without a resume token should be comparable
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(1)
}).comparable <
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10)
}).comparable
).true;

// Values with resume tokens should correctly compare
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(1),
resume_token: { _data: 'resume1' }
}).comparable <
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10),
resume_token: { _data: 'resume2' }
}).comparable
).true;

// The resume token should not affect comparison
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(1),
resume_token: { _data: '2' }
}).comparable <
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10),
resume_token: { _data: '1' }
}).comparable
).true;

// Resume token should not be required for comparison
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10),
resume_token: { _data: '2' }
}).comparable > // Switching the order to test this case
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(9)
}).comparable
).true;

// Comparison should be backwards compatible with old LSNs
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10),
resume_token: { _data: '2' }
}).comparable > ZERO_LSN
).true;
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10),
resume_token: { _data: '2' }
}).comparable >
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(1)
}).comparable.split('|')[0] // Simulate an old LSN
).true;
expect(
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(1),
resume_token: { _data: '2' }
}).comparable <
new MongoLSN({
timestamp: mongo.Timestamp.fromNumber(10)
}).comparable.split('|')[0] // Simulate an old LSN
).true;
});
});

0 comments on commit 0da4c50

Please sign in to comment.