RedisManagerTests.js 36 KB

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