2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
2021-04-01 15:51:00 -04:00
|
|
|
const { expect } = require('chai')
|
2020-05-06 06:11:36 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2017-03-15 10:12:06 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
const MODULE_PATH = '../../../../app/js/RangesManager.js'
|
2024-02-13 08:15:43 -05:00
|
|
|
const TEST_USER_ID = 'user-id-123'
|
2024-02-06 08:08:59 -05:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('RangesManager', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.RangesManager = SandboxedModule.require(MODULE_PATH, {
|
2023-05-23 04:10:44 -04:00
|
|
|
requires: {
|
|
|
|
'@overleaf/metrics': (this.Metrics = { histogram: sinon.stub() }),
|
|
|
|
},
|
2022-04-07 05:02:19 -04:00
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
this.doc_id = 'doc-id-123'
|
|
|
|
this.project_id = 'project-id-123'
|
2024-02-13 08:15:43 -05:00
|
|
|
this.user_id = TEST_USER_ID
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2017-03-15 10:12:06 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('applyUpdate', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ops = [{ i: 'two ', p: 4 }]
|
|
|
|
this.historyOps = [{ i: 'two ', p: 4, hpos: 4 }]
|
|
|
|
this.meta = { user_id: this.user_id }
|
|
|
|
this.updates = [{ meta: this.meta, op: this.ops }]
|
|
|
|
this.ranges = {
|
|
|
|
comments: makeRanges([{ c: 'three ', p: 4 }]),
|
|
|
|
changes: makeRanges([{ i: 'five', p: 15 }]),
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
this.newDocLines = ['one two three four five']
|
2024-02-13 08:15:43 -05:00
|
|
|
// old is "one three four five"
|
|
|
|
})
|
2017-05-09 11:16:25 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.updates,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.newDocLines
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should return the modified the comments and changes', function () {
|
|
|
|
expect(this.result.rangesWereCollapsed).to.equal(false)
|
|
|
|
this.result.newRanges.comments[0].op.should.deep.equal({
|
2020-05-06 06:11:36 -04:00
|
|
|
c: 'three ',
|
2021-07-13 07:04:42 -04:00
|
|
|
p: 8,
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result.newRanges.changes[0].op.should.deep.equal({
|
2020-05-06 06:11:36 -04:00
|
|
|
i: 'five',
|
2021-07-13 07:04:42 -04:00
|
|
|
p: 19,
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
|
|
|
it('should return unmodified updates for the history', function () {
|
|
|
|
expect(this.result.historyUpdates).to.deep.equal(this.updates)
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('with empty comments', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges.comments = []
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.updates,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.newDocLines
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
2019-04-11 08:25:03 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should return an object with no comments', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
// Save space in redis and don't store just {}
|
2024-02-06 08:08:59 -05:00
|
|
|
expect(this.result.newRanges.comments).to.be.undefined
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
|
|
|
it('should return unmodified updates for the history', function () {
|
|
|
|
expect(this.result.historyUpdates).to.deep.equal(this.updates)
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('with empty changes', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges.changes = []
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.updates,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.newDocLines
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
2017-05-09 11:16:25 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should return an object with no changes', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
// Save space in redis and don't store just {}
|
2024-02-06 08:08:59 -05:00
|
|
|
expect(this.result.newRanges.changes).to.be.undefined
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
|
|
|
it('should return unmodified updates for the history', function () {
|
|
|
|
expect(this.result.historyUpdates).to.deep.equal(this.updates)
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2017-05-09 11:16:25 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('with too many comments', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RangesManager.MAX_COMMENTS = 2
|
2024-02-13 08:15:43 -05:00
|
|
|
this.updates = makeUpdates([{ c: 'one', p: 0, t: 'thread-id-1' }])
|
|
|
|
this.ranges = {
|
|
|
|
comments: makeRanges([
|
|
|
|
{ c: 'three ', p: 4, t: 'thread-id-2' },
|
|
|
|
{ c: 'four ', p: 10, t: 'thread-id-3' },
|
|
|
|
]),
|
2021-07-13 07:04:42 -04:00
|
|
|
changes: [],
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
})
|
2017-05-09 11:16:25 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should throw an error', function () {
|
|
|
|
expect(() => {
|
|
|
|
this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.updates,
|
|
|
|
this.newDocLines
|
|
|
|
)
|
|
|
|
}).to.throw('too many comments or tracked changes')
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('with too many changes', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RangesManager.MAX_CHANGES = 2
|
2024-02-13 08:15:43 -05:00
|
|
|
this.updates = makeUpdates([{ i: 'one ', p: 0 }], {
|
|
|
|
tc: 'track-changes-id-yes',
|
|
|
|
})
|
|
|
|
this.ranges = {
|
|
|
|
changes: makeRanges([
|
2020-05-06 06:11:36 -04:00
|
|
|
{
|
2024-02-13 08:15:43 -05:00
|
|
|
i: 'three',
|
|
|
|
p: 4,
|
2020-05-06 06:11:36 -04:00
|
|
|
},
|
|
|
|
{
|
2024-02-13 08:15:43 -05:00
|
|
|
i: 'four',
|
|
|
|
p: 10,
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2024-02-13 08:15:43 -05:00
|
|
|
]),
|
2021-07-13 07:04:42 -04:00
|
|
|
comments: [],
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
|
|
|
this.newDocLines = ['one two three four']
|
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should throw an error', function () {
|
|
|
|
expect(() => {
|
|
|
|
this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.updates,
|
|
|
|
this.newDocLines
|
|
|
|
)
|
|
|
|
}).to.throw('too many comments or tracked changes')
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('inconsistent changes', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
this.updates = makeUpdates([{ c: "doesn't match", p: 0 }])
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2017-05-09 11:16:25 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should throw an error', function () {
|
|
|
|
expect(() => {
|
|
|
|
this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.updates,
|
|
|
|
this.newDocLines
|
|
|
|
)
|
2024-03-04 05:35:09 -05:00
|
|
|
}).to.throw('insertion does not match text in document')
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2022-04-07 05:02:19 -04:00
|
|
|
describe('with an update that collapses a range', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
this.updates = makeUpdates([{ d: 'one', p: 0, t: 'thread-id-1' }])
|
|
|
|
this.ranges = {
|
|
|
|
comments: makeRanges([
|
2020-05-06 06:11:36 -04:00
|
|
|
{
|
2024-02-13 08:15:43 -05:00
|
|
|
c: 'n',
|
|
|
|
p: 1,
|
|
|
|
t: 'thread-id-2',
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2024-02-13 08:15:43 -05:00
|
|
|
]),
|
2021-07-13 07:04:42 -04:00
|
|
|
changes: [],
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.applyUpdate(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2020-05-06 06:11:36 -04:00
|
|
|
this.updates,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.newDocLines
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should return ranges_were_collapsed == true', function () {
|
|
|
|
expect(this.result.rangesWereCollapsed).to.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
2022-04-07 05:02:19 -04:00
|
|
|
|
|
|
|
describe('with an update that deletes ranges', function () {
|
|
|
|
beforeEach(function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
this.updates = makeUpdates([{ d: 'one two three four five', p: 0 }])
|
|
|
|
this.ranges = {
|
|
|
|
comments: makeRanges([{ c: 'n', p: 1, t: 'thread-id-2' }]),
|
|
|
|
changes: makeRanges([{ i: 'hello', p: 1, t: 'thread-id-2' }]),
|
2022-04-07 05:02:19 -04:00
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.applyUpdate(
|
2022-04-07 05:02:19 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
2024-02-13 08:15:43 -05:00
|
|
|
this.ranges,
|
2022-04-07 05:02:19 -04:00
|
|
|
this.updates,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.newDocLines
|
2022-04-07 05:02:19 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should increment the range-delta histogram', function () {
|
|
|
|
this.Metrics.histogram.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should return ranges_were_collapsed == true', function () {
|
|
|
|
expect(this.result.rangesWereCollapsed).to.equal(true)
|
2022-04-07 05:02:19 -04:00
|
|
|
})
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
describe('with comment updates', function () {
|
2024-02-13 08:15:43 -05:00
|
|
|
beforeEach(function () {
|
|
|
|
this.updates = makeUpdates([
|
2024-02-16 07:40:13 -05:00
|
|
|
{ i: 'two ', p: 4 },
|
|
|
|
{ c: 'one', p: 0 },
|
2024-02-13 08:15:43 -05:00
|
|
|
])
|
2024-02-16 07:40:13 -05:00
|
|
|
this.ranges = {}
|
2024-02-13 08:15:43 -05:00
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
it('should not send comments to the history', function () {
|
|
|
|
expect(this.result.historyUpdates[0].op).to.deep.equal([
|
|
|
|
{ i: 'two ', p: 4 },
|
2024-02-13 08:15:43 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
describe('with history ranges support', function () {
|
|
|
|
describe('inserts among tracked deletes', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// original text is "on[1]e[22] [333](three) fo[4444]ur five"
|
|
|
|
// [] denotes tracked deletes
|
|
|
|
// () denotes tracked inserts
|
|
|
|
this.ranges = {
|
|
|
|
changes: makeRanges([
|
|
|
|
{ d: '1', p: 2 },
|
|
|
|
{ d: '22', p: 3 },
|
|
|
|
{ d: '333', p: 4 },
|
|
|
|
{ i: 'three', p: 4 },
|
|
|
|
{ d: '4444', p: 12 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
this.updates = makeUpdates([
|
|
|
|
{ i: 'zero ', p: 0 },
|
|
|
|
{ i: 'two ', p: 9, u: true },
|
|
|
|
])
|
|
|
|
this.newDocLines = ['zero one two three four five']
|
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines,
|
|
|
|
{ historyRangesSupport: true }
|
|
|
|
)
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
it('should offset the hpos by the length of tracked deletes before the insert', function () {
|
|
|
|
expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
|
|
|
|
[{ i: 'zero ', p: 0 }],
|
|
|
|
// 'two' is added just before the "333" tracked delete
|
|
|
|
[{ i: 'two ', p: 9, u: true, hpos: 12 }],
|
|
|
|
])
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
})
|
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
describe('tracked delete rejections', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// original text is "one [two ]three four five"
|
|
|
|
// [] denotes tracked deletes
|
|
|
|
this.ranges = {
|
|
|
|
changes: makeRanges([{ d: 'two ', p: 4 }]),
|
|
|
|
}
|
|
|
|
this.updates = makeUpdates([{ i: 'tw', p: 4, u: true }])
|
|
|
|
this.newDocLines = ['one twthree four five']
|
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines,
|
|
|
|
{ historyRangesSupport: true }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should mark the insert as a tracked delete rejection where appropriate', function () {
|
|
|
|
expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
|
|
|
|
[{ i: 'tw', p: 4, u: true, trackedDeleteRejection: true }],
|
|
|
|
])
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
})
|
|
|
|
|
2024-03-26 07:28:14 -04:00
|
|
|
describe('deletes over tracked changes', function () {
|
2024-02-16 07:40:13 -05:00
|
|
|
beforeEach(function () {
|
|
|
|
// original text is "on[1]e [22](three) f[333]ou[4444]r [55555]five"
|
|
|
|
// [] denotes tracked deletes
|
|
|
|
// () denotes tracked inserts
|
|
|
|
this.ranges = {
|
|
|
|
comments: [],
|
|
|
|
changes: makeRanges([
|
|
|
|
{ d: '1', p: 2 },
|
|
|
|
{ d: '22', p: 4 },
|
|
|
|
{ i: 'three', p: 4 },
|
|
|
|
{ d: '333', p: 11 },
|
|
|
|
{ d: '4444', p: 13 },
|
|
|
|
{ d: '55555', p: 15 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
this.updates = makeUpdates([
|
|
|
|
{ d: 'four ', p: 10 },
|
|
|
|
{ d: 'three ', p: 4 },
|
|
|
|
])
|
|
|
|
this.newDocLines = ['one five']
|
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines,
|
|
|
|
{ historyRangesSupport: true }
|
|
|
|
)
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
it('should split and offset deletes appropriately', function () {
|
|
|
|
expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
|
|
|
|
[
|
|
|
|
// the "four" delete has tracked deletes inside it, add splits
|
|
|
|
{
|
|
|
|
d: 'four ',
|
|
|
|
p: 10,
|
|
|
|
hpos: 13,
|
2024-03-26 07:28:14 -04:00
|
|
|
trackedChanges: [
|
|
|
|
{ type: 'delete', offset: 1, length: 3 },
|
|
|
|
{ type: 'delete', offset: 3, length: 4 },
|
2024-02-16 07:40:13 -05:00
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
// the "three" delete is offset to the right by the two first tracked
|
|
|
|
// deletes
|
2024-03-26 07:28:14 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
d: 'three ',
|
|
|
|
p: 4,
|
|
|
|
hpos: 7,
|
|
|
|
trackedChanges: [{ type: 'insert', offset: 0, length: 5 }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('deletes that overlap tracked inserts', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// original text is "(one) (three) (four) five"
|
|
|
|
// [] denotes tracked deletes
|
|
|
|
// () denotes tracked inserts
|
|
|
|
this.ranges = {
|
|
|
|
comments: [],
|
|
|
|
changes: makeRanges([
|
|
|
|
{ i: 'one', p: 0 },
|
|
|
|
{ i: 'three', p: 4 },
|
|
|
|
{ i: 'four', p: 10 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
this.updates = makeUpdates(
|
|
|
|
[
|
|
|
|
{ d: 'ne th', p: 1 },
|
|
|
|
{ d: 'ou', p: 6 },
|
|
|
|
],
|
|
|
|
{ tc: 'tracked-change-id' }
|
|
|
|
)
|
|
|
|
this.newDocLines = ['oree fr five']
|
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines,
|
|
|
|
{ historyRangesSupport: true }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should split and offset deletes appropriately', function () {
|
|
|
|
expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
|
|
|
|
[
|
|
|
|
{
|
|
|
|
d: 'ne th',
|
|
|
|
p: 1,
|
|
|
|
trackedChanges: [
|
|
|
|
{ type: 'insert', offset: 0, length: 2 },
|
|
|
|
{ type: 'insert', offset: 3, length: 2 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
d: 'ou',
|
|
|
|
p: 6,
|
|
|
|
hpos: 7,
|
|
|
|
trackedChanges: [{ type: 'insert', offset: 0, length: 2 }],
|
|
|
|
},
|
|
|
|
],
|
2024-02-16 07:40:13 -05:00
|
|
|
])
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
})
|
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
describe('comments among tracked deletes', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// original text is "on[1]e[22] [333](three) fo[4444]ur five"
|
|
|
|
// [] denotes tracked deletes
|
|
|
|
// () denotes tracked inserts
|
|
|
|
this.ranges = {
|
|
|
|
changes: makeRanges([
|
|
|
|
{ d: '1', p: 2 },
|
|
|
|
{ d: '22', p: 3 },
|
|
|
|
{ d: '333', p: 4 },
|
|
|
|
{ i: 'three', p: 4 },
|
|
|
|
{ d: '4444', p: 12 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
this.updates = makeUpdates([
|
|
|
|
{ c: 'three ', p: 4 },
|
|
|
|
{ c: 'four ', p: 10 },
|
|
|
|
])
|
|
|
|
this.newDocLines = ['one three four five']
|
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines,
|
|
|
|
{ historyRangesSupport: true }
|
|
|
|
)
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
it('should offset the hpos by the length of tracked deletes before the insert', function () {
|
|
|
|
expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
|
|
|
|
[{ c: 'three ', p: 4, hpos: 10 }],
|
|
|
|
[{ c: 'four ', p: 10, hpos: 16, hlen: 9 }],
|
|
|
|
])
|
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
})
|
2024-02-14 07:40:51 -05:00
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
describe('inserts inside comments', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
// original text is "one three four five"
|
|
|
|
this.ranges = {
|
|
|
|
comments: makeRanges([
|
|
|
|
{ c: 'three', p: 4, t: 'comment-id-1' },
|
|
|
|
{ c: 'ree four', p: 6, t: 'comment-id-2' },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
this.updates = makeUpdates([
|
|
|
|
{ i: '[before]', p: 4 },
|
|
|
|
{ i: '[inside]', p: 13 }, // 4 + 8 + 1
|
|
|
|
{ i: '[overlap]', p: 23 }, // 13 + 8 + 2
|
|
|
|
{ i: '[after]', p: 39 }, // 23 + 9 + 7
|
|
|
|
])
|
|
|
|
this.newDocLines = [
|
|
|
|
'one [before]t[inside]hr[overlap]ee four[after] five',
|
|
|
|
]
|
|
|
|
this.result = this.RangesManager.applyUpdate(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.ranges,
|
|
|
|
this.updates,
|
|
|
|
this.newDocLines,
|
|
|
|
{ historyRangesSupport: true }
|
|
|
|
)
|
|
|
|
})
|
2024-02-14 07:40:51 -05:00
|
|
|
|
2024-02-16 07:40:13 -05:00
|
|
|
it('should add the proper commentIds properties to ops', function () {
|
|
|
|
expect(this.result.historyUpdates.map(x => x.op)).to.deep.equal([
|
|
|
|
[{ i: '[before]', p: 4 }],
|
|
|
|
[{ i: '[inside]', p: 13, commentIds: ['comment-id-1'] }],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
i: '[overlap]',
|
|
|
|
p: 23,
|
|
|
|
commentIds: ['comment-id-1', 'comment-id-2'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[{ i: '[after]', p: 39 }],
|
|
|
|
])
|
|
|
|
})
|
2024-02-14 07:40:51 -05:00
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2017-05-12 09:42:40 -04:00
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
describe('acceptChanges', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.RangesManager = SandboxedModule.require(MODULE_PATH, {
|
2020-05-06 06:11:36 -04:00
|
|
|
requires: {
|
2022-10-05 08:17:32 -04:00
|
|
|
'@overleaf/ranges-tracker': (this.RangesTracker =
|
|
|
|
SandboxedModule.require('@overleaf/ranges-tracker')),
|
2023-05-23 04:10:44 -04:00
|
|
|
'@overleaf/metrics': {},
|
2021-07-13 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
this.ranges = {
|
|
|
|
comments: [],
|
2024-02-13 08:15:43 -05:00
|
|
|
changes: makeRanges([
|
|
|
|
{ i: 'lorem', p: 0 },
|
|
|
|
{ i: 'ipsum', p: 10 },
|
|
|
|
{ i: 'dolor', p: 20 },
|
|
|
|
{ i: 'sit', p: 30 },
|
|
|
|
{ i: 'amet', p: 40 },
|
|
|
|
]),
|
2020-05-06 06:11:36 -04:00
|
|
|
}
|
2024-02-06 08:08:59 -05:00
|
|
|
this.removeChangeIdsSpy = sinon.spy(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.RangesTracker.prototype,
|
|
|
|
'removeChangeIds'
|
2024-02-06 08:08:59 -05:00
|
|
|
)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully with a single change', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
beforeEach(function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.change_ids = [this.ranges.changes[1].id]
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.acceptChanges(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.change_ids,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.ranges
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log the call with the correct number of changes', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.logger.debug
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith('accepting 1 changes in ranges')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should delegate the change removal to the ranges tracker', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove the change', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
expect(
|
|
|
|
this.result.changes.find(
|
2021-07-13 07:04:42 -04:00
|
|
|
change => change.id === this.ranges.changes[1].id
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
).to.be.undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the original number of changes minus 1', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result.changes.length.should.equal(this.ranges.changes.length - 1)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should not touch other changes', function () {
|
|
|
|
for (const i of [0, 2, 3, 4]) {
|
2020-05-06 06:11:36 -04:00
|
|
|
expect(
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result.changes.find(
|
2021-07-13 07:04:42 -04:00
|
|
|
change => change.id === this.ranges.changes[i].id
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
).to.deep.equal(this.ranges.changes[i])
|
2024-02-06 08:08:59 -05:00
|
|
|
}
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
describe('successfully with multiple changes', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
this.change_ids = [
|
|
|
|
this.ranges.changes[1].id,
|
|
|
|
this.ranges.changes[3].id,
|
2021-07-13 07:04:42 -04:00
|
|
|
this.ranges.changes[4].id,
|
2020-05-06 06:11:36 -04:00
|
|
|
]
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result = this.RangesManager.acceptChanges(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.change_ids,
|
2024-02-06 08:08:59 -05:00
|
|
|
this.ranges
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should log the call with the correct number of changes', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.logger.debug
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(`accepting ${this.change_ids.length} changes in ranges`)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should delegate the change removal to the ranges tracker', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.removeChangeIdsSpy.calledWith(this.change_ids).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove the changes', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
for (const i of [1, 3, 4]) {
|
|
|
|
expect(
|
|
|
|
this.result.changes.find(
|
|
|
|
change => change.id === this.ranges.changes[i].id
|
|
|
|
)
|
|
|
|
).to.be.undefined
|
|
|
|
}
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the original number of changes minus the number of accepted changes', function () {
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result.changes.length.should.equal(this.ranges.changes.length - 3)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2024-02-06 08:08:59 -05:00
|
|
|
it('should not touch other changes', function () {
|
|
|
|
for (const i of [0, 2]) {
|
2020-05-06 06:11:36 -04:00
|
|
|
expect(
|
2024-02-06 08:08:59 -05:00
|
|
|
this.result.changes.find(
|
2021-07-13 07:04:42 -04:00
|
|
|
change => change.id === this.ranges.changes[i].id
|
2020-05-06 06:11:36 -04:00
|
|
|
)
|
|
|
|
).to.deep.equal(this.ranges.changes[i])
|
2024-02-06 08:08:59 -05:00
|
|
|
}
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2024-04-16 04:55:56 -04:00
|
|
|
|
|
|
|
describe('getHistoryUpdatesForAcceptedChanges', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.RangesManager = SandboxedModule.require(MODULE_PATH, {
|
|
|
|
requires: {
|
|
|
|
'@overleaf/ranges-tracker': (this.RangesTracker =
|
|
|
|
SandboxedModule.require('@overleaf/ranges-tracker')),
|
|
|
|
'@overleaf/metrics': {},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create history updates for accepted track inserts', function () {
|
|
|
|
// 'one two three four five' <-- text before changes
|
|
|
|
const ranges = {
|
|
|
|
comments: [],
|
|
|
|
changes: makeRanges([
|
|
|
|
{ i: 'lorem', p: 0 },
|
|
|
|
{ i: 'ipsum', p: 15 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
const lines = ['loremone two thipsumree four five']
|
|
|
|
|
|
|
|
const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
|
|
|
|
docId: this.doc_id,
|
|
|
|
acceptedChangeIds: ranges.changes.map(change => change.id),
|
|
|
|
changes: ranges.changes,
|
|
|
|
pathname: '',
|
|
|
|
projectHistoryId: '',
|
|
|
|
lines,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 33,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[0].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
r: 'lorem',
|
|
|
|
p: 0,
|
2024-05-16 08:23:57 -04:00
|
|
|
tracking: { type: 'none' },
|
2024-04-16 04:55:56 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 33,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[1].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
r: 'ipsum',
|
|
|
|
p: 15,
|
2024-05-16 08:23:57 -04:00
|
|
|
tracking: { type: 'none' },
|
2024-04-16 04:55:56 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create history updates for accepted track deletes', function () {
|
|
|
|
// 'one two three four five' <-- text before changes
|
|
|
|
const ranges = {
|
|
|
|
comments: [],
|
|
|
|
changes: makeRanges([
|
|
|
|
{ d: 'two', p: 4 },
|
|
|
|
{ d: 'three', p: 5 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
const lines = ['one four five']
|
|
|
|
|
|
|
|
const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
|
|
|
|
docId: this.doc_id,
|
|
|
|
acceptedChangeIds: ranges.changes.map(change => change.id),
|
|
|
|
changes: ranges.changes,
|
|
|
|
pathname: '',
|
|
|
|
projectHistoryId: '',
|
|
|
|
lines,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 15,
|
|
|
|
history_doc_length: 23,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[0].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
d: 'two',
|
|
|
|
p: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 15,
|
|
|
|
history_doc_length: 20,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[1].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
d: 'three',
|
|
|
|
p: 5,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create history updates with unaccepted deletes', function () {
|
|
|
|
// 'one two three four five' <-- text before changes
|
|
|
|
const ranges = {
|
|
|
|
comments: [],
|
|
|
|
changes: makeRanges([
|
|
|
|
{ d: 'two', p: 4 },
|
|
|
|
{ d: 'three', p: 5 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
const lines = ['one four five']
|
|
|
|
|
|
|
|
const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
|
|
|
|
docId: this.doc_id,
|
|
|
|
acceptedChangeIds: [ranges.changes[1].id],
|
|
|
|
changes: ranges.changes,
|
|
|
|
pathname: '',
|
|
|
|
projectHistoryId: '',
|
|
|
|
lines,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 15,
|
|
|
|
history_doc_length: 23,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[1].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
d: 'three',
|
|
|
|
p: 5,
|
|
|
|
hpos: 8,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create history updates with mixed track changes', function () {
|
|
|
|
// 'one two three four five' <-- text before changes
|
|
|
|
const ranges = {
|
|
|
|
comments: [],
|
|
|
|
changes: makeRanges([
|
|
|
|
{ d: 'two', p: 4 },
|
|
|
|
{ d: 'three', p: 5 },
|
|
|
|
{ i: 'xxx ', p: 6 },
|
|
|
|
{ d: 'five', p: 15 },
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
const lines = ['one xxx four ']
|
|
|
|
|
|
|
|
const result = this.RangesManager.getHistoryUpdatesForAcceptedChanges({
|
|
|
|
docId: this.doc_id,
|
|
|
|
acceptedChangeIds: [
|
|
|
|
ranges.changes[0].id,
|
|
|
|
// ranges.changes[1].id - second delete is not accepted
|
|
|
|
ranges.changes[2].id,
|
|
|
|
ranges.changes[3].id,
|
|
|
|
],
|
|
|
|
changes: ranges.changes,
|
|
|
|
pathname: '',
|
|
|
|
projectHistoryId: '',
|
|
|
|
lines,
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(result).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 15,
|
|
|
|
history_doc_length: 27,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[0].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
d: 'two',
|
|
|
|
p: 4,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 15,
|
|
|
|
history_doc_length: 24,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[1].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
r: 'xxx ',
|
|
|
|
p: 6,
|
|
|
|
hpos: 11,
|
2024-05-16 08:23:57 -04:00
|
|
|
tracking: { type: 'none' },
|
2024-04-16 04:55:56 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.doc_id,
|
|
|
|
meta: {
|
|
|
|
user_id: TEST_USER_ID,
|
|
|
|
doc_length: 15,
|
|
|
|
history_doc_length: 24,
|
|
|
|
pathname: '',
|
|
|
|
ts: ranges.changes[2].metadata.ts,
|
|
|
|
},
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
d: 'five',
|
|
|
|
p: 15,
|
|
|
|
hpos: 20,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
2024-02-13 08:15:43 -05:00
|
|
|
|
|
|
|
function makeRanges(ops) {
|
|
|
|
let id = 1
|
|
|
|
const changes = []
|
|
|
|
for (const op of ops) {
|
|
|
|
changes.push({
|
|
|
|
id: id.toString(),
|
|
|
|
op,
|
2024-04-16 04:55:56 -04:00
|
|
|
metadata: { user_id: TEST_USER_ID, ts: new Date() },
|
2024-02-13 08:15:43 -05:00
|
|
|
})
|
|
|
|
id += 1
|
|
|
|
}
|
|
|
|
return changes
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeUpdates(ops, meta = {}) {
|
|
|
|
const updates = []
|
|
|
|
for (const op of ops) {
|
|
|
|
updates.push({
|
|
|
|
meta: { user_id: TEST_USER_ID, ...meta },
|
|
|
|
op: [op],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return updates
|
|
|
|
}
|