DocumentManagerTests.js 32 KB

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