RedisManagerTests.js 42 KB

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