From 064baf31ad70f5bacc600d126c0e45e146c5b65e Mon Sep 17 00:00:00 2001 From: Eric Mc Sween <5454374+emcsween@users.noreply.github.com> Date: Fri, 7 Feb 2025 08:34:03 -0500 Subject: [PATCH] Merge pull request #23478 from overleaf/em-doc-hash-diff Fix doc hash on delete + insert combination GitOrigin-RevId: ecea5f8ab01f24a9064be88611256b502500d91f --- .../app/js/UpdateCompressor.js | 9 +++++++- .../UpdateCompressor/UpdateCompressorTests.js | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/services/project-history/app/js/UpdateCompressor.js b/services/project-history/app/js/UpdateCompressor.js index 9010348598..b548b7529e 100644 --- a/services/project-history/app/js/UpdateCompressor.js +++ b/services/project-history/app/js/UpdateCompressor.js @@ -397,9 +397,16 @@ function _concatTwoUpdates(firstUpdate, secondUpdate) { // Make sure that commentIds metadata is propagated to inserts op.commentIds = secondOp.commentIds } - return mergeUpdatesWithOp(firstUpdate, secondUpdate, op) + const update = mergeUpdatesWithOp(firstUpdate, secondUpdate, op) + // Set the doc hash only on the last update + delete update.meta.doc_hash + return update } ) + const docHash = secondUpdate.meta.doc_hash + if (docHash != null && diffUpdates.length > 0) { + diffUpdates[diffUpdates.length - 1].meta.doc_hash = docHash + } // Doing a diff like this loses track of the doc lengths for each // update, so recalculate them diff --git a/services/project-history/test/unit/js/UpdateCompressor/UpdateCompressorTests.js b/services/project-history/test/unit/js/UpdateCompressor/UpdateCompressorTests.js index 04d5163476..c8d50b16f4 100644 --- a/services/project-history/test/unit/js/UpdateCompressor/UpdateCompressorTests.js +++ b/services/project-history/test/unit/js/UpdateCompressor/UpdateCompressorTests.js @@ -1565,6 +1565,27 @@ describe('UpdateCompressor', function () { }, ]) }) + + it('special case for delete + insert triggering diff', function () { + const meta = { ts: this.ts1, user_id: this.user_id, doc_length: 10 } + expect( + this.UpdateCompressor.compressUpdates([ + { op: { p: 3, d: 'foo' }, meta, v: 42 }, + { + op: { p: 3, i: 'bar' }, + meta: { ...meta, doc_hash: 'hash1' }, + v: 43, + }, + ]) + ).to.deep.equal([ + { op: { p: 3, d: 'foo' }, meta, v: 43 }, + { + op: { p: 3, i: 'bar' }, + meta: { ...meta, doc_length: 7, doc_hash: 'hash1' }, + v: 43, + }, + ]) + }) }) }) })