RedisManagerTests.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. '@overleaf/redis-wrapper': {
  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(sinon.match.instanceOf(Error))
  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(sinon.match.instanceOf(Errors.NotFoundError))
  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(sinon.match.instanceOf(Errors.OpRangeNotAvailableError))
  503. .should.equal(true)
  504. })
  505. return it('should log out the problem', function () {
  506. return this.logger.warn.called.should.equal(true)
  507. })
  508. })
  509. return describe('with a slow request to redis', function () {
  510. beforeEach(function () {
  511. this.first_version_in_redis = 30
  512. this.version = 70
  513. this.length = this.version - this.first_version_in_redis
  514. this.start = 50
  515. this.end = 60
  516. this.ops = [{ mock: 'op-1' }, { mock: 'op-2' }]
  517. this.jsonOps = this.ops.map((op) => JSON.stringify(op))
  518. this.rclient.llen = sinon.stub().callsArgWith(1, null, this.length)
  519. this.rclient.get = sinon
  520. .stub()
  521. .callsArgWith(1, null, this.version.toString())
  522. this.clock = sinon.useFakeTimers()
  523. this.rclient.lrange = (key, start, end, cb) => {
  524. this.clock.tick(6000)
  525. return cb(null, this.jsonOps)
  526. }
  527. return this.RedisManager.getPreviousDocOps(
  528. this.doc_id,
  529. this.start,
  530. this.end,
  531. this.callback
  532. )
  533. })
  534. afterEach(function () {
  535. return this.clock.restore()
  536. })
  537. return it('should return an error', function () {
  538. return this.callback
  539. .calledWith(sinon.match.instanceOf(Error))
  540. .should.equal(true)
  541. })
  542. })
  543. })
  544. describe('updateDocument', function () {
  545. beforeEach(function () {
  546. this.lines = ['one', 'two', 'three', 'これは']
  547. this.ops = [{ op: [{ i: 'foo', p: 4 }] }, { op: [{ i: 'bar', p: 8 }] }]
  548. this.version = 42
  549. this.hash = crypto
  550. .createHash('sha1')
  551. .update(JSON.stringify(this.lines), 'utf8')
  552. .digest('hex')
  553. this.ranges = { comments: 'mock', entries: 'mock' }
  554. this.updateMeta = { user_id: 'last-author-fake-id' }
  555. this.doc_update_list_length = sinon.stub()
  556. this.project_update_list_length = sinon.stub()
  557. this.RedisManager.getDocVersion = sinon.stub()
  558. this.multi.set = sinon.stub()
  559. this.multi.rpush = sinon.stub()
  560. this.multi.expire = sinon.stub()
  561. this.multi.ltrim = sinon.stub()
  562. this.multi.del = sinon.stub()
  563. this.multi.exec = sinon
  564. .stub()
  565. .callsArgWith(0, null, [
  566. this.hash,
  567. null,
  568. null,
  569. null,
  570. null,
  571. null,
  572. null,
  573. this.doc_update_list_length,
  574. null,
  575. null
  576. ])
  577. return (this.ProjectHistoryRedisManager.queueOps = sinon
  578. .stub()
  579. .callsArgWith(
  580. this.ops.length + 1,
  581. null,
  582. this.project_update_list_length
  583. ))
  584. })
  585. describe('with a consistent version', function () {
  586. beforeEach(function () {})
  587. describe('with project history enabled', function () {
  588. beforeEach(function () {
  589. this.settings.apis.project_history.enabled = true
  590. this.RedisManager.getDocVersion
  591. .withArgs(this.doc_id)
  592. .yields(null, this.version - this.ops.length)
  593. return this.RedisManager.updateDocument(
  594. this.project_id,
  595. this.doc_id,
  596. this.lines,
  597. this.version,
  598. this.ops,
  599. this.ranges,
  600. this.updateMeta,
  601. this.callback
  602. )
  603. })
  604. it('should get the current doc version to check for consistency', function () {
  605. return this.RedisManager.getDocVersion
  606. .calledWith(this.doc_id)
  607. .should.equal(true)
  608. })
  609. it('should set the doclines', function () {
  610. return this.multi.set
  611. .calledWith(`doclines:${this.doc_id}`, JSON.stringify(this.lines))
  612. .should.equal(true)
  613. })
  614. it('should set the version', function () {
  615. return this.multi.set
  616. .calledWith(`DocVersion:${this.doc_id}`, this.version)
  617. .should.equal(true)
  618. })
  619. it('should set the hash', function () {
  620. return this.multi.set
  621. .calledWith(`DocHash:${this.doc_id}`, this.hash)
  622. .should.equal(true)
  623. })
  624. it('should set the ranges', function () {
  625. return this.multi.set
  626. .calledWith(`Ranges:${this.doc_id}`, JSON.stringify(this.ranges))
  627. .should.equal(true)
  628. })
  629. it('should set the unflushed time', function () {
  630. return this.multi.set
  631. .calledWith(`UnflushedTime:${this.doc_id}`, Date.now(), 'NX')
  632. .should.equal(true)
  633. })
  634. it('should set the last updated time', function () {
  635. return this.multi.set
  636. .calledWith(`lastUpdatedAt:${this.doc_id}`, Date.now())
  637. .should.equal(true)
  638. })
  639. it('should set the last updater', function () {
  640. return this.multi.set
  641. .calledWith(`lastUpdatedBy:${this.doc_id}`, 'last-author-fake-id')
  642. .should.equal(true)
  643. })
  644. it('should push the doc op into the doc ops list', function () {
  645. return this.multi.rpush
  646. .calledWith(
  647. `DocOps:${this.doc_id}`,
  648. JSON.stringify(this.ops[0]),
  649. JSON.stringify(this.ops[1])
  650. )
  651. .should.equal(true)
  652. })
  653. it('should renew the expiry ttl on the doc ops array', function () {
  654. return this.multi.expire
  655. .calledWith(`DocOps:${this.doc_id}`, this.RedisManager.DOC_OPS_TTL)
  656. .should.equal(true)
  657. })
  658. it('should truncate the list to 100 members', function () {
  659. return this.multi.ltrim
  660. .calledWith(
  661. `DocOps:${this.doc_id}`,
  662. -this.RedisManager.DOC_OPS_MAX_LENGTH,
  663. -1
  664. )
  665. .should.equal(true)
  666. })
  667. it('should push the updates into the history ops list', function () {
  668. return this.multi.rpush
  669. .calledWith(
  670. `UncompressedHistoryOps:${this.doc_id}`,
  671. JSON.stringify(this.ops[0]),
  672. JSON.stringify(this.ops[1])
  673. )
  674. .should.equal(true)
  675. })
  676. it('should push the updates into the project history ops list', function () {
  677. return this.ProjectHistoryRedisManager.queueOps
  678. .calledWith(this.project_id, JSON.stringify(this.ops[0]))
  679. .should.equal(true)
  680. })
  681. it('should call the callback', function () {
  682. return this.callback
  683. .calledWith(
  684. null,
  685. this.doc_update_list_length,
  686. this.project_update_list_length
  687. )
  688. .should.equal(true)
  689. })
  690. return it('should not log any errors', function () {
  691. return this.logger.error.calledWith().should.equal(false)
  692. })
  693. })
  694. describe('with project history disabled', function () {
  695. beforeEach(function () {
  696. this.settings.apis.project_history.enabled = false
  697. this.RedisManager.getDocVersion
  698. .withArgs(this.doc_id)
  699. .yields(null, this.version - this.ops.length)
  700. return this.RedisManager.updateDocument(
  701. this.project_id,
  702. this.doc_id,
  703. this.lines,
  704. this.version,
  705. this.ops,
  706. this.ranges,
  707. this.updateMeta,
  708. this.callback
  709. )
  710. })
  711. it('should not push the updates into the project history ops list', function () {
  712. return this.ProjectHistoryRedisManager.queueOps.called.should.equal(
  713. false
  714. )
  715. })
  716. return it('should call the callback', function () {
  717. return this.callback
  718. .calledWith(null, this.doc_update_list_length)
  719. .should.equal(true)
  720. })
  721. })
  722. return describe('with a doc using project history only', function () {
  723. beforeEach(function () {
  724. this.RedisManager.getDocVersion
  725. .withArgs(this.doc_id)
  726. .yields(null, this.version - this.ops.length, 'project-history')
  727. return this.RedisManager.updateDocument(
  728. this.project_id,
  729. this.doc_id,
  730. this.lines,
  731. this.version,
  732. this.ops,
  733. this.ranges,
  734. this.updateMeta,
  735. this.callback
  736. )
  737. })
  738. it('should not push the updates to the track-changes ops list', function () {
  739. return this.multi.rpush
  740. .calledWith(`UncompressedHistoryOps:${this.doc_id}`)
  741. .should.equal(false)
  742. })
  743. it('should push the updates into the project history ops list', function () {
  744. return this.ProjectHistoryRedisManager.queueOps
  745. .calledWith(this.project_id, JSON.stringify(this.ops[0]))
  746. .should.equal(true)
  747. })
  748. return it('should call the callback with the project update count only', function () {
  749. return this.callback
  750. .calledWith(null, undefined, this.project_update_list_length)
  751. .should.equal(true)
  752. })
  753. })
  754. })
  755. describe('with an inconsistent version', function () {
  756. beforeEach(function () {
  757. this.RedisManager.getDocVersion
  758. .withArgs(this.doc_id)
  759. .yields(null, this.version - this.ops.length - 1)
  760. return this.RedisManager.updateDocument(
  761. this.project_id,
  762. this.doc_id,
  763. this.lines,
  764. this.version,
  765. this.ops,
  766. this.ranges,
  767. this.updateMeta,
  768. this.callback
  769. )
  770. })
  771. it('should not call multi.exec', function () {
  772. return this.multi.exec.called.should.equal(false)
  773. })
  774. return it('should call the callback with an error', function () {
  775. return this.callback
  776. .calledWith(sinon.match.instanceOf(Error))
  777. .should.equal(true)
  778. })
  779. })
  780. describe('with no updates', function () {
  781. beforeEach(function () {
  782. this.RedisManager.getDocVersion
  783. .withArgs(this.doc_id)
  784. .yields(null, this.version)
  785. return this.RedisManager.updateDocument(
  786. this.project_id,
  787. this.doc_id,
  788. this.lines,
  789. this.version,
  790. [],
  791. this.ranges,
  792. this.updateMeta,
  793. this.callback
  794. )
  795. })
  796. it('should not try to enqueue doc updates', function () {
  797. return this.multi.rpush.called.should.equal(false)
  798. })
  799. it('should not try to enqueue project updates', function () {
  800. return this.ProjectHistoryRedisManager.queueOps.called.should.equal(
  801. false
  802. )
  803. })
  804. return it('should still set the doclines', function () {
  805. return this.multi.set
  806. .calledWith(`doclines:${this.doc_id}`, JSON.stringify(this.lines))
  807. .should.equal(true)
  808. })
  809. })
  810. describe('with empty ranges', function () {
  811. beforeEach(function () {
  812. this.RedisManager.getDocVersion
  813. .withArgs(this.doc_id)
  814. .yields(null, this.version - this.ops.length)
  815. return this.RedisManager.updateDocument(
  816. this.project_id,
  817. this.doc_id,
  818. this.lines,
  819. this.version,
  820. this.ops,
  821. {},
  822. this.updateMeta,
  823. this.callback
  824. )
  825. })
  826. it('should not set the ranges', function () {
  827. return this.multi.set
  828. .calledWith(`Ranges:${this.doc_id}`, JSON.stringify(this.ranges))
  829. .should.equal(false)
  830. })
  831. return it('should delete the ranges key', function () {
  832. return this.multi.del
  833. .calledWith(`Ranges:${this.doc_id}`)
  834. .should.equal(true)
  835. })
  836. })
  837. describe('with null bytes in the serialized doc lines', function () {
  838. beforeEach(function () {
  839. this.RedisManager.getDocVersion
  840. .withArgs(this.doc_id)
  841. .yields(null, this.version - this.ops.length)
  842. this._stringify = JSON.stringify
  843. this.JSON.stringify = () => '["bad bytes! \u0000 <- here"]'
  844. return this.RedisManager.updateDocument(
  845. this.project_id,
  846. this.doc_id,
  847. this.lines,
  848. this.version,
  849. this.ops,
  850. this.ranges,
  851. this.updateMeta,
  852. this.callback
  853. )
  854. })
  855. afterEach(function () {
  856. return (this.JSON.stringify = this._stringify)
  857. })
  858. it('should log an error', function () {
  859. return this.logger.error.called.should.equal(true)
  860. })
  861. return it('should call the callback with an error', function () {
  862. return this.callback
  863. .calledWith(sinon.match.instanceOf(Error))
  864. .should.equal(true)
  865. })
  866. })
  867. describe('with ranges that are too big', function () {
  868. beforeEach(function () {
  869. this.RedisManager.getDocVersion
  870. .withArgs(this.doc_id)
  871. .yields(null, this.version - this.ops.length)
  872. this.RedisManager._serializeRanges = sinon
  873. .stub()
  874. .yields(new Error('ranges are too large'))
  875. return this.RedisManager.updateDocument(
  876. this.project_id,
  877. this.doc_id,
  878. this.lines,
  879. this.version,
  880. this.ops,
  881. this.ranges,
  882. this.updateMeta,
  883. this.callback
  884. )
  885. })
  886. it('should log an error', function () {
  887. return this.logger.error.called.should.equal(true)
  888. })
  889. return it('should call the callback with the error', function () {
  890. return this.callback
  891. .calledWith(sinon.match.instanceOf(Error))
  892. .should.equal(true)
  893. })
  894. })
  895. return describe('without user id from meta', function () {
  896. beforeEach(function () {
  897. this.RedisManager.getDocVersion
  898. .withArgs(this.doc_id)
  899. .yields(null, this.version - this.ops.length)
  900. return this.RedisManager.updateDocument(
  901. this.project_id,
  902. this.doc_id,
  903. this.lines,
  904. this.version,
  905. this.ops,
  906. this.ranges,
  907. {},
  908. this.callback
  909. )
  910. })
  911. it('should set the last updater to null', function () {
  912. return this.multi.del
  913. .calledWith(`lastUpdatedBy:${this.doc_id}`)
  914. .should.equal(true)
  915. })
  916. return it('should still set the last updated time', function () {
  917. return this.multi.set
  918. .calledWith(`lastUpdatedAt:${this.doc_id}`, Date.now())
  919. .should.equal(true)
  920. })
  921. })
  922. })
  923. describe('putDocInMemory', function () {
  924. beforeEach(function () {
  925. this.multi.set = sinon.stub()
  926. this.rclient.sadd = sinon.stub().yields()
  927. this.multi.del = sinon.stub()
  928. this.lines = ['one', 'two', 'three', 'これは']
  929. this.version = 42
  930. this.hash = crypto
  931. .createHash('sha1')
  932. .update(JSON.stringify(this.lines), 'utf8')
  933. .digest('hex')
  934. this.multi.exec = sinon.stub().callsArgWith(0, null, [this.hash])
  935. this.ranges = { comments: 'mock', entries: 'mock' }
  936. return (this.pathname = '/a/b/c.tex')
  937. })
  938. describe('with non-empty ranges', function () {
  939. beforeEach(function (done) {
  940. return this.RedisManager.putDocInMemory(
  941. this.project_id,
  942. this.doc_id,
  943. this.lines,
  944. this.version,
  945. this.ranges,
  946. this.pathname,
  947. this.projectHistoryId,
  948. done
  949. )
  950. })
  951. it('should set the lines', function () {
  952. return this.multi.set
  953. .calledWith(`doclines:${this.doc_id}`, JSON.stringify(this.lines))
  954. .should.equal(true)
  955. })
  956. it('should set the version', function () {
  957. return this.multi.set
  958. .calledWith(`DocVersion:${this.doc_id}`, this.version)
  959. .should.equal(true)
  960. })
  961. it('should set the hash', function () {
  962. return this.multi.set
  963. .calledWith(`DocHash:${this.doc_id}`, this.hash)
  964. .should.equal(true)
  965. })
  966. it('should set the ranges', function () {
  967. return this.multi.set
  968. .calledWith(`Ranges:${this.doc_id}`, JSON.stringify(this.ranges))
  969. .should.equal(true)
  970. })
  971. it('should set the project_id for the doc', function () {
  972. return this.multi.set
  973. .calledWith(`ProjectId:${this.doc_id}`, this.project_id)
  974. .should.equal(true)
  975. })
  976. it('should set the pathname for the doc', function () {
  977. return this.multi.set
  978. .calledWith(`Pathname:${this.doc_id}`, this.pathname)
  979. .should.equal(true)
  980. })
  981. it('should set the projectHistoryId for the doc', function () {
  982. return this.multi.set
  983. .calledWith(`ProjectHistoryId:${this.doc_id}`, this.projectHistoryId)
  984. .should.equal(true)
  985. })
  986. it('should add the doc_id to the project set', function () {
  987. return this.rclient.sadd
  988. .calledWith(`DocsIn:${this.project_id}`, this.doc_id)
  989. .should.equal(true)
  990. })
  991. return it('should not log any errors', function () {
  992. return this.logger.error.calledWith().should.equal(false)
  993. })
  994. })
  995. describe('with empty ranges', function () {
  996. beforeEach(function (done) {
  997. return this.RedisManager.putDocInMemory(
  998. this.project_id,
  999. this.doc_id,
  1000. this.lines,
  1001. this.version,
  1002. {},
  1003. this.pathname,
  1004. this.projectHistoryId,
  1005. done
  1006. )
  1007. })
  1008. it('should delete the ranges key', function () {
  1009. return this.multi.del
  1010. .calledWith(`Ranges:${this.doc_id}`)
  1011. .should.equal(true)
  1012. })
  1013. return it('should not set the ranges', function () {
  1014. return this.multi.set
  1015. .calledWith(`Ranges:${this.doc_id}`, JSON.stringify(this.ranges))
  1016. .should.equal(false)
  1017. })
  1018. })
  1019. describe('with null bytes in the serialized doc lines', function () {
  1020. beforeEach(function () {
  1021. this._stringify = JSON.stringify
  1022. this.JSON.stringify = () => '["bad bytes! \u0000 <- here"]'
  1023. return this.RedisManager.putDocInMemory(
  1024. this.project_id,
  1025. this.doc_id,
  1026. this.lines,
  1027. this.version,
  1028. this.ranges,
  1029. this.pathname,
  1030. this.projectHistoryId,
  1031. this.callback
  1032. )
  1033. })
  1034. afterEach(function () {
  1035. return (this.JSON.stringify = this._stringify)
  1036. })
  1037. it('should log an error', function () {
  1038. return this.logger.error.called.should.equal(true)
  1039. })
  1040. return it('should call the callback with an error', function () {
  1041. return this.callback
  1042. .calledWith(sinon.match.instanceOf(Error))
  1043. .should.equal(true)
  1044. })
  1045. })
  1046. return describe('with ranges that are too big', function () {
  1047. beforeEach(function () {
  1048. this.RedisManager._serializeRanges = sinon
  1049. .stub()
  1050. .yields(new Error('ranges are too large'))
  1051. return this.RedisManager.putDocInMemory(
  1052. this.project_id,
  1053. this.doc_id,
  1054. this.lines,
  1055. this.version,
  1056. this.ranges,
  1057. this.pathname,
  1058. this.projectHistoryId,
  1059. this.callback
  1060. )
  1061. })
  1062. it('should log an error', function () {
  1063. return this.logger.error.called.should.equal(true)
  1064. })
  1065. return it('should call the callback with the error', function () {
  1066. return this.callback
  1067. .calledWith(sinon.match.instanceOf(Error))
  1068. .should.equal(true)
  1069. })
  1070. })
  1071. })
  1072. describe('removeDocFromMemory', function () {
  1073. beforeEach(function (done) {
  1074. this.multi.strlen = sinon.stub()
  1075. this.multi.del = sinon.stub()
  1076. this.multi.srem = sinon.stub()
  1077. this.multi.exec.yields()
  1078. return this.RedisManager.removeDocFromMemory(
  1079. this.project_id,
  1080. this.doc_id,
  1081. done
  1082. )
  1083. })
  1084. it('should check the length of the current doclines', function () {
  1085. return this.multi.strlen
  1086. .calledWith(`doclines:${this.doc_id}`)
  1087. .should.equal(true)
  1088. })
  1089. it('should delete the lines', function () {
  1090. return this.multi.del
  1091. .calledWith(`doclines:${this.doc_id}`)
  1092. .should.equal(true)
  1093. })
  1094. it('should delete the version', function () {
  1095. return this.multi.del
  1096. .calledWith(`DocVersion:${this.doc_id}`)
  1097. .should.equal(true)
  1098. })
  1099. it('should delete the hash', function () {
  1100. return this.multi.del
  1101. .calledWith(`DocHash:${this.doc_id}`)
  1102. .should.equal(true)
  1103. })
  1104. it('should delete the unflushed time', function () {
  1105. return this.multi.del
  1106. .calledWith(`UnflushedTime:${this.doc_id}`)
  1107. .should.equal(true)
  1108. })
  1109. it('should delete the project_id for the doc', function () {
  1110. return this.multi.del
  1111. .calledWith(`ProjectId:${this.doc_id}`)
  1112. .should.equal(true)
  1113. })
  1114. it('should remove the doc_id from the project set', function () {
  1115. return this.multi.srem
  1116. .calledWith(`DocsIn:${this.project_id}`, this.doc_id)
  1117. .should.equal(true)
  1118. })
  1119. it('should delete the pathname for the doc', function () {
  1120. return this.multi.del
  1121. .calledWith(`Pathname:${this.doc_id}`)
  1122. .should.equal(true)
  1123. })
  1124. it('should delete the pathname for the doc', function () {
  1125. return this.multi.del
  1126. .calledWith(`ProjectHistoryId:${this.doc_id}`)
  1127. .should.equal(true)
  1128. })
  1129. it('should delete lastUpdatedAt', function () {
  1130. return this.multi.del
  1131. .calledWith(`lastUpdatedAt:${this.doc_id}`)
  1132. .should.equal(true)
  1133. })
  1134. return it('should delete lastUpdatedBy', function () {
  1135. return this.multi.del
  1136. .calledWith(`lastUpdatedBy:${this.doc_id}`)
  1137. .should.equal(true)
  1138. })
  1139. })
  1140. describe('clearProjectState', function () {
  1141. beforeEach(function (done) {
  1142. this.rclient.del = sinon.stub().callsArg(1)
  1143. return this.RedisManager.clearProjectState(this.project_id, done)
  1144. })
  1145. return it('should delete the project state', function () {
  1146. return this.rclient.del
  1147. .calledWith(`ProjectState:${this.project_id}`)
  1148. .should.equal(true)
  1149. })
  1150. })
  1151. return describe('renameDoc', function () {
  1152. beforeEach(function () {
  1153. this.rclient.rpush = sinon.stub().yields()
  1154. this.rclient.set = sinon.stub().yields()
  1155. return (this.update = {
  1156. id: this.doc_id,
  1157. pathname: (this.pathname = 'pathname'),
  1158. newPathname: (this.newPathname = 'new-pathname')
  1159. })
  1160. })
  1161. describe('the document is cached in redis', function () {
  1162. beforeEach(function () {
  1163. this.RedisManager.getDoc = sinon
  1164. .stub()
  1165. .callsArgWith(2, null, 'lines', 'version')
  1166. this.ProjectHistoryRedisManager.queueRenameEntity = sinon
  1167. .stub()
  1168. .yields()
  1169. return this.RedisManager.renameDoc(
  1170. this.project_id,
  1171. this.doc_id,
  1172. this.userId,
  1173. this.update,
  1174. this.projectHistoryId,
  1175. this.callback
  1176. )
  1177. })
  1178. it('update the cached pathname', function () {
  1179. return this.rclient.set
  1180. .calledWith(`Pathname:${this.doc_id}`, this.newPathname)
  1181. .should.equal(true)
  1182. })
  1183. return it('should queue an update', function () {
  1184. return this.ProjectHistoryRedisManager.queueRenameEntity
  1185. .calledWithExactly(
  1186. this.project_id,
  1187. this.projectHistoryId,
  1188. 'doc',
  1189. this.doc_id,
  1190. this.userId,
  1191. this.update,
  1192. this.callback
  1193. )
  1194. .should.equal(true)
  1195. })
  1196. })
  1197. describe('the document is not cached in redis', function () {
  1198. beforeEach(function () {
  1199. this.RedisManager.getDoc = sinon
  1200. .stub()
  1201. .callsArgWith(2, null, null, null)
  1202. this.ProjectHistoryRedisManager.queueRenameEntity = sinon
  1203. .stub()
  1204. .yields()
  1205. return this.RedisManager.renameDoc(
  1206. this.project_id,
  1207. this.doc_id,
  1208. this.userId,
  1209. this.update,
  1210. this.projectHistoryId,
  1211. this.callback
  1212. )
  1213. })
  1214. it('does not update the cached pathname', function () {
  1215. return this.rclient.set.called.should.equal(false)
  1216. })
  1217. return it('should queue an update', function () {
  1218. return this.ProjectHistoryRedisManager.queueRenameEntity
  1219. .calledWithExactly(
  1220. this.project_id,
  1221. this.projectHistoryId,
  1222. 'doc',
  1223. this.doc_id,
  1224. this.userId,
  1225. this.update,
  1226. this.callback
  1227. )
  1228. .should.equal(true)
  1229. })
  1230. })
  1231. return describe('getDocVersion', function () {
  1232. beforeEach(function () {
  1233. return (this.version = 12345)
  1234. })
  1235. describe('when the document does not have a project history type set', function () {
  1236. beforeEach(function () {
  1237. this.rclient.mget = sinon
  1238. .stub()
  1239. .withArgs(
  1240. `DocVersion:${this.doc_id}`,
  1241. `ProjectHistoryType:${this.doc_id}`
  1242. )
  1243. .callsArgWith(2, null, [`${this.version}`])
  1244. return this.RedisManager.getDocVersion(this.doc_id, this.callback)
  1245. })
  1246. return it('should return the document version and an undefined history type', function () {
  1247. return this.callback
  1248. .calledWithExactly(null, this.version, undefined)
  1249. .should.equal(true)
  1250. })
  1251. })
  1252. return describe('when the document has a project history type set', function () {
  1253. beforeEach(function () {
  1254. this.rclient.mget = sinon
  1255. .stub()
  1256. .withArgs(
  1257. `DocVersion:${this.doc_id}`,
  1258. `ProjectHistoryType:${this.doc_id}`
  1259. )
  1260. .callsArgWith(2, null, [`${this.version}`, 'project-history'])
  1261. return this.RedisManager.getDocVersion(this.doc_id, this.callback)
  1262. })
  1263. return it('should return the document version and history type', function () {
  1264. return this.callback
  1265. .calledWithExactly(null, this.version, 'project-history')
  1266. .should.equal(true)
  1267. })
  1268. })
  1269. })
  1270. })
  1271. })