| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491 |
- import sinon from 'sinon'
- import { expect } from 'chai'
- import mongodb from 'mongodb-legacy'
- import tk from 'timekeeper'
- import { Comment, TrackedChange, Range } from 'overleaf-editor-core'
- import { strict as esmock } from 'esmock'
- const { ObjectId } = mongodb
- const MODULE_PATH = '../../../../app/js/SyncManager.js'
- const TIMESTAMP = new Date().toISOString()
- const USER_ID = 'user-id'
- function resyncProjectStructureUpdate(docs, files) {
- return {
- resyncProjectStructure: { docs, files },
- meta: {
- ts: TIMESTAMP,
- },
- }
- }
- function docContentSyncUpdate(
- doc,
- content,
- ranges = {},
- resolvedCommentIds = []
- ) {
- return {
- path: doc.path,
- doc: doc.doc,
- resyncDocContent: {
- content,
- ranges,
- resolvedCommentIds,
- },
- meta: {
- ts: TIMESTAMP,
- },
- }
- }
- function makeComment(commentId, pos, text) {
- return {
- id: commentId,
- op: { p: pos, c: text, t: commentId },
- metadata: {
- user_id: USER_ID,
- ts: TIMESTAMP,
- },
- }
- }
- function makeTrackedChange(id, op) {
- return {
- id,
- op,
- metadata: { user_id: USER_ID, ts: TIMESTAMP },
- }
- }
- 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: {
- findOne: sinon.stub().resolves(this.syncState),
- updateOne: sinon.stub().resolves(),
- },
- }
- this.extendLock = sinon.stub().resolves()
- this.LockManager = {
- promises: {
- runWithLock: sinon.stub().callsFake(async (key, runner) => {
- await runner(this.extendLock)
- }),
- },
- }
- this.UpdateCompressor = {
- diffAsShareJsOps: sinon.stub().returns([]),
- }
- this.UpdateTranslator = {
- isTextUpdate: sinon.stub(),
- _convertPathname: sinon.stub(),
- }
- this.WebApiManager = {
- promises: {
- getHistoryId: sinon.stub(),
- requestResync: sinon.stub().resolves(),
- },
- }
- this.WebApiManager.promises.getHistoryId
- .withArgs(this.projectId)
- .resolves(this.historyId)
- this.ErrorRecorder = {
- promises: {
- record: sinon.stub().resolves(),
- recordSyncStart: sinon.stub().resolves(),
- },
- }
- this.RedisManager = {}
- this.SnapshotManager = {
- promises: {
- getLatestSnapshot: sinon.stub(),
- },
- }
- this.HistoryStoreManager = {
- getBlobStore: sinon.stub(),
- _getBlobHashFromString: sinon.stub().returns('random-hash'),
- }
- this.HashManager = {
- _getBlobHashFromString: sinon.stub(),
- }
- this.Metrics = { inc: sinon.stub() }
- this.Settings = {
- redis: {
- lock: {
- key_schema: {
- projectHistoryLock({ project_id: projectId }) {
- return `ProjectHistoryLock:${projectId}`
- },
- },
- },
- },
- }
- 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 () {
- beforeEach(async function () {
- this.db.projectHistorySyncState.findOne.resolves({})
- await this.SyncManager.promises.startResync(this.projectId)
- })
- it('takes the project lock', function () {
- expect(this.LockManager.promises.runWithLock).to.have.been.calledWith(
- `ProjectHistoryLock:${this.projectId}`
- )
- })
- it('gets the sync state from mongo', function () {
- expect(this.db.projectHistorySyncState.findOne).to.have.been.calledWith(
- { project_id: new ObjectId(this.projectId) }
- )
- })
- it('requests a resync from web', function () {
- expect(
- this.WebApiManager.promises.requestResync
- ).to.have.been.calledWith(this.projectId)
- })
- it('sets the sync state in mongo and prevents it expiring', function () {
- expect(
- this.db.projectHistorySyncState.updateOne
- ).to.have.been.calledWith(
- {
- project_id: new ObjectId(this.projectId),
- },
- 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 }
- this.db.projectHistorySyncState.findOne.resolves(syncState)
- })
- it('returns an error if already syncing', async function () {
- await expect(
- this.SyncManager.promises.startResync(this.projectId)
- ).to.be.rejectedWith('sync ongoing')
- })
- })
- describe('if doc content sync in is progress', function () {
- beforeEach(async function () {
- const syncState = { resyncDocContents: ['/foo.tex'] }
- this.db.projectHistorySyncState.findOne.resolves(syncState)
- })
- it('returns an error if already syncing', async function () {
- await expect(
- this.SyncManager.promises.startResync(this.projectId)
- ).to.be.rejectedWith('sync ongoing')
- })
- })
- })
- 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),
- }
- })
- it('sets the sync state in mongo and prevents it expiring', async function () {
- // SyncState is a private class of SyncManager
- // we know the interface however:
- await this.SyncManager.promises.setResyncState(
- this.projectId,
- 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 }
- )
- })
- })
- 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),
- }
- })
- it('sets the sync state entry in mongo to expire', async function () {
- await this.SyncManager.promises.setResyncState(
- this.projectId,
- 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 }
- )
- })
- })
- describe('when the new sync state is null', function () {
- it('does not update the sync state in mongo', async function () {
- // SyncState is a private class of SyncManager
- // we know the interface however:
- await this.SyncManager.promises.setResyncState(this.projectId, null)
- expect(this.db.projectHistorySyncState.updateOne).to.not.have.been
- .called
- })
- })
- })
- describe('skipUpdatesDuringSync', function () {
- describe('if a sync is not in progress', function () {
- beforeEach(async function () {
- this.db.projectHistorySyncState.findOne.resolves({})
- this.updates = ['some', 'mock', 'updates']
- this.result = await this.SyncManager.promises.skipUpdatesDuringSync(
- this.projectId,
- this.updates
- )
- })
- it('returns all updates', function () {
- expect(this.result.updates).to.deep.equal(this.updates)
- })
- it('should not return any newSyncState', function () {
- expect(this.result.syncState).to.be.null
- })
- })
- 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' },
- }
- this.db.projectHistorySyncState.findOne.resolves(syncState)
- })
- it('remove updates before a project structure sync update', async function () {
- const updates = [
- this.renameUpdate,
- this.textUpdate,
- this.projectStructureSyncUpdate,
- ]
- 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' },
- })
- })
- it('allow project structure updates after project structure sync update', async function () {
- const updates = [this.projectStructureSyncUpdate, this.renameUpdate]
- 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' },
- })
- })
- it('remove text updates for a doc before doc sync update', async function () {
- const updates = [
- this.projectStructureSyncUpdate,
- this.textUpdate,
- this.docContentSyncUpdate,
- ]
- 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' },
- })
- })
- it('allow text updates for a doc after doc sync update', async function () {
- const updates = [
- this.projectStructureSyncUpdate,
- this.docContentSyncUpdate,
- this.textUpdate,
- ]
- 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' },
- })
- })
- })
- })
- describe('expandSyncUpdates', function () {
- beforeEach(function () {
- this.persistedDoc = {
- doc: 'doc-id',
- path: 'main.tex',
- }
- this.persistedDocContent = 'the quick brown fox jumps over the lazy dog'
- this.persistedFile = {
- file: 'file-id',
- path: '1.png',
- _hash: 'abcde',
- }
- this.loadedSnapshotDoc = {
- isEditable: sinon.stub().returns(true),
- getContent: sinon.stub().returns(this.persistedDocContent),
- getComments: sinon
- .stub()
- .returns({ toArray: sinon.stub().returns([]) }),
- getTrackedChanges: sinon
- .stub()
- .returns({ asSorted: sinon.stub().returns([]) }),
- getHash: sinon.stub().returns(null),
- }
- this.fileMap = {
- 'main.tex': {
- isEditable: sinon.stub().returns(true),
- getContent: sinon.stub().returns(null),
- getHash: sinon.stub().returns(null),
- load: sinon.stub().resolves(this.loadedSnapshotDoc),
- },
- '1.png': {
- isEditable: sinon.stub().returns(false),
- data: { hash: this.persistedFile._hash },
- },
- }
- 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')
- this.SnapshotManager.promises.getLatestSnapshot.resolves(this.fileMap)
- })
- it('returns updates if no sync updates are queued', async function () {
- const updates = ['some', 'mock', 'updates']
- const expandedUpdates = await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.equal(updates)
- expect(this.SnapshotManager.promises.getLatestSnapshot).to.not.have.been
- .called
- expect(this.extendLock).to.not.have.been.called
- })
- describe('expanding project structure sync updates', function () {
- it('queues nothing for expected docs and files', async function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- ]
- 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
- })
- it('queues file removes for unexpected files', async function () {
- const updates = [resyncProjectStructureUpdate([this.persistedDoc], [])]
- 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,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('queues doc removes for unexpected docs', async function () {
- const updates = [resyncProjectStructureUpdate([], [this.persistedFile])]
- 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,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('queues file additions for missing files', async function () {
- const newFile = {
- path: '2.png',
- file: {},
- url: 'filestore/2.png',
- }
- 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,
- meta: {
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('queues blank doc additions for missing docs', async function () {
- const newDoc = {
- path: 'another.tex',
- doc: new ObjectId().toString(),
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc, newDoc],
- [this.persistedFile]
- ),
- ]
- 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,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('removes and re-adds files if whether they are binary differs', async function () {
- const fileWichWasADoc = {
- path: this.persistedDoc.path,
- url: 'filestore/2.png',
- _hash: 'other-hash',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [],
- [fileWichWasADoc, this.persistedFile]
- ),
- ]
- 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,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- {
- pathname: fileWichWasADoc.path,
- file: fileWichWasADoc.file,
- url: fileWichWasADoc.url,
- meta: {
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it("does not remove and re-add files if the expected file doesn't have a hash", async function () {
- const fileWichWasADoc = {
- path: this.persistedDoc.path,
- url: 'filestore/2.png',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [],
- [fileWichWasADoc, this.persistedFile]
- ),
- ]
- 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
- })
- it('does not remove and re-add editable files if there is a binary file with same hash', async function () {
- 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]),
- ]
- 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
- })
- it('removes and re-adds binary files if they do not have same hash', async function () {
- const persistedFileWithNewContent = {
- _hash: 'anotherhashvalue',
- hello: 'world',
- path: '1.png',
- url: 'filestore-new-url',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [persistedFileWithNewContent]
- ),
- ]
- 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,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- {
- pathname: persistedFileWithNewContent.path,
- file: persistedFileWithNewContent.file,
- url: persistedFileWithNewContent.url,
- meta: {
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('preserves other updates', async function () {
- const update = { mock: 'update' }
- const updates = [
- update,
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- ]
- 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
- })
- })
- describe('expanding doc contents sync updates', function () {
- it('returns errors from diffAsShareJsOps', async function () {
- const diffError = new Error('test')
- this.UpdateCompressor.diffAsShareJsOps.throws(diffError)
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, this.persistedDocContent),
- ]
- await expect(
- this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock
- )
- ).to.be.rejectedWith(diffError)
- expect(this.extendLock).to.have.been.called
- })
- it('handles an update for a file that is missing from the snapshot', async function () {
- const updates = [docContentSyncUpdate('not-in-snapshot.txt', 'test')]
- await expect(
- this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock
- )
- ).to.be.rejectedWith('unrecognised file: not in snapshot')
- })
- it('queues nothing for in docs whose contents is in sync', async function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, this.persistedDocContent),
- ]
- this.UpdateCompressor.diffAsShareJsOps.returns([])
- 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
- })
- it('queues text updates for docs whose contents is out of sync', async function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(
- this.persistedDoc,
- 'the fox jumps over the lazy dog'
- ),
- ]
- this.UpdateCompressor.diffAsShareJsOps.returns([
- { d: 'quick brown ', p: 4 },
- ])
- 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: 'quick brown ', p: 4 }],
- meta: {
- pathname: this.persistedDoc.path,
- doc_length: this.persistedDocContent.length,
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('queues text updates for docs created by project structure sync', async function () {
- this.UpdateCompressor.diffAsShareJsOps.returns([{ i: 'a', p: 0 }])
- const newDoc = {
- path: 'another.tex',
- doc: new ObjectId().toString(),
- }
- const newDocContent = 'a'
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc, newDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(newDoc, newDocContent),
- ]
- 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,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- {
- doc: newDoc.doc,
- op: [{ i: 'a', p: 0 }],
- meta: {
- pathname: newDoc.path,
- doc_length: 0,
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('skips text updates for docs when hashes match', async function () {
- this.fileMap['main.tex'].getHash.returns('special-hash')
- this.HashManager._getBlobHashFromString.returns('special-hash')
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, 'hello'),
- ]
- 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
- })
- it('computes text updates for docs when hashes differ', async function () {
- this.fileMap['main.tex'].getHash.returns('first-hash')
- 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'),
- ]
- 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,
- doc_length: this.persistedDocContent.length,
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- describe('for docs whose contents is out of sync', function () {
- beforeEach(async function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, 'a'),
- ]
- this.loadedSnapshotDoc.getContent.returns('stored content')
- this.UpdateCompressor.diffAsShareJsOps.returns([{ d: 'sdf', p: 1 }])
- this.expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock
- )
- })
- it('loads content from the history service when needed', function () {
- expect(this.expandedUpdates).to.deep.equal([
- {
- doc: this.persistedDoc.doc,
- op: [{ d: 'sdf', p: 1 }],
- meta: {
- pathname: this.persistedDoc.path,
- doc_length: 'stored content'.length,
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- })
- })
- describe('syncing comments', function () {
- beforeEach(function () {
- this.loadedSnapshotDoc.getComments.returns({
- toArray: sinon
- .stub()
- .returns([
- new Comment('comment1', [new Range(4, 5)]),
- new Comment('comment2', [new Range(10, 5)], true),
- ]),
- })
- this.comments = [
- makeComment('comment1', 4, 'quick'),
- makeComment('comment2', 10, 'brown'),
- ]
- this.resolvedCommentIds = ['comment2']
- })
- it('does nothing if comments have not changed', async function () {
- 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([])
- })
- it('adds missing comments', async function () {
- this.comments.push(makeComment('comment3', 20, 'jumps'))
- 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([
- {
- doc: this.persistedDoc.doc,
- op: [
- {
- c: 'jumps',
- p: 20,
- t: 'comment3',
- resolved: false,
- },
- ],
- meta: {
- origin: {
- kind: 'history-resync',
- },
- pathname: this.persistedDoc.path,
- resync: true,
- ts: TIMESTAMP,
- 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,
- },
- this.resolvedCommentIds
- ),
- ]
- 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,
- ts: TIMESTAMP,
- },
- },
- ])
- })
- 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,
- },
- this.resolvedCommentIds
- ),
- ]
- 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',
- resolved: true,
- },
- ],
- meta: {
- origin: {
- kind: 'history-resync',
- },
- resync: true,
- ts: TIMESTAMP,
- pathname: this.persistedDoc.path,
- doc_length: this.persistedDocContent.length,
- },
- },
- ])
- })
- it('sets the resolved state when it differs', async function () {
- this.resolvedCommentIds = ['comment1']
- 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.have.deep.members([
- {
- pathname: this.persistedDoc.path,
- commentId: 'comment1',
- resolved: true,
- meta: {
- origin: {
- kind: 'history-resync',
- },
- resync: true,
- ts: TIMESTAMP,
- },
- },
- {
- pathname: this.persistedDoc.path,
- commentId: 'comment2',
- resolved: false,
- meta: {
- origin: {
- kind: 'history-resync',
- },
- resync: true,
- ts: TIMESTAMP,
- },
- },
- ])
- })
- })
- describe('syncing tracked changes', function () {
- beforeEach(function () {
- this.loadedSnapshotDoc.getTrackedChanges.returns({
- asSorted: sinon.stub().returns([
- new TrackedChange(new Range(4, 6), {
- type: 'delete',
- userId: USER_ID,
- ts: new Date(TIMESTAMP),
- }),
- new TrackedChange(new Range(10, 6), {
- type: 'insert',
- userId: USER_ID,
- ts: new Date(TIMESTAMP),
- }),
- new TrackedChange(new Range(20, 6), {
- type: 'delete',
- userId: USER_ID,
- ts: new Date(TIMESTAMP),
- }),
- new TrackedChange(new Range(40, 3), {
- type: 'insert',
- userId: USER_ID,
- ts: new Date(TIMESTAMP),
- }),
- ]),
- })
- 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,
- },
- },
- ])
- })
- })
- })
- })
|