RedisManagerTests.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  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 { expect } = require('chai')
  16. const modulePath = '../../../../app/js/RedisManager.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('RedisManager', function () {
  19. beforeEach(function () {
  20. this.RedisManager = SandboxedModule.require(modulePath, {
  21. requires: {
  22. '@overleaf/redis-wrapper': {
  23. createClient: () => {
  24. return (this.rclient = {
  25. auth: sinon.stub(),
  26. multi: () => this.rclient
  27. })
  28. }
  29. },
  30. 'settings-sharelatex': {
  31. redis: {
  32. history: {
  33. key_schema: {
  34. uncompressedHistoryOps({ doc_id }) {
  35. return `UncompressedHistoryOps:${doc_id}`
  36. },
  37. docsWithHistoryOps({ project_id }) {
  38. return `DocsWithHistoryOps:${project_id}`
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. })
  46. this.doc_id = 'doc-id-123'
  47. this.project_id = 'project-id-123'
  48. this.batchSize = 100
  49. return (this.callback = sinon.stub())
  50. })
  51. describe('getOldestDocUpdates', function () {
  52. beforeEach(function () {
  53. this.rawUpdates = [
  54. { v: 42, op: 'mock-op-42' },
  55. { v: 45, op: 'mock-op-45' }
  56. ]
  57. this.jsonUpdates = Array.from(this.rawUpdates).map((update) =>
  58. JSON.stringify(update)
  59. )
  60. this.rclient.lrange = sinon.stub().callsArgWith(3, null, this.jsonUpdates)
  61. return this.RedisManager.getOldestDocUpdates(
  62. this.doc_id,
  63. this.batchSize,
  64. this.callback
  65. )
  66. })
  67. it('should read the updates from redis', function () {
  68. return this.rclient.lrange
  69. .calledWith(
  70. `UncompressedHistoryOps:${this.doc_id}`,
  71. 0,
  72. this.batchSize - 1
  73. )
  74. .should.equal(true)
  75. })
  76. it('should call the callback with the unparsed ops', function () {
  77. return this.callback.calledWith(null, this.jsonUpdates).should.equal(true)
  78. })
  79. describe('expandDocUpdates', function () {
  80. beforeEach(function () {
  81. return this.RedisManager.expandDocUpdates(
  82. this.jsonUpdates,
  83. this.callback
  84. )
  85. })
  86. return it('should call the callback with the parsed ops', function () {
  87. return this.callback
  88. .calledWith(null, this.rawUpdates)
  89. .should.equal(true)
  90. })
  91. })
  92. return describe('deleteAppliedDocUpdates', function () {
  93. beforeEach(function () {
  94. this.rclient.lrem = sinon.stub()
  95. this.rclient.srem = sinon.stub()
  96. this.rclient.exec = sinon.stub().callsArgWith(0)
  97. return this.RedisManager.deleteAppliedDocUpdates(
  98. this.project_id,
  99. this.doc_id,
  100. this.jsonUpdates,
  101. this.callback
  102. )
  103. })
  104. it('should delete the first update from redis', function () {
  105. return this.rclient.lrem
  106. .calledWith(
  107. `UncompressedHistoryOps:${this.doc_id}`,
  108. 1,
  109. this.jsonUpdates[0]
  110. )
  111. .should.equal(true)
  112. })
  113. it('should delete the second update from redis', function () {
  114. return this.rclient.lrem
  115. .calledWith(
  116. `UncompressedHistoryOps:${this.doc_id}`,
  117. 1,
  118. this.jsonUpdates[1]
  119. )
  120. .should.equal(true)
  121. })
  122. it('should delete the doc from the set of docs with history ops', function () {
  123. return this.rclient.srem
  124. .calledWith(`DocsWithHistoryOps:${this.project_id}`, this.doc_id)
  125. .should.equal(true)
  126. })
  127. return it('should call the callback ', function () {
  128. return this.callback.called.should.equal(true)
  129. })
  130. })
  131. })
  132. return describe('getDocIdsWithHistoryOps', function () {
  133. beforeEach(function () {
  134. this.doc_ids = ['mock-id-1', 'mock-id-2']
  135. this.rclient.smembers = sinon.stub().callsArgWith(1, null, this.doc_ids)
  136. return this.RedisManager.getDocIdsWithHistoryOps(
  137. this.project_id,
  138. this.callback
  139. )
  140. })
  141. it('should read the doc_ids from redis', function () {
  142. return this.rclient.smembers
  143. .calledWith(`DocsWithHistoryOps:${this.project_id}`)
  144. .should.equal(true)
  145. })
  146. return it('should call the callback with the doc_ids', function () {
  147. return this.callback.calledWith(null, this.doc_ids).should.equal(true)
  148. })
  149. })
  150. })