SyncManagerTests.js 70 KB

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