2020-02-17 12:35:01 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 12:34:50 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 12:35:16 -05:00
|
|
|
const sinon = require('sinon')
|
2021-03-23 15:08:32 -04:00
|
|
|
const { expect } = require('chai')
|
2020-02-17 12:35:16 -05:00
|
|
|
const modulePath = '../../../../app/js/DiffGenerator.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('DiffGenerator', function () {
|
|
|
|
beforeEach(function () {
|
2021-03-23 15:08:32 -04:00
|
|
|
this.DiffGenerator = SandboxedModule.require(modulePath, {})
|
2020-02-17 12:35:16 -05:00
|
|
|
this.ts = Date.now()
|
|
|
|
this.user_id = 'mock-user-id'
|
|
|
|
this.user_id_2 = 'mock-user-id-2'
|
|
|
|
return (this.meta = {
|
|
|
|
start_ts: this.ts,
|
|
|
|
end_ts: this.ts,
|
2021-07-13 07:04:43 -04:00
|
|
|
user_id: this.user_id,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('rewindOp', function () {
|
|
|
|
describe('rewinding an insert', function () {
|
|
|
|
return it('should undo the insert', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const content = 'hello world'
|
|
|
|
const rewoundContent = this.DiffGenerator.rewindOp(content, {
|
|
|
|
p: 6,
|
2021-07-13 07:04:43 -04:00
|
|
|
i: 'wo',
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
return rewoundContent.should.equal('hello rld')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('rewinding a delete', function () {
|
|
|
|
return it('should undo the delete', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const content = 'hello rld'
|
|
|
|
const rewoundContent = this.DiffGenerator.rewindOp(content, {
|
|
|
|
p: 6,
|
2021-07-13 07:04:43 -04:00
|
|
|
d: 'wo',
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
return rewoundContent.should.equal('hello world')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with an inconsistent update', function () {
|
|
|
|
return it('should throw an error', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const content = 'hello world'
|
|
|
|
return expect(() => {
|
|
|
|
return this.DiffGenerator.rewindOp(content, { p: 6, i: 'foo' })
|
|
|
|
}).to.throw(this.DiffGenerator.ConsistencyError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('with an update which is beyond the length of the content', function () {
|
|
|
|
return it('should undo the insert as if it were at the end of the content', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const content = 'foobar'
|
|
|
|
const rewoundContent = this.DiffGenerator.rewindOp(content, {
|
|
|
|
p: 4,
|
2021-07-13 07:04:43 -04:00
|
|
|
i: 'bar',
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
return rewoundContent.should.equal('foo')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('rewindUpdate', function () {
|
|
|
|
return it('should rewind ops in reverse', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const content = 'aaabbbccc'
|
|
|
|
const update = {
|
|
|
|
op: [
|
|
|
|
{ p: 3, i: 'bbb' },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ p: 6, i: 'ccc' },
|
|
|
|
],
|
2020-02-17 12:35:16 -05:00
|
|
|
}
|
|
|
|
const rewoundContent = this.DiffGenerator.rewindUpdate(content, update)
|
|
|
|
return rewoundContent.should.equal('aaa')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('rewindUpdates', function () {
|
|
|
|
return it('should rewind updates in reverse', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const content = 'aaabbbccc'
|
|
|
|
const updates = [
|
|
|
|
{ op: [{ p: 3, i: 'bbb' }] },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ op: [{ p: 6, i: 'ccc' }] },
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
const rewoundContent = this.DiffGenerator.rewindUpdates(content, updates)
|
|
|
|
return rewoundContent.should.equal('aaa')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('buildDiff', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
this.diff = [{ u: 'mock-diff' }]
|
|
|
|
this.content = 'Hello world'
|
|
|
|
this.updates = [
|
|
|
|
{ i: 'mock-update-1' },
|
|
|
|
{ i: 'mock-update-2' },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ i: 'mock-update-3' },
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
this.DiffGenerator.applyUpdateToDiff = sinon.stub().returns(this.diff)
|
|
|
|
this.DiffGenerator.compressDiff = sinon.stub().returns(this.diff)
|
|
|
|
return (this.result = this.DiffGenerator.buildDiff(
|
|
|
|
this.content,
|
|
|
|
this.updates
|
|
|
|
))
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should return the diff', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.result.should.deep.equal(this.diff)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should build the content into an initial diff', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffGenerator.applyUpdateToDiff
|
|
|
|
.calledWith(
|
|
|
|
[
|
|
|
|
{
|
2021-07-13 07:04:43 -04:00
|
|
|
u: this.content,
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
],
|
|
|
|
this.updates[0]
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should apply each update', function () {
|
2021-07-13 07:04:43 -04:00
|
|
|
return Array.from(this.updates).map(update =>
|
2020-02-17 12:35:16 -05:00
|
|
|
this.DiffGenerator.applyUpdateToDiff
|
|
|
|
.calledWith(sinon.match.any, update)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should compress the diff', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return this.DiffGenerator.compressDiff
|
|
|
|
.calledWith(this.diff)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('compressDiff', function () {
|
|
|
|
describe('with adjacent inserts with the same user_id', function () {
|
|
|
|
return it('should create one update with combined meta data and min/max timestamps', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.compressDiff([
|
|
|
|
{
|
|
|
|
i: 'foo',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
i: 'bar',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id } },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{
|
|
|
|
i: 'foobar',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 5, end_ts: 20, user: { id: this.user_id } },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with adjacent inserts with different user_ids', function () {
|
|
|
|
return it('should leave the inserts unchanged', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const input = [
|
|
|
|
{
|
|
|
|
i: 'foo',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
i: 'bar',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id_2 } },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
const output = this.DiffGenerator.compressDiff(input)
|
|
|
|
return expect(output).to.deep.equal(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('with adjacent deletes with the same user_id', function () {
|
|
|
|
return it('should create one update with combined meta data and min/max timestamps', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.compressDiff([
|
|
|
|
{
|
|
|
|
d: 'foo',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
d: 'bar',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id } },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{
|
|
|
|
d: 'foobar',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 5, end_ts: 20, user: { id: this.user_id } },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('with adjacent deletes with different user_ids', function () {
|
|
|
|
return it('should leave the deletes unchanged', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const input = [
|
|
|
|
{
|
|
|
|
d: 'foo',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
|
2020-02-17 12:35:16 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
d: 'bar',
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id_2 } },
|
|
|
|
},
|
2020-02-17 12:35:16 -05:00
|
|
|
]
|
|
|
|
const output = this.DiffGenerator.compressDiff(input)
|
|
|
|
return expect(output).to.deep.equal(input)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('applyUpdateToDiff', function () {
|
|
|
|
describe('an insert', function () {
|
|
|
|
it('should insert into the middle of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff([{ u: 'foobar' }], {
|
|
|
|
op: [{ p: 3, i: 'baz' }],
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: this.meta,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'foo' },
|
|
|
|
{ i: 'baz', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'bar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert into the start of (u)changed text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff([{ u: 'foobar' }], {
|
|
|
|
op: [{ p: 0, i: 'baz' }],
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: this.meta,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ i: 'baz', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'foobar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert into the end of (u)changed text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff([{ u: 'foobar' }], {
|
|
|
|
op: [{ p: 6, i: 'baz' }],
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: this.meta,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'foobar' },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ i: 'baz', meta: this.meta },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should insert into the middle of (i)inserted text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ i: 'foobar', meta: this.meta }],
|
|
|
|
{ op: [{ p: 3, i: 'baz' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ i: 'foo', meta: this.meta },
|
|
|
|
{ i: 'baz', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ i: 'bar', meta: this.meta },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should not count deletes in the running length total', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ d: 'deleted', meta: this.meta }, { u: 'foobar' }],
|
|
|
|
{ op: [{ p: 3, i: 'baz' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ d: 'deleted', meta: this.meta },
|
|
|
|
{ u: 'foo' },
|
|
|
|
{ i: 'baz', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'bar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('a delete', function () {
|
|
|
|
describe('deleting unchanged text', function () {
|
|
|
|
it('should delete from the middle of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foobazbar' }],
|
|
|
|
{ op: [{ p: 3, d: 'baz' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'foo' },
|
|
|
|
{ d: 'baz', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'bar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should delete from the start of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foobazbar' }],
|
|
|
|
{ op: [{ p: 0, d: 'foo' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ d: 'foo', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'bazbar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should delete from the end of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foobazbar' }],
|
|
|
|
{ op: [{ p: 6, d: 'bar' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'foobaz' },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ d: 'bar', meta: this.meta },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should delete across multiple (u)changed text parts', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foo' }, { u: 'baz' }, { u: 'bar' }],
|
|
|
|
{ op: [{ p: 2, d: 'obazb' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'fo' },
|
|
|
|
{ d: 'o', meta: this.meta },
|
|
|
|
{ d: 'baz', meta: this.meta },
|
|
|
|
{ d: 'b', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'ar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('deleting inserts', function () {
|
|
|
|
it('should delete from the middle of (i)nserted text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ i: 'foobazbar', meta: this.meta }],
|
|
|
|
{ op: [{ p: 3, d: 'baz' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ i: 'foo', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ i: 'bar', meta: this.meta },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should delete from the start of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ i: 'foobazbar', meta: this.meta }],
|
|
|
|
{ op: [{ p: 0, d: 'foo' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([{ i: 'bazbar', meta: this.meta }])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should delete from the end of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ i: 'foobazbar', meta: this.meta }],
|
|
|
|
{ op: [{ p: 6, d: 'bar' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([{ i: 'foobaz', meta: this.meta }])
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should delete across multiple (u)changed and (i)nserted text parts', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foo' }, { i: 'baz', meta: this.meta }, { u: 'bar' }],
|
|
|
|
{ op: [{ p: 2, d: 'obazb' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'fo' },
|
|
|
|
{ d: 'o', meta: this.meta },
|
|
|
|
{ d: 'b', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'ar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('deleting over existing deletes', function () {
|
|
|
|
return it('should delete across multiple (u)changed and (d)deleted text parts', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foo' }, { d: 'baz', meta: this.meta }, { u: 'bar' }],
|
|
|
|
{ op: [{ p: 2, d: 'ob' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'fo' },
|
|
|
|
{ d: 'o', meta: this.meta },
|
|
|
|
{ d: 'baz', meta: this.meta },
|
|
|
|
{ d: 'b', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ u: 'ar' },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe("deleting when the text doesn't match", function () {
|
|
|
|
it('should throw an error when deleting from the middle of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return expect(() =>
|
|
|
|
this.DiffGenerator.applyUpdateToDiff([{ u: 'foobazbar' }], {
|
|
|
|
op: [{ p: 3, d: 'xxx' }],
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: this.meta,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
).to.throw(this.DiffGenerator.ConsistencyError)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
it('should throw an error when deleting from the start of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return expect(() =>
|
|
|
|
this.DiffGenerator.applyUpdateToDiff([{ u: 'foobazbar' }], {
|
|
|
|
op: [{ p: 0, d: 'xxx' }],
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: this.meta,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
).to.throw(this.DiffGenerator.ConsistencyError)
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return it('should throw an error when deleting from the end of (u)nchanged text', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
return expect(() =>
|
|
|
|
this.DiffGenerator.applyUpdateToDiff([{ u: 'foobazbar' }], {
|
|
|
|
op: [{ p: 6, d: 'xxx' }],
|
2021-07-13 07:04:43 -04:00
|
|
|
meta: this.meta,
|
2020-02-17 12:35:16 -05:00
|
|
|
})
|
|
|
|
).to.throw(this.DiffGenerator.ConsistencyError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
describe('when the last update in the existing diff is a delete', function () {
|
|
|
|
return it('should insert the new update before the delete', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ u: 'foo' }, { d: 'bar', meta: this.meta }],
|
|
|
|
{ op: [{ p: 3, i: 'baz' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ u: 'foo' },
|
|
|
|
{ i: 'baz', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ d: 'bar', meta: this.meta },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 04:24:21 -04:00
|
|
|
return describe('when the only update in the existing diff is a delete', function () {
|
|
|
|
return it('should insert the new update after the delete', function () {
|
2020-02-17 12:35:16 -05:00
|
|
|
const diff = this.DiffGenerator.applyUpdateToDiff(
|
|
|
|
[{ d: 'bar', meta: this.meta }],
|
|
|
|
{ op: [{ p: 0, i: 'baz' }], meta: this.meta }
|
|
|
|
)
|
|
|
|
return expect(diff).to.deep.equal([
|
|
|
|
{ d: 'bar', meta: this.meta },
|
2021-07-13 07:04:43 -04:00
|
|
|
{ i: 'baz', meta: this.meta },
|
2020-02-17 12:35:16 -05:00
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|