SyncManagerTests.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import mongodb from 'mongodb-legacy'
  4. import tk from 'timekeeper'
  5. import { File, Comment, TrackedChange, Range } from 'overleaf-editor-core'
  6. import { strict as esmock } from 'esmock'
  7. const { ObjectId } = mongodb
  8. const MODULE_PATH = '../../../../app/js/SyncManager.js'
  9. const TIMESTAMP = new Date().toISOString()
  10. const USER_ID = 'user-id'
  11. function resyncProjectStructureUpdate(docs, files) {
  12. return {
  13. resyncProjectStructure: { docs, files },
  14. meta: {
  15. ts: TIMESTAMP,
  16. },
  17. }
  18. }
  19. function docContentSyncUpdate(
  20. doc,
  21. content,
  22. ranges = {},
  23. resolvedCommentIds = []
  24. ) {
  25. return {
  26. path: doc.path,
  27. doc: doc.doc,
  28. resyncDocContent: {
  29. content,
  30. ranges,
  31. resolvedCommentIds,
  32. },
  33. meta: {
  34. ts: TIMESTAMP,
  35. },
  36. }
  37. }
  38. function makeComment(commentId, pos, text) {
  39. return {
  40. id: commentId,
  41. op: { p: pos, c: text, t: commentId },
  42. metadata: {
  43. user_id: USER_ID,
  44. ts: TIMESTAMP,
  45. },
  46. }
  47. }
  48. function makeTrackedChange(id, op) {
  49. return {
  50. id,
  51. op,
  52. metadata: { user_id: USER_ID, ts: TIMESTAMP },
  53. }
  54. }
  55. describe('SyncManager', function () {
  56. beforeEach(async function () {
  57. this.now = new Date()
  58. tk.freeze(this.now)
  59. this.projectId = new ObjectId().toString()
  60. this.historyId = 'mock-overleaf-id'
  61. this.syncState = { origin: { kind: 'history-resync' } }
  62. this.db = {
  63. projectHistorySyncState: {
  64. findOne: sinon.stub().resolves(this.syncState),
  65. updateOne: sinon.stub().resolves(),
  66. },
  67. }
  68. this.extendLock = sinon.stub().resolves()
  69. this.LockManager = {
  70. promises: {
  71. runWithLock: sinon.stub().callsFake(async (key, runner) => {
  72. await runner(this.extendLock)
  73. }),
  74. },
  75. }
  76. this.UpdateCompressor = {
  77. diffAsShareJsOps: sinon.stub().returns([]),
  78. }
  79. this.UpdateTranslator = {
  80. isTextUpdate: sinon.stub(),
  81. _convertPathname: sinon.stub(),
  82. }
  83. this.WebApiManager = {
  84. promises: {
  85. getHistoryId: sinon.stub(),
  86. requestResync: sinon.stub().resolves(),
  87. },
  88. }
  89. this.WebApiManager.promises.getHistoryId
  90. .withArgs(this.projectId)
  91. .resolves(this.historyId)
  92. this.ErrorRecorder = {
  93. promises: {
  94. record: sinon.stub().resolves(),
  95. recordSyncStart: sinon.stub().resolves(),
  96. },
  97. }
  98. this.RedisManager = {}
  99. this.SnapshotManager = {
  100. promises: {
  101. getLatestSnapshotFilesForChunk: sinon.stub(),
  102. },
  103. }
  104. this.HistoryStoreManager = {
  105. getBlobStore: sinon.stub(),
  106. _getBlobHashFromString: sinon.stub().returns('random-hash'),
  107. }
  108. this.HashManager = {
  109. _getBlobHashFromString: sinon.stub(),
  110. }
  111. this.Metrics = { inc: sinon.stub() }
  112. this.Settings = {
  113. redis: {
  114. lock: {
  115. key_schema: {
  116. projectHistoryLock({ project_id: projectId }) {
  117. return `ProjectHistoryLock:${projectId}`
  118. },
  119. },
  120. },
  121. },
  122. }
  123. this.SyncManager = await esmock(MODULE_PATH, {
  124. '../../../../app/js/LockManager.js': this.LockManager,
  125. '../../../../app/js/UpdateCompressor.js': this.UpdateCompressor,
  126. '../../../../app/js/UpdateTranslator.js': this.UpdateTranslator,
  127. '../../../../app/js/mongodb.js': { ObjectId, db: this.db },
  128. '../../../../app/js/WebApiManager.js': this.WebApiManager,
  129. '../../../../app/js/ErrorRecorder.js': this.ErrorRecorder,
  130. '../../../../app/js/RedisManager.js': this.RedisManager,
  131. '../../../../app/js/SnapshotManager.js': this.SnapshotManager,
  132. '../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
  133. '../../../../app/js/HashManager.js': this.HashManager,
  134. '@overleaf/metrics': this.Metrics,
  135. '@overleaf/settings': this.Settings,
  136. })
  137. })
  138. afterEach(function () {
  139. tk.reset()
  140. })
  141. describe('startResync', function () {
  142. describe('if a sync is not in progress', function () {
  143. beforeEach(async function () {
  144. this.db.projectHistorySyncState.findOne.resolves({})
  145. await this.SyncManager.promises.startResync(this.projectId)
  146. })
  147. it('takes the project lock', function () {
  148. expect(this.LockManager.promises.runWithLock).to.have.been.calledWith(
  149. `ProjectHistoryLock:${this.projectId}`
  150. )
  151. })
  152. it('gets the sync state from mongo', function () {
  153. expect(this.db.projectHistorySyncState.findOne).to.have.been.calledWith(
  154. { project_id: new ObjectId(this.projectId) }
  155. )
  156. })
  157. it('requests a resync from web', function () {
  158. expect(
  159. this.WebApiManager.promises.requestResync
  160. ).to.have.been.calledWith(this.projectId)
  161. })
  162. it('sets the sync state in mongo and prevents it expiring', function () {
  163. expect(
  164. this.db.projectHistorySyncState.updateOne
  165. ).to.have.been.calledWith(
  166. {
  167. project_id: new ObjectId(this.projectId),
  168. },
  169. sinon.match({
  170. $set: {
  171. resyncProjectStructure: true,
  172. resyncDocContents: [],
  173. origin: { kind: 'history-resync' },
  174. },
  175. $currentDate: { lastUpdated: true },
  176. $inc: { resyncCount: 1 },
  177. $unset: { expiresAt: true },
  178. }),
  179. {
  180. upsert: true,
  181. }
  182. )
  183. })
  184. })
  185. describe('if project structure sync is in progress', function () {
  186. beforeEach(function () {
  187. const syncState = { resyncProjectStructure: true }
  188. this.db.projectHistorySyncState.findOne.resolves(syncState)
  189. })
  190. it('returns an error if already syncing', async function () {
  191. await expect(
  192. this.SyncManager.promises.startResync(this.projectId)
  193. ).to.be.rejectedWith('sync ongoing')
  194. })
  195. })
  196. describe('if doc content sync in is progress', function () {
  197. beforeEach(async function () {
  198. const syncState = { resyncDocContents: ['/foo.tex'] }
  199. this.db.projectHistorySyncState.findOne.resolves(syncState)
  200. })
  201. it('returns an error if already syncing', async function () {
  202. await expect(
  203. this.SyncManager.promises.startResync(this.projectId)
  204. ).to.be.rejectedWith('sync ongoing')
  205. })
  206. })
  207. })
  208. describe('setResyncState', function () {
  209. describe('when the sync is starting', function () {
  210. beforeEach(function () {
  211. this.syncState = {
  212. toRaw() {
  213. return {
  214. resyncProjectStructure: true,
  215. resyncDocContents: [],
  216. origin: { kind: 'history-resync' },
  217. }
  218. },
  219. isSyncOngoing: sinon.stub().returns(true),
  220. }
  221. })
  222. it('sets the sync state in mongo and prevents it expiring', async function () {
  223. // SyncState is a private class of SyncManager
  224. // we know the interface however:
  225. await this.SyncManager.promises.setResyncState(
  226. this.projectId,
  227. this.syncState
  228. )
  229. expect(
  230. this.db.projectHistorySyncState.updateOne
  231. ).to.have.been.calledWith(
  232. { project_id: new ObjectId(this.projectId) },
  233. sinon.match({
  234. $set: this.syncState.toRaw(),
  235. $currentDate: { lastUpdated: true },
  236. $inc: { resyncCount: 1 },
  237. $unset: { expiresAt: true },
  238. }),
  239. { upsert: true }
  240. )
  241. })
  242. })
  243. describe('when the sync is ending', function () {
  244. beforeEach(function () {
  245. this.syncState = {
  246. toRaw() {
  247. return {
  248. resyncProjectStructure: false,
  249. resyncDocContents: [],
  250. origin: { kind: 'history-resync' },
  251. }
  252. },
  253. isSyncOngoing: sinon.stub().returns(false),
  254. }
  255. })
  256. it('sets the sync state entry in mongo to expire', async function () {
  257. await this.SyncManager.promises.setResyncState(
  258. this.projectId,
  259. this.syncState
  260. )
  261. expect(
  262. this.db.projectHistorySyncState.updateOne
  263. ).to.have.been.calledWith(
  264. { project_id: new ObjectId(this.projectId) },
  265. sinon.match({
  266. $set: {
  267. resyncProjectStructure: false,
  268. resyncDocContents: [],
  269. origin: { kind: 'history-resync' },
  270. expiresAt: new Date(this.now.getTime() + 90 * 24 * 3600 * 1000),
  271. },
  272. $currentDate: { lastUpdated: true },
  273. }),
  274. { upsert: true }
  275. )
  276. })
  277. })
  278. describe('when the new sync state is null', function () {
  279. it('does not update the sync state in mongo', async function () {
  280. // SyncState is a private class of SyncManager
  281. // we know the interface however:
  282. await this.SyncManager.promises.setResyncState(this.projectId, null)
  283. expect(this.db.projectHistorySyncState.updateOne).to.not.have.been
  284. .called
  285. })
  286. })
  287. })
  288. describe('skipUpdatesDuringSync', function () {
  289. describe('if a sync is not in progress', function () {
  290. beforeEach(async function () {
  291. this.db.projectHistorySyncState.findOne.resolves({})
  292. this.updates = ['some', 'mock', 'updates']
  293. this.result = await this.SyncManager.promises.skipUpdatesDuringSync(
  294. this.projectId,
  295. this.updates
  296. )
  297. })
  298. it('returns all updates', function () {
  299. expect(this.result.updates).to.deep.equal(this.updates)
  300. })
  301. it('should not return any newSyncState', function () {
  302. expect(this.result.syncState).to.be.null
  303. })
  304. })
  305. describe('if a sync in is progress', function () {
  306. beforeEach(function () {
  307. this.renameUpdate = {
  308. pathname: 'old.tex',
  309. newPathname: 'new.tex',
  310. }
  311. this.projectStructureSyncUpdate = {
  312. resyncProjectStructure: {
  313. docs: [{ path: 'new.tex' }],
  314. files: [],
  315. },
  316. }
  317. this.textUpdate = {
  318. doc: new ObjectId(),
  319. op: [{ i: 'a', p: 4 }],
  320. meta: {
  321. pathname: 'new.tex',
  322. doc_length: 4,
  323. },
  324. }
  325. this.docContentSyncUpdate = {
  326. path: 'new.tex',
  327. resyncDocContent: {
  328. content: 'a',
  329. },
  330. }
  331. this.UpdateTranslator.isTextUpdate
  332. .withArgs(this.renameUpdate)
  333. .returns(false)
  334. this.UpdateTranslator.isTextUpdate
  335. .withArgs(this.projectStructureSyncUpdate)
  336. .returns(false)
  337. this.UpdateTranslator.isTextUpdate
  338. .withArgs(this.docContentSyncUpdate)
  339. .returns(false)
  340. this.UpdateTranslator.isTextUpdate
  341. .withArgs(this.textUpdate)
  342. .returns(true)
  343. const syncState = {
  344. resyncProjectStructure: true,
  345. resyncDocContents: [],
  346. origin: { kind: 'history-resync' },
  347. }
  348. this.db.projectHistorySyncState.findOne.resolves(syncState)
  349. })
  350. it('remove updates before a project structure sync update', async function () {
  351. const updates = [
  352. this.renameUpdate,
  353. this.textUpdate,
  354. this.projectStructureSyncUpdate,
  355. ]
  356. const { updates: filteredUpdates, syncState } =
  357. await this.SyncManager.promises.skipUpdatesDuringSync(
  358. this.projectId,
  359. updates
  360. )
  361. expect(filteredUpdates).to.deep.equal([this.projectStructureSyncUpdate])
  362. expect(syncState.toRaw()).to.deep.equal({
  363. resyncProjectStructure: false,
  364. resyncDocContents: ['new.tex'],
  365. origin: { kind: 'history-resync' },
  366. })
  367. })
  368. it('records docs to resync when resyncProjectStructureOnly=true is not set', async function () {
  369. const updates = [this.projectStructureSyncUpdate]
  370. const { updates: filteredUpdates, syncState } =
  371. await this.SyncManager.promises.skipUpdatesDuringSync(
  372. this.projectId,
  373. updates
  374. )
  375. expect(filteredUpdates).to.deep.equal([this.projectStructureSyncUpdate])
  376. expect(syncState.toRaw()).to.deep.equal({
  377. resyncProjectStructure: false,
  378. resyncDocContents: ['new.tex'],
  379. origin: { kind: 'history-resync' },
  380. })
  381. })
  382. it('records no docs to resync with resyncProjectStructureOnly=true', async function () {
  383. this.projectStructureSyncUpdate.resyncProjectStructureOnly = true
  384. const updates = [this.projectStructureSyncUpdate]
  385. const { updates: filteredUpdates, syncState } =
  386. await this.SyncManager.promises.skipUpdatesDuringSync(
  387. this.projectId,
  388. updates
  389. )
  390. expect(filteredUpdates).to.deep.equal([this.projectStructureSyncUpdate])
  391. expect(syncState.toRaw()).to.deep.equal({
  392. resyncProjectStructure: false,
  393. resyncDocContents: [],
  394. origin: { kind: 'history-resync' },
  395. })
  396. })
  397. it('allow project structure updates after project structure sync update', async function () {
  398. const updates = [this.projectStructureSyncUpdate, this.renameUpdate]
  399. const { updates: filteredUpdates, syncState } =
  400. await this.SyncManager.promises.skipUpdatesDuringSync(
  401. this.projectId,
  402. updates
  403. )
  404. expect(filteredUpdates).to.deep.equal([
  405. this.projectStructureSyncUpdate,
  406. this.renameUpdate,
  407. ])
  408. expect(syncState.toRaw()).to.deep.equal({
  409. resyncProjectStructure: false,
  410. resyncDocContents: ['new.tex'],
  411. origin: { kind: 'history-resync' },
  412. })
  413. })
  414. it('remove text updates for a doc before doc sync update', async function () {
  415. const updates = [
  416. this.projectStructureSyncUpdate,
  417. this.textUpdate,
  418. this.docContentSyncUpdate,
  419. ]
  420. const { updates: filteredUpdates, syncState } =
  421. await this.SyncManager.promises.skipUpdatesDuringSync(
  422. this.projectId,
  423. updates
  424. )
  425. expect(filteredUpdates).to.deep.equal([
  426. this.projectStructureSyncUpdate,
  427. this.docContentSyncUpdate,
  428. ])
  429. expect(syncState.toRaw()).to.deep.equal({
  430. resyncProjectStructure: false,
  431. resyncDocContents: [],
  432. origin: { kind: 'history-resync' },
  433. })
  434. })
  435. it('allow text updates for a doc after doc sync update', async function () {
  436. const updates = [
  437. this.projectStructureSyncUpdate,
  438. this.docContentSyncUpdate,
  439. this.textUpdate,
  440. ]
  441. const { updates: filteredUpdates, syncState } =
  442. await this.SyncManager.promises.skipUpdatesDuringSync(
  443. this.projectId,
  444. updates
  445. )
  446. expect(filteredUpdates).to.deep.equal([
  447. this.projectStructureSyncUpdate,
  448. this.docContentSyncUpdate,
  449. this.textUpdate,
  450. ])
  451. expect(syncState.toRaw()).to.deep.equal({
  452. resyncProjectStructure: false,
  453. resyncDocContents: [],
  454. origin: { kind: 'history-resync' },
  455. })
  456. })
  457. })
  458. })
  459. describe('expandSyncUpdates', function () {
  460. beforeEach(function () {
  461. this.persistedDoc = {
  462. doc: 'doc-id',
  463. path: 'main.tex',
  464. }
  465. this.persistedDocContent = 'the quick brown fox jumps over the lazy dog'
  466. this.persistedFile = {
  467. file: 'file-id',
  468. path: '1.png',
  469. _hash: 'abcde',
  470. }
  471. this.loadedSnapshotDoc = File.fromString(this.persistedDocContent)
  472. this.mostRecentChunk = 'fake chunk'
  473. this.fileMap = {
  474. 'main.tex': {
  475. isEditable: sinon.stub().returns(true),
  476. getContent: sinon.stub().returns(null),
  477. getHash: sinon.stub().returns(null),
  478. load: sinon.stub().resolves(this.loadedSnapshotDoc),
  479. getMetadata: sinon.stub().returns({}),
  480. },
  481. '1.png': {
  482. isEditable: sinon.stub().returns(false),
  483. data: { hash: this.persistedFile._hash },
  484. getMetadata: sinon.stub().returns({}),
  485. },
  486. }
  487. this.UpdateTranslator._convertPathname
  488. .withArgs('main.tex')
  489. .returns('main.tex')
  490. this.UpdateTranslator._convertPathname
  491. .withArgs('/main.tex')
  492. .returns('main.tex')
  493. this.UpdateTranslator._convertPathname
  494. .withArgs('another.tex')
  495. .returns('another.tex')
  496. this.UpdateTranslator._convertPathname.withArgs('1.png').returns('1.png')
  497. this.UpdateTranslator._convertPathname.withArgs('2.png').returns('2.png')
  498. this.SnapshotManager.promises.getLatestSnapshotFilesForChunk.resolves(
  499. this.fileMap
  500. )
  501. })
  502. it('returns updates if no sync updates are queued', async function () {
  503. const updates = ['some', 'mock', 'updates']
  504. const expandedUpdates = await this.SyncManager.promises.expandSyncUpdates(
  505. this.projectId,
  506. this.historyId,
  507. this.mostRecentChunk,
  508. updates,
  509. this.extendLock
  510. )
  511. expect(expandedUpdates).to.equal(updates)
  512. expect(this.SnapshotManager.promises.getLatestSnapshotFilesForChunk).to
  513. .not.have.been.called
  514. expect(this.extendLock).to.not.have.been.called
  515. })
  516. describe('expanding project structure sync updates', function () {
  517. it('queues nothing for expected docs and files', async function () {
  518. const updates = [
  519. resyncProjectStructureUpdate(
  520. [this.persistedDoc],
  521. [this.persistedFile]
  522. ),
  523. ]
  524. const expandedUpdates =
  525. await this.SyncManager.promises.expandSyncUpdates(
  526. this.projectId,
  527. this.historyId,
  528. this.mostRecentChunk,
  529. updates,
  530. this.extendLock
  531. )
  532. expect(expandedUpdates).to.deep.equal([])
  533. expect(this.extendLock).to.have.been.called
  534. })
  535. it('queues file removes for unexpected files', async function () {
  536. const updates = [resyncProjectStructureUpdate([this.persistedDoc], [])]
  537. const expandedUpdates =
  538. await this.SyncManager.promises.expandSyncUpdates(
  539. this.projectId,
  540. this.historyId,
  541. this.mostRecentChunk,
  542. updates,
  543. this.extendLock
  544. )
  545. expect(expandedUpdates).to.deep.equal([
  546. {
  547. pathname: this.persistedFile.path,
  548. new_pathname: '',
  549. meta: {
  550. resync: true,
  551. ts: TIMESTAMP,
  552. origin: { kind: 'history-resync' },
  553. },
  554. },
  555. ])
  556. expect(this.extendLock).to.have.been.called
  557. })
  558. it('queues doc removes for unexpected docs', async function () {
  559. const updates = [resyncProjectStructureUpdate([], [this.persistedFile])]
  560. const expandedUpdates =
  561. await this.SyncManager.promises.expandSyncUpdates(
  562. this.projectId,
  563. this.historyId,
  564. this.mostRecentChunk,
  565. updates,
  566. this.extendLock
  567. )
  568. expect(expandedUpdates).to.deep.equal([
  569. {
  570. pathname: this.persistedDoc.path,
  571. new_pathname: '',
  572. meta: {
  573. resync: true,
  574. ts: TIMESTAMP,
  575. origin: { kind: 'history-resync' },
  576. },
  577. },
  578. ])
  579. expect(this.extendLock).to.have.been.called
  580. })
  581. it('queues file additions for missing regular files', async function () {
  582. const newFile = {
  583. path: '2.png',
  584. file: {},
  585. url: 'filestore/2.png',
  586. _hash: 'hash-42',
  587. }
  588. const updates = [
  589. resyncProjectStructureUpdate(
  590. [this.persistedDoc],
  591. [this.persistedFile, newFile]
  592. ),
  593. ]
  594. const expandedUpdates =
  595. await this.SyncManager.promises.expandSyncUpdates(
  596. this.projectId,
  597. this.historyId,
  598. this.mostRecentChunk,
  599. updates,
  600. this.extendLock
  601. )
  602. expect(expandedUpdates).to.deep.equal([
  603. {
  604. pathname: newFile.path,
  605. file: newFile.file,
  606. url: newFile.url,
  607. hash: 'hash-42',
  608. meta: {
  609. resync: true,
  610. ts: TIMESTAMP,
  611. origin: { kind: 'history-resync' },
  612. },
  613. },
  614. ])
  615. expect(this.extendLock).to.have.been.called
  616. })
  617. it('queues file additions for missing regular files w/o url', async function () {
  618. const newFile = {
  619. path: '2.png',
  620. file: {},
  621. _hash: 'hash-42',
  622. createdBlob: true,
  623. }
  624. const updates = [
  625. resyncProjectStructureUpdate(
  626. [this.persistedDoc],
  627. [this.persistedFile, newFile]
  628. ),
  629. ]
  630. const expandedUpdates =
  631. await this.SyncManager.promises.expandSyncUpdates(
  632. this.projectId,
  633. this.historyId,
  634. this.mostRecentChunk,
  635. updates,
  636. this.extendLock
  637. )
  638. expect(expandedUpdates).to.deep.equal([
  639. {
  640. pathname: newFile.path,
  641. file: newFile.file,
  642. hash: 'hash-42',
  643. createdBlob: true,
  644. meta: {
  645. resync: true,
  646. ts: TIMESTAMP,
  647. origin: { kind: 'history-resync' },
  648. },
  649. },
  650. ])
  651. expect(this.extendLock).to.have.been.called
  652. })
  653. it('queues file additions for missing linked files', async function () {
  654. const newFile = {
  655. path: '2.png',
  656. file: {},
  657. url: 'filestore/2.png',
  658. metadata: {
  659. importedAt: '2024-07-30T09:14:45.928Z',
  660. provider: 'references-provider',
  661. },
  662. _hash: 'hash-42',
  663. }
  664. const updates = [
  665. resyncProjectStructureUpdate(
  666. [this.persistedDoc],
  667. [this.persistedFile, newFile]
  668. ),
  669. ]
  670. const expandedUpdates =
  671. await this.SyncManager.promises.expandSyncUpdates(
  672. this.projectId,
  673. this.historyId,
  674. this.mostRecentChunk,
  675. updates,
  676. this.extendLock
  677. )
  678. expect(expandedUpdates).to.deep.equal([
  679. {
  680. pathname: newFile.path,
  681. file: newFile.file,
  682. url: newFile.url,
  683. hash: 'hash-42',
  684. metadata: {
  685. importedAt: '2024-07-30T09:14:45.928Z',
  686. provider: 'references-provider',
  687. },
  688. meta: {
  689. resync: true,
  690. ts: TIMESTAMP,
  691. origin: { kind: 'history-resync' },
  692. },
  693. },
  694. ])
  695. expect(this.extendLock).to.have.been.called
  696. })
  697. it('queues blank doc additions for missing docs', async function () {
  698. const newDoc = {
  699. path: 'another.tex',
  700. doc: new ObjectId().toString(),
  701. }
  702. const updates = [
  703. resyncProjectStructureUpdate(
  704. [this.persistedDoc, newDoc],
  705. [this.persistedFile]
  706. ),
  707. ]
  708. const expandedUpdates =
  709. await this.SyncManager.promises.expandSyncUpdates(
  710. this.projectId,
  711. this.historyId,
  712. this.mostRecentChunk,
  713. updates,
  714. this.extendLock
  715. )
  716. expect(expandedUpdates).to.deep.equal([
  717. {
  718. pathname: newDoc.path,
  719. doc: newDoc.doc,
  720. docLines: '',
  721. meta: {
  722. resync: true,
  723. ts: TIMESTAMP,
  724. origin: { kind: 'history-resync' },
  725. },
  726. },
  727. ])
  728. expect(this.extendLock).to.have.been.called
  729. })
  730. it('removes and re-adds files if whether they are binary differs', async function () {
  731. const fileWichWasADoc = {
  732. path: this.persistedDoc.path,
  733. url: 'filestore/2.png',
  734. _hash: 'other-hash',
  735. }
  736. const updates = [
  737. resyncProjectStructureUpdate(
  738. [],
  739. [fileWichWasADoc, this.persistedFile]
  740. ),
  741. ]
  742. const expandedUpdates =
  743. await this.SyncManager.promises.expandSyncUpdates(
  744. this.projectId,
  745. this.historyId,
  746. this.mostRecentChunk,
  747. updates,
  748. this.extendLock
  749. )
  750. expect(expandedUpdates).to.deep.equal([
  751. {
  752. pathname: fileWichWasADoc.path,
  753. new_pathname: '',
  754. meta: {
  755. resync: true,
  756. ts: TIMESTAMP,
  757. origin: { kind: 'history-resync' },
  758. },
  759. },
  760. {
  761. pathname: fileWichWasADoc.path,
  762. file: fileWichWasADoc.file,
  763. url: fileWichWasADoc.url,
  764. hash: 'other-hash',
  765. meta: {
  766. resync: true,
  767. ts: TIMESTAMP,
  768. origin: { kind: 'history-resync' },
  769. },
  770. },
  771. ])
  772. expect(this.extendLock).to.have.been.called
  773. })
  774. it('removes and re-adds linked-files if their binary state differs', async function () {
  775. const fileWhichWasADoc = {
  776. path: this.persistedDoc.path,
  777. url: 'filestore/references.txt',
  778. _hash: 'other-hash',
  779. metadata: {
  780. importedAt: '2024-07-30T09:14:45.928Z',
  781. provider: 'references-provider',
  782. },
  783. }
  784. const updates = [
  785. resyncProjectStructureUpdate(
  786. [],
  787. [fileWhichWasADoc, this.persistedFile]
  788. ),
  789. ]
  790. const expandedUpdates =
  791. await this.SyncManager.promises.expandSyncUpdates(
  792. this.projectId,
  793. this.historyId,
  794. this.mostRecentChunk,
  795. updates,
  796. this.extendLock
  797. )
  798. expect(expandedUpdates).to.deep.equal([
  799. {
  800. pathname: fileWhichWasADoc.path,
  801. new_pathname: '',
  802. meta: {
  803. resync: true,
  804. ts: TIMESTAMP,
  805. origin: { kind: 'history-resync' },
  806. },
  807. },
  808. {
  809. pathname: fileWhichWasADoc.path,
  810. file: fileWhichWasADoc.file,
  811. url: fileWhichWasADoc.url,
  812. hash: 'other-hash',
  813. metadata: {
  814. importedAt: '2024-07-30T09:14:45.928Z',
  815. provider: 'references-provider',
  816. },
  817. meta: {
  818. resync: true,
  819. ts: TIMESTAMP,
  820. origin: { kind: 'history-resync' },
  821. },
  822. },
  823. ])
  824. expect(this.extendLock).to.have.been.called
  825. })
  826. it('add linked file data with same hash', async function () {
  827. const nowLinkedFile = {
  828. path: this.persistedFile.path,
  829. url: 'filestore/1.png',
  830. _hash: this.persistedFile._hash,
  831. metadata: {
  832. importedAt: '2024-07-30T09:14:45.928Z',
  833. provider: 'image-provider',
  834. },
  835. }
  836. const updates = [
  837. resyncProjectStructureUpdate([this.persistedDoc], [nowLinkedFile]),
  838. ]
  839. const expandedUpdates =
  840. await this.SyncManager.promises.expandSyncUpdates(
  841. this.projectId,
  842. this.historyId,
  843. this.mostRecentChunk,
  844. updates,
  845. this.extendLock
  846. )
  847. expect(expandedUpdates).to.deep.equal([
  848. {
  849. pathname: nowLinkedFile.path,
  850. metadata: {
  851. importedAt: '2024-07-30T09:14:45.928Z',
  852. provider: 'image-provider',
  853. },
  854. meta: {
  855. resync: true,
  856. ts: TIMESTAMP,
  857. origin: { kind: 'history-resync' },
  858. },
  859. },
  860. ])
  861. expect(this.extendLock).to.have.been.called
  862. })
  863. it('updates linked file data when hash remains the same', async function () {
  864. this.fileMap[this.persistedFile.path].getMetadata.returns({
  865. importedAt: '2024-07-30T09:14:45.928Z',
  866. provider: 'image-provider',
  867. })
  868. const updatedLinkedFile = {
  869. path: this.persistedFile.path,
  870. url: 'filestore/1.png',
  871. _hash: this.persistedFile._hash,
  872. metadata: {
  873. importedAt: '2024-07-31T00:00:00.000Z',
  874. provider: 'image-provider',
  875. },
  876. }
  877. const updates = [
  878. resyncProjectStructureUpdate(
  879. [this.persistedDoc],
  880. [updatedLinkedFile]
  881. ),
  882. ]
  883. const expandedUpdates =
  884. await this.SyncManager.promises.expandSyncUpdates(
  885. this.projectId,
  886. this.historyId,
  887. this.mostRecentChunk,
  888. updates,
  889. this.extendLock
  890. )
  891. expect(expandedUpdates).to.deep.equal([
  892. {
  893. pathname: updatedLinkedFile.path,
  894. metadata: {
  895. importedAt: '2024-07-31T00:00:00.000Z',
  896. provider: 'image-provider',
  897. },
  898. meta: {
  899. resync: true,
  900. ts: TIMESTAMP,
  901. origin: { kind: 'history-resync' },
  902. },
  903. },
  904. ])
  905. expect(this.extendLock).to.have.been.called
  906. })
  907. it('remove linked file data', async function () {
  908. this.fileMap[this.persistedFile.path].getMetadata.returns({
  909. importedAt: '2024-07-30T09:14:45.928Z',
  910. provider: 'image-provider',
  911. })
  912. const noLongerLinkedFile = {
  913. path: this.persistedFile.path,
  914. url: 'filestore/1.png',
  915. _hash: this.persistedFile._hash,
  916. }
  917. const updates = [
  918. resyncProjectStructureUpdate(
  919. [this.persistedDoc],
  920. [noLongerLinkedFile]
  921. ),
  922. ]
  923. const expandedUpdates =
  924. await this.SyncManager.promises.expandSyncUpdates(
  925. this.projectId,
  926. this.historyId,
  927. this.mostRecentChunk,
  928. updates,
  929. this.extendLock
  930. )
  931. expect(expandedUpdates).to.deep.equal([
  932. {
  933. pathname: noLongerLinkedFile.path,
  934. metadata: {},
  935. meta: {
  936. resync: true,
  937. ts: TIMESTAMP,
  938. origin: { kind: 'history-resync' },
  939. },
  940. },
  941. ])
  942. expect(this.extendLock).to.have.been.called
  943. })
  944. it("does not remove and re-add files if the expected file doesn't have a hash", async function () {
  945. const fileWichWasADoc = {
  946. path: this.persistedDoc.path,
  947. url: 'filestore/2.png',
  948. }
  949. const updates = [
  950. resyncProjectStructureUpdate(
  951. [],
  952. [fileWichWasADoc, this.persistedFile]
  953. ),
  954. ]
  955. const expandedUpdates =
  956. await this.SyncManager.promises.expandSyncUpdates(
  957. this.projectId,
  958. this.historyId,
  959. this.mostRecentChunk,
  960. updates,
  961. this.extendLock
  962. )
  963. expect(expandedUpdates).to.deep.equal([])
  964. expect(this.extendLock).to.have.been.called
  965. })
  966. it('does not remove and re-add editable files if there is a binary file with same hash', async function () {
  967. const binaryFile = {
  968. file: Object().toString(),
  969. // The paths in the resyncProjectStructureUpdate must have a leading slash ('/')
  970. // The other unit tests in this file are incorrectly missing the leading slash.
  971. // The leading slash is present in web where the paths are created with
  972. // ProjectEntityHandler.getAllEntitiesFromProject in ProjectEntityUpdateHandler.resyncProjectHistory.
  973. path: '/' + this.persistedDoc.path,
  974. url: 'filestore/12345',
  975. _hash: 'abcdef',
  976. }
  977. this.fileMap['main.tex'].data = { hash: 'abcdef' }
  978. const updates = [
  979. resyncProjectStructureUpdate([], [binaryFile, this.persistedFile]),
  980. ]
  981. const expandedUpdates =
  982. await this.SyncManager.promises.expandSyncUpdates(
  983. this.projectId,
  984. this.historyId,
  985. this.mostRecentChunk,
  986. updates,
  987. this.extendLock
  988. )
  989. expect(expandedUpdates).to.deep.equal([])
  990. expect(this.extendLock).to.have.been.called
  991. })
  992. it('removes and re-adds binary files if they do not have same hash', async function () {
  993. const persistedFileWithNewContent = {
  994. _hash: 'anotherhashvalue',
  995. hello: 'world',
  996. path: '1.png',
  997. url: 'filestore-new-url',
  998. }
  999. const updates = [
  1000. resyncProjectStructureUpdate(
  1001. [this.persistedDoc],
  1002. [persistedFileWithNewContent]
  1003. ),
  1004. ]
  1005. const expandedUpdates =
  1006. await this.SyncManager.promises.expandSyncUpdates(
  1007. this.projectId,
  1008. this.historyId,
  1009. this.mostRecentChunk,
  1010. updates,
  1011. this.extendLock
  1012. )
  1013. expect(expandedUpdates).to.deep.equal([
  1014. {
  1015. pathname: persistedFileWithNewContent.path,
  1016. new_pathname: '',
  1017. meta: {
  1018. resync: true,
  1019. ts: TIMESTAMP,
  1020. origin: { kind: 'history-resync' },
  1021. },
  1022. },
  1023. {
  1024. pathname: persistedFileWithNewContent.path,
  1025. file: persistedFileWithNewContent.file,
  1026. url: persistedFileWithNewContent.url,
  1027. hash: 'anotherhashvalue',
  1028. meta: {
  1029. resync: true,
  1030. ts: TIMESTAMP,
  1031. origin: { kind: 'history-resync' },
  1032. },
  1033. },
  1034. ])
  1035. expect(this.extendLock).to.have.been.called
  1036. })
  1037. it('removes and re-adds binary files w/o url if they do not have same hash', async function () {
  1038. const persistedFileWithNewContent = {
  1039. _hash: 'anotherhashvalue',
  1040. hello: 'world',
  1041. path: '1.png',
  1042. createdBlob: true,
  1043. }
  1044. const updates = [
  1045. resyncProjectStructureUpdate(
  1046. [this.persistedDoc],
  1047. [persistedFileWithNewContent]
  1048. ),
  1049. ]
  1050. const expandedUpdates =
  1051. await this.SyncManager.promises.expandSyncUpdates(
  1052. this.projectId,
  1053. this.historyId,
  1054. this.mostRecentChunk,
  1055. updates,
  1056. this.extendLock
  1057. )
  1058. expect(expandedUpdates).to.deep.equal([
  1059. {
  1060. pathname: persistedFileWithNewContent.path,
  1061. new_pathname: '',
  1062. meta: {
  1063. resync: true,
  1064. ts: TIMESTAMP,
  1065. origin: { kind: 'history-resync' },
  1066. },
  1067. },
  1068. {
  1069. pathname: persistedFileWithNewContent.path,
  1070. file: persistedFileWithNewContent.file,
  1071. hash: 'anotherhashvalue',
  1072. createdBlob: true,
  1073. meta: {
  1074. resync: true,
  1075. ts: TIMESTAMP,
  1076. origin: { kind: 'history-resync' },
  1077. },
  1078. },
  1079. ])
  1080. expect(this.extendLock).to.have.been.called
  1081. })
  1082. it('preserves other updates', async function () {
  1083. const update = { mock: 'update' }
  1084. const updates = [
  1085. update,
  1086. resyncProjectStructureUpdate(
  1087. [this.persistedDoc],
  1088. [this.persistedFile]
  1089. ),
  1090. ]
  1091. const expandedUpdates =
  1092. await this.SyncManager.promises.expandSyncUpdates(
  1093. this.projectId,
  1094. this.historyId,
  1095. this.mostRecentChunk,
  1096. updates,
  1097. this.extendLock
  1098. )
  1099. expect(expandedUpdates).to.deep.equal([update])
  1100. expect(this.extendLock).to.have.been.called
  1101. })
  1102. })
  1103. describe('expanding doc contents sync updates', function () {
  1104. it('returns errors from diffAsShareJsOps', async function () {
  1105. const diffError = new Error('test')
  1106. this.UpdateCompressor.diffAsShareJsOps.throws(diffError)
  1107. const updates = [
  1108. resyncProjectStructureUpdate(
  1109. [this.persistedDoc],
  1110. [this.persistedFile]
  1111. ),
  1112. docContentSyncUpdate(this.persistedDoc, this.persistedDocContent),
  1113. ]
  1114. await expect(
  1115. this.SyncManager.promises.expandSyncUpdates(
  1116. this.projectId,
  1117. this.historyId,
  1118. this.mostRecentChunk,
  1119. updates,
  1120. this.extendLock
  1121. )
  1122. ).to.be.rejectedWith(diffError)
  1123. expect(this.extendLock).to.have.been.called
  1124. })
  1125. it('handles an update for a file that is missing from the snapshot', async function () {
  1126. const updates = [docContentSyncUpdate('not-in-snapshot.txt', 'test')]
  1127. await expect(
  1128. this.SyncManager.promises.expandSyncUpdates(
  1129. this.projectId,
  1130. this.historyId,
  1131. this.mostRecentChunk,
  1132. updates,
  1133. this.extendLock
  1134. )
  1135. ).to.be.rejectedWith('unrecognised file: not in snapshot')
  1136. })
  1137. it('queues nothing for in docs whose contents is in sync', async function () {
  1138. const updates = [
  1139. resyncProjectStructureUpdate(
  1140. [this.persistedDoc],
  1141. [this.persistedFile]
  1142. ),
  1143. docContentSyncUpdate(this.persistedDoc, this.persistedDocContent),
  1144. ]
  1145. this.UpdateCompressor.diffAsShareJsOps.returns([])
  1146. const expandedUpdates =
  1147. await this.SyncManager.promises.expandSyncUpdates(
  1148. this.projectId,
  1149. this.historyId,
  1150. this.mostRecentChunk,
  1151. updates,
  1152. this.extendLock
  1153. )
  1154. expect(expandedUpdates).to.deep.equal([])
  1155. expect(this.extendLock).to.have.been.called
  1156. })
  1157. it('queues text updates for docs whose contents is out of sync', async function () {
  1158. const updates = [
  1159. resyncProjectStructureUpdate(
  1160. [this.persistedDoc],
  1161. [this.persistedFile]
  1162. ),
  1163. docContentSyncUpdate(
  1164. this.persistedDoc,
  1165. 'the fox jumps over the lazy dog'
  1166. ),
  1167. ]
  1168. this.UpdateCompressor.diffAsShareJsOps.returns([
  1169. { d: 'quick brown ', p: 4 },
  1170. ])
  1171. const expandedUpdates =
  1172. await this.SyncManager.promises.expandSyncUpdates(
  1173. this.projectId,
  1174. this.historyId,
  1175. this.mostRecentChunk,
  1176. updates,
  1177. this.extendLock
  1178. )
  1179. expect(expandedUpdates).to.deep.equal([
  1180. {
  1181. doc: this.persistedDoc.doc,
  1182. op: [{ d: 'quick brown ', p: 4 }],
  1183. meta: {
  1184. pathname: this.persistedDoc.path,
  1185. doc_length: this.persistedDocContent.length,
  1186. resync: true,
  1187. ts: TIMESTAMP,
  1188. origin: { kind: 'history-resync' },
  1189. },
  1190. },
  1191. ])
  1192. expect(this.extendLock).to.have.been.called
  1193. })
  1194. it('queues text updates for docs created by project structure sync', async function () {
  1195. this.UpdateCompressor.diffAsShareJsOps.returns([{ i: 'a', p: 0 }])
  1196. const newDoc = {
  1197. path: 'another.tex',
  1198. doc: new ObjectId().toString(),
  1199. }
  1200. const newDocContent = 'a'
  1201. const updates = [
  1202. resyncProjectStructureUpdate(
  1203. [this.persistedDoc, newDoc],
  1204. [this.persistedFile]
  1205. ),
  1206. docContentSyncUpdate(newDoc, newDocContent),
  1207. ]
  1208. const expandedUpdates =
  1209. await this.SyncManager.promises.expandSyncUpdates(
  1210. this.projectId,
  1211. this.historyId,
  1212. this.mostRecentChunk,
  1213. updates,
  1214. this.extendLock
  1215. )
  1216. expect(expandedUpdates).to.deep.equal([
  1217. {
  1218. pathname: newDoc.path,
  1219. doc: newDoc.doc,
  1220. docLines: '',
  1221. meta: {
  1222. resync: true,
  1223. ts: TIMESTAMP,
  1224. origin: { kind: 'history-resync' },
  1225. },
  1226. },
  1227. {
  1228. doc: newDoc.doc,
  1229. op: [{ i: 'a', p: 0 }],
  1230. meta: {
  1231. pathname: newDoc.path,
  1232. doc_length: 0,
  1233. resync: true,
  1234. ts: TIMESTAMP,
  1235. origin: { kind: 'history-resync' },
  1236. },
  1237. },
  1238. ])
  1239. expect(this.extendLock).to.have.been.called
  1240. })
  1241. it('skips text updates for docs when hashes match', async function () {
  1242. this.fileMap['main.tex'].getHash.returns('special-hash')
  1243. this.HashManager._getBlobHashFromString.returns('special-hash')
  1244. const updates = [
  1245. resyncProjectStructureUpdate(
  1246. [this.persistedDoc],
  1247. [this.persistedFile]
  1248. ),
  1249. docContentSyncUpdate(this.persistedDoc, 'hello'),
  1250. ]
  1251. const expandedUpdates =
  1252. await this.SyncManager.promises.expandSyncUpdates(
  1253. this.projectId,
  1254. this.historyId,
  1255. this.mostRecentChunk,
  1256. updates,
  1257. this.extendLock
  1258. )
  1259. expect(expandedUpdates).to.deep.equal([])
  1260. expect(this.extendLock).to.have.been.called
  1261. })
  1262. it('computes text updates for docs when hashes differ', async function () {
  1263. this.fileMap['main.tex'].getHash.returns('first-hash')
  1264. this.HashManager._getBlobHashFromString.returns('second-hash')
  1265. this.UpdateCompressor.diffAsShareJsOps.returns([
  1266. { i: 'test diff', p: 0 },
  1267. ])
  1268. const updates = [
  1269. resyncProjectStructureUpdate(
  1270. [this.persistedDoc],
  1271. [this.persistedFile]
  1272. ),
  1273. docContentSyncUpdate(this.persistedDoc, 'hello'),
  1274. ]
  1275. const expandedUpdates =
  1276. await this.SyncManager.promises.expandSyncUpdates(
  1277. this.projectId,
  1278. this.historyId,
  1279. this.mostRecentChunk,
  1280. updates,
  1281. this.extendLock
  1282. )
  1283. expect(expandedUpdates).to.deep.equal([
  1284. {
  1285. doc: this.persistedDoc.doc,
  1286. op: [{ i: 'test diff', p: 0 }],
  1287. meta: {
  1288. pathname: this.persistedDoc.path,
  1289. doc_length: this.persistedDocContent.length,
  1290. resync: true,
  1291. ts: TIMESTAMP,
  1292. origin: { kind: 'history-resync' },
  1293. },
  1294. },
  1295. ])
  1296. expect(this.extendLock).to.have.been.called
  1297. })
  1298. describe('for docs whose contents is out of sync', function () {
  1299. beforeEach(async function () {
  1300. const updates = [
  1301. resyncProjectStructureUpdate(
  1302. [this.persistedDoc],
  1303. [this.persistedFile]
  1304. ),
  1305. docContentSyncUpdate(this.persistedDoc, 'the quick brown fox'),
  1306. ]
  1307. this.UpdateCompressor.diffAsShareJsOps.returns([
  1308. { d: ' jumps over the lazy dog', p: 19 },
  1309. ])
  1310. this.expandedUpdates =
  1311. await this.SyncManager.promises.expandSyncUpdates(
  1312. this.projectId,
  1313. this.historyId,
  1314. this.mostRecentChunk,
  1315. updates,
  1316. this.extendLock
  1317. )
  1318. })
  1319. it('loads content from the history service when needed', function () {
  1320. expect(this.expandedUpdates).to.deep.equal([
  1321. {
  1322. doc: this.persistedDoc.doc,
  1323. op: [{ d: ' jumps over the lazy dog', p: 19 }],
  1324. meta: {
  1325. pathname: this.persistedDoc.path,
  1326. doc_length: this.persistedDocContent.length,
  1327. resync: true,
  1328. ts: TIMESTAMP,
  1329. origin: { kind: 'history-resync' },
  1330. },
  1331. },
  1332. ])
  1333. expect(this.extendLock).to.have.been.called
  1334. })
  1335. })
  1336. })
  1337. describe('syncing comments', function () {
  1338. beforeEach(function () {
  1339. this.loadedSnapshotDoc
  1340. .getComments()
  1341. .add(new Comment('comment1', [new Range(4, 5)]))
  1342. this.loadedSnapshotDoc
  1343. .getComments()
  1344. .add(new Comment('comment2', [new Range(10, 5)], true))
  1345. this.comments = [
  1346. makeComment('comment1', 4, 'quick'),
  1347. makeComment('comment2', 10, 'brown'),
  1348. ]
  1349. this.resolvedCommentIds = ['comment2']
  1350. })
  1351. it('does nothing if comments have not changed', async function () {
  1352. const updates = [
  1353. docContentSyncUpdate(
  1354. this.persistedDoc,
  1355. this.persistedDocContent,
  1356. {
  1357. comments: this.comments,
  1358. },
  1359. this.resolvedCommentIds
  1360. ),
  1361. ]
  1362. const expandedUpdates =
  1363. await this.SyncManager.promises.expandSyncUpdates(
  1364. this.projectId,
  1365. this.historyId,
  1366. this.mostRecentChunk,
  1367. updates,
  1368. this.extendLock
  1369. )
  1370. expect(expandedUpdates).to.deep.equal([])
  1371. })
  1372. it('adds missing comments', async function () {
  1373. this.comments.push(makeComment('comment3', 20, 'jumps'))
  1374. const updates = [
  1375. docContentSyncUpdate(
  1376. this.persistedDoc,
  1377. this.persistedDocContent,
  1378. {
  1379. comments: this.comments,
  1380. },
  1381. this.resolvedCommentIds
  1382. ),
  1383. ]
  1384. const expandedUpdates =
  1385. await this.SyncManager.promises.expandSyncUpdates(
  1386. this.projectId,
  1387. this.historyId,
  1388. this.mostRecentChunk,
  1389. updates,
  1390. this.extendLock
  1391. )
  1392. expect(expandedUpdates).to.deep.equal([
  1393. {
  1394. doc: this.persistedDoc.doc,
  1395. op: [
  1396. {
  1397. c: 'jumps',
  1398. p: 20,
  1399. t: 'comment3',
  1400. resolved: false,
  1401. },
  1402. ],
  1403. meta: {
  1404. origin: {
  1405. kind: 'history-resync',
  1406. },
  1407. pathname: this.persistedDoc.path,
  1408. resync: true,
  1409. ts: TIMESTAMP,
  1410. doc_length: this.persistedDocContent.length,
  1411. },
  1412. },
  1413. ])
  1414. })
  1415. it('deletes extra comments', async function () {
  1416. this.comments.splice(0, 1)
  1417. const updates = [
  1418. docContentSyncUpdate(
  1419. this.persistedDoc,
  1420. this.persistedDocContent,
  1421. {
  1422. comments: this.comments,
  1423. },
  1424. this.resolvedCommentIds
  1425. ),
  1426. ]
  1427. const expandedUpdates =
  1428. await this.SyncManager.promises.expandSyncUpdates(
  1429. this.projectId,
  1430. this.historyId,
  1431. this.mostRecentChunk,
  1432. updates,
  1433. this.extendLock
  1434. )
  1435. expect(expandedUpdates).to.deep.equal([
  1436. {
  1437. pathname: this.persistedDoc.path,
  1438. deleteComment: 'comment1',
  1439. meta: {
  1440. origin: {
  1441. kind: 'history-resync',
  1442. },
  1443. resync: true,
  1444. ts: TIMESTAMP,
  1445. },
  1446. },
  1447. ])
  1448. })
  1449. it('updates comments when ranges differ', async function () {
  1450. this.comments[1] = makeComment('comment2', 16, 'fox')
  1451. const updates = [
  1452. docContentSyncUpdate(
  1453. this.persistedDoc,
  1454. this.persistedDocContent,
  1455. {
  1456. comments: this.comments,
  1457. },
  1458. this.resolvedCommentIds
  1459. ),
  1460. ]
  1461. const expandedUpdates =
  1462. await this.SyncManager.promises.expandSyncUpdates(
  1463. this.projectId,
  1464. this.historyId,
  1465. this.mostRecentChunk,
  1466. updates,
  1467. this.extendLock
  1468. )
  1469. expect(expandedUpdates).to.deep.equal([
  1470. {
  1471. doc: 'doc-id',
  1472. op: [
  1473. {
  1474. c: 'fox',
  1475. p: 16,
  1476. t: 'comment2',
  1477. resolved: true,
  1478. },
  1479. ],
  1480. meta: {
  1481. origin: {
  1482. kind: 'history-resync',
  1483. },
  1484. resync: true,
  1485. ts: TIMESTAMP,
  1486. pathname: this.persistedDoc.path,
  1487. doc_length: this.persistedDocContent.length,
  1488. },
  1489. },
  1490. ])
  1491. })
  1492. it('sets the resolved state when it differs', async function () {
  1493. this.resolvedCommentIds = ['comment1']
  1494. const updates = [
  1495. docContentSyncUpdate(
  1496. this.persistedDoc,
  1497. this.persistedDocContent,
  1498. {
  1499. comments: this.comments,
  1500. },
  1501. this.resolvedCommentIds
  1502. ),
  1503. ]
  1504. const expandedUpdates =
  1505. await this.SyncManager.promises.expandSyncUpdates(
  1506. this.projectId,
  1507. this.historyId,
  1508. this.mostRecentChunk,
  1509. updates,
  1510. this.extendLock
  1511. )
  1512. expect(expandedUpdates).to.have.deep.members([
  1513. {
  1514. pathname: this.persistedDoc.path,
  1515. commentId: 'comment1',
  1516. resolved: true,
  1517. meta: {
  1518. origin: {
  1519. kind: 'history-resync',
  1520. },
  1521. resync: true,
  1522. ts: TIMESTAMP,
  1523. },
  1524. },
  1525. {
  1526. pathname: this.persistedDoc.path,
  1527. commentId: 'comment2',
  1528. resolved: false,
  1529. meta: {
  1530. origin: {
  1531. kind: 'history-resync',
  1532. },
  1533. resync: true,
  1534. ts: TIMESTAMP,
  1535. },
  1536. },
  1537. ])
  1538. })
  1539. it('treats zero length comments as detached comments', async function () {
  1540. this.loadedSnapshotDoc.getComments().add(new Comment('comment1', []))
  1541. this.comments = [
  1542. makeComment('comment1', 16, ''),
  1543. makeComment('comment2', 10, 'brown'),
  1544. ]
  1545. this.resolvedCommentIds = ['comment2']
  1546. const updates = [
  1547. docContentSyncUpdate(
  1548. this.persistedDoc,
  1549. this.persistedDocContent,
  1550. {
  1551. comments: this.comments,
  1552. },
  1553. this.resolvedCommentIds
  1554. ),
  1555. ]
  1556. const expandedUpdates =
  1557. await this.SyncManager.promises.expandSyncUpdates(
  1558. this.projectId,
  1559. this.historyId,
  1560. this.mostRecentChunk,
  1561. updates,
  1562. this.extendLock
  1563. )
  1564. expect(expandedUpdates).to.deep.equal([])
  1565. })
  1566. it('adjusts comment positions when the underlying text has changed', async function () {
  1567. const updates = [
  1568. docContentSyncUpdate(
  1569. this.persistedDoc,
  1570. 'quick brown fox',
  1571. {
  1572. comments: [
  1573. makeComment('comment1', 0, 'quick'),
  1574. makeComment('comment2', 12, 'fox'),
  1575. ],
  1576. },
  1577. this.resolvedCommentIds
  1578. ),
  1579. ]
  1580. this.UpdateCompressor.diffAsShareJsOps.returns([
  1581. { d: 'the ', p: 0 },
  1582. { d: ' jumps over the lazy dog', p: 15 },
  1583. ])
  1584. const expandedUpdates =
  1585. await this.SyncManager.promises.expandSyncUpdates(
  1586. this.projectId,
  1587. this.historyId,
  1588. this.mostRecentChunk,
  1589. updates,
  1590. this.extendLock
  1591. )
  1592. expect(expandedUpdates).to.deep.equal([
  1593. {
  1594. doc: this.persistedDoc.doc,
  1595. op: [
  1596. { d: 'the ', p: 0 },
  1597. { d: ' jumps over the lazy dog', p: 15 },
  1598. ],
  1599. meta: {
  1600. pathname: this.persistedDoc.path,
  1601. doc_length: this.persistedDocContent.length,
  1602. resync: true,
  1603. ts: TIMESTAMP,
  1604. origin: { kind: 'history-resync' },
  1605. },
  1606. },
  1607. {
  1608. doc: this.persistedDoc.doc,
  1609. op: [
  1610. {
  1611. c: 'fox',
  1612. p: 12,
  1613. t: 'comment2',
  1614. resolved: true,
  1615. },
  1616. ],
  1617. meta: {
  1618. origin: {
  1619. kind: 'history-resync',
  1620. },
  1621. pathname: this.persistedDoc.path,
  1622. resync: true,
  1623. ts: TIMESTAMP,
  1624. doc_length: 'quick brown fox'.length,
  1625. },
  1626. },
  1627. ])
  1628. })
  1629. })
  1630. describe('syncing tracked changes', function () {
  1631. beforeEach(function () {
  1632. this.loadedSnapshotDoc.getTrackedChanges().add(
  1633. new TrackedChange(new Range(4, 6), {
  1634. type: 'delete',
  1635. userId: USER_ID,
  1636. ts: new Date(TIMESTAMP),
  1637. })
  1638. )
  1639. this.loadedSnapshotDoc.getTrackedChanges().add(
  1640. new TrackedChange(new Range(10, 6), {
  1641. type: 'insert',
  1642. userId: USER_ID,
  1643. ts: new Date(TIMESTAMP),
  1644. })
  1645. )
  1646. this.loadedSnapshotDoc.getTrackedChanges().add(
  1647. new TrackedChange(new Range(20, 6), {
  1648. type: 'delete',
  1649. userId: USER_ID,
  1650. ts: new Date(TIMESTAMP),
  1651. })
  1652. )
  1653. this.loadedSnapshotDoc.getTrackedChanges().add(
  1654. new TrackedChange(new Range(40, 3), {
  1655. type: 'insert',
  1656. userId: USER_ID,
  1657. ts: new Date(TIMESTAMP),
  1658. })
  1659. )
  1660. this.changes = [
  1661. makeTrackedChange('td1', { p: 4, d: 'quick ' }),
  1662. makeTrackedChange('ti1', { p: 4, hpos: 10, i: 'brown ' }),
  1663. makeTrackedChange('td2', { p: 14, hpos: 20, d: 'jumps ' }),
  1664. makeTrackedChange('ti2', { p: 28, hpos: 40, i: 'dog' }),
  1665. ]
  1666. })
  1667. it('does nothing if tracked changes have not changed', async function () {
  1668. const updates = [
  1669. docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
  1670. changes: this.changes,
  1671. }),
  1672. ]
  1673. const expandedUpdates =
  1674. await this.SyncManager.promises.expandSyncUpdates(
  1675. this.projectId,
  1676. this.historyId,
  1677. this.mostRecentChunk,
  1678. updates,
  1679. this.extendLock
  1680. )
  1681. expect(expandedUpdates).to.deep.equal([])
  1682. })
  1683. it('adds new tracked changes', async function () {
  1684. this.changes.splice(
  1685. 3,
  1686. 0,
  1687. makeTrackedChange('td3', { p: 29, hpos: 35, d: 'lazy ' })
  1688. )
  1689. const updates = [
  1690. docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
  1691. changes: this.changes,
  1692. }),
  1693. ]
  1694. const expandedUpdates =
  1695. await this.SyncManager.promises.expandSyncUpdates(
  1696. this.projectId,
  1697. this.historyId,
  1698. this.mostRecentChunk,
  1699. updates,
  1700. this.extendLock
  1701. )
  1702. expect(expandedUpdates).to.deep.equal([
  1703. {
  1704. doc: this.persistedDoc.doc,
  1705. op: [
  1706. {
  1707. p: 35,
  1708. r: 'lazy ',
  1709. tracking: {
  1710. type: 'delete',
  1711. userId: USER_ID,
  1712. ts: TIMESTAMP,
  1713. },
  1714. },
  1715. ],
  1716. meta: {
  1717. origin: {
  1718. kind: 'history-resync',
  1719. },
  1720. pathname: this.persistedDoc.path,
  1721. resync: true,
  1722. ts: TIMESTAMP,
  1723. doc_length: this.persistedDocContent.length,
  1724. },
  1725. },
  1726. ])
  1727. })
  1728. it('removes extra tracked changes', async function () {
  1729. this.changes.splice(0, 1)
  1730. const updates = [
  1731. docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
  1732. changes: this.changes,
  1733. }),
  1734. ]
  1735. const expandedUpdates =
  1736. await this.SyncManager.promises.expandSyncUpdates(
  1737. this.projectId,
  1738. this.historyId,
  1739. this.mostRecentChunk,
  1740. updates,
  1741. this.extendLock
  1742. )
  1743. expect(expandedUpdates).to.deep.equal([
  1744. {
  1745. doc: this.persistedDoc.doc,
  1746. op: [
  1747. {
  1748. p: 4,
  1749. r: 'quick ',
  1750. tracking: { type: 'none' },
  1751. },
  1752. ],
  1753. meta: {
  1754. origin: {
  1755. kind: 'history-resync',
  1756. },
  1757. pathname: this.persistedDoc.path,
  1758. resync: true,
  1759. ts: TIMESTAMP,
  1760. doc_length: this.persistedDocContent.length,
  1761. },
  1762. },
  1763. ])
  1764. })
  1765. it('handles overlapping ranges', async function () {
  1766. this.changes = [
  1767. makeTrackedChange('ti1', { p: 0, i: 'the quic' }),
  1768. makeTrackedChange('td1', { p: 8, d: 'k br' }),
  1769. makeTrackedChange('ti2', { p: 14, hpos: 23, i: 'ps over' }),
  1770. ]
  1771. const updates = [
  1772. docContentSyncUpdate(this.persistedDoc, this.persistedDocContent, {
  1773. changes: this.changes,
  1774. }),
  1775. ]
  1776. const expandedUpdates =
  1777. await this.SyncManager.promises.expandSyncUpdates(
  1778. this.projectId,
  1779. this.historyId,
  1780. this.mostRecentChunk,
  1781. updates,
  1782. this.extendLock
  1783. )
  1784. // Before: the [quick ][brown ] fox [jumps ]over the lazy dog
  1785. // After: [the quic][k br]own fox jum[ps over] the lazy dog
  1786. expect(expandedUpdates).to.deep.equal([
  1787. {
  1788. doc: this.persistedDoc.doc,
  1789. op: [
  1790. {
  1791. p: 0,
  1792. r: 'the quic',
  1793. tracking: { type: 'insert', userId: USER_ID, ts: TIMESTAMP },
  1794. },
  1795. {
  1796. p: 10,
  1797. r: 'br',
  1798. tracking: { type: 'delete', userId: USER_ID, ts: TIMESTAMP },
  1799. },
  1800. {
  1801. p: 12,
  1802. r: 'own ',
  1803. tracking: { type: 'none' },
  1804. },
  1805. {
  1806. p: 20,
  1807. r: 'jum',
  1808. tracking: { type: 'none' },
  1809. },
  1810. {
  1811. p: 23,
  1812. r: 'ps over',
  1813. tracking: { type: 'insert', userId: USER_ID, ts: TIMESTAMP },
  1814. },
  1815. {
  1816. p: 40,
  1817. r: 'dog',
  1818. tracking: { type: 'none' },
  1819. },
  1820. ],
  1821. meta: {
  1822. origin: { kind: 'history-resync' },
  1823. pathname: this.persistedDoc.path,
  1824. resync: true,
  1825. ts: TIMESTAMP,
  1826. doc_length: this.persistedDocContent.length,
  1827. },
  1828. },
  1829. ])
  1830. })
  1831. it('adjusts tracked change positions when the underlying text has changed', async function () {
  1832. const updates = [
  1833. docContentSyncUpdate(
  1834. this.persistedDoc,
  1835. 'every fox jumps over the lazy dog',
  1836. {
  1837. changes: [
  1838. makeTrackedChange('ti1', { p: 5, i: ' ' }), // the space after every is still a tracked insert
  1839. makeTrackedChange('td2', { p: 10, d: 'jumps ' }),
  1840. makeTrackedChange('ti2', { p: 24, hpos: 30, i: 'dog' }),
  1841. ],
  1842. },
  1843. this.resolvedCommentIds
  1844. ),
  1845. ]
  1846. this.UpdateCompressor.diffAsShareJsOps.returns([
  1847. { d: 'the quick brown', p: 0 },
  1848. { i: 'every', p: 0 },
  1849. ])
  1850. const expandedUpdates =
  1851. await this.SyncManager.promises.expandSyncUpdates(
  1852. this.projectId,
  1853. this.historyId,
  1854. this.mostRecentChunk,
  1855. updates,
  1856. this.extendLock
  1857. )
  1858. expect(expandedUpdates).to.deep.equal([
  1859. {
  1860. doc: this.persistedDoc.doc,
  1861. op: [
  1862. { d: 'the quick brown', p: 0 },
  1863. { i: 'every', p: 0 },
  1864. ],
  1865. meta: {
  1866. pathname: this.persistedDoc.path,
  1867. doc_length: this.persistedDocContent.length,
  1868. resync: true,
  1869. ts: TIMESTAMP,
  1870. origin: { kind: 'history-resync' },
  1871. },
  1872. },
  1873. ])
  1874. })
  1875. })
  1876. })
  1877. })