DocumentManagerTests.js 30 KB

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