DocumentManagerTests.js 41 KB

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