RedisManagerTests.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const SandboxedModule = require('sandboxed-module')
  4. const Errors = require('../../../../app/js/Errors')
  5. const crypto = require('node:crypto')
  6. const tk = require('timekeeper')
  7. const MODULE_PATH = '../../../../app/js/RedisManager.js'
  8. describe('RedisManager', function () {
  9. beforeEach(function () {
  10. this.multi = { exec: sinon.stub().resolves() }
  11. this.rclient = { multi: () => this.multi, srem: sinon.stub().resolves() }
  12. tk.freeze(new Date())
  13. this.RedisManager = SandboxedModule.require(MODULE_PATH, {
  14. requires: {
  15. '@overleaf/settings': (this.settings = {
  16. documentupdater: { logHashErrors: { write: true, read: true } },
  17. redis: {
  18. documentupdater: {
  19. key_schema: {
  20. blockingKey({ doc_id: docId }) {
  21. return `Blocking:${docId}`
  22. },
  23. docLines({ doc_id: docId }) {
  24. return `doclines:${docId}`
  25. },
  26. docOps({ doc_id: docId }) {
  27. return `DocOps:${docId}`
  28. },
  29. docVersion({ doc_id: docId }) {
  30. return `DocVersion:${docId}`
  31. },
  32. docHash({ doc_id: docId }) {
  33. return `DocHash:${docId}`
  34. },
  35. projectKey({ doc_id: docId }) {
  36. return `ProjectId:${docId}`
  37. },
  38. pendingUpdates({ doc_id: docId }) {
  39. return `PendingUpdates:${docId}`
  40. },
  41. docsInProject({ project_id: projectId }) {
  42. return `DocsIn:${projectId}`
  43. },
  44. ranges({ doc_id: docId }) {
  45. return `Ranges:${docId}`
  46. },
  47. pathname({ doc_id: docId }) {
  48. return `Pathname:${docId}`
  49. },
  50. projectHistoryId({ doc_id: docId }) {
  51. return `ProjectHistoryId:${docId}`
  52. },
  53. projectState({ project_id: projectId }) {
  54. return `ProjectState:${projectId}`
  55. },
  56. projectBlock({ project_id: projectId }) {
  57. return `ProjectBlock:${projectId}`
  58. },
  59. unflushedTime({ doc_id: docId }) {
  60. return `UnflushedTime:${docId}`
  61. },
  62. lastUpdatedBy({ doc_id: docId }) {
  63. return `lastUpdatedBy:${docId}`
  64. },
  65. lastUpdatedAt({ doc_id: docId }) {
  66. return `lastUpdatedAt:${docId}`
  67. },
  68. historyRangesSupport() {
  69. return 'HistoryRangesSupport'
  70. },
  71. resolvedCommentIds({ doc_id: docId }) {
  72. return `ResolvedCommentIds:${docId}`
  73. },
  74. },
  75. },
  76. },
  77. }),
  78. '@overleaf/redis-wrapper': {
  79. createClient: () => this.rclient,
  80. },
  81. './Metrics': (this.metrics = {
  82. inc: sinon.stub(),
  83. summary: sinon.stub(),
  84. Timer: class Timer {
  85. constructor() {
  86. this.start = new Date()
  87. }
  88. done() {
  89. const timeSpan = new Date() - this.start
  90. return timeSpan
  91. }
  92. },
  93. }),
  94. './Errors': Errors,
  95. },
  96. })
  97. this.docId = 'doc-id-123'
  98. this.project_id = 'project-id-123'
  99. this.projectHistoryId = '123'
  100. this.historyRangesSupport = false
  101. })
  102. afterEach(function () {
  103. tk.reset()
  104. })
  105. describe('getDoc', function () {
  106. beforeEach(function () {
  107. this.lines = ['one', 'two', 'three', 'これは'] // include some utf8
  108. this.jsonlines = JSON.stringify(this.lines)
  109. this.version = 42
  110. this.hash = crypto
  111. .createHash('sha1')
  112. .update(this.jsonlines, 'utf8')
  113. .digest('hex')
  114. this.ranges = { comments: 'mock', entries: 'mock' }
  115. this.resolvedCommentIds = ['comment-1']
  116. this.json_ranges = JSON.stringify(this.ranges)
  117. this.unflushed_time = 12345
  118. this.pathname = '/a/b/c.tex'
  119. this.rclient.mget = sinon
  120. .stub()
  121. .resolves([
  122. this.jsonlines,
  123. this.version,
  124. this.hash,
  125. this.project_id,
  126. this.json_ranges,
  127. this.pathname,
  128. this.projectHistoryId.toString(),
  129. this.unflushed_time,
  130. ])
  131. this.rclient.sismember = sinon.stub()
  132. this.rclient.sismember
  133. .withArgs('HistoryRangesSupport', this.docId)
  134. .resolves(0)
  135. this.rclient.smembers = sinon.stub()
  136. this.rclient.smembers
  137. .withArgs(`ResolvedCommentIds:${this.docId}`)
  138. .resolves(this.resolvedCommentIds)
  139. })
  140. describe('successfully', function () {
  141. beforeEach(async function () {
  142. this.result = await this.RedisManager.promises.getDoc(
  143. this.project_id,
  144. this.docId
  145. )
  146. })
  147. it('should get all the details in one call to redis', function () {
  148. this.rclient.mget
  149. .calledWith(
  150. `doclines:${this.docId}`,
  151. `DocVersion:${this.docId}`,
  152. `DocHash:${this.docId}`,
  153. `ProjectId:${this.docId}`,
  154. `Ranges:${this.docId}`,
  155. `Pathname:${this.docId}`,
  156. `ProjectHistoryId:${this.docId}`,
  157. `UnflushedTime:${this.docId}`,
  158. `lastUpdatedAt:${this.docId}`,
  159. `lastUpdatedBy:${this.docId}`
  160. )
  161. .should.equal(true)
  162. })
  163. it('should return the document', function () {
  164. expect(this.result).to.deep.equal({
  165. lines: this.lines,
  166. version: this.version,
  167. ranges: this.ranges,
  168. pathname: this.pathname,
  169. projectHistoryId: this.projectHistoryId,
  170. unflushedTime: this.unflushed_time,
  171. lastUpdatedAt: this.lastUpdatedAt,
  172. lastUpdatedBy: this.lastUpdatedBy,
  173. historyRangesSupport: this.historyRangesSupport,
  174. resolvedCommentIds: this.resolvedCommentIds,
  175. })
  176. })
  177. it('should not log any errors', function () {
  178. this.logger.error.calledWith().should.equal(false)
  179. })
  180. })
  181. describe('with a corrupted document', function () {
  182. beforeEach(async function () {
  183. this.badHash = 'INVALID-HASH-VALUE'
  184. this.rclient.mget = sinon
  185. .stub()
  186. .resolves([
  187. this.jsonlines,
  188. this.version,
  189. this.badHash,
  190. this.project_id,
  191. this.json_ranges,
  192. ])
  193. this.result = await this.RedisManager.promises.getDoc(
  194. this.project_id,
  195. this.docId
  196. )
  197. })
  198. it('should log a hash error', function () {
  199. this.logger.error.calledWith().should.equal(true)
  200. })
  201. it('should return the document', function () {
  202. expect(this.result).to.deep.include({
  203. lines: this.lines,
  204. version: this.version,
  205. ranges: this.ranges,
  206. })
  207. })
  208. })
  209. describe('with a slow request to redis', function () {
  210. beforeEach(function () {
  211. this.clock = sinon.useFakeTimers()
  212. this.rclient.mget = async (...args) => {
  213. this.clock.tick(6000)
  214. return [
  215. this.jsonlines,
  216. this.version,
  217. this.another_project_id,
  218. this.json_ranges,
  219. this.pathname,
  220. this.unflushed_time,
  221. ]
  222. }
  223. })
  224. afterEach(function () {
  225. this.clock.restore()
  226. })
  227. it('should return an error', async function () {
  228. await expect(
  229. this.RedisManager.promises.getDoc(this.project_id, this.docId)
  230. ).to.be.rejected
  231. })
  232. })
  233. describe('getDoc with an invalid project id', function () {
  234. beforeEach(async function () {
  235. this.another_project_id = 'project-id-456'
  236. this.rclient.mget = sinon
  237. .stub()
  238. .resolves([
  239. this.jsonlines,
  240. this.version,
  241. this.hash,
  242. this.another_project_id,
  243. this.json_ranges,
  244. this.pathname,
  245. this.unflushed_time,
  246. ])
  247. })
  248. it('should throw an error', async function () {
  249. await expect(
  250. this.RedisManager.promises.getDoc(this.project_id, this.docId)
  251. ).to.be.rejectedWith(Errors.NotFoundError)
  252. })
  253. })
  254. describe('with history ranges support', function () {
  255. beforeEach(async function () {
  256. this.rclient.sismember
  257. .withArgs('HistoryRangesSupport', this.docId)
  258. .resolves(1)
  259. this.result = await this.RedisManager.promises.getDoc(
  260. this.project_id,
  261. this.docId
  262. )
  263. })
  264. it('should return the document with the history ranges flag set', function () {
  265. expect(this.result).to.deep.equal({
  266. lines: this.lines,
  267. version: this.version,
  268. ranges: this.ranges,
  269. pathname: this.pathname,
  270. projectHistoryId: this.projectHistoryId,
  271. unflushedTime: this.unflushed_time,
  272. lastUpdatedAt: this.lastUpdatedAt,
  273. lastUpdatedBy: this.lastUpdatedBy,
  274. historyRangesSupport: true,
  275. resolvedCommentIds: this.resolvedCommentIds,
  276. })
  277. })
  278. })
  279. })
  280. describe('getPreviousDocOpsTests', function () {
  281. describe('with a start and an end value', function () {
  282. beforeEach(async function () {
  283. this.first_version_in_redis = 30
  284. this.version = 70
  285. this.length = this.version - this.first_version_in_redis
  286. this.start = 50
  287. this.end = 60
  288. this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
  289. this.jsonOps = this.ops.map(op => JSON.stringify(op))
  290. this.rclient.llen = sinon.stub().resolves(this.length)
  291. this.rclient.get = sinon.stub().resolves(this.version.toString())
  292. this.rclient.lrange = sinon.stub().resolves(this.jsonOps)
  293. this.result = await this.RedisManager.promises.getPreviousDocOps(
  294. this.docId,
  295. this.start,
  296. this.end
  297. )
  298. })
  299. it('should get the length of the existing doc ops', function () {
  300. this.rclient.llen.calledWith(`DocOps:${this.docId}`).should.equal(true)
  301. })
  302. it('should get the current version of the doc', function () {
  303. this.rclient.get
  304. .calledWith(`DocVersion:${this.docId}`)
  305. .should.equal(true)
  306. })
  307. it('should get the appropriate docs ops', function () {
  308. this.rclient.lrange
  309. .calledWith(
  310. `DocOps:${this.docId}`,
  311. this.start - this.first_version_in_redis,
  312. this.end - this.first_version_in_redis
  313. )
  314. .should.equal(true)
  315. })
  316. it('should return the docs with the doc ops deserialized', function () {
  317. this.result.should.deep.equal(this.ops)
  318. })
  319. })
  320. describe('with an end value of -1', function () {
  321. beforeEach(async function () {
  322. this.first_version_in_redis = 30
  323. this.version = 70
  324. this.length = this.version - this.first_version_in_redis
  325. this.start = 50
  326. this.end = -1
  327. this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
  328. this.jsonOps = this.ops.map(op => JSON.stringify(op))
  329. this.rclient.llen = sinon.stub().resolves(this.length)
  330. this.rclient.get = sinon.stub().resolves(this.version.toString())
  331. this.rclient.lrange = sinon.stub().resolves(this.jsonOps)
  332. this.result = await this.RedisManager.promises.getPreviousDocOps(
  333. this.docId,
  334. this.start,
  335. this.end
  336. )
  337. })
  338. it('should get the appropriate docs ops to the end of list', function () {
  339. this.rclient.lrange
  340. .calledWith(
  341. `DocOps:${this.docId}`,
  342. this.start - this.first_version_in_redis,
  343. -1
  344. )
  345. .should.equal(true)
  346. })
  347. it('should return the docs with the doc ops deserialized', function () {
  348. this.result.should.deep.equal(this.ops)
  349. })
  350. })
  351. describe('when the requested range is not in Redis', function () {
  352. beforeEach(async function () {
  353. this.first_version_in_redis = 30
  354. this.version = 70
  355. this.length = this.version - this.first_version_in_redis
  356. this.start = 20
  357. this.end = -1
  358. this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
  359. this.jsonOps = this.ops.map(op => JSON.stringify(op))
  360. this.rclient.llen = sinon.stub().resolves(this.length)
  361. this.rclient.get = sinon.stub().resolves(this.version.toString())
  362. this.rclient.lrange = sinon.stub().resolves(this.jsonOps)
  363. try {
  364. await this.RedisManager.promises.getPreviousDocOps(
  365. this.docId,
  366. this.start,
  367. this.end
  368. )
  369. } catch (err) {
  370. this.err = err
  371. }
  372. })
  373. it('should throw an error', function () {
  374. expect(this.err).to.be.instanceOf(Errors.OpRangeNotAvailableError)
  375. })
  376. it('should send details for metrics', function () {
  377. expect(this.err.info).to.deep.equal({
  378. firstVersionInRedis: this.first_version_in_redis,
  379. version: this.version,
  380. ttlInS: this.RedisManager.DOC_OPS_TTL,
  381. })
  382. })
  383. })
  384. describe('with a slow request to redis', function () {
  385. beforeEach(async function () {
  386. this.first_version_in_redis = 30
  387. this.version = 70
  388. this.length = this.version - this.first_version_in_redis
  389. this.start = 50
  390. this.end = 60
  391. this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
  392. this.jsonOps = this.ops.map(op => JSON.stringify(op))
  393. this.rclient.llen = sinon.stub().resolves(this.length)
  394. this.rclient.get = sinon.stub().resolves(this.version.toString())
  395. this.clock = sinon.useFakeTimers()
  396. this.rclient.lrange = async (key, start, end) => {
  397. this.clock.tick(6000)
  398. return this.jsonOps
  399. }
  400. })
  401. afterEach(function () {
  402. this.clock.restore()
  403. })
  404. it('should return an error', async function () {
  405. await expect(
  406. this.RedisManager.promises.getPreviousDocOps(
  407. this.docId,
  408. this.start,
  409. this.end
  410. )
  411. ).to.be.rejected
  412. })
  413. })
  414. })
  415. describe('updateDocument', function () {
  416. beforeEach(function () {
  417. this.lines = ['one', 'two', 'three', 'これは']
  418. this.ops = [{ op: [{ i: 'foo', p: 4 }] }, { op: [{ i: 'bar', p: 8 }] }]
  419. this.version = 42
  420. this.hash = crypto
  421. .createHash('sha1')
  422. .update(JSON.stringify(this.lines), 'utf8')
  423. .digest('hex')
  424. this.ranges = { comments: 'mock', entries: 'mock' }
  425. this.updateMeta = { user_id: 'last-author-fake-id' }
  426. this.doc_update_list_length = sinon.stub()
  427. this.project_update_list_length = sinon.stub()
  428. this.RedisManager.promises.getDocVersion = sinon.stub()
  429. this.multi.mset = sinon.stub()
  430. this.multi.set = sinon.stub()
  431. this.multi.rpush = sinon.stub()
  432. this.multi.expire = sinon.stub()
  433. this.multi.ltrim = sinon.stub()
  434. this.multi.del = sinon.stub()
  435. this.multi.exec = sinon
  436. .stub()
  437. .resolves([
  438. null,
  439. null,
  440. null,
  441. null,
  442. this.doc_update_list_length,
  443. null,
  444. null,
  445. ])
  446. })
  447. describe('with a consistent version', function () {
  448. beforeEach(async function () {
  449. this.RedisManager.promises.getDocVersion
  450. .withArgs(this.docId)
  451. .resolves(this.version - this.ops.length)
  452. await this.RedisManager.promises.updateDocument(
  453. this.project_id,
  454. this.docId,
  455. this.lines,
  456. this.version,
  457. this.ops,
  458. this.ranges,
  459. this.updateMeta
  460. )
  461. })
  462. it('should get the current doc version to check for consistency', function () {
  463. this.RedisManager.promises.getDocVersion.should.have.been.calledWith(
  464. this.docId
  465. )
  466. })
  467. it('should set most details in a single MSET call', function () {
  468. this.multi.mset
  469. .calledWith({
  470. [`doclines:${this.docId}`]: JSON.stringify(this.lines),
  471. [`DocVersion:${this.docId}`]: this.version,
  472. [`DocHash:${this.docId}`]: this.hash,
  473. [`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
  474. [`lastUpdatedAt:${this.docId}`]: Date.now(),
  475. [`lastUpdatedBy:${this.docId}`]: 'last-author-fake-id',
  476. })
  477. .should.equal(true)
  478. })
  479. it('should set the unflushed time', function () {
  480. this.multi.set
  481. .calledWith(`UnflushedTime:${this.docId}`, Date.now(), 'NX')
  482. .should.equal(true)
  483. })
  484. it('should push the doc op into the doc ops list', function () {
  485. this.multi.rpush
  486. .calledWith(
  487. `DocOps:${this.docId}`,
  488. JSON.stringify(this.ops[0]),
  489. JSON.stringify(this.ops[1])
  490. )
  491. .should.equal(true)
  492. })
  493. it('should renew the expiry ttl on the doc ops array', function () {
  494. this.multi.expire
  495. .calledWith(`DocOps:${this.docId}`, this.RedisManager.DOC_OPS_TTL)
  496. .should.equal(true)
  497. })
  498. it('should truncate the list to 100 members', function () {
  499. this.multi.ltrim
  500. .calledWith(
  501. `DocOps:${this.docId}`,
  502. -this.RedisManager.DOC_OPS_MAX_LENGTH,
  503. -1
  504. )
  505. .should.equal(true)
  506. })
  507. it('should not log any errors', function () {
  508. this.logger.error.calledWith().should.equal(false)
  509. })
  510. describe('with a doc using project history only', function () {
  511. beforeEach(function () {
  512. this.RedisManager.promises.getDocVersion
  513. .withArgs(this.docId)
  514. .resolves(this.version - this.ops.length)
  515. })
  516. it('should succeed', async function () {
  517. await this.RedisManager.promises.updateDocument(
  518. this.project_id,
  519. this.docId,
  520. this.lines,
  521. this.version,
  522. this.ops,
  523. this.ranges,
  524. this.updateMeta
  525. )
  526. })
  527. })
  528. })
  529. describe('with an inconsistent version', function () {
  530. beforeEach(async function () {
  531. this.RedisManager.promises.getDocVersion
  532. .withArgs(this.docId)
  533. .resolves(this.version - this.ops.length - 1)
  534. await expect(
  535. this.RedisManager.promises.updateDocument(
  536. this.project_id,
  537. this.docId,
  538. this.lines,
  539. this.version,
  540. this.ops,
  541. this.ranges,
  542. this.updateMeta
  543. )
  544. ).to.be.rejected
  545. })
  546. it('should not call multi.exec', function () {
  547. this.multi.exec.called.should.equal(false)
  548. })
  549. })
  550. describe('with no updates', function () {
  551. beforeEach(async function () {
  552. this.RedisManager.promises.getDocVersion
  553. .withArgs(this.docId)
  554. .resolves(this.version)
  555. await this.RedisManager.promises.updateDocument(
  556. this.project_id,
  557. this.docId,
  558. this.lines,
  559. this.version,
  560. [],
  561. this.ranges,
  562. this.updateMeta
  563. )
  564. })
  565. it('should set the unflushed time (potential ranges changes)', function () {
  566. this.multi.set
  567. .calledWith(`UnflushedTime:${this.docId}`, Date.now(), 'NX')
  568. .should.equal(true)
  569. })
  570. it('should not try to enqueue doc updates', function () {
  571. this.multi.rpush.called.should.equal(false)
  572. })
  573. it('should still set the doclines', function () {
  574. this.multi.mset
  575. .calledWith({
  576. [`doclines:${this.docId}`]: JSON.stringify(this.lines),
  577. [`DocVersion:${this.docId}`]: this.version,
  578. [`DocHash:${this.docId}`]: this.hash,
  579. [`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
  580. [`lastUpdatedAt:${this.docId}`]: Date.now(),
  581. [`lastUpdatedBy:${this.docId}`]: 'last-author-fake-id',
  582. })
  583. .should.equal(true)
  584. })
  585. })
  586. describe('with empty ranges', function () {
  587. beforeEach(async function () {
  588. this.RedisManager.promises.getDocVersion
  589. .withArgs(this.docId)
  590. .resolves(this.version - this.ops.length)
  591. await this.RedisManager.promises.updateDocument(
  592. this.project_id,
  593. this.docId,
  594. this.lines,
  595. this.version,
  596. this.ops,
  597. {},
  598. this.updateMeta
  599. )
  600. })
  601. it('should set empty ranges', function () {
  602. this.multi.mset
  603. .calledWith({
  604. [`doclines:${this.docId}`]: JSON.stringify(this.lines),
  605. [`DocVersion:${this.docId}`]: this.version,
  606. [`DocHash:${this.docId}`]: this.hash,
  607. [`Ranges:${this.docId}`]: null,
  608. [`lastUpdatedAt:${this.docId}`]: Date.now(),
  609. [`lastUpdatedBy:${this.docId}`]: 'last-author-fake-id',
  610. })
  611. .should.equal(true)
  612. })
  613. })
  614. describe('with null bytes in the serialized doc lines', function () {
  615. beforeEach(function () {
  616. this.RedisManager.promises.getDocVersion
  617. .withArgs(this.docId)
  618. .resolves(this.version - this.ops.length)
  619. this.stringifyStub = sinon
  620. .stub(JSON, 'stringify')
  621. .callsFake(() => '["bad bytes! \u0000 <- here"]')
  622. })
  623. afterEach(function () {
  624. this.stringifyStub.restore()
  625. })
  626. it('should throw an error', async function () {
  627. await expect(
  628. this.RedisManager.promises.updateDocument(
  629. this.project_id,
  630. this.docId,
  631. this.lines,
  632. this.version,
  633. this.ops,
  634. this.ranges,
  635. this.updateMeta
  636. )
  637. ).to.be.rejected
  638. })
  639. })
  640. describe('with ranges that are too big', function () {
  641. beforeEach(function () {
  642. this.RedisManager.promises.getDocVersion
  643. .withArgs(this.docId)
  644. .resolves(this.version - this.ops.length)
  645. this.RedisManager.promises._serializeRanges = sinon
  646. .stub()
  647. .throws(new Error('ranges are too large'))
  648. })
  649. it('should throw an error', async function () {
  650. await expect(
  651. this.RedisManager.promises.updateDocument(
  652. this.project_id,
  653. this.docId,
  654. this.lines,
  655. this.version,
  656. this.ops,
  657. this.ranges,
  658. this.updateMeta
  659. )
  660. ).to.be.rejected
  661. })
  662. })
  663. describe('without user id from meta', function () {
  664. beforeEach(async function () {
  665. this.RedisManager.promises.getDocVersion
  666. .withArgs(this.docId)
  667. .resolves(this.version - this.ops.length)
  668. await this.RedisManager.promises.updateDocument(
  669. this.project_id,
  670. this.docId,
  671. this.lines,
  672. this.version,
  673. this.ops,
  674. this.ranges,
  675. {}
  676. )
  677. })
  678. it('should unset last updater', function () {
  679. this.multi.mset
  680. .calledWith({
  681. [`doclines:${this.docId}`]: JSON.stringify(this.lines),
  682. [`DocVersion:${this.docId}`]: this.version,
  683. [`DocHash:${this.docId}`]: this.hash,
  684. [`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
  685. [`lastUpdatedAt:${this.docId}`]: Date.now(),
  686. [`lastUpdatedBy:${this.docId}`]: undefined,
  687. })
  688. .should.equal(true)
  689. })
  690. })
  691. })
  692. describe('putDocInMemory', function () {
  693. beforeEach(function () {
  694. this.multi.mset = sinon.stub()
  695. this.multi.sadd = sinon.stub()
  696. this.multi.del = sinon.stub()
  697. this.multi.exists = sinon.stub()
  698. this.multi.exec.onCall(0).resolves([0])
  699. this.rclient.sadd = sinon.stub().resolves()
  700. this.lines = ['one', 'two', 'three', 'これは']
  701. this.version = 42
  702. this.hash = crypto
  703. .createHash('sha1')
  704. .update(JSON.stringify(this.lines), 'utf8')
  705. .digest('hex')
  706. this.ranges = { comments: 'mock', entries: 'mock' }
  707. this.resolvedCommentIds = ['comment-1']
  708. this.pathname = '/a/b/c.tex'
  709. })
  710. describe('with non-empty ranges', function () {
  711. beforeEach(async function () {
  712. await this.RedisManager.promises.putDocInMemory(
  713. this.project_id,
  714. this.docId,
  715. this.lines,
  716. this.version,
  717. this.ranges,
  718. this.resolvedCommentIds,
  719. this.pathname,
  720. this.projectHistoryId,
  721. this.historyRangesSupport
  722. )
  723. })
  724. it('should set all the details in a single MSET call', function () {
  725. this.multi.mset
  726. .calledWith({
  727. [`doclines:${this.docId}`]: JSON.stringify(this.lines),
  728. [`ProjectId:${this.docId}`]: this.project_id,
  729. [`DocVersion:${this.docId}`]: this.version,
  730. [`DocHash:${this.docId}`]: this.hash,
  731. [`Ranges:${this.docId}`]: JSON.stringify(this.ranges),
  732. [`Pathname:${this.docId}`]: this.pathname,
  733. [`ProjectHistoryId:${this.docId}`]: this.projectHistoryId,
  734. })
  735. .should.equal(true)
  736. })
  737. it('should add the docId to the project set', function () {
  738. this.multi.sadd
  739. .calledWith(`DocsIn:${this.project_id}`, this.docId)
  740. .should.equal(true)
  741. })
  742. it('should not log any errors', function () {
  743. this.logger.error.calledWith().should.equal(false)
  744. })
  745. it('should remove the document from the HistoryRangesSupport set in Redis', function () {
  746. this.rclient.srem.should.have.been.calledWith(
  747. 'HistoryRangesSupport',
  748. this.docId
  749. )
  750. })
  751. it('should not store the resolved comments in Redis', function () {
  752. this.multi.sadd.should.not.have.been.calledWith(
  753. `ResolvedCommentIds:${this.docId}`
  754. )
  755. })
  756. })
  757. describe('with empty ranges', function () {
  758. beforeEach(async function () {
  759. await this.RedisManager.promises.putDocInMemory(
  760. this.project_id,
  761. this.docId,
  762. this.lines,
  763. this.version,
  764. {},
  765. [],
  766. this.pathname,
  767. this.projectHistoryId,
  768. this.historyRangesSupport
  769. )
  770. })
  771. it('should unset ranges', function () {
  772. this.multi.mset.should.have.been.calledWith({
  773. [`doclines:${this.docId}`]: JSON.stringify(this.lines),
  774. [`ProjectId:${this.docId}`]: this.project_id,
  775. [`DocVersion:${this.docId}`]: this.version,
  776. [`DocHash:${this.docId}`]: this.hash,
  777. [`Ranges:${this.docId}`]: null,
  778. [`Pathname:${this.docId}`]: this.pathname,
  779. [`ProjectHistoryId:${this.docId}`]: this.projectHistoryId,
  780. })
  781. })
  782. })
  783. describe('with null bytes in the serialized doc lines', function () {
  784. beforeEach(function () {
  785. this.stringifyStub = sinon
  786. .stub(JSON, 'stringify')
  787. .callsFake(() => '["bad bytes! \u0000 <- here"]')
  788. })
  789. afterEach(function () {
  790. this.stringifyStub.restore()
  791. })
  792. it('should throw an error', async function () {
  793. await expect(
  794. this.RedisManager.promises.putDocInMemory(
  795. this.project_id,
  796. this.docId,
  797. this.lines,
  798. this.version,
  799. this.ranges,
  800. this.resolvedCommentIds,
  801. this.pathname,
  802. this.projectHistoryId,
  803. this.historyRangesSupport
  804. )
  805. ).to.be.rejected
  806. })
  807. })
  808. describe('with ranges that are too big', function () {
  809. beforeEach(function () {
  810. this.RedisManager.promises._serializeRanges = sinon
  811. .stub()
  812. .throws(new Error('ranges are too large'))
  813. })
  814. it('should throw an error', async function () {
  815. await expect(
  816. this.RedisManager.promises.putDocInMemory(
  817. this.project_id,
  818. this.docId,
  819. this.lines,
  820. this.version,
  821. this.ranges,
  822. this.resolvedCommentIds,
  823. this.pathname,
  824. this.projectHistoryId,
  825. this.historyRangesSupport
  826. )
  827. ).to.be.rejected
  828. })
  829. })
  830. describe('with history ranges support', function () {
  831. beforeEach(async function () {
  832. this.historyRangesSupport = true
  833. await this.RedisManager.promises.putDocInMemory(
  834. this.project_id,
  835. this.docId,
  836. this.lines,
  837. this.version,
  838. this.ranges,
  839. this.resolvedCommentIds,
  840. this.pathname,
  841. this.projectHistoryId,
  842. this.historyRangesSupport
  843. )
  844. })
  845. it('should add the document to the HistoryRangesSupport set in Redis', function () {
  846. this.rclient.sadd.should.have.been.calledWith(
  847. 'HistoryRangesSupport',
  848. this.docId
  849. )
  850. })
  851. it('should store the resolved comments in Redis', function () {
  852. this.multi.del.should.have.been.calledWith(
  853. `ResolvedCommentIds:${this.docId}`
  854. )
  855. this.multi.sadd.should.have.been.calledWith(
  856. `ResolvedCommentIds:${this.docId}`,
  857. ...this.resolvedCommentIds
  858. )
  859. })
  860. })
  861. describe('when the project is blocked', function () {
  862. beforeEach(async function () {
  863. this.multi.exec.onCall(0).resolves([1])
  864. await expect(
  865. this.RedisManager.promises.putDocInMemory(
  866. this.project_id,
  867. this.docId,
  868. this.lines,
  869. this.version,
  870. this.ranges,
  871. this.resolvedCommentIds,
  872. this.pathname,
  873. this.projectHistoryId,
  874. this.historyRangesSupport
  875. )
  876. ).to.be.rejectedWith('Project blocked from loading docs')
  877. })
  878. it('should not store the doc', function () {
  879. expect(this.multi.mset).to.not.have.been.called
  880. })
  881. })
  882. })
  883. describe('removeDocFromMemory', function () {
  884. beforeEach(async function () {
  885. this.multi.strlen = sinon.stub()
  886. this.multi.del = sinon.stub()
  887. this.multi.srem = sinon.stub()
  888. this.multi.exec.resolves()
  889. await this.RedisManager.promises.removeDocFromMemory(
  890. this.project_id,
  891. this.docId
  892. )
  893. })
  894. it('should check the length of the current doclines', function () {
  895. this.multi.strlen.calledWith(`doclines:${this.docId}`).should.equal(true)
  896. })
  897. it('should delete the details in a singe call', function () {
  898. this.multi.del
  899. .calledWith(
  900. `doclines:${this.docId}`,
  901. `ProjectId:${this.docId}`,
  902. `DocVersion:${this.docId}`,
  903. `DocHash:${this.docId}`,
  904. `Ranges:${this.docId}`,
  905. `Pathname:${this.docId}`,
  906. `ProjectHistoryId:${this.docId}`,
  907. `UnflushedTime:${this.docId}`,
  908. `lastUpdatedAt:${this.docId}`,
  909. `lastUpdatedBy:${this.docId}`,
  910. `ResolvedCommentIds:${this.docId}`
  911. )
  912. .should.equal(true)
  913. })
  914. it('should remove the docId from the project set', function () {
  915. this.multi.srem
  916. .calledWith(`DocsIn:${this.project_id}`, this.docId)
  917. .should.equal(true)
  918. })
  919. it('should remove the docId from the HistoryRangesSupport set', function () {
  920. this.rclient.srem.should.have.been.calledWith(
  921. 'HistoryRangesSupport',
  922. this.docId
  923. )
  924. })
  925. })
  926. describe('clearProjectState', function () {
  927. beforeEach(async function () {
  928. this.rclient.del = sinon.stub().resolves()
  929. await this.RedisManager.promises.clearProjectState(this.project_id)
  930. })
  931. it('should delete the project state', function () {
  932. this.rclient.del
  933. .calledWith(`ProjectState:${this.project_id}`)
  934. .should.equal(true)
  935. })
  936. })
  937. describe('renameDoc', function () {
  938. beforeEach(function () {
  939. this.rclient.rpush = sinon.stub().resolves()
  940. this.rclient.set = sinon.stub().resolves()
  941. this.update = {
  942. id: this.docId,
  943. pathname: (this.pathname = 'pathname'),
  944. newPathname: (this.newPathname = 'new-pathname'),
  945. }
  946. })
  947. describe('the document is cached in redis', function () {
  948. beforeEach(async function () {
  949. this.RedisManager.promises.getDoc = sinon
  950. .stub()
  951. .resolves({ lines: 'lines', version: 'version' })
  952. await this.RedisManager.promises.renameDoc(
  953. this.project_id,
  954. this.docId,
  955. this.userId,
  956. this.update,
  957. this.projectHistoryId
  958. )
  959. })
  960. it('update the cached pathname', function () {
  961. this.rclient.set
  962. .calledWith(`Pathname:${this.docId}`, this.newPathname)
  963. .should.equal(true)
  964. })
  965. })
  966. describe('the document is not cached in redis', function () {
  967. beforeEach(async function () {
  968. this.RedisManager.promises.getDoc = sinon
  969. .stub()
  970. .resolves({ lines: null, version: null })
  971. await this.RedisManager.promises.renameDoc(
  972. this.project_id,
  973. this.docId,
  974. this.userId,
  975. this.update,
  976. this.projectHistoryId
  977. )
  978. })
  979. it('does not update the cached pathname', function () {
  980. this.rclient.set.called.should.equal(false)
  981. })
  982. })
  983. describe('getDocVersion', function () {
  984. beforeEach(async function () {
  985. this.version = 12345
  986. this.rclient.mget = sinon
  987. .stub()
  988. .withArgs(`DocVersion:${this.docId}`)
  989. .resolves([`${this.version}`])
  990. this.result = await this.RedisManager.promises.getDocVersion(this.docId)
  991. })
  992. it('should return the document version', function () {
  993. this.result.should.equal(this.version)
  994. })
  995. })
  996. })
  997. })