DocumentManagerTests.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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.project_id,
  635. this.doc_id,
  636. [this.change_id],
  637. this.ranges
  638. )
  639. })
  640. it('should save the updated ranges', function () {
  641. this.RedisManager.promises.updateDocument
  642. .calledWith(
  643. this.project_id,
  644. this.doc_id,
  645. this.lines,
  646. this.version,
  647. [],
  648. this.updated_ranges,
  649. {}
  650. )
  651. .should.equal(true)
  652. })
  653. })
  654. describe('successfully with multiple changes', function () {
  655. beforeEach(async function () {
  656. await this.DocumentManager.promises.acceptChanges(
  657. this.project_id,
  658. this.doc_id,
  659. this.change_ids
  660. )
  661. })
  662. it('should apply the accept change to the ranges', function () {
  663. this.RangesManager.acceptChanges
  664. .calledWith(
  665. this.project_id,
  666. this.doc_id,
  667. this.change_ids,
  668. this.ranges
  669. )
  670. .should.equal(true)
  671. })
  672. })
  673. describe('when the doc is not found', function () {
  674. beforeEach(async function () {
  675. this.DocumentManager.promises.getDoc = sinon
  676. .stub()
  677. .resolves({ lines: null, version: null, ranges: null })
  678. await expect(
  679. this.DocumentManager.promises.acceptChanges(
  680. this.project_id,
  681. this.doc_id,
  682. [this.change_id]
  683. )
  684. ).to.be.rejectedWith(Errors.NotFoundError)
  685. })
  686. it('should not save anything', function () {
  687. this.RedisManager.promises.updateDocument.called.should.equal(false)
  688. })
  689. })
  690. })
  691. describe('deleteComment', function () {
  692. beforeEach(function () {
  693. this.comment_id = 'mock-comment-id'
  694. this.version = 34
  695. this.lines = ['original', 'lines']
  696. this.ranges = { comments: ['one', 'two', 'three'] }
  697. this.resolvedCommentIds = ['comment1']
  698. this.updated_ranges = { comments: ['one', 'three'] }
  699. this.historyRangesSupport = true
  700. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  701. lines: this.lines,
  702. version: this.version,
  703. ranges: this.ranges,
  704. resolvedCommentIds: this.resolvedCommentIds,
  705. pathname: this.pathname,
  706. projectHistoryId: this.projectHistoryId,
  707. unflushedTime: Date.now() - 1e9,
  708. alreadyLoaded: true,
  709. historyRangesSupport: this.historyRangesSupport,
  710. })
  711. this.RangesManager.deleteComment.returns(this.updated_ranges)
  712. })
  713. describe('successfully', function () {
  714. beforeEach(async function () {
  715. await this.DocumentManager.promises.deleteComment(
  716. this.project_id,
  717. this.doc_id,
  718. this.comment_id,
  719. this.user_id
  720. )
  721. })
  722. it("should get the document's current ranges", function () {
  723. this.DocumentManager.promises.getDoc
  724. .calledWith(this.project_id, this.doc_id)
  725. .should.equal(true)
  726. })
  727. it('should delete the comment from the ranges', function () {
  728. this.RangesManager.deleteComment
  729. .calledWith(this.comment_id, this.ranges)
  730. .should.equal(true)
  731. })
  732. it('should save the updated ranges', function () {
  733. this.RedisManager.promises.updateDocument
  734. .calledWith(
  735. this.project_id,
  736. this.doc_id,
  737. this.lines,
  738. this.version,
  739. [],
  740. this.updated_ranges,
  741. {}
  742. )
  743. .should.equal(true)
  744. })
  745. it('should unset the comment resolved state', function () {
  746. this.RedisManager.promises.updateCommentState.should.have.been.calledWith(
  747. this.doc_id,
  748. this.comment_id,
  749. false
  750. )
  751. })
  752. it('should queue the delete comment operation', function () {
  753. this.ProjectHistoryRedisManager.promises.queueOps.should.have.been.calledWith(
  754. this.project_id,
  755. JSON.stringify({
  756. pathname: this.pathname,
  757. deleteComment: this.comment_id,
  758. meta: {
  759. ts: new Date(),
  760. user_id: this.user_id,
  761. },
  762. })
  763. )
  764. })
  765. })
  766. describe('when the doc is not found', function () {
  767. beforeEach(async function () {
  768. this.DocumentManager.promises.getDoc = sinon
  769. .stub()
  770. .resolves({ lines: null, version: null, ranges: null })
  771. await expect(
  772. this.DocumentManager.promises.acceptChanges(
  773. this.project_id,
  774. this.doc_id,
  775. [this.comment_id]
  776. )
  777. ).to.be.rejectedWith(Errors.NotFoundError)
  778. })
  779. it('should not save anything', function () {
  780. this.RedisManager.promises.updateDocument.called.should.equal(false)
  781. })
  782. })
  783. })
  784. describe('getDocAndFlushIfOld', function () {
  785. beforeEach(function () {
  786. this.DocumentManager.promises.flushDocIfLoaded = sinon.stub().resolves()
  787. })
  788. describe('when the doc is in Redis', function () {
  789. describe('and has changes to be flushed', function () {
  790. beforeEach(async function () {
  791. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  792. lines: this.lines,
  793. version: this.version,
  794. ranges: this.ranges,
  795. projectHistoryId: this.projectHistoryId,
  796. pathname: this.pathname,
  797. unflushedTime: Date.now() - 1e9,
  798. alreadyLoaded: true,
  799. })
  800. this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
  801. this.project_id,
  802. this.doc_id
  803. )
  804. })
  805. it('should get the doc', function () {
  806. this.DocumentManager.promises.getDoc
  807. .calledWith(this.project_id, this.doc_id)
  808. .should.equal(true)
  809. })
  810. it('should flush the doc', function () {
  811. this.DocumentManager.promises.flushDocIfLoaded
  812. .calledWith(this.project_id, this.doc_id)
  813. .should.equal(true)
  814. })
  815. it('should return the lines and versions', function () {
  816. expect(this.result).to.deep.equal({
  817. lines: this.lines,
  818. version: this.version,
  819. })
  820. })
  821. })
  822. describe("and has only changes that don't need to be flushed", function () {
  823. beforeEach(async function () {
  824. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  825. lines: this.lines,
  826. version: this.version,
  827. ranges: this.ranges,
  828. pathname: this.pathname,
  829. unflushedTime: Date.now() - 100,
  830. alreadyLoaded: true,
  831. })
  832. this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
  833. this.project_id,
  834. this.doc_id
  835. )
  836. })
  837. it('should get the doc', function () {
  838. this.DocumentManager.promises.getDoc
  839. .calledWith(this.project_id, this.doc_id)
  840. .should.equal(true)
  841. })
  842. it('should not flush the doc', function () {
  843. this.DocumentManager.promises.flushDocIfLoaded.called.should.equal(
  844. false
  845. )
  846. })
  847. it('should return the lines and versions', function () {
  848. expect(this.result).to.deep.equal({
  849. lines: this.lines,
  850. version: this.version,
  851. })
  852. })
  853. })
  854. })
  855. describe('when the doc is not in Redis', function () {
  856. beforeEach(async function () {
  857. this.DocumentManager.promises.getDoc = sinon.stub().resolves({
  858. lines: this.lines,
  859. version: this.version,
  860. ranges: this.ranges,
  861. alreadyLoaded: false,
  862. })
  863. this.result = await this.DocumentManager.promises.getDocAndFlushIfOld(
  864. this.project_id,
  865. this.doc_id
  866. )
  867. })
  868. it('should get the doc', function () {
  869. this.DocumentManager.promises.getDoc
  870. .calledWith(this.project_id, this.doc_id)
  871. .should.equal(true)
  872. })
  873. it('should not flush the doc', function () {
  874. this.DocumentManager.promises.flushDocIfLoaded.called.should.equal(
  875. false
  876. )
  877. })
  878. it('should return the lines and versions', function () {
  879. expect(this.result).to.deep.equal({
  880. lines: this.lines,
  881. version: this.version,
  882. })
  883. })
  884. })
  885. })
  886. describe('renameDoc', function () {
  887. beforeEach(function () {
  888. this.update = 'some-update'
  889. })
  890. describe('successfully', function () {
  891. beforeEach(async function () {
  892. await this.DocumentManager.promises.renameDoc(
  893. this.project_id,
  894. this.doc_id,
  895. this.user_id,
  896. this.update,
  897. this.projectHistoryId
  898. )
  899. })
  900. it('should rename the document', function () {
  901. this.RedisManager.promises.renameDoc
  902. .calledWith(
  903. this.project_id,
  904. this.doc_id,
  905. this.user_id,
  906. this.update,
  907. this.projectHistoryId
  908. )
  909. .should.equal(true)
  910. })
  911. })
  912. })
  913. describe('resyncDocContents', function () {
  914. describe('when doc is loaded in redis', function () {
  915. beforeEach(async function () {
  916. this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
  917. this.RedisManager.promises.getDoc.resolves({
  918. lines: this.lines,
  919. version: this.version,
  920. ranges: this.ranges,
  921. resolvedCommentIds: this.resolvedCommentIds,
  922. pathname: this.pathname,
  923. projectHistoryId: this.projectHistoryId,
  924. historyRangesSupport: this.historyRangesSupport,
  925. })
  926. await this.DocumentManager.promises.resyncDocContents(
  927. this.project_id,
  928. this.doc_id,
  929. this.pathnameFromProjectStructureUpdate
  930. )
  931. })
  932. it('gets the doc contents from redis', function () {
  933. this.RedisManager.promises.getDoc
  934. .calledWith(this.project_id, this.doc_id)
  935. .should.equal(true)
  936. })
  937. it('queues a resync doc content update', function () {
  938. this.ProjectHistoryRedisManager.promises.queueResyncDocContent
  939. .calledWith(
  940. this.project_id,
  941. this.projectHistoryId,
  942. this.doc_id,
  943. this.lines,
  944. this.ranges,
  945. this.resolvedCommentIds,
  946. this.version,
  947. this.pathnameFromProjectStructureUpdate,
  948. this.historyRangesSupport
  949. )
  950. .should.equal(true)
  951. })
  952. })
  953. describe('when doc is not loaded in redis', function () {
  954. beforeEach(async function () {
  955. this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
  956. this.RedisManager.promises.getDoc.resolves({})
  957. this.PersistenceManager.promises.getDoc.resolves({
  958. lines: this.lines,
  959. version: this.version,
  960. ranges: this.ranges,
  961. resolvedCommentIds: this.resolvedCommentIds,
  962. pathname: this.pathname,
  963. projectHistoryId: this.projectHistoryId,
  964. historyRangesSupport: this.historyRangesSupport,
  965. })
  966. await this.DocumentManager.promises.resyncDocContents(
  967. this.project_id,
  968. this.doc_id,
  969. this.pathnameFromProjectStructureUpdate
  970. )
  971. })
  972. it('tries to get the doc contents from redis', function () {
  973. this.RedisManager.promises.getDoc
  974. .calledWith(this.project_id, this.doc_id)
  975. .should.equal(true)
  976. })
  977. it('gets the doc contents from web', function () {
  978. this.PersistenceManager.promises.getDoc
  979. .calledWith(this.project_id, this.doc_id, { peek: true })
  980. .should.equal(true)
  981. })
  982. it('queues a resync doc content update', function () {
  983. this.ProjectHistoryRedisManager.promises.queueResyncDocContent
  984. .calledWith(
  985. this.project_id,
  986. this.projectHistoryId,
  987. this.doc_id,
  988. this.lines,
  989. this.ranges,
  990. this.resolvedCommentIds,
  991. this.version,
  992. this.pathnameFromProjectStructureUpdate,
  993. this.historyRangesSupport
  994. )
  995. .should.equal(true)
  996. })
  997. })
  998. describe('when a doc has no ranges in docstore', function () {
  999. beforeEach(async function () {
  1000. this.pathnameFromProjectStructureUpdate = '/foo/bar.tex'
  1001. this.RedisManager.promises.getDoc.resolves({})
  1002. this.PersistenceManager.promises.getDoc.resolves({
  1003. lines: this.lines,
  1004. version: this.version,
  1005. ranges: undefined,
  1006. resolvedCommentIds: [],
  1007. pathname: this.pathname,
  1008. projectHistoryId: this.projectHistoryId,
  1009. historyRangesSupport: this.historyRangesSupport,
  1010. })
  1011. await this.DocumentManager.promises.resyncDocContents(
  1012. this.project_id,
  1013. this.doc_id,
  1014. this.pathnameFromProjectStructureUpdate
  1015. )
  1016. })
  1017. it('gets the doc contents from web', function () {
  1018. this.PersistenceManager.promises.getDoc
  1019. .calledWith(this.project_id, this.doc_id, { peek: true })
  1020. .should.equal(true)
  1021. })
  1022. it('queues a resync doc content update with an empty ranges object', function () {
  1023. this.ProjectHistoryRedisManager.promises.queueResyncDocContent
  1024. .calledWith(
  1025. this.project_id,
  1026. this.projectHistoryId,
  1027. this.doc_id,
  1028. this.lines,
  1029. {},
  1030. [],
  1031. this.version,
  1032. this.pathnameFromProjectStructureUpdate,
  1033. this.historyRangesSupport
  1034. )
  1035. .should.equal(true)
  1036. })
  1037. })
  1038. })
  1039. })