DocumentManagerTests.js 32 KB

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