DocumentManagerTests.js 34 KB

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