diff --git a/src/sdam/server_description.ts b/src/sdam/server_description.ts index 5068931b6a..c95f7cc905 100644 --- a/src/sdam/server_description.ts +++ b/src/sdam/server_description.ts @@ -258,10 +258,19 @@ export function compareTopologyVersion( } // TODO(NODE-2674): Preserve int64 sent from MongoDB - const currentCounter = Long.isLong(currentTv.counter) - ? currentTv.counter - : Long.fromNumber(currentTv.counter); - const newCounter = Long.isLong(newTv.counter) ? newTv.counter : Long.fromNumber(newTv.counter); + const currentCounter = + typeof currentTv.counter === 'bigint' + ? Long.fromBigInt(currentTv.counter) + : Long.isLong(currentTv.counter) + ? currentTv.counter + : Long.fromNumber(currentTv.counter); + + const newCounter = + typeof newTv.counter === 'bigint' + ? Long.fromBigInt(newTv.counter) + : Long.isLong(newTv.counter) + ? newTv.counter + : Long.fromNumber(newTv.counter); return currentCounter.compare(newCounter); } diff --git a/test/unit/sdam/server_description.test.ts b/test/unit/sdam/server_description.test.ts index e91a0863bd..14c3ae0dbd 100644 --- a/test/unit/sdam/server_description.test.ts +++ b/test/unit/sdam/server_description.test.ts @@ -130,6 +130,22 @@ describe('ServerDescription', function () { currentTv: { processId: processIdZero, counter: Long.fromNumber(2) }, newTv: { processId: processIdZero, counter: Long.fromNumber(2) }, out: 0 + }, + { + title: 'when process ids are equal and both counter values are zero bigints', + // @ts-expect-error: Testing that the function handles bigints + currentTv: { processId: processIdZero, counter: 0n }, + // @ts-expect-error: Testing that the function handles bigints + newTv: { processId: processIdZero, counter: 0n }, + out: 0 + }, + { + title: 'when process ids are equal and both counter values are non-zero bigints', + // @ts-expect-error: Testing that the function handles bigints + currentTv: { processId: processIdZero, counter: 2n }, + // @ts-expect-error: Testing that the function handles bigints + newTv: { processId: processIdZero, counter: 2n }, + out: 0 } ]; const compareTopologyVersionLessThanTests: CompareTopologyVersionTest[] = [ @@ -178,6 +194,12 @@ describe('ServerDescription', function () { currentTv: { processId: processIdZero, counter: Long.fromNumber(3) }, newTv: { processId: processIdZero, counter: Long.fromNumber(2) }, out: 1 + }, + { + title: 'when processIds are equal but new counter is less than current (bigint)', + currentTv: { processId: processIdZero, counter: 3n }, + newTv: { processId: processIdZero, counter: 2n }, + out: 1 } ];