DocumentManagerTests.js 32 KB

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