DocumentManagerTests.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/js/DocumentManager.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const Errors = require('../../../../app/js/Errors')
  6. const tk = require('timekeeper')
  7. describe('DocumentManager', function () {
  8. beforeEach(function () {
  9. tk.freeze(new Date())
  10. this.Metrics = {
  11. Timer: class Timer {},
  12. inc: sinon.stub(),
  13. }
  14. this.Metrics.Timer.prototype.done = sinon.stub()
  15. this.RedisManager = {
  16. promises: {
  17. clearUnflushedTime: sinon.stub().resolves(),
  18. getDoc: sinon.stub(),
  19. getPreviousDocOps: sinon.stub(),
  20. putDocInMemory: sinon.stub().resolves(),
  21. removeDocFromMemory: sinon.stub().resolves(),
  22. renameDoc: sinon.stub().resolves(),
  23. updateCommentState: sinon.stub().resolves(),
  24. updateDocument: sinon.stub().resolves(),
  25. },
  26. }
  27. this.ProjectHistoryRedisManager = {
  28. promises: {
  29. queueOps: sinon.stub().resolves(),
  30. queueResyncDocContent: sinon.stub().resolves(),
  31. },
  32. }
  33. this.PersistenceManager = {
  34. promises: {
  35. getDoc: sinon.stub(),
  36. setDoc: sinon.stub().resolves(),
  37. },
  38. }
  39. this.HistoryManager = {
  40. flushProjectChangesAsync: sinon.stub(),
  41. }
  42. this.DiffCodec = {
  43. diffAsShareJsOp: sinon.stub(),
  44. }
  45. this.UpdateManager = {
  46. promises: {
  47. applyUpdate: sinon.stub().resolves(),
  48. },
  49. }
  50. this.RangesManager = {
  51. acceptChanges: sinon.stub(),
  52. deleteComment: sinon.stub(),
  53. }
  54. this.DocumentManager = SandboxedModule.require(modulePath, {
  55. requires: {
  56. './RedisManager': this.RedisManager,
  57. './ProjectHistoryRedisManager': this.ProjectHistoryRedisManager,
  58. './PersistenceManager': this.PersistenceManager,
  59. './HistoryManager': this.HistoryManager,
  60. './Metrics': this.Metrics,
  61. './DiffCodec': this.DiffCodec,
  62. './UpdateManager': this.UpdateManager,
  63. './RangesManager': this.RangesManager,
  64. './Errors': Errors,
  65. },
  66. })
  67. this.project_id = 'project-id-123'
  68. this.projectHistoryId = 'history-id-123'
  69. this.doc_id = 'doc-id-123'
  70. this.user_id = 1234
  71. this.lines = ['one', 'two', 'three']
  72. this.version = 42
  73. this.ranges = { comments: 'mock', entries: 'mock' }
  74. this.resolvedCommentIds = ['comment-1']
  75. this.pathname = '/a/b/c.tex'
  76. this.unflushedTime = Date.now()
  77. this.lastUpdatedAt = Date.now()
  78. this.lastUpdatedBy = 'last-author-id'
  79. this.source = 'external-source'
  80. this.historyRangesSupport = false
  81. })
  82. afterEach(function () {
  83. tk.reset()
  84. })
  85. describe('flushAndDeleteDoc', function () {
  86. describe('successfully', function () {
  87. beforeEach(async function () {
  88. this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
  89. await this.DocumentManager.promises.flushAndDeleteDoc(
  90. this.project_id,
  91. this.doc_id,
  92. {}
  93. )
  94. })
  95. it('should flush the doc', function () {
  96. this.DocumentManager.promises.flushDocIfLoaded
  97. .calledWith(this.project_id, this.doc_id)
  98. .should.equal(true)
  99. })
  100. it('should remove the doc from redis', function () {
  101. this.RedisManager.promises.removeDocFromMemory
  102. .calledWith(this.project_id, this.doc_id)
  103. .should.equal(true)
  104. })
  105. })
  106. describe('when a flush error occurs', function () {
  107. beforeEach(async function () {
  108. this.DocumentManager.promises.flushDocIfLoaded = sinon
  109. .stub()
  110. .rejects(new Error('boom!'))
  111. await expect(
  112. this.DocumentManager.promises.flushAndDeleteDoc(
  113. this.project_id,
  114. this.doc_id,
  115. {}
  116. )
  117. ).to.be.rejected
  118. })
  119. it('should not remove the doc from redis', function () {
  120. this.RedisManager.promises.removeDocFromMemory.called.should.equal(
  121. false
  122. )
  123. })
  124. describe('when ignoring flush errors', function () {
  125. it('should remove the doc from redis', async function () {
  126. await this.DocumentManager.promises.flushAndDeleteDoc(
  127. this.project_id,
  128. this.doc_id,
  129. { ignoreFlushErrors: true }
  130. )
  131. this.RedisManager.promises.removeDocFromMemory.called.should.equal(
  132. true
  133. )
  134. })
  135. })
  136. })
  137. })
  138. describe('flushDocIfLoaded', function () {
  139. describe('when the doc is in Redis', function () {
  140. beforeEach(async function () {
  141. this.RedisManager.promises.getDoc.resolves({
  142. lines: this.lines,
  143. version: this.version,
  144. ranges: this.ranges,
  145. pathname: this.pathname,
  146. projectHistoryId: this.projectHistoryId,
  147. unflushedTime: this.unflushedTime,
  148. lastUpdatedAt: this.lastUpdatedAt,
  149. lastUpdatedBy: this.lastUpdatedBy,
  150. })
  151. await this.DocumentManager.promises.flushDocIfLoaded(
  152. this.project_id,
  153. this.doc_id
  154. )
  155. })
  156. it('should get the doc from redis', function () {
  157. this.RedisManager.promises.getDoc
  158. .calledWith(this.project_id, this.doc_id)
  159. .should.equal(true)
  160. })
  161. it('should write the doc lines to the persistence layer', function () {
  162. this.PersistenceManager.promises.setDoc.should.have.been.calledWith(
  163. this.project_id,
  164. this.doc_id,
  165. this.lines,
  166. this.version,
  167. this.ranges,
  168. this.lastUpdatedAt,
  169. this.lastUpdatedBy
  170. )
  171. })
  172. })
  173. describe('when the document is not in Redis', function () {
  174. beforeEach(async function () {
  175. this.RedisManager.promises.getDoc.resolves({
  176. lines: null,
  177. version: null,
  178. ranges: null,
  179. })
  180. await this.DocumentManager.promises.flushDocIfLoaded(
  181. this.project_id,
  182. this.doc_id
  183. )
  184. })
  185. it('should get the doc from redis', function () {
  186. this.RedisManager.promises.getDoc
  187. .calledWith(this.project_id, this.doc_id)
  188. .should.equal(true)
  189. })
  190. it('should not write anything to the persistence layer', function () {
  191. this.PersistenceManager.promises.setDoc.called.should.equal(false)
  192. })
  193. })
  194. })
  195. describe('getDocAndRecentOps', function () {
  196. describe('with a previous version specified', function () {
  197. beforeEach(async function () {
  198. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  199. lines: this.lines,
  200. version: this.version,
  201. ranges: this.ranges,
  202. pathname: this.pathname,
  203. projectHistoryId: this.projectHistoryId,
  204. })
  205. this.RedisManager.promises.getPreviousDocOps.resolves(this.ops)
  206. this.result = await this.DocumentManager.promises.getDocAndRecentOps(
  207. this.project_id,
  208. this.doc_id,
  209. this.fromVersion
  210. )
  211. })
  212. it('should get the doc', function () {
  213. this.DocumentManager.promises.getDoc
  214. .calledWith(this.project_id, this.doc_id)
  215. .should.equal(true)
  216. })
  217. it('should get the doc ops', function () {
  218. this.RedisManager.promises.getPreviousDocOps
  219. .calledWith(this.doc_id, this.fromVersion, this.version)
  220. .should.equal(true)
  221. })
  222. it('should return the doc info', function () {
  223. expect(this.result).to.deep.equal({
  224. lines: this.lines,
  225. version: this.version,
  226. ops: this.ops,
  227. ranges: this.ranges,
  228. pathname: this.pathname,
  229. projectHistoryId: this.projectHistoryId,
  230. })
  231. })
  232. })
  233. describe('with no previous version specified', function () {
  234. beforeEach(async function () {
  235. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  236. lines: this.lines,
  237. version: this.version,
  238. ranges: this.ranges,
  239. pathname: this.pathname,
  240. projectHistoryId: this.projectHistoryId,
  241. })
  242. this.RedisManager.promises.getPreviousDocOps.resolves(this.ops)
  243. this.result = await this.DocumentManager.promises.getDocAndRecentOps(
  244. this.project_id,
  245. this.doc_id,
  246. -1
  247. )
  248. })
  249. it('should get the doc', function () {
  250. this.DocumentManager.promises.getDoc
  251. .calledWith(this.project_id, this.doc_id)
  252. .should.equal(true)
  253. })
  254. it('should not need to get the doc ops', function () {
  255. this.RedisManager.promises.getPreviousDocOps.called.should.equal(false)
  256. })
  257. it('should return the doc info', function () {
  258. expect(this.result).to.deep.equal({
  259. lines: this.lines,
  260. version: this.version,
  261. ops: [],
  262. ranges: this.ranges,
  263. pathname: this.pathname,
  264. projectHistoryId: this.projectHistoryId,
  265. })
  266. })
  267. })
  268. })
  269. describe('getDoc', function () {
  270. describe('when the doc exists in Redis', function () {
  271. beforeEach(async function () {
  272. this.RedisManager.promises.getDoc.resolves({
  273. lines: this.lines,
  274. version: this.version,
  275. ranges: this.ranges,
  276. resolvedCommentIds: this.resolvedCommentIds,
  277. pathname: this.pathname,
  278. projectHistoryId: this.projectHistoryId,
  279. unflushedTime: this.unflushedTime,
  280. lastUpdatedAt: this.lastUpdatedAt,
  281. lastUpdatedBy: this.lastUpdatedBy,
  282. historyRangesSupport: this.historyRangesSupport,
  283. })
  284. this.result = await this.DocumentManager.promises.getDoc(
  285. this.project_id,
  286. this.doc_id
  287. )
  288. })
  289. it('should get the doc from Redis', function () {
  290. this.RedisManager.promises.getDoc
  291. .calledWith(this.project_id, this.doc_id)
  292. .should.equal(true)
  293. })
  294. it('should return the doc info', function () {
  295. expect(this.result).to.deep.equal({
  296. lines: this.lines,
  297. version: this.version,
  298. ranges: this.ranges,
  299. resolvedCommentIds: this.resolvedCommentIds,
  300. pathname: this.pathname,
  301. projectHistoryId: this.projectHistoryId,
  302. unflushedTime: this.unflushedTime,
  303. alreadyLoaded: true,
  304. historyRangesSupport: this.historyRangesSupport,
  305. })
  306. })
  307. })
  308. describe('when the doc does not exist in Redis', function () {
  309. beforeEach(async function () {
  310. this.RedisManager.promises.getDoc.resolves({
  311. lines: null,
  312. version: null,
  313. ranges: null,
  314. pathname: null,
  315. projectHistoryId: null,
  316. })
  317. this.PersistenceManager.promises.getDoc.resolves({
  318. lines: this.lines,
  319. version: this.version,
  320. ranges: this.ranges,
  321. resolvedCommentIds: this.resolvedCommentIds,
  322. pathname: this.pathname,
  323. projectHistoryId: this.projectHistoryId,
  324. historyRangesSupport: this.historyRangesSupport,
  325. })
  326. this.result = await this.DocumentManager.promises.getDoc(
  327. this.project_id,
  328. this.doc_id
  329. )
  330. })
  331. it('should try to get the doc from Redis', function () {
  332. this.RedisManager.promises.getDoc
  333. .calledWith(this.project_id, this.doc_id)
  334. .should.equal(true)
  335. })
  336. it('should get the doc from the PersistenceManager', function () {
  337. this.PersistenceManager.promises.getDoc
  338. .calledWith(this.project_id, this.doc_id)
  339. .should.equal(true)
  340. })
  341. it('should set the doc in Redis', function () {
  342. this.RedisManager.promises.putDocInMemory
  343. .calledWith(
  344. this.project_id,
  345. this.doc_id,
  346. this.lines,
  347. this.version,
  348. this.ranges,
  349. this.resolvedCommentIds,
  350. this.pathname,
  351. this.projectHistoryId,
  352. this.historyRangesSupport
  353. )
  354. .should.equal(true)
  355. })
  356. it('should return doc info', function () {
  357. expect(this.result).to.deep.equal({
  358. lines: this.lines,
  359. version: this.version,
  360. ranges: this.ranges,
  361. resolvedCommentIds: this.resolvedCommentIds,
  362. pathname: this.pathname,
  363. projectHistoryId: this.projectHistoryId,
  364. unflushedTime: null,
  365. alreadyLoaded: false,
  366. historyRangesSupport: this.historyRangesSupport,
  367. })
  368. })
  369. })
  370. })
  371. describe('setDoc', function () {
  372. describe('with plain tex lines', function () {
  373. beforeEach(function () {
  374. this.beforeLines = ['before', 'lines']
  375. this.afterLines = ['after', 'lines']
  376. this.ops = [
  377. { i: 'foo', p: 4 },
  378. { d: 'bar', p: 42 },
  379. ]
  380. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  381. lines: this.beforeLines,
  382. version: this.version,
  383. ranges: this.ranges,
  384. resolvedCommentIds: this.resolvedCommentIds,
  385. pathname: this.pathname,
  386. projectHistoryId: this.projectHistoryId,
  387. unflushedTime: this.unflushedTime,
  388. alreadyLoaded: true,
  389. })
  390. this.DiffCodec.diffAsShareJsOp.returns(this.ops)
  391. this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
  392. this.DocumentManager.promises.flushAndDeleteDoc = sinon
  393. .stub()
  394. .resolves()
  395. })
  396. describe('when not loaded but with the same content', function () {
  397. beforeEach(async function () {
  398. this.DiffCodec.diffAsShareJsOp.returns([])
  399. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  400. lines: this.beforeLines,
  401. version: this.version,
  402. ranges: this.ranges,
  403. resolvedCommentIds: this.resolvedCommentIds,
  404. pathname: this.pathname,
  405. projectHistoryId: this.projectHistoryId,
  406. unflushedTime: this.unflushedTime,
  407. alreadyLoaded: false,
  408. })
  409. await this.DocumentManager.promises.setDoc(
  410. this.project_id,
  411. this.doc_id,
  412. this.beforeLines,
  413. this.source,
  414. this.user_id,
  415. false
  416. )
  417. })
  418. it('should not apply the diff as a ShareJS op', function () {
  419. this.UpdateManager.promises.applyUpdate.called.should.equal(false)
  420. })
  421. it('should increment the external update metric', function () {
  422. this.Metrics.inc
  423. .calledWith('external-update', 1, {
  424. status: 'noop',
  425. method: 'evict',
  426. path: this.source,
  427. })
  428. .should.equal(true)
  429. })
  430. it('should flush and delete the doc from redis', function () {
  431. this.DocumentManager.promises.flushAndDeleteDoc
  432. .calledWith(this.project_id, this.doc_id)
  433. .should.equal(true)
  434. })
  435. })
  436. describe('when already loaded with the same content', function () {
  437. beforeEach(async function () {
  438. this.DiffCodec.diffAsShareJsOp.returns([])
  439. await this.DocumentManager.promises.setDoc(
  440. this.project_id,
  441. this.doc_id,
  442. this.beforeLines,
  443. this.source,
  444. this.user_id,
  445. false
  446. )
  447. })
  448. it('should not apply the diff as a ShareJS op', function () {
  449. this.UpdateManager.promises.applyUpdate.called.should.equal(false)
  450. })
  451. it('should increment the external update metric', function () {
  452. this.Metrics.inc
  453. .calledWith('external-update', 1, {
  454. status: 'noop',
  455. method: 'flush',
  456. path: this.source,
  457. })
  458. .should.equal(true)
  459. })
  460. it('should flush the doc to Mongo', function () {
  461. this.DocumentManager.promises.flushDocIfLoaded
  462. .calledWith(this.project_id, this.doc_id)
  463. .should.equal(true)
  464. })
  465. })
  466. describe('when already loaded', function () {
  467. beforeEach(async function () {
  468. await this.DocumentManager.promises.setDoc(
  469. this.project_id,
  470. this.doc_id,
  471. this.afterLines,
  472. this.source,
  473. this.user_id,
  474. false
  475. )
  476. })
  477. it('should get the current doc lines', function () {
  478. this.DocumentManager.promises.getDoc
  479. .calledWith(this.project_id, this.doc_id)
  480. .should.equal(true)
  481. })
  482. it('should return a diff of the old and new lines', function () {
  483. this.DiffCodec.diffAsShareJsOp
  484. .calledWith(this.beforeLines, this.afterLines)
  485. .should.equal(true)
  486. })
  487. it('should apply the diff as a ShareJS op', function () {
  488. this.UpdateManager.promises.applyUpdate
  489. .calledWith(this.project_id, this.doc_id, {
  490. doc: this.doc_id,
  491. v: this.version,
  492. op: this.ops,
  493. meta: {
  494. type: 'external',
  495. source: this.source,
  496. user_id: this.user_id,
  497. },
  498. })
  499. .should.equal(true)
  500. })
  501. it('should increment the external update metric', function () {
  502. this.Metrics.inc
  503. .calledWith('external-update', 1, {
  504. status: 'diff',
  505. method: 'flush',
  506. path: this.source,
  507. })
  508. .should.equal(true)
  509. })
  510. it('should flush the doc to Mongo', function () {
  511. this.DocumentManager.promises.flushDocIfLoaded
  512. .calledWith(this.project_id, this.doc_id)
  513. .should.equal(true)
  514. })
  515. it('should not flush the project history', function () {
  516. this.HistoryManager.flushProjectChangesAsync.called.should.equal(
  517. false
  518. )
  519. })
  520. })
  521. describe('when not already loaded', function () {
  522. beforeEach(async function () {
  523. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  524. lines: this.beforeLines,
  525. version: this.version,
  526. pathname: this.pathname,
  527. unflushedTime: null,
  528. alreadyLoaded: false,
  529. })
  530. await this.DocumentManager.promises.setDoc(
  531. this.project_id,
  532. this.doc_id,
  533. this.afterLines,
  534. this.source,
  535. this.user_id,
  536. false
  537. )
  538. })
  539. it('should flush and delete the doc from the doc updater', function () {
  540. this.DocumentManager.promises.flushAndDeleteDoc
  541. .calledWith(this.project_id, this.doc_id, {})
  542. .should.equal(true)
  543. })
  544. it('should increment the external update metric', function () {
  545. this.Metrics.inc
  546. .calledWith('external-update', 1, {
  547. status: 'diff',
  548. method: 'evict',
  549. path: this.source,
  550. })
  551. .should.equal(true)
  552. })
  553. it('should not flush the project history', function () {
  554. this.HistoryManager.flushProjectChangesAsync
  555. .calledWithExactly(this.project_id)
  556. .should.equal(true)
  557. })
  558. })
  559. describe('without new lines', function () {
  560. beforeEach(async function () {
  561. await expect(
  562. this.DocumentManager.promises.setDoc(
  563. this.project_id,
  564. this.doc_id,
  565. null,
  566. this.source,
  567. this.user_id,
  568. false
  569. )
  570. ).to.be.rejectedWith('No lines were provided to setDoc')
  571. })
  572. it('should not try to get the doc lines', function () {
  573. this.DocumentManager.promises.getDoc.called.should.equal(false)
  574. })
  575. })
  576. describe('with the undoing flag', function () {
  577. beforeEach(async function () {
  578. // Copy ops so we don't interfere with other tests
  579. this.ops = [
  580. { i: 'foo', p: 4 },
  581. { d: 'bar', p: 42 },
  582. ]
  583. this.DiffCodec.diffAsShareJsOp.returns(this.ops)
  584. await this.DocumentManager.promises.setDoc(
  585. this.project_id,
  586. this.doc_id,
  587. this.afterLines,
  588. this.source,
  589. this.user_id,
  590. true
  591. )
  592. })
  593. it('should set the undo flag on each op', function () {
  594. this.ops.map(op => op.u.should.equal(true))
  595. })
  596. })
  597. })
  598. })
  599. describe('acceptChanges', function () {
  600. beforeEach(function () {
  601. this.change_id = 'mock-change-id'
  602. this.change_ids = [
  603. 'mock-change-id-1',
  604. 'mock-change-id-2',
  605. 'mock-change-id-3',
  606. 'mock-change-id-4',
  607. ]
  608. this.version = 34
  609. this.lines = ['original', 'lines']
  610. this.ranges = { entries: 'mock', comments: 'mock' }
  611. this.updated_ranges = { entries: 'updated', comments: 'updated' }
  612. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  613. lines: this.lines,
  614. version: this.version,
  615. ranges: this.ranges,
  616. })
  617. this.RangesManager.acceptChanges.returns(this.updated_ranges)
  618. })
  619. describe('successfully with a single change', function () {
  620. beforeEach(async function () {
  621. await this.DocumentManager.promises.acceptChanges(
  622. this.project_id,
  623. this.doc_id,
  624. [this.change_id]
  625. )
  626. })
  627. it("should get the document's current ranges", function () {
  628. this.DocumentManager.promises.getDoc
  629. .calledWith(this.project_id, this.doc_id)
  630. .should.equal(true)
  631. })
  632. it('should apply the accept change to the ranges', function () {
  633. this.RangesManager.acceptChanges.should.have.been.calledWith(
  634. [this.change_id],
  635. this.ranges
  636. )
  637. })
  638. it('should save the updated ranges', function () {
  639. this.RedisManager.promises.updateDocument
  640. .calledWith(
  641. this.project_id,
  642. this.doc_id,
  643. this.lines,
  644. this.version,
  645. [],
  646. this.updated_ranges,
  647. {}
  648. )
  649. .should.equal(true)
  650. })
  651. })
  652. describe('successfully with multiple changes', function () {
  653. beforeEach(async function () {
  654. await this.DocumentManager.promises.acceptChanges(
  655. this.project_id,
  656. this.doc_id,
  657. this.change_ids
  658. )
  659. })
  660. it('should apply the accept change to the ranges', function () {
  661. this.RangesManager.acceptChanges
  662. .calledWith(this.change_ids, this.ranges)
  663. .should.equal(true)
  664. })
  665. })
  666. describe('when the doc is not found', function () {
  667. beforeEach(async function () {
  668. this.DocumentManager.promises.getDoc = sinon
  669. .stub()
  670. .resolves({ lines: null, version: null, ranges: null })
  671. await expect(
  672. this.DocumentManager.promises.acceptChanges(
  673. this.project_id,
  674. this.doc_id,
  675. [this.change_id]
  676. )
  677. ).to.be.rejectedWith(Errors.NotFoundError)
  678. })
  679. it('should not save anything', function () {
  680. this.RedisManager.promises.updateDocument.called.should.equal(false)
  681. })
  682. })
  683. })
  684. describe('deleteComment', function () {
  685. beforeEach(function () {
  686. this.comment_id = 'mock-comment-id'
  687. this.version = 34
  688. this.lines = ['original', 'lines']
  689. this.ranges = { comments: ['one', 'two', 'three'] }
  690. this.resolvedCommentIds = ['comment1']
  691. this.updated_ranges = { comments: ['one', 'three'] }
  692. this.historyRangesSupport = true
  693. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  694. lines: this.lines,
  695. version: this.version,
  696. ranges: this.ranges,
  697. resolvedCommentIds: this.resolvedCommentIds,
  698. pathname: this.pathname,
  699. projectHistoryId: this.projectHistoryId,
  700. unflushedTime: Date.now() - 1e9,
  701. alreadyLoaded: true,
  702. historyRangesSupport: this.historyRangesSupport,
  703. })
  704. this.RangesManager.deleteComment.returns(this.updated_ranges)
  705. })
  706. describe('successfully', function () {
  707. beforeEach(async function () {
  708. await this.DocumentManager.promises.deleteComment(
  709. this.project_id,
  710. this.doc_id,
  711. this.comment_id,
  712. this.user_id
  713. )
  714. })
  715. it("should get the document's current ranges", function () {
  716. this.DocumentManager.promises.getDoc
  717. .calledWith(this.project_id, this.doc_id)
  718. .should.equal(true)
  719. })
  720. it('should delete the comment from the ranges', function () {
  721. this.RangesManager.deleteComment
  722. .calledWith(this.comment_id, this.ranges)
  723. .should.equal(true)
  724. })
  725. it('should save the updated ranges', function () {
  726. this.RedisManager.promises.updateDocument
  727. .calledWith(
  728. this.project_id,
  729. this.doc_id,
  730. this.lines,
  731. this.version,
  732. [],
  733. this.updated_ranges,
  734. {}
  735. )
  736. .should.equal(true)
  737. })
  738. it('should unset the comment resolved state', function () {
  739. this.RedisManager.promises.updateCommentState.should.have.been.calledWith(
  740. this.doc_id,
  741. this.comment_id,
  742. false
  743. )
  744. })
  745. it('should queue the delete comment operation', function () {
  746. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith(
  747. this.project_id,
  748. JSON.stringify({
  749. pathname: this.pathname,
  750. deleteComment: this.comment_id,
  751. meta: {
  752. ts: new Date(),
  753. user_id: this.user_id,
  754. },
  755. })
  756. )
  757. })
  758. })
  759. describe('when the doc is not found', function () {
  760. beforeEach(async function () {
  761. this.DocumentManager.promises.getDoc = sinon
  762. .stub()
  763. .resolves({ lines: null, version: null, ranges: null })
  764. await expect(
  765. this.DocumentManager.promises.acceptChanges(
  766. this.project_id,
  767. this.doc_id,
  768. [this.comment_id]
  769. )
  770. ).to.be.rejectedWith(Errors.NotFoundError)
  771. })
  772. it('should not save anything', function () {
  773. this.RedisManager.promises.updateDocument.called.should.equal(false)
  774. })
  775. })
  776. })
  777. describe('getDocAndFlushIfOld', function () {
  778. beforeEach(function () {
  779. this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
  780. })
  781. describe('when the doc is in Redis', function () {
  782. describe('and has changes to be flushed', function () {
  783. beforeEach(async function () {
  784. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  785. lines: this.lines,
  786. version: this.version,
  787. ranges: this.ranges,
  788. projectHistoryId: this.projectHistoryId,
  789. pathname: this.pathname,
  790. unflushedTime: Date.now() - 1e9,
  791. alreadyLoaded: true,
  792. })
  793. this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
  794. this.project_id,
  795. this.doc_id
  796. )
  797. })
  798. it('should get the doc', function () {
  799. this.DocumentManager.promises.getDoc
  800. .calledWith(this.project_id, this.doc_id)
  801. .should.equal(true)
  802. })
  803. it('should flush the doc', function () {
  804. this.DocumentManager.promises.flushDocIfLoaded
  805. .calledWith(this.project_id, this.doc_id)
  806. .should.equal(true)
  807. })
  808. it('should return the lines and versions', function () {
  809. expect(this.result).to.deep.equal({
  810. lines: this.lines,
  811. version: this.version,
  812. })
  813. })
  814. })
  815. describe("and has only changes that don't need to be flushed", function () {
  816. beforeEach(async function () {
  817. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  818. lines: this.lines,
  819. version: this.version,
  820. ranges: this.ranges,
  821. pathname: this.pathname,
  822. unflushedTime: Date.now() - 100,
  823. alreadyLoaded: true,
  824. })
  825. this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
  826. this.project_id,
  827. this.doc_id
  828. )
  829. })
  830. it('should get the doc', function () {
  831. this.DocumentManager.promises.getDoc
  832. .calledWith(this.project_id, this.doc_id)
  833. .should.equal(true)
  834. })
  835. it('should not flush the doc', function () {
  836. this.DocumentManager.promises.flushDocIfLoaded.called.should.equal(
  837. false
  838. )
  839. })
  840. it('should return the lines and versions', function () {
  841. expect(this.result).to.deep.equal({
  842. lines: this.lines,
  843. version: this.version,
  844. })
  845. })
  846. })
  847. })
  848. describe('when the doc is not in Redis', function () {
  849. beforeEach(async function () {
  850. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  851. lines: this.lines,
  852. version: this.version,
  853. ranges: this.ranges,
  854. alreadyLoaded: false,
  855. })
  856. this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
  857. this.project_id,
  858. this.doc_id
  859. )
  860. })
  861. it('should get the doc', function () {
  862. this.DocumentManager.promises.getDoc
  863. .calledWith(this.project_id, this.doc_id)
  864. .should.equal(true)
  865. })
  866. it('should not flush the doc', function () {
  867. this.DocumentManager.promises.flushDocIfLoaded.called.should.equal(
  868. false
  869. )
  870. })
  871. it('should return the lines and versions', function () {
  872. expect(this.result).to.deep.equal({
  873. lines: this.lines,
  874. version: this.version,
  875. })
  876. })
  877. })
  878. })
  879. describe('renameDoc', function () {
  880. beforeEach(function () {
  881. this.update = 'some-update'
  882. })
  883. describe('successfully', function () {
  884. beforeEach(async function () {
  885. await this.DocumentManager.promises.renameDoc(
  886. this.project_id,
  887. this.doc_id,
  888. this.user_id,
  889. this.update,
  890. this.projectHistoryId
  891. )
  892. })
  893. it('should rename the document', function () {
  894. this.RedisManager.promises.renameDoc
  895. .calledWith(
  896. this.project_id,
  897. this.doc_id,
  898. this.user_id,
  899. this.update,
  900. this.projectHistoryId
  901. )
  902. .should.equal(true)
  903. })
  904. })
  905. })
  906. describe('resyncDocContents', function () {
  907. describe('when doc is loaded in redis', function () {
  908. beforeEach(async function () {
  909. this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
  910. this.RedisManager.promises.getDoc.resolves({
  911. lines: this.lines,
  912. version: this.version,
  913. ranges: this.ranges,
  914. resolvedCommentIds: this.resolvedCommentIds,
  915. pathname: this.pathname,
  916. projectHistoryId: this.projectHistoryId,
  917. historyRangesSupport: this.historyRangesSupport,
  918. })
  919. await this.DocumentManager.promises.resyncDocContents(
  920. this.project_id,
  921. this.doc_id,
  922. this.pathnameFromProjectStructureUpdate
  923. )
  924. })
  925. it('gets the doc contents from redis', function () {
  926. this.RedisManager.promises.getDoc
  927. .calledWith(this.project_id, this.doc_id)
  928. .should.equal(true)
  929. })
  930. it('queues a resync doc content update', function () {
  931. this.ProjectHistoryRedisManager.promises.queueResyncDocContent
  932. .calledWith(
  933. this.project_id,
  934. this.projectHistoryId,
  935. this.doc_id,
  936. this.lines,
  937. this.ranges,
  938. this.resolvedCommentIds,
  939. this.version,
  940. this.pathnameFromProjectStructureUpdate,
  941. this.historyRangesSupport
  942. )
  943. .should.equal(true)
  944. })
  945. })
  946. describe('when doc is not loaded in redis', function () {
  947. beforeEach(async function () {
  948. this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
  949. this.RedisManager.promises.getDoc.resolves({})
  950. this.PersistenceManager.promises.getDoc.resolves({
  951. lines: this.lines,
  952. version: this.version,
  953. ranges: this.ranges,
  954. resolvedCommentIds: this.resolvedCommentIds,
  955. pathname: this.pathname,
  956. projectHistoryId: this.projectHistoryId,
  957. historyRangesSupport: this.historyRangesSupport,
  958. })
  959. await this.DocumentManager.promises.resyncDocContents(
  960. this.project_id,
  961. this.doc_id,
  962. this.pathnameFromProjectStructureUpdate
  963. )
  964. })
  965. it('tries to get the doc contents from redis', function () {
  966. this.RedisManager.promises.getDoc
  967. .calledWith(this.project_id, this.doc_id)
  968. .should.equal(true)
  969. })
  970. it('gets the doc contents from web', function () {
  971. this.PersistenceManager.promises.getDoc
  972. .calledWith(this.project_id, this.doc_id, { peek: true })
  973. .should.equal(true)
  974. })
  975. it('queues a resync doc content update', function () {
  976. this.ProjectHistoryRedisManager.promises.queueResyncDocContent
  977. .calledWith(
  978. this.project_id,
  979. this.projectHistoryId,
  980. this.doc_id,
  981. this.lines,
  982. this.ranges,
  983. this.resolvedCommentIds,
  984. this.version,
  985. this.pathnameFromProjectStructureUpdate,
  986. this.historyRangesSupport
  987. )
  988. .should.equal(true)
  989. })
  990. })
  991. describe('when a doc has no ranges in docstore', function () {
  992. beforeEach(async function () {
  993. this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
  994. this.RedisManager.promises.getDoc.resolves({})
  995. this.PersistenceManager.promises.getDoc.resolves({
  996. lines: this.lines,
  997. version: this.version,
  998. ranges: undefined,
  999. resolvedCommentIds: [],
  1000. pathname: this.pathname,
  1001. projectHistoryId: this.projectHistoryId,
  1002. historyRangesSupport: this.historyRangesSupport,
  1003. })
  1004. await this.DocumentManager.promises.resyncDocContents(
  1005. this.project_id,
  1006. this.doc_id,
  1007. this.pathnameFromProjectStructureUpdate
  1008. )
  1009. })
  1010. it('gets the doc contents from web', function () {
  1011. this.PersistenceManager.promises.getDoc
  1012. .calledWith(this.project_id, this.doc_id, { peek: true })
  1013. .should.equal(true)
  1014. })
  1015. it('queues a resync doc content update with an empty ranges object', function () {
  1016. this.ProjectHistoryRedisManager.promises.queueResyncDocContent
  1017. .calledWith(
  1018. this.project_id,
  1019. this.projectHistoryId,
  1020. this.doc_id,
  1021. this.lines,
  1022. {},
  1023. [],
  1024. this.version,
  1025. this.pathnameFromProjectStructureUpdate,
  1026. this.historyRangesSupport
  1027. )
  1028. .should.equal(true)
  1029. })
  1030. })
  1031. })
  1032. })