| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008 |
- import sinon from 'sinon'
- import { expect } from 'chai'
- import mongodb from 'mongodb-legacy'
- import tk from 'timekeeper'
- import { File, 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: {
- getLatestSnapshotFilesForChunk: 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('records docs to resync when resyncProjectStructureOnly=true is not set', async function () {
- const updates = [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('records no docs to resync with resyncProjectStructureOnly=true', async function () {
- this.projectStructureSyncUpdate.resyncProjectStructureOnly = true
- const updates = [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: [],
- 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 = File.fromString(this.persistedDocContent)
- this.mostRecentChunk = 'fake chunk'
- 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),
- getMetadata: sinon.stub().returns({}),
- },
- '1.png': {
- isEditable: sinon.stub().returns(false),
- data: { hash: this.persistedFile._hash },
- getMetadata: sinon.stub().returns({}),
- },
- }
- 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.getLatestSnapshotFilesForChunk.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,
- this.mostRecentChunk,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.equal(updates)
- expect(this.SnapshotManager.promises.getLatestSnapshotFilesForChunk).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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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 regular files', async function () {
- const newFile = {
- path: '2.png',
- file: {},
- url: 'filestore/2.png',
- _hash: 'hash-42',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile, newFile]
- ),
- ]
- const expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- this.mostRecentChunk,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.deep.equal([
- {
- pathname: newFile.path,
- file: newFile.file,
- url: newFile.url,
- hash: 'hash-42',
- meta: {
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('queues file additions for missing regular files w/o url', async function () {
- const newFile = {
- path: '2.png',
- file: {},
- _hash: 'hash-42',
- createdBlob: true,
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile, newFile]
- ),
- ]
- const expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- this.mostRecentChunk,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.deep.equal([
- {
- pathname: newFile.path,
- file: newFile.file,
- hash: 'hash-42',
- createdBlob: true,
- 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',
- },
- _hash: 'hash-42',
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [this.persistedFile, newFile]
- ),
- ]
- const expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- this.mostRecentChunk,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.deep.equal([
- {
- pathname: newFile.path,
- file: newFile.file,
- url: newFile.url,
- hash: 'hash-42',
- 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('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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- hash: 'other-hash',
- 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,
- this.mostRecentChunk,
- 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,
- hash: 'other-hash',
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.deep.equal([
- {
- pathname: noLongerLinkedFile.path,
- metadata: {},
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- hash: 'anotherhashvalue',
- meta: {
- resync: true,
- ts: TIMESTAMP,
- origin: { kind: 'history-resync' },
- },
- },
- ])
- expect(this.extendLock).to.have.been.called
- })
- it('removes and re-adds binary files w/o url if they do not have same hash', async function () {
- const persistedFileWithNewContent = {
- _hash: 'anotherhashvalue',
- hello: 'world',
- path: '1.png',
- createdBlob: true,
- }
- const updates = [
- resyncProjectStructureUpdate(
- [this.persistedDoc],
- [persistedFileWithNewContent]
- ),
- ]
- const expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- this.mostRecentChunk,
- 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,
- hash: 'anotherhashvalue',
- createdBlob: true,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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, 'the quick brown fox'),
- ]
- this.UpdateCompressor.diffAsShareJsOps.returns([
- { d: ' jumps over the lazy dog', p: 19 },
- ])
- this.expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- this.mostRecentChunk,
- 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: ' jumps over the lazy dog', p: 19 }],
- 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('syncing comments', function () {
- beforeEach(function () {
- this.loadedSnapshotDoc
- .getComments()
- .add(new Comment('comment1', [new Range(4, 5)]))
- this.loadedSnapshotDoc
- .getComments()
- .add(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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- },
- },
- ])
- })
- it('treats zero length comments as detached comments', async function () {
- this.loadedSnapshotDoc.getComments().add(new Comment('comment1', []))
- this.comments = [
- makeComment('comment1', 16, ''),
- makeComment('comment2', 10, 'brown'),
- ]
- this.resolvedCommentIds = ['comment2']
- const updates = [
- docContentSyncUpdate(
- this.persistedDoc,
- this.persistedDocContent,
- {
- comments: this.comments,
- },
- this.resolvedCommentIds
- ),
- ]
- const expandedUpdates =
- await this.SyncManager.promises.expandSyncUpdates(
- this.projectId,
- this.historyId,
- this.mostRecentChunk,
- updates,
- this.extendLock
- )
- expect(expandedUpdates).to.deep.equal([])
- })
- 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,
- this.mostRecentChunk,
- 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,
- },
- },
- ])
- })
- })
- describe('syncing tracked changes', function () {
- beforeEach(function () {
- 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),
- })
- )
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- this.mostRecentChunk,
- 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,
- },
- },
- ])
- })
- 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,
- this.mostRecentChunk,
- 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' },
- },
- },
- ])
- })
- })
- })
- })
|