| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- import sinon from 'sinon'
- import { expect } from 'chai'
- import { ObjectId } from 'mongodb'
- import tk from 'timekeeper'
- import { strict as esmock } from 'esmock'
- const MODULE_PATH = '../../../../app/js/SyncManager.js'
- const timestamp = new Date()
- const resyncProjectStructureUpdate = (docs, files) => ({
- resyncProjectStructure: { docs, files },
- meta: {
- ts: timestamp,
- },
- })
- const docContentSyncUpdate = (doc, content) => ({
- path: doc.path,
- doc: doc.doc,
- resyncDocContent: {
- content,
- },
- meta: {
- 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().yields(null, this.syncState),
- updateOne: sinon.stub().yields(),
- },
- }
- this.callback = sinon.stub()
- this.extendLock = sinon.stub().yields()
- this.LockManager = {
- runWithLock: sinon.spy((key, runner, callback) => {
- runner(this.extendLock, callback)
- }),
- }
- this.UpdateCompressor = {
- diffAsShareJsOps: sinon.stub(),
- }
- this.UpdateTranslator = {
- isTextUpdate: sinon.stub(),
- _convertPathname: sinon.stub(),
- }
- this.WebApiManager = {
- getHistoryId: sinon.stub(),
- requestResync: sinon.stub().yields(),
- }
- this.WebApiManager.getHistoryId
- .withArgs(this.projectId)
- .yields(null, this.historyId)
- this.ErrorRecorder = {
- record: sinon.stub().yields(),
- recordSyncStart: sinon.stub().yields(),
- }
- this.RedisManager = {}
- this.SnapshotManager = {
- 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(function () {
- this.db.projectHistorySyncState.findOne.yields(null, {})
- this.SyncManager.startResync(this.projectId, this.callback)
- })
- it('takes the project lock', function () {
- expect(this.LockManager.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: ObjectId(this.projectId) }
- )
- })
- it('requests a resync from web', function () {
- expect(this.WebApiManager.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: ObjectId(this.projectId),
- },
- sinon.match({
- $set: {
- resyncProjectStructure: true,
- resyncDocContents: [],
- origin: { kind: 'history-resync' },
- },
- $currentDate: { lastUpdated: true },
- $inc: { resyncCount: 1 },
- $unset: { expiresAt: true },
- }),
- {
- upsert: true,
- }
- )
- })
- it('calls the callback', function () {
- expect(this.callback).to.have.been.called
- })
- })
- describe('if project structure sync is in progress', function () {
- beforeEach(function () {
- const syncState = { resyncProjectStructure: true }
- this.db.projectHistorySyncState.findOne.yields(null, syncState)
- this.SyncManager.startResync(this.projectId, this.callback)
- })
- it('returns an error if already syncing', function () {
- expect(this.callback).to.have.been.calledWithMatch({
- message: 'sync ongoing',
- })
- })
- })
- describe('if doc content sync in is progress', function () {
- beforeEach(function () {
- const syncState = { resyncDocContents: ['/foo.tex'] }
- this.db.projectHistorySyncState.findOne.yields(null, syncState)
- this.SyncManager.startResync(this.projectId, this.callback)
- })
- it('returns an error if already syncing', function () {
- expect(this.callback).to.have.been.calledWithMatch({
- message: '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', function (done) {
- // SyncState is a private class of SyncManager
- // we know the interface however:
- this.SyncManager.setResyncState(
- this.projectId,
- this.syncState,
- error => {
- expect(error).to.be.undefined
- expect(
- this.db.projectHistorySyncState.updateOne
- ).to.have.been.calledWith(
- {
- project_id: ObjectId(this.projectId),
- },
- sinon.match({
- $set: this.syncState.toRaw(),
- $currentDate: { lastUpdated: true },
- $inc: { resyncCount: 1 },
- $unset: { expiresAt: true },
- }),
- {
- upsert: true,
- }
- )
- done()
- }
- )
- })
- })
- 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', function (done) {
- this.SyncManager.setResyncState(
- this.projectId,
- this.syncState,
- error => {
- expect(error).to.be.undefined
- expect(
- this.db.projectHistorySyncState.updateOne
- ).to.have.been.calledWith(
- {
- project_id: 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,
- }
- )
- done()
- }
- )
- })
- })
- describe('when the new sync state is null', function () {
- it('does not update the sync state in mongo', function (done) {
- // SyncState is a private class of SyncManager
- // we know the interface however:
- this.SyncManager.setResyncState(this.projectId, null, error => {
- expect(error).to.be.undefined
- expect(this.db.projectHistorySyncState.updateOne).to.not.have.been
- .called
- done()
- })
- })
- })
- })
- describe('skipUpdatesDuringSync', function () {
- describe('if a sync is not in progress', function () {
- beforeEach(function () {
- this.db.projectHistorySyncState.findOne.yields(null, {})
- this.updates = ['some', 'mock', 'updates']
- this.SyncManager.skipUpdatesDuringSync(
- this.projectId,
- this.updates,
- this.callback
- )
- })
- it('returns all updates', function () {
- expect(this.callback).to.have.been.calledWith(null, this.updates)
- })
- it('should not return any newSyncState', function () {
- expect(this.callback).to.have.been.calledWithExactly(null, this.updates)
- })
- })
- 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.yields(null, syncState)
- })
- it('remove updates before a project structure sync update', function () {
- const updates = [
- this.renameUpdate,
- this.textUpdate,
- this.projectStructureSyncUpdate,
- ]
- this.SyncManager.skipUpdatesDuringSync(
- this.projectId,
- updates,
- (error, filteredUpdates, syncState) => {
- expect(error).to.be.null
- 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', function () {
- const updates = [this.projectStructureSyncUpdate, this.renameUpdate]
- this.SyncManager.skipUpdatesDuringSync(
- this.projectId,
- updates,
- (error, filteredUpdates, syncState) => {
- expect(error).to.be.null
- 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', function () {
- const updates = [
- this.projectStructureSyncUpdate,
- this.textUpdate,
- this.docContentSyncUpdate,
- ]
- this.SyncManager.skipUpdatesDuringSync(
- this.projectId,
- updates,
- (error, filteredUpdates, syncState) => {
- expect(error).to.be.null
- 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', function () {
- const updates = [
- this.projectStructureSyncUpdate,
- this.docContentSyncUpdate,
- this.textUpdate,
- ]
- this.SyncManager.skipUpdatesDuringSync(
- this.projectId,
- updates,
- (error, filteredUpdates, syncState) => {
- expect(error).to.be.null
- 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: { data: { hash: 'abcdef' } },
- path: 'main.tex',
- content: 'asdf',
- }
- this.persistedFile = {
- file: { data: { hash: '123456789a' } },
- path: '1.png',
- }
- this.fileMap = {
- 'main.tex': {
- isEditable: sinon.stub().returns(true),
- content: this.persistedDoc.content,
- },
- '1.png': {
- isEditable: sinon.stub().returns(false),
- data: { hash: this.persistedFile.file.data.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.getLatestSnapshot.yields(null, this.fileMap)
- })
- it('returns updates if no sync updates are queued', function () {
- const updates = ['some', 'mock', 'updates']
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- expect(expandedUpdates).to.equal(updates)
- expect(this.SnapshotManager.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', function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- expect(expandedUpdates).to.deep.equal([])
- expect(this.extendLock).to.have.been.called
- }
- )
- })
- it('queues file removes for unexpected files', function () {
- const updates = [resyncProjectStructureUpdate([this.persistedDoc], [])]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const updates = [resyncProjectStructureUpdate([], [this.persistedFile])]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const newFile = {
- path: '2.png',
- file: {},
- url: 'filestore/2.png',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile, newFile]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const newDoc = {
- path: 'another.tex',
- doc: ObjectId().toString(),
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc, newDoc],
- [this.persistedFile]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const fileWichWasADoc = {
- path: this.persistedDoc.path,
- url: 'filestore/2.png',
- _hash: 'other-hash',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [],
- [fileWichWasADoc, this.persistedFile]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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", function () {
- const fileWichWasADoc = {
- path: this.persistedDoc.path,
- url: 'filestore/2.png',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [],
- [fileWichWasADoc, this.persistedFile]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', 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]),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const persistedFileWithNewContent = {
- _hash: 'anotherhashvalue',
- hello: 'world',
- path: '1.png',
- url: 'filestore-new-url',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [persistedFileWithNewContent]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const update = 'mock-update'
- const updates = [
- update,
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- const diffError = new Error('test')
- this.UpdateCompressor.diffAsShareJsOps.throws(diffError)
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, this.persistedDoc.content),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.equal(diffError)
- expect(this.extendLock).to.have.been.called
- }
- )
- })
- it('handles an update for a file that is missing from the snapshot', function () {
- const updates = [docContentSyncUpdate('not-in-snapshot.txt', 'test')]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.exist
- expect(error.message).to.equal('unrecognised file: not in snapshot')
- }
- )
- })
- it('queues nothing for in docs whose contents is in sync', function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, this.persistedDoc.content),
- ]
- this.UpdateCompressor.diffAsShareJsOps.returns([])
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- expect(expandedUpdates).to.deep.equal([])
- expect(this.extendLock).to.have.been.called
- }
- )
- })
- it('queues text updates for docs whose contents is out of sync', function () {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, 'a'),
- ]
- this.UpdateCompressor.diffAsShareJsOps.returns([{ d: 'sdf', p: 1 }])
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- expect(expandedUpdates).to.deep.equal([
- {
- doc: this.persistedDoc.doc,
- op: [{ d: 'sdf', p: 1 }],
- meta: {
- pathname: this.persistedDoc.path,
- doc_length: 4,
- 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', function () {
- this.UpdateCompressor.diffAsShareJsOps.returns([{ i: 'a', p: 0 }])
- const newDoc = {
- path: 'another.tex',
- doc: ObjectId().toString(),
- content: 'a',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc, newDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(newDoc, newDoc.content),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- 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', function () {
- this.fileMap['main.tex'].getHash = sinon.stub().returns('special-hash')
- this.HashManager._getBlobHashFromString.returns('special-hash')
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, 'hello'),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- expect(expandedUpdates).to.deep.equal([])
- expect(this.extendLock).to.have.been.called
- }
- )
- })
- it('computes text updates for docs when hashes differ', function () {
- this.fileMap['main.tex'].getHash = sinon.stub().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'),
- ]
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- expect(error).to.be.null
- expect(expandedUpdates).to.deep.equal([
- {
- doc: this.persistedDoc.doc,
- op: [{ i: 'test diff', p: 0 }],
- meta: {
- pathname: this.persistedDoc.path,
- doc_length: 4,
- 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(function (done) {
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile]
- ),
- docContentSyncUpdate(this.persistedDoc, 'a'),
- ]
- const file = { getContent: sinon.stub().returns('stored content') }
- this.fileMap['main.tex'].load = sinon.stub().resolves(file)
- this.UpdateCompressor.diffAsShareJsOps.returns([{ d: 'sdf', p: 1 }])
- this.SyncManager.expandSyncUpdates(
- this.projectId,
- this.historyId,
- updates,
- this.extendLock,
- (error, expandedUpdates) => {
- this.error = error
- this.expandedUpdates = expandedUpdates
- done()
- }
- )
- })
- it('loads content from the history service when needed', function () {
- expect(this.error).to.be.null
- 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
- })
- })
- })
- })
- })
|