2023-01-13 07:42:29 -05:00
|
|
|
import sinon from 'sinon'
|
|
|
|
import { expect } from 'chai'
|
2023-12-15 05:51:11 -05:00
|
|
|
import mongodb from 'mongodb-legacy'
|
2023-01-13 07:42:29 -05:00
|
|
|
import tk from 'timekeeper'
|
2024-06-18 09:59:44 -04:00
|
|
|
import { File, Comment, TrackedChange, Range } from 'overleaf-editor-core'
|
2023-01-13 07:42:29 -05:00
|
|
|
import { strict as esmock } from 'esmock'
|
2023-12-15 05:51:11 -05:00
|
|
|
const { ObjectId } = mongodb
|
2023-01-13 07:42:29 -05:00
|
|
|
|
|
|
|
const MODULE_PATH = '../../../../app/js/SyncManager.js'
|
2024-05-22 08:15:07 -04:00
|
|
|
const TIMESTAMP = new Date().toISOString()
|
|
|
|
const USER_ID = 'user-id'
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-05-03 09:27:39 -04:00
|
|
|
function resyncProjectStructureUpdate(docs, files) {
|
|
|
|
return {
|
|
|
|
resyncProjectStructure: { docs, files },
|
|
|
|
|
|
|
|
meta: {
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function docContentSyncUpdate(
|
|
|
|
doc,
|
|
|
|
content,
|
|
|
|
ranges = {},
|
2024-05-28 07:24:06 -04:00
|
|
|
resolvedCommentIds = []
|
2024-05-03 09:27:39 -04:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
path: doc.path,
|
|
|
|
doc: doc.doc,
|
|
|
|
|
|
|
|
resyncDocContent: {
|
|
|
|
content,
|
|
|
|
ranges,
|
2024-05-28 07:24:06 -04:00
|
|
|
resolvedCommentIds,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
meta: {
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeComment(commentId, pos, text) {
|
|
|
|
return {
|
|
|
|
id: commentId,
|
|
|
|
op: { p: pos, c: text, t: commentId },
|
2024-05-22 08:15:07 -04:00
|
|
|
metadata: {
|
|
|
|
user_id: USER_ID,
|
|
|
|
ts: TIMESTAMP,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 07:42:29 -05:00
|
|
|
|
2024-05-22 08:15:07 -04:00
|
|
|
function makeTrackedChange(id, op) {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
op,
|
|
|
|
metadata: { user_id: USER_ID, ts: TIMESTAMP },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
describe('SyncManager', function () {
|
|
|
|
beforeEach(async function () {
|
|
|
|
this.now = new Date()
|
|
|
|
tk.freeze(this.now)
|
|
|
|
this.projectId = new ObjectId().toString()
|
|
|
|
this.historyId = 'mock-overleaf-id'
|
|
|
|
this.syncState = { origin: { kind: 'history-resync' } }
|
|
|
|
this.db = {
|
|
|
|
projectHistorySyncState: {
|
2024-04-11 08:32:51 -04:00
|
|
|
findOne: sinon.stub().resolves(this.syncState),
|
|
|
|
updateOne: sinon.stub().resolves(),
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
this.extendLock = sinon.stub().resolves()
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.LockManager = {
|
2024-04-11 08:32:51 -04:00
|
|
|
promises: {
|
|
|
|
runWithLock: sinon.stub().callsFake(async (key, runner) => {
|
|
|
|
await runner(this.extendLock)
|
|
|
|
}),
|
|
|
|
},
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.UpdateCompressor = {
|
2024-05-03 09:27:39 -04:00
|
|
|
diffAsShareJsOps: sinon.stub().returns([]),
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.UpdateTranslator = {
|
|
|
|
isTextUpdate: sinon.stub(),
|
|
|
|
_convertPathname: sinon.stub(),
|
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.WebApiManager = {
|
2024-04-11 08:32:51 -04:00
|
|
|
promises: {
|
|
|
|
getHistoryId: sinon.stub(),
|
|
|
|
requestResync: sinon.stub().resolves(),
|
|
|
|
},
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
this.WebApiManager.promises.getHistoryId
|
2023-01-13 07:42:29 -05:00
|
|
|
.withArgs(this.projectId)
|
2024-04-11 08:32:51 -04:00
|
|
|
.resolves(this.historyId)
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.ErrorRecorder = {
|
2024-04-11 08:32:51 -04:00
|
|
|
promises: {
|
|
|
|
record: sinon.stub().resolves(),
|
|
|
|
recordSyncStart: sinon.stub().resolves(),
|
|
|
|
},
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.RedisManager = {}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.SnapshotManager = {
|
2024-04-11 08:32:51 -04:00
|
|
|
promises: {
|
|
|
|
getLatestSnapshot: sinon.stub(),
|
|
|
|
},
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.HistoryStoreManager = {
|
|
|
|
getBlobStore: sinon.stub(),
|
|
|
|
_getBlobHashFromString: sinon.stub().returns('random-hash'),
|
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.HashManager = {
|
|
|
|
_getBlobHashFromString: sinon.stub(),
|
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.Metrics = { inc: sinon.stub() }
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.Settings = {
|
|
|
|
redis: {
|
|
|
|
lock: {
|
|
|
|
key_schema: {
|
|
|
|
projectHistoryLock({ project_id: projectId }) {
|
|
|
|
return `ProjectHistoryLock:${projectId}`
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
this.SyncManager = await esmock(MODULE_PATH, {
|
|
|
|
'../../../../app/js/LockManager.js': this.LockManager,
|
|
|
|
'../../../../app/js/UpdateCompressor.js': this.UpdateCompressor,
|
|
|
|
'../../../../app/js/UpdateTranslator.js': this.UpdateTranslator,
|
|
|
|
'../../../../app/js/mongodb.js': { ObjectId, db: this.db },
|
|
|
|
'../../../../app/js/WebApiManager.js': this.WebApiManager,
|
|
|
|
'../../../../app/js/ErrorRecorder.js': this.ErrorRecorder,
|
|
|
|
'../../../../app/js/RedisManager.js': this.RedisManager,
|
|
|
|
'../../../../app/js/SnapshotManager.js': this.SnapshotManager,
|
|
|
|
'../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
|
|
|
|
'../../../../app/js/HashManager.js': this.HashManager,
|
|
|
|
'@overleaf/metrics': this.Metrics,
|
|
|
|
'@overleaf/settings': this.Settings,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
tk.reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('startResync', function () {
|
|
|
|
describe('if a sync is not in progress', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.db.projectHistorySyncState.findOne.resolves({})
|
|
|
|
await this.SyncManager.promises.startResync(this.projectId)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('takes the project lock', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
expect(this.LockManager.promises.runWithLock).to.have.been.calledWith(
|
2023-01-13 07:42:29 -05:00
|
|
|
`ProjectHistoryLock:${this.projectId}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('gets the sync state from mongo', function () {
|
|
|
|
expect(this.db.projectHistorySyncState.findOne).to.have.been.calledWith(
|
2023-12-15 05:51:11 -05:00
|
|
|
{ project_id: new ObjectId(this.projectId) }
|
2023-01-13 07:42:29 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('requests a resync from web', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
expect(
|
|
|
|
this.WebApiManager.promises.requestResync
|
|
|
|
).to.have.been.calledWith(this.projectId)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('sets the sync state in mongo and prevents it expiring', function () {
|
|
|
|
expect(
|
|
|
|
this.db.projectHistorySyncState.updateOne
|
|
|
|
).to.have.been.calledWith(
|
|
|
|
{
|
2023-12-15 05:51:11 -05:00
|
|
|
project_id: new ObjectId(this.projectId),
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
sinon.match({
|
|
|
|
$set: {
|
|
|
|
resyncProjectStructure: true,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
$currentDate: { lastUpdated: true },
|
|
|
|
$inc: { resyncCount: 1 },
|
|
|
|
$unset: { expiresAt: true },
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
upsert: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('if project structure sync is in progress', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
const syncState = { resyncProjectStructure: true }
|
2024-04-11 08:32:51 -04:00
|
|
|
this.db.projectHistorySyncState.findOne.resolves(syncState)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('returns an error if already syncing', async function () {
|
|
|
|
await expect(
|
|
|
|
this.SyncManager.promises.startResync(this.projectId)
|
|
|
|
).to.be.rejectedWith('sync ongoing')
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('if doc content sync in is progress', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
beforeEach(async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const syncState = { resyncDocContents: ['/foo.tex'] }
|
2024-04-11 08:32:51 -04:00
|
|
|
this.db.projectHistorySyncState.findOne.resolves(syncState)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('returns an error if already syncing', async function () {
|
|
|
|
await expect(
|
|
|
|
this.SyncManager.promises.startResync(this.projectId)
|
|
|
|
).to.be.rejectedWith('sync ongoing')
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('setResyncState', function () {
|
|
|
|
describe('when the sync is starting', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.syncState = {
|
|
|
|
toRaw() {
|
|
|
|
return {
|
|
|
|
resyncProjectStructure: true,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isSyncOngoing: sinon.stub().returns(true),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('sets the sync state in mongo and prevents it expiring', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
// SyncState is a private class of SyncManager
|
|
|
|
// we know the interface however:
|
2024-04-11 08:32:51 -04:00
|
|
|
await this.SyncManager.promises.setResyncState(
|
2023-01-13 07:42:29 -05:00
|
|
|
this.projectId,
|
2024-04-11 08:32:51 -04:00
|
|
|
this.syncState
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
this.db.projectHistorySyncState.updateOne
|
|
|
|
).to.have.been.calledWith(
|
|
|
|
{ project_id: new ObjectId(this.projectId) },
|
|
|
|
sinon.match({
|
|
|
|
$set: this.syncState.toRaw(),
|
|
|
|
$currentDate: { lastUpdated: true },
|
|
|
|
$inc: { resyncCount: 1 },
|
|
|
|
$unset: { expiresAt: true },
|
|
|
|
}),
|
|
|
|
{ upsert: true }
|
2023-01-13 07:42:29 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the sync is ending', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.syncState = {
|
|
|
|
toRaw() {
|
|
|
|
return {
|
|
|
|
resyncProjectStructure: false,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isSyncOngoing: sinon.stub().returns(false),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('sets the sync state entry in mongo to expire', async function () {
|
|
|
|
await this.SyncManager.promises.setResyncState(
|
2023-01-13 07:42:29 -05:00
|
|
|
this.projectId,
|
2024-04-11 08:32:51 -04:00
|
|
|
this.syncState
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
this.db.projectHistorySyncState.updateOne
|
|
|
|
).to.have.been.calledWith(
|
|
|
|
{ project_id: new ObjectId(this.projectId) },
|
|
|
|
sinon.match({
|
|
|
|
$set: {
|
|
|
|
resyncProjectStructure: false,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
expiresAt: new Date(this.now.getTime() + 90 * 24 * 3600 * 1000),
|
|
|
|
},
|
|
|
|
$currentDate: { lastUpdated: true },
|
|
|
|
}),
|
|
|
|
{ upsert: true }
|
2023-01-13 07:42:29 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the new sync state is null', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
it('does not update the sync state in mongo', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
// SyncState is a private class of SyncManager
|
|
|
|
// we know the interface however:
|
2024-04-11 08:32:51 -04:00
|
|
|
await this.SyncManager.promises.setResyncState(this.projectId, null)
|
|
|
|
expect(this.db.projectHistorySyncState.updateOne).to.not.have.been
|
|
|
|
.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('skipUpdatesDuringSync', function () {
|
|
|
|
describe('if a sync is not in progress', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
beforeEach(async function () {
|
|
|
|
this.db.projectHistorySyncState.findOne.resolves({})
|
2023-01-13 07:42:29 -05:00
|
|
|
this.updates = ['some', 'mock', 'updates']
|
2024-04-11 08:32:51 -04:00
|
|
|
this.result = await this.SyncManager.promises.skipUpdatesDuringSync(
|
2023-01-13 07:42:29 -05:00
|
|
|
this.projectId,
|
2024-04-11 08:32:51 -04:00
|
|
|
this.updates
|
2023-01-13 07:42:29 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns all updates', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
expect(this.result.updates).to.deep.equal(this.updates)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not return any newSyncState', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
expect(this.result.syncState).to.be.null
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('if a sync in is progress', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.renameUpdate = {
|
|
|
|
pathname: 'old.tex',
|
|
|
|
newPathname: 'new.tex',
|
|
|
|
}
|
|
|
|
this.projectStructureSyncUpdate = {
|
|
|
|
resyncProjectStructure: {
|
|
|
|
docs: [{ path: 'new.tex' }],
|
|
|
|
files: [],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.textUpdate = {
|
|
|
|
doc: new ObjectId(),
|
|
|
|
op: [{ i: 'a', p: 4 }],
|
|
|
|
meta: {
|
|
|
|
pathname: 'new.tex',
|
|
|
|
doc_length: 4,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.docContentSyncUpdate = {
|
|
|
|
path: 'new.tex',
|
|
|
|
resyncDocContent: {
|
|
|
|
content: 'a',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.UpdateTranslator.isTextUpdate
|
|
|
|
.withArgs(this.renameUpdate)
|
|
|
|
.returns(false)
|
|
|
|
this.UpdateTranslator.isTextUpdate
|
|
|
|
.withArgs(this.projectStructureSyncUpdate)
|
|
|
|
.returns(false)
|
|
|
|
this.UpdateTranslator.isTextUpdate
|
|
|
|
.withArgs(this.docContentSyncUpdate)
|
|
|
|
.returns(false)
|
|
|
|
this.UpdateTranslator.isTextUpdate
|
|
|
|
.withArgs(this.textUpdate)
|
|
|
|
.returns(true)
|
|
|
|
|
|
|
|
const syncState = {
|
|
|
|
resyncProjectStructure: true,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
}
|
2024-04-11 08:32:51 -04:00
|
|
|
this.db.projectHistorySyncState.findOne.resolves(syncState)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('remove updates before a project structure sync update', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
this.renameUpdate,
|
|
|
|
this.textUpdate,
|
|
|
|
this.projectStructureSyncUpdate,
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const { updates: filteredUpdates, syncState } =
|
|
|
|
await this.SyncManager.promises.skipUpdatesDuringSync(
|
|
|
|
this.projectId,
|
|
|
|
updates
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(filteredUpdates).to.deep.equal([this.projectStructureSyncUpdate])
|
|
|
|
expect(syncState.toRaw()).to.deep.equal({
|
|
|
|
resyncProjectStructure: false,
|
|
|
|
resyncDocContents: ['new.tex'],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('allow project structure updates after project structure sync update', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [this.projectStructureSyncUpdate, this.renameUpdate]
|
2024-04-11 08:32:51 -04:00
|
|
|
const { updates: filteredUpdates, syncState } =
|
|
|
|
await this.SyncManager.promises.skipUpdatesDuringSync(
|
|
|
|
this.projectId,
|
|
|
|
updates
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(filteredUpdates).to.deep.equal([
|
|
|
|
this.projectStructureSyncUpdate,
|
|
|
|
this.renameUpdate,
|
|
|
|
])
|
|
|
|
expect(syncState.toRaw()).to.deep.equal({
|
|
|
|
resyncProjectStructure: false,
|
|
|
|
resyncDocContents: ['new.tex'],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('remove text updates for a doc before doc sync update', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
this.projectStructureSyncUpdate,
|
|
|
|
this.textUpdate,
|
|
|
|
this.docContentSyncUpdate,
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const { updates: filteredUpdates, syncState } =
|
|
|
|
await this.SyncManager.promises.skipUpdatesDuringSync(
|
|
|
|
this.projectId,
|
|
|
|
updates
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(filteredUpdates).to.deep.equal([
|
|
|
|
this.projectStructureSyncUpdate,
|
|
|
|
this.docContentSyncUpdate,
|
|
|
|
])
|
|
|
|
expect(syncState.toRaw()).to.deep.equal({
|
|
|
|
resyncProjectStructure: false,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('allow text updates for a doc after doc sync update', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
this.projectStructureSyncUpdate,
|
|
|
|
this.docContentSyncUpdate,
|
|
|
|
this.textUpdate,
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const { updates: filteredUpdates, syncState } =
|
|
|
|
await this.SyncManager.promises.skipUpdatesDuringSync(
|
|
|
|
this.projectId,
|
|
|
|
updates
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(filteredUpdates).to.deep.equal([
|
|
|
|
this.projectStructureSyncUpdate,
|
|
|
|
this.docContentSyncUpdate,
|
|
|
|
this.textUpdate,
|
|
|
|
])
|
|
|
|
expect(syncState.toRaw()).to.deep.equal({
|
|
|
|
resyncProjectStructure: false,
|
|
|
|
resyncDocContents: [],
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('expandSyncUpdates', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.persistedDoc = {
|
2024-05-03 09:27:39 -04:00
|
|
|
doc: 'doc-id',
|
2023-01-13 07:42:29 -05:00
|
|
|
path: 'main.tex',
|
|
|
|
}
|
2024-05-22 08:15:07 -04:00
|
|
|
this.persistedDocContent = 'the quick brown fox jumps over the lazy dog'
|
2023-01-13 07:42:29 -05:00
|
|
|
this.persistedFile = {
|
2024-05-03 09:27:39 -04:00
|
|
|
file: 'file-id',
|
2023-01-13 07:42:29 -05:00
|
|
|
path: '1.png',
|
2024-05-03 09:27:39 -04:00
|
|
|
_hash: 'abcde',
|
|
|
|
}
|
2024-06-18 09:59:44 -04:00
|
|
|
this.loadedSnapshotDoc = File.fromString(this.persistedDocContent)
|
2023-01-13 07:42:29 -05:00
|
|
|
this.fileMap = {
|
|
|
|
'main.tex': {
|
|
|
|
isEditable: sinon.stub().returns(true),
|
2024-05-03 09:27:39 -04:00
|
|
|
getContent: sinon.stub().returns(null),
|
|
|
|
getHash: sinon.stub().returns(null),
|
|
|
|
load: sinon.stub().resolves(this.loadedSnapshotDoc),
|
2024-08-05 04:52:23 -04:00
|
|
|
getMetadata: sinon.stub().returns({}),
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
'1.png': {
|
|
|
|
isEditable: sinon.stub().returns(false),
|
2024-05-03 09:27:39 -04:00
|
|
|
data: { hash: this.persistedFile._hash },
|
2024-08-05 04:52:23 -04:00
|
|
|
getMetadata: sinon.stub().returns({}),
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
this.UpdateTranslator._convertPathname
|
|
|
|
.withArgs('main.tex')
|
|
|
|
.returns('main.tex')
|
|
|
|
this.UpdateTranslator._convertPathname
|
|
|
|
.withArgs('/main.tex')
|
|
|
|
.returns('main.tex')
|
|
|
|
this.UpdateTranslator._convertPathname
|
|
|
|
.withArgs('another.tex')
|
|
|
|
.returns('another.tex')
|
|
|
|
this.UpdateTranslator._convertPathname.withArgs('1.png').returns('1.png')
|
|
|
|
this.UpdateTranslator._convertPathname.withArgs('2.png').returns('2.png')
|
2024-04-11 08:32:51 -04:00
|
|
|
this.SnapshotManager.promises.getLatestSnapshot.resolves(this.fileMap)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('returns updates if no sync updates are queued', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = ['some', 'mock', 'updates']
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates = await this.SyncManager.promises.expandSyncUpdates(
|
2023-01-13 07:42:29 -05:00
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
2024-04-11 08:32:51 -04:00
|
|
|
this.extendLock
|
2023-01-13 07:42:29 -05:00
|
|
|
)
|
2024-04-11 08:32:51 -04:00
|
|
|
|
|
|
|
expect(expandedUpdates).to.equal(updates)
|
|
|
|
expect(this.SnapshotManager.promises.getLatestSnapshot).to.not.have.been
|
|
|
|
.called
|
|
|
|
expect(this.extendLock).to.not.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('expanding project structure sync updates', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues nothing for expected docs and files', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues file removes for unexpected files', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [resyncProjectStructureUpdate([this.persistedDoc], [])]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: this.persistedFile.path,
|
|
|
|
new_pathname: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues doc removes for unexpected docs', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [resyncProjectStructureUpdate([], [this.persistedFile])]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
new_pathname: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-08-05 04:52:23 -04:00
|
|
|
it('queues file additions for missing regular files', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const newFile = {
|
|
|
|
path: '2.png',
|
|
|
|
file: {},
|
|
|
|
url: 'filestore/2.png',
|
|
|
|
}
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile, newFile]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: newFile.path,
|
|
|
|
file: newFile.file,
|
|
|
|
url: newFile.url,
|
2024-08-05 04:52:23 -04:00
|
|
|
metadata: undefined,
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('queues file additions for missing linked files', async function () {
|
|
|
|
const newFile = {
|
|
|
|
path: '2.png',
|
|
|
|
file: {},
|
|
|
|
url: 'filestore/2.png',
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'references-provider',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile, newFile]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: newFile.path,
|
|
|
|
file: newFile.file,
|
|
|
|
url: newFile.url,
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'references-provider',
|
|
|
|
},
|
2024-04-11 08:32:51 -04:00
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues blank doc additions for missing docs', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const newDoc = {
|
|
|
|
path: 'another.tex',
|
2023-12-15 05:51:11 -05:00
|
|
|
doc: new ObjectId().toString(),
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc, newDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: newDoc.path,
|
|
|
|
doc: newDoc.doc,
|
|
|
|
docLines: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('removes and re-adds files if whether they are binary differs', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const fileWichWasADoc = {
|
|
|
|
path: this.persistedDoc.path,
|
|
|
|
url: 'filestore/2.png',
|
|
|
|
_hash: 'other-hash',
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[],
|
|
|
|
[fileWichWasADoc, this.persistedFile]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: fileWichWasADoc.path,
|
|
|
|
new_pathname: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pathname: fileWichWasADoc.path,
|
|
|
|
file: fileWichWasADoc.file,
|
|
|
|
url: fileWichWasADoc.url,
|
2024-08-05 04:52:23 -04:00
|
|
|
metadata: undefined,
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('removes and re-adds linked-files if their binary state differs', async function () {
|
|
|
|
const fileWhichWasADoc = {
|
|
|
|
path: this.persistedDoc.path,
|
|
|
|
url: 'filestore/references.txt',
|
|
|
|
_hash: 'other-hash',
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'references-provider',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[],
|
|
|
|
[fileWhichWasADoc, this.persistedFile]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: fileWhichWasADoc.path,
|
|
|
|
new_pathname: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pathname: fileWhichWasADoc.path,
|
|
|
|
file: fileWhichWasADoc.file,
|
|
|
|
url: fileWhichWasADoc.url,
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'references-provider',
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('add linked file data with same hash', async function () {
|
|
|
|
const nowLinkedFile = {
|
|
|
|
path: this.persistedFile.path,
|
|
|
|
url: 'filestore/1.png',
|
|
|
|
_hash: this.persistedFile._hash,
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'image-provider',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate([this.persistedDoc], [nowLinkedFile]),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: nowLinkedFile.path,
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'image-provider',
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('updates linked file data when hash remains the same', async function () {
|
|
|
|
this.fileMap[this.persistedFile.path].getMetadata.returns({
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'image-provider',
|
|
|
|
})
|
|
|
|
const updatedLinkedFile = {
|
|
|
|
path: this.persistedFile.path,
|
|
|
|
url: 'filestore/1.png',
|
|
|
|
_hash: this.persistedFile._hash,
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-31T00:00:00.000Z',
|
|
|
|
provider: 'image-provider',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[updatedLinkedFile]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: updatedLinkedFile.path,
|
|
|
|
metadata: {
|
|
|
|
importedAt: '2024-07-31T00:00:00.000Z',
|
|
|
|
provider: 'image-provider',
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('remove linked file data', async function () {
|
|
|
|
this.fileMap[this.persistedFile.path].getMetadata.returns({
|
|
|
|
importedAt: '2024-07-30T09:14:45.928Z',
|
|
|
|
provider: 'image-provider',
|
|
|
|
})
|
|
|
|
|
|
|
|
const noLongerLinkedFile = {
|
|
|
|
path: this.persistedFile.path,
|
|
|
|
url: 'filestore/1.png',
|
|
|
|
_hash: this.persistedFile._hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[noLongerLinkedFile]
|
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: noLongerLinkedFile.path,
|
|
|
|
metadata: {},
|
2024-04-11 08:32:51 -04:00
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it("does not remove and re-add files if the expected file doesn't have a hash", async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const fileWichWasADoc = {
|
|
|
|
path: this.persistedDoc.path,
|
|
|
|
url: 'filestore/2.png',
|
|
|
|
}
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[],
|
|
|
|
[fileWichWasADoc, this.persistedFile]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('does not remove and re-add editable files if there is a binary file with same hash', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const binaryFile = {
|
|
|
|
file: Object().toString(),
|
|
|
|
// The paths in the resyncProjectStructureUpdate must have a leading slash ('/')
|
|
|
|
// The other unit tests in this file are incorrectly missing the leading slash.
|
|
|
|
// The leading slash is present in web where the paths are created with
|
|
|
|
// ProjectEntityHandler.getAllEntitiesFromProject in ProjectEntityUpdateHandler.resyncProjectHistory.
|
|
|
|
path: '/' + this.persistedDoc.path,
|
|
|
|
url: 'filestore/12345',
|
|
|
|
_hash: 'abcdef',
|
|
|
|
}
|
|
|
|
this.fileMap['main.tex'].data = { hash: 'abcdef' }
|
|
|
|
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate([], [binaryFile, this.persistedFile]),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('removes and re-adds binary files if they do not have same hash', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const persistedFileWithNewContent = {
|
|
|
|
_hash: 'anotherhashvalue',
|
|
|
|
hello: 'world',
|
|
|
|
path: '1.png',
|
|
|
|
url: 'filestore-new-url',
|
|
|
|
}
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[persistedFileWithNewContent]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: persistedFileWithNewContent.path,
|
|
|
|
new_pathname: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pathname: persistedFileWithNewContent.path,
|
|
|
|
file: persistedFileWithNewContent.file,
|
|
|
|
url: persistedFileWithNewContent.url,
|
2024-08-05 04:52:23 -04:00
|
|
|
metadata: undefined,
|
2024-04-11 08:32:51 -04:00
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('preserves other updates', async function () {
|
2024-05-03 09:27:39 -04:00
|
|
|
const update = { mock: 'update' }
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
update,
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([update])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('expanding doc contents sync updates', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
it('returns errors from diffAsShareJsOps', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const diffError = new Error('test')
|
|
|
|
this.UpdateCompressor.diffAsShareJsOps.throws(diffError)
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
2024-05-03 09:27:39 -04:00
|
|
|
docContentSyncUpdate(this.persistedDoc, this.persistedDocContent),
|
2023-01-13 07:42:29 -05:00
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
await expect(
|
|
|
|
this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith(diffError)
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('handles an update for a file that is missing from the snapshot', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [docContentSyncUpdate('not-in-snapshot.txt', 'test')]
|
2024-04-11 08:32:51 -04:00
|
|
|
await expect(
|
|
|
|
this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
).to.be.rejectedWith('unrecognised file: not in snapshot')
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues nothing for in docs whose contents is in sync', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
2024-05-03 09:27:39 -04:00
|
|
|
docContentSyncUpdate(this.persistedDoc, this.persistedDocContent),
|
2023-01-13 07:42:29 -05:00
|
|
|
]
|
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([])
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues text updates for docs whose contents is out of sync', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
2024-05-03 09:27:39 -04:00
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
'the fox jumps over the lazy dog'
|
|
|
|
),
|
2023-01-13 07:42:29 -05:00
|
|
|
]
|
2024-05-03 09:27:39 -04:00
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([
|
|
|
|
{ d: 'quick brown ', p: 4 },
|
|
|
|
])
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
2024-05-03 09:27:39 -04:00
|
|
|
op: [{ d: 'quick brown ', p: 4 }],
|
2024-04-11 08:32:51 -04:00
|
|
|
meta: {
|
|
|
|
pathname: this.persistedDoc.path,
|
2024-05-03 09:27:39 -04:00
|
|
|
doc_length: this.persistedDocContent.length,
|
2024-04-11 08:32:51 -04:00
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('queues text updates for docs created by project structure sync', async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([{ i: 'a', p: 0 }])
|
|
|
|
const newDoc = {
|
|
|
|
path: 'another.tex',
|
2023-12-15 05:51:11 -05:00
|
|
|
doc: new ObjectId().toString(),
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-05-03 09:27:39 -04:00
|
|
|
const newDocContent = 'a'
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc, newDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
2024-05-03 09:27:39 -04:00
|
|
|
docContentSyncUpdate(newDoc, newDocContent),
|
2023-01-13 07:42:29 -05:00
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: newDoc.path,
|
|
|
|
doc: newDoc.doc,
|
|
|
|
docLines: '',
|
|
|
|
meta: {
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: newDoc.doc,
|
|
|
|
op: [{ i: 'a', p: 0 }],
|
|
|
|
meta: {
|
|
|
|
pathname: newDoc.path,
|
|
|
|
doc_length: 0,
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('skips text updates for docs when hashes match', async function () {
|
2024-05-03 09:27:39 -04:00
|
|
|
this.fileMap['main.tex'].getHash.returns('special-hash')
|
2023-01-13 07:42:29 -05:00
|
|
|
this.HashManager._getBlobHashFromString.returns('special-hash')
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
|
|
|
docContentSyncUpdate(this.persistedDoc, 'hello'),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
2024-04-11 08:32:51 -04:00
|
|
|
it('computes text updates for docs when hashes differ', async function () {
|
2024-05-03 09:27:39 -04:00
|
|
|
this.fileMap['main.tex'].getHash.returns('first-hash')
|
2023-01-13 07:42:29 -05:00
|
|
|
this.HashManager._getBlobHashFromString.returns('second-hash')
|
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([
|
|
|
|
{ i: 'test diff', p: 0 },
|
|
|
|
])
|
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
|
|
|
docContentSyncUpdate(this.persistedDoc, 'hello'),
|
|
|
|
]
|
2024-04-11 08:32:51 -04:00
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [{ i: 'test diff', p: 0 }],
|
|
|
|
meta: {
|
|
|
|
pathname: this.persistedDoc.path,
|
2024-05-03 09:27:39 -04:00
|
|
|
doc_length: this.persistedDocContent.length,
|
2024-04-11 08:32:51 -04:00
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-04-11 08:32:51 -04:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('for docs whose contents is out of sync', function () {
|
2024-04-11 08:32:51 -04:00
|
|
|
beforeEach(async function () {
|
2023-01-13 07:42:29 -05:00
|
|
|
const updates = [
|
|
|
|
resyncProjectStructureUpdate(
|
|
|
|
[this.persistedDoc],
|
|
|
|
[this.persistedFile]
|
|
|
|
),
|
2024-06-18 09:59:44 -04:00
|
|
|
docContentSyncUpdate(this.persistedDoc, 'the quick brown fox'),
|
2023-01-13 07:42:29 -05:00
|
|
|
]
|
2024-06-18 09:59:44 -04:00
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([
|
|
|
|
{ d: ' jumps over the lazy dog', p: 19 },
|
|
|
|
])
|
2024-04-11 08:32:51 -04:00
|
|
|
this.expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('loads content from the history service when needed', function () {
|
|
|
|
expect(this.expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
2024-06-18 09:59:44 -04:00
|
|
|
op: [{ d: ' jumps over the lazy dog', p: 19 }],
|
2023-01-13 07:42:29 -05:00
|
|
|
meta: {
|
|
|
|
pathname: this.persistedDoc.path,
|
2024-06-18 09:59:44 -04:00
|
|
|
doc_length: this.persistedDocContent.length,
|
2023-01-13 07:42:29 -05:00
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2023-01-13 07:42:29 -05:00
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(this.extendLock).to.have.been.called
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2024-05-03 09:27:39 -04:00
|
|
|
|
|
|
|
describe('syncing comments', function () {
|
|
|
|
beforeEach(function () {
|
2024-06-18 09:59:44 -04:00
|
|
|
this.loadedSnapshotDoc
|
|
|
|
.getComments()
|
|
|
|
.add(new Comment('comment1', [new Range(4, 5)]))
|
|
|
|
this.loadedSnapshotDoc
|
|
|
|
.getComments()
|
|
|
|
.add(new Comment('comment2', [new Range(10, 5)], true))
|
2024-05-03 09:27:39 -04:00
|
|
|
this.comments = [
|
|
|
|
makeComment('comment1', 4, 'quick'),
|
|
|
|
makeComment('comment2', 10, 'brown'),
|
|
|
|
]
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds = ['comment2']
|
2024-05-03 09:27:39 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing if comments have not changed', async function () {
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
this.persistedDocContent,
|
|
|
|
{
|
|
|
|
comments: this.comments,
|
|
|
|
},
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds
|
2024-05-03 09:27:39 -04:00
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds missing comments', async function () {
|
|
|
|
this.comments.push(makeComment('comment3', 20, 'jumps'))
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
this.persistedDocContent,
|
|
|
|
{
|
|
|
|
comments: this.comments,
|
|
|
|
},
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds
|
2024-05-03 09:27:39 -04:00
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
c: 'jumps',
|
|
|
|
p: 20,
|
|
|
|
t: 'comment3',
|
2024-05-28 07:24:06 -04:00
|
|
|
resolved: false,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-05-03 09:27:39 -04:00
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('deletes extra comments', async function () {
|
|
|
|
this.comments.splice(0, 1)
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
this.persistedDocContent,
|
|
|
|
{
|
|
|
|
comments: this.comments,
|
|
|
|
},
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds
|
2024-05-03 09:27:39 -04:00
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
deleteComment: 'comment1',
|
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('updates comments when ranges differ', async function () {
|
|
|
|
this.comments[1] = makeComment('comment2', 16, 'fox')
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
this.persistedDocContent,
|
|
|
|
{
|
|
|
|
comments: this.comments,
|
|
|
|
},
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds
|
2024-05-03 09:27:39 -04:00
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: 'doc-id',
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
c: 'fox',
|
|
|
|
p: 16,
|
|
|
|
t: 'comment2',
|
2024-05-28 07:24:06 -04:00
|
|
|
resolved: true,
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
resync: true,
|
2024-05-22 08:15:07 -04:00
|
|
|
ts: TIMESTAMP,
|
2024-05-03 09:27:39 -04:00
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('sets the resolved state when it differs', async function () {
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds = ['comment1']
|
2024-05-03 09:27:39 -04:00
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
this.persistedDocContent,
|
|
|
|
{
|
|
|
|
comments: this.comments,
|
|
|
|
},
|
2024-05-28 07:24:06 -04:00
|
|
|
this.resolvedCommentIds
|
2024-05-03 09:27:39 -04:00
|
|
|
),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.have.deep.members([
|
|
|
|
{
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
commentId: 'comment1',
|
|
|
|
resolved: true,
|
2024-05-28 07:24:06 -04:00
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
},
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
commentId: 'comment2',
|
|
|
|
resolved: false,
|
2024-05-28 07:24:06 -04:00
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
},
|
2024-05-03 09:27:39 -04:00
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2024-06-12 08:49:31 -04:00
|
|
|
|
|
|
|
it('treats zero length comments as detached comments', async function () {
|
2024-06-18 09:59:44 -04:00
|
|
|
this.loadedSnapshotDoc.getComments().add(new Comment('comment1', []))
|
|
|
|
this.comments = [
|
|
|
|
makeComment('comment1', 16, ''),
|
|
|
|
makeComment('comment2', 10, 'brown'),
|
|
|
|
]
|
|
|
|
this.resolvedCommentIds = ['comment2']
|
2024-06-12 08:49:31 -04:00
|
|
|
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
this.persistedDocContent,
|
|
|
|
{
|
|
|
|
comments: this.comments,
|
|
|
|
},
|
|
|
|
this.resolvedCommentIds
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
})
|
2024-06-18 09:59:44 -04:00
|
|
|
|
|
|
|
it('adjusts comment positions when the underlying text has changed', async function () {
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
'quick brown fox',
|
|
|
|
{
|
|
|
|
comments: [
|
|
|
|
makeComment('comment1', 0, 'quick'),
|
|
|
|
makeComment('comment2', 12, 'fox'),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
this.resolvedCommentIds
|
|
|
|
),
|
|
|
|
]
|
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([
|
|
|
|
{ d: 'the ', p: 0 },
|
|
|
|
{ d: ' jumps over the lazy dog', p: 15 },
|
|
|
|
])
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{ d: 'the ', p: 0 },
|
|
|
|
{ d: ' jumps over the lazy dog', p: 15 },
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
c: 'fox',
|
|
|
|
p: 12,
|
|
|
|
t: 'comment2',
|
|
|
|
resolved: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
doc_length: 'quick brown fox'.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2024-05-03 09:27:39 -04:00
|
|
|
})
|
2024-05-22 08:15:07 -04:00
|
|
|
|
|
|
|
describe('syncing tracked changes', function () {
|
|
|
|
beforeEach(function () {
|
2024-06-18 09:59:44 -04:00
|
|
|
this.loadedSnapshotDoc.getTrackedChanges().add(
|
|
|
|
new TrackedChange(new Range(4, 6), {
|
|
|
|
type: 'delete',
|
|
|
|
userId: USER_ID,
|
|
|
|
ts: new Date(TIMESTAMP),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
this.loadedSnapshotDoc.getTrackedChanges().add(
|
|
|
|
new TrackedChange(new Range(10, 6), {
|
|
|
|
type: 'insert',
|
|
|
|
userId: USER_ID,
|
|
|
|
ts: new Date(TIMESTAMP),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
this.loadedSnapshotDoc.getTrackedChanges().add(
|
|
|
|
new TrackedChange(new Range(20, 6), {
|
|
|
|
type: 'delete',
|
|
|
|
userId: USER_ID,
|
|
|
|
ts: new Date(TIMESTAMP),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
this.loadedSnapshotDoc.getTrackedChanges().add(
|
|
|
|
new TrackedChange(new Range(40, 3), {
|
|
|
|
type: 'insert',
|
|
|
|
userId: USER_ID,
|
|
|
|
ts: new Date(TIMESTAMP),
|
|
|
|
})
|
|
|
|
)
|
2024-05-22 08:15:07 -04:00
|
|
|
this.changes = [
|
|
|
|
makeTrackedChange('td1', { p: 4, d: 'quick ' }),
|
|
|
|
makeTrackedChange('ti1', { p: 4, hpos: 10, i: 'brown ' }),
|
|
|
|
makeTrackedChange('td2', { p: 14, hpos: 20, d: 'jumps ' }),
|
|
|
|
makeTrackedChange('ti2', { p: 28, hpos: 40, i: 'dog' }),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does nothing if tracked changes have not changed', async function () {
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
|
|
|
|
changes: this.changes,
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('adds new tracked changes', async function () {
|
|
|
|
this.changes.splice(
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
makeTrackedChange('td3', { p: 29, hpos: 35, d: 'lazy ' })
|
|
|
|
)
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
|
|
|
|
changes: this.changes,
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
p: 35,
|
|
|
|
r: 'lazy ',
|
|
|
|
tracking: {
|
|
|
|
type: 'delete',
|
|
|
|
userId: USER_ID,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('removes extra tracked changes', async function () {
|
|
|
|
this.changes.splice(0, 1)
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
|
|
|
|
changes: this.changes,
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
p: 4,
|
|
|
|
r: 'quick ',
|
|
|
|
tracking: { type: 'none' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
origin: {
|
|
|
|
kind: 'history-resync',
|
|
|
|
},
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('handles overlapping ranges', async function () {
|
|
|
|
this.changes = [
|
|
|
|
makeTrackedChange('ti1', { p: 0, i: 'the quic' }),
|
|
|
|
makeTrackedChange('td1', { p: 8, d: 'k br' }),
|
|
|
|
makeTrackedChange('ti2', { p: 14, hpos: 23, i: 'ps over' }),
|
|
|
|
]
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
|
|
|
|
changes: this.changes,
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
|
|
|
|
// Before: the [quick ][brown ] fox [jumps ]over the lazy dog
|
|
|
|
// After: [the quic][k br]own fox jum[ps over] the lazy dog
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{
|
|
|
|
p: 0,
|
|
|
|
r: 'the quic',
|
|
|
|
tracking: { type: 'insert', userId: USER_ID, ts: TIMESTAMP },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
p: 10,
|
|
|
|
r: 'br',
|
|
|
|
tracking: { type: 'delete', userId: USER_ID, ts: TIMESTAMP },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
p: 12,
|
|
|
|
r: 'own ',
|
|
|
|
tracking: { type: 'none' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
p: 20,
|
|
|
|
r: 'jum',
|
|
|
|
tracking: { type: 'none' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
p: 23,
|
|
|
|
r: 'ps over',
|
|
|
|
tracking: { type: 'insert', userId: USER_ID, ts: TIMESTAMP },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
p: 40,
|
|
|
|
r: 'dog',
|
|
|
|
tracking: { type: 'none' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2024-06-18 09:59:44 -04:00
|
|
|
|
|
|
|
it('adjusts tracked change positions when the underlying text has changed', async function () {
|
|
|
|
const updates = [
|
|
|
|
docContentSyncUpdate(
|
|
|
|
this.persistedDoc,
|
|
|
|
'every fox jumps over the lazy dog',
|
|
|
|
{
|
|
|
|
changes: [
|
|
|
|
makeTrackedChange('ti1', { p: 5, i: ' ' }), // the space after every is still a tracked insert
|
|
|
|
makeTrackedChange('td2', { p: 10, d: 'jumps ' }),
|
|
|
|
makeTrackedChange('ti2', { p: 24, hpos: 30, i: 'dog' }),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
this.resolvedCommentIds
|
|
|
|
),
|
|
|
|
]
|
|
|
|
this.UpdateCompressor.diffAsShareJsOps.returns([
|
|
|
|
{ d: 'the quick brown', p: 0 },
|
|
|
|
{ i: 'every', p: 0 },
|
|
|
|
])
|
|
|
|
const expandedUpdates =
|
|
|
|
await this.SyncManager.promises.expandSyncUpdates(
|
|
|
|
this.projectId,
|
|
|
|
this.historyId,
|
|
|
|
updates,
|
|
|
|
this.extendLock
|
|
|
|
)
|
|
|
|
expect(expandedUpdates).to.deep.equal([
|
|
|
|
{
|
|
|
|
doc: this.persistedDoc.doc,
|
|
|
|
op: [
|
|
|
|
{ d: 'the quick brown', p: 0 },
|
|
|
|
{ i: 'every', p: 0 },
|
|
|
|
],
|
|
|
|
meta: {
|
|
|
|
pathname: this.persistedDoc.path,
|
|
|
|
doc_length: this.persistedDocContent.length,
|
|
|
|
resync: true,
|
|
|
|
ts: TIMESTAMP,
|
|
|
|
origin: { kind: 'history-resync' },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
2024-05-22 08:15:07 -04:00
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|