DocumentManagerTests.js 33 KB

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