DocumentManagerTests.js 33 KB

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