Files
overleaf-cep/libraries/overleaf-editor-core/test/unit/rebase.test.js
T
Jakob AckermannandClaude Opus 5 7ae8db6b12 [editor-core] extract shared pieces for history-ot in editor (#36828)
* [editor-core] share the pieces the editor, real-time and history-v1 all need

Project-level history-OT has the browser, real-time and history-v1 all reason
about the same changes, so the rules they reason with move into the shared
library rather than being reimplemented on each side.

- `origin/editor_origin.js` plus `EDITOR_ORIGIN_KIND` and its schema entry: the
  origin an editor client's change carries, holding the editorId that scopes a
  change's timestamp.
- `change_identity.js`: how a submission is recognised when it comes back, so a
  resend of a change that already landed is deduplicated rather than applied
  twice.
- `rebase.js`: transforming a change onto concurrent ones, in the same order on
  both sides, which is what lets the server acknowledge a change with only the
  version it landed at.
- `text_file_defaults.js`: the doc-vs-file extension lists, now read from here
  by web's settings instead of being spelled out again.
- `blob_string_length.js`: the string length of a blob, used by history-v1's
  blob store and by web when it writes an uploaded file into history.
- `file_tree_diff.js`: file-tree operations to a diff, used by
  project-history's FileTreeDiffGenerator.
- `Blob.EMPTY_HASH`, so callers can answer for the empty blob without storing
  it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* [project-history] build the file-tree diff with the shared helper

FileTreeDiffGenerator's hand-rolled walk over add/edit/move/delete operations
becomes a call to `buildFileTreeDiff` from overleaf-editor-core, so the editor
and project-history read a file-tree change the same way. Adds the unit tests
the decaffeinated original never had.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* [web] read the editable-file lists from overleaf-editor-core

web's settings spelled out the text extensions and editable filenames that
decide whether a file becomes a doc, and every other service that has to make
that call carried its own copy. The lists now live in overleaf-editor-core and
web builds settings.textExtensions and settings.editableFilenames from them,
keeping the ADDITIONAL_TEXT_EXTENSIONS override for Server CE/Pro on top.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* [history-v1] read a blob's string length from overleaf-editor-core

Two copies of getStringLengthOfFile lived here, the one in recover_zip.js
carrying a TODO asking for exactly this. Both now import the shared
implementation, which the blob store keeps re-exporting for backupArchiver.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
GitOrigin-RevId: 31948332f416f56848e3b0dfb5142a4c9f93f508
2026-09-17 12:06:17 +00:00

175 lines
5.3 KiB
JavaScript

'use strict'
const { expect } = require('chai')
const {
Change,
File,
Operation,
Origin,
TextOperation,
rebaseChanges,
EDITOR_ORIGIN_KIND,
} = require('../..')
describe('rebaseChanges', function () {
const TIMESTAMP = new Date('2025-01-02T03:04:05.678Z')
function change(operations, overrides = {}) {
return new Change(
operations,
overrides.timestamp || TIMESTAMP,
overrides.authors,
overrides.origin,
overrides.v2Authors
)
}
function addFile(pathname, content = 'content') {
return Operation.addFile(pathname, File.fromString(content))
}
it('leaves operations alone when nothing conflicts', function () {
const ours = [change([addFile('ours.tex')])]
const rebased = rebaseChanges(ours, [change([addFile('theirs.tex')])])
expect(rebased).to.have.length(1)
expect(rebased[0].getOperations()).to.have.length(1)
expect(rebased[0].getOperations()[0].getPathname()).to.equal('ours.tex')
})
it('drops a change whose only operation transforms away', function () {
// Two clients add the same pathname. transformAddFileAddFile resolves this
// in favour of the change that landed first, so ours becomes a no-op.
const ours = [change([addFile('main.tex', 'ours')])]
const rebased = rebaseChanges(ours, [
change([addFile('main.tex', 'theirs')]),
])
expect(rebased).to.deep.equal([])
})
it('prunes no-op operations but keeps the change when others survive', function () {
const ours = [change([addFile('main.tex'), addFile('other.tex')])]
const rebased = rebaseChanges(ours, [change([addFile('main.tex')])])
expect(rebased).to.have.length(1)
const operations = rebased[0].getOperations()
expect(operations).to.have.length(1)
expect(operations[0].getPathname()).to.equal('other.tex')
})
it('drops only the changes that empty out, keeping the rest in order', function () {
const ours = [
change([addFile('a.tex')]),
change([addFile('collides.tex')]),
change([addFile('b.tex')]),
]
const rebased = rebaseChanges(ours, [change([addFile('collides.tex')])])
expect(rebased.map(c => c.getOperations()[0].getPathname())).to.deep.equal([
'a.tex',
'b.tex',
])
})
it('returns an empty array when every change transforms away', function () {
const ours = [change([addFile('a.tex')]), change([addFile('b.tex')])]
const rebased = rebaseChanges(ours, [
change([addFile('a.tex')]),
change([addFile('b.tex')]),
])
expect(rebased).to.deep.equal([])
})
it('transforms against each intervening change in turn', function () {
// A rename chain: they moved a.tex -> b.tex, then b.tex -> c.tex. Our edit
// of a.tex has to end up addressing c.tex, which only happens if the second
// intervening change is applied to the already-transformed operation.
const ours = [
change([
Operation.editFile(
'a.tex',
TextOperation.fromJSON({ textOperation: ['hello'] })
),
]),
]
const rebased = rebaseChanges(ours, [
change([Operation.moveFile('a.tex', 'b.tex')]),
change([Operation.moveFile('b.tex', 'c.tex')]),
])
expect(rebased).to.have.length(1)
expect(rebased[0].getOperations()[0].getPathname()).to.equal('c.tex')
})
it('transforms a sequence of our changes against one of theirs', function () {
// Both of our changes address a.tex, which they renamed. Each of ours has
// to be transformed, not just the first.
const ours = [
change([
Operation.editFile(
'a.tex',
TextOperation.fromJSON({ textOperation: ['one'] })
),
]),
change([
Operation.editFile(
'a.tex',
TextOperation.fromJSON({ textOperation: [3, 'two'] })
),
]),
]
const rebased = rebaseChanges(ours, [
change([Operation.moveFile('a.tex', 'renamed.tex')]),
])
expect(rebased).to.have.length(2)
expect(rebased.map(c => c.getOperations()[0].getPathname())).to.deep.equal([
'renamed.tex',
'renamed.tex',
])
})
it('preserves the fields a resend is recognised by', function () {
// history-v1 identifies its own change read back from history by origin
// kind, author and timestamp. A rebase must not disturb any of them, or
// deduplicating a resend stops working.
const authorId = '65b9d7fb2a1b2c3d4e5f6a7b'
const ours = [
change([addFile('main.tex'), addFile('survivor.tex')], {
origin: new Origin(EDITOR_ORIGIN_KIND),
v2Authors: [authorId],
}),
]
const rebased = rebaseChanges(ours, [change([addFile('main.tex')])])
expect(rebased).to.have.length(1)
const raw = rebased[0].toRaw()
expect(raw.origin).to.deep.equal({ kind: EDITOR_ORIGIN_KIND })
expect(raw.v2Authors).to.deep.equal([authorId])
expect(raw.timestamp).to.equal(TIMESTAMP.toISOString())
})
it('leaves ours untouched when theirs is empty', function () {
const ours = [change([addFile('main.tex')])]
const rebased = rebaseChanges(ours, [])
expect(rebased).to.have.length(1)
expect(rebased[0].getOperations()[0].getPathname()).to.equal('main.tex')
})
it('returns an empty array when ours is empty', function () {
expect(rebaseChanges([], [change([addFile('main.tex')])])).to.deep.equal([])
})
})