SyncManagerTests.js 54 KB

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