From 1833bd3d00c6c5f271d4f899dc76ed6315cc4a55 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween <5454374+emcsween@users.noreply.github.com> Date: Tue, 15 Jul 2025 10:15:02 -0400 Subject: [PATCH] Merge pull request #27126 from overleaf/em-fix-persist-changes Fix chunk creation over a one change chunk GitOrigin-RevId: aecae334849522975b83c77224ee27db64de4ed8 --- .../storage/lib/chunk_store/index.js | 2 +- .../acceptance/js/storage/chunk_store.test.js | 3 ++- .../js/storage/persist_changes.test.js | 22 +++++++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/services/history-v1/storage/lib/chunk_store/index.js b/services/history-v1/storage/lib/chunk_store/index.js index 286a8d8764..f387b68d90 100644 --- a/services/history-v1/storage/lib/chunk_store/index.js +++ b/services/history-v1/storage/lib/chunk_store/index.js @@ -246,7 +246,7 @@ async function create(projectId, chunk, earliestChangeTimestamp) { const opts = {} if (chunkStart > 0) { - const oldChunk = await backend.getChunkForVersion(projectId, chunkStart - 1) + const oldChunk = await backend.getChunkForVersion(projectId, chunkStart) if (oldChunk.endVersion !== chunkStart) { throw new ChunkVersionConflictError( diff --git a/services/history-v1/test/acceptance/js/storage/chunk_store.test.js b/services/history-v1/test/acceptance/js/storage/chunk_store.test.js index 8b06b8e412..df1e36e9cd 100644 --- a/services/history-v1/test/acceptance/js/storage/chunk_store.test.js +++ b/services/history-v1/test/acceptance/js/storage/chunk_store.test.js @@ -8,6 +8,7 @@ const { ObjectId } = require('mongodb') const { projects } = require('../../../../storage/lib/mongodb') const { ChunkVersionConflictError, + VersionNotFoundError, } = require('../../../../storage/lib/chunk_store/errors') const { @@ -396,7 +397,7 @@ describe('chunkStore', function () { const newChunk = makeChunk([change], 7) await expect( chunkStore.create(projectId, newChunk) - ).to.be.rejectedWith(ChunkVersionConflictError) + ).to.be.rejectedWith(VersionNotFoundError) const latestChunk = await chunkStore.loadLatest(projectId) expect(latestChunk.toRaw()).to.deep.equal(thirdChunk.toRaw()) }) diff --git a/services/history-v1/test/acceptance/js/storage/persist_changes.test.js b/services/history-v1/test/acceptance/js/storage/persist_changes.test.js index 0bb8836cc1..4c491db4a3 100644 --- a/services/history-v1/test/acceptance/js/storage/persist_changes.test.js +++ b/services/history-v1/test/acceptance/js/storage/persist_changes.test.js @@ -67,7 +67,7 @@ describe('persistChanges', function () { expect(chunk.getChanges().length).to.equal(1) }) - it('persists changes in two chunks', async function () { + it('persists changes in three chunks', async function () { const limitsToPersistImmediately = { maxChunkChanges: 1, minChangeTimestamp: farFuture, @@ -84,7 +84,12 @@ describe('persistChanges', function () { new Date(), [] ) - const changes = [firstChange, secondChange] + const thirdChange = new Change( + [new AddFileOperation('c.tex', File.fromString(''))], + new Date(), + [] + ) + const changes = [firstChange, secondChange, thirdChange] await chunkStore.initializeProject(projectId) const result = await persistChanges( @@ -99,20 +104,23 @@ describe('persistChanges', function () { 'a.tex': { content: '', }, + 'b.tex': { + content: '', + }, }, }) - const history = new History(snapshot, [secondChange]) - const currentChunk = new Chunk(history, 1) + const history = new History(snapshot, [thirdChange]) + const currentChunk = new Chunk(history, 2) expect(result).to.deep.equal({ - numberOfChangesPersisted: 2, + numberOfChangesPersisted: 3, originalEndVersion: 0, currentChunk, resyncNeeded: false, }) const chunk = await chunkStore.loadLatest(projectId) - expect(chunk.getStartVersion()).to.equal(1) - expect(chunk.getEndVersion()).to.equal(2) + expect(chunk.getStartVersion()).to.equal(2) + expect(chunk.getEndVersion()).to.equal(3) expect(chunk.getChanges().length).to.equal(1) })