DocumentManagerTests.js 42 KB

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