SyncManagerTests.js 65 KB

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