RedisManagerTests.js 4.8 KB

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