From 263537294ef9d8553614f1b09fee896902c06498 Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Thu, 22 Oct 2026 13:30:11 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Cover=20`getCommittedOpVersion`=20d?= =?UTF-8?q?eterministically?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At the moment, Coveralls swings by 0.8% between runs of identical code: #186 only touches `package.json`, but still lost coverage. The only test that reaches the found-op path of `getCommittedOpVersion` is sharedb's "does not fail when resubmitting a create op", and only when it wins a race: about once in 60 suite runs. This change tests `getCommittedOpVersion` directly, including its bounded read from the earliest matching op. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- test/test_mongo.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/test/test_mongo.js b/test/test_mongo.js index 62b367b..016f09d 100644 --- a/test/test_mongo.js +++ b/test/test_mongo.js @@ -676,6 +676,92 @@ describe('mongo db', function() { done(); }); }); + + describe('getCommittedOpVersion', function() { + var sandbox = sinon.createSandbox(); + + beforeEach(function(done) { + var self = this; + var db = this.db; + var create = {v: 0, create: {}, src: 'xyz', seq: 1}; + var edit = {v: 1, op: [], src: 'abc', seq: 1}; + async.waterfall([ + db.commit.bind(db, 'testcollection', 'foo', create, {type: 'json0', v: 1, data: {}}, null), + function(succeeded, next) { + db.getSnapshot('testcollection', 'foo', {$submit: true}, null, next); + }, + function(snapshot, next) { + snapshot.v = 2; + db.commit('testcollection', 'foo', edit, snapshot, null, next); + }, + function(succeeded, next) { + db.getSnapshot('testcollection', 'foo', {$submit: true}, null, next); + }, + function(snapshot, next) { + self.snapshot = snapshot; + sandbox.spy(db, 'getOpsToSnapshot'); + next(); + } + ], done); + }); + + afterEach(function() { + sandbox.restore(); + }); + + function insertUnlinkedOp(mongo, op) { + return mongo.collection('o_testcollection').insertOne(Object.assign({d: 'foo', o: null}, op)); + } + + it('returns the version of a committed op, reading ops only from that version', function(done) { + var db = this.db; + var op = {src: 'abc', seq: 1}; + db.getCommittedOpVersion('testcollection', 'foo', this.snapshot, op, null, function(err, version) { + if (err) return done(err); + expect(version).to.equal(1); + expect(db.getOpsToSnapshot.calledOnceWith('testcollection', 'foo', 1)).to.equal(true); + done(); + }); + }); + + it('returns no version without reading ops for an op that was never written', function(done) { + var db = this.db; + var op = {src: 'abc', seq: 2}; + db.getCommittedOpVersion('testcollection', 'foo', this.snapshot, op, null, function(err, version) { + if (err) return done(err); + expect(version).not.to.exist; + expect(db.getOpsToSnapshot.notCalled).to.equal(true); + done(); + }); + }); + + it('returns no version for a written op that the snapshot does not link to', function(done) { + var db = this.db; + var snapshot = this.snapshot; + var op = {src: 'def', seq: 1}; + insertUnlinkedOp(this.mongo, {v: 1, op: [], src: 'def', seq: 1}).then(function() { + db.getCommittedOpVersion('testcollection', 'foo', snapshot, op, null, function(err, version) { + if (err) return done(err); + expect(version).not.to.exist; + expect(db.getOpsToSnapshot.calledOnce).to.equal(true); + done(); + }); + }, done); + }); + + it('returns the committed version of an op that was written again later', function(done) { + var db = this.db; + var snapshot = this.snapshot; + var op = {src: 'abc', seq: 1}; + insertUnlinkedOp(this.mongo, {v: 2, op: [], src: 'abc', seq: 1}).then(function() { + db.getCommittedOpVersion('testcollection', 'foo', snapshot, op, null, function(err, version) { + if (err) return done(err); + expect(version).to.equal(1); + done(); + }); + }, done); + }); + }); }); describe('options', function() {