ProjectHistoryRedisManagerTests.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 modulePath = '../../../../app/js/ProjectHistoryRedisManager.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. const tk = require('timekeeper')
  18. describe('ProjectHistoryRedisManager', function () {
  19. beforeEach(function () {
  20. this.project_id = 'project-id-123'
  21. this.projectHistoryId = 'history-id-123'
  22. this.user_id = 'user-id-123'
  23. this.callback = sinon.stub()
  24. this.rclient = {}
  25. tk.freeze(new Date())
  26. return (this.ProjectHistoryRedisManager = SandboxedModule.require(
  27. modulePath,
  28. {
  29. requires: {
  30. '@overleaf/settings': (this.settings = {
  31. redis: {
  32. project_history: {
  33. key_schema: {
  34. projectHistoryOps({ project_id }) {
  35. return `ProjectHistory:Ops:${project_id}`
  36. },
  37. projectHistoryFirstOpTimestamp({ project_id }) {
  38. return `ProjectHistory:FirstOpTimestamp:${project_id}`
  39. },
  40. },
  41. },
  42. },
  43. }),
  44. '@overleaf/redis-wrapper': {
  45. createClient: () => this.rclient,
  46. },
  47. './Metrics': (this.metrics = { summary: sinon.stub() }),
  48. },
  49. }
  50. ))
  51. })
  52. afterEach(function () {
  53. return tk.reset()
  54. })
  55. describe('queueOps', function () {
  56. beforeEach(function () {
  57. this.ops = ['mock-op-1', 'mock-op-2']
  58. this.multi = { exec: sinon.stub() }
  59. this.multi.rpush = sinon.stub()
  60. this.multi.setnx = sinon.stub()
  61. this.rclient.multi = () => this.multi
  62. // @rclient = multi: () => @multi
  63. return this.ProjectHistoryRedisManager.queueOps(
  64. this.project_id,
  65. ...Array.from(this.ops),
  66. this.callback
  67. )
  68. })
  69. it('should queue an update', function () {
  70. return this.multi.rpush
  71. .calledWithExactly(
  72. `ProjectHistory:Ops:${this.project_id}`,
  73. this.ops[0],
  74. this.ops[1]
  75. )
  76. .should.equal(true)
  77. })
  78. return it('should set the queue timestamp if not present', function () {
  79. return this.multi.setnx
  80. .calledWithExactly(
  81. `ProjectHistory:FirstOpTimestamp:${this.project_id}`,
  82. Date.now()
  83. )
  84. .should.equal(true)
  85. })
  86. })
  87. describe('queueRenameEntity', function () {
  88. beforeEach(function () {
  89. this.file_id = 1234
  90. this.rawUpdate = {
  91. pathname: (this.pathname = '/old'),
  92. newPathname: (this.newPathname = '/new'),
  93. version: (this.version = 2),
  94. }
  95. this.ProjectHistoryRedisManager.queueOps = sinon.stub()
  96. return this.ProjectHistoryRedisManager.queueRenameEntity(
  97. this.project_id,
  98. this.projectHistoryId,
  99. 'file',
  100. this.file_id,
  101. this.user_id,
  102. this.rawUpdate,
  103. this.callback
  104. )
  105. })
  106. return it('should queue an update', function () {
  107. const update = {
  108. pathname: this.pathname,
  109. new_pathname: this.newPathname,
  110. meta: {
  111. user_id: this.user_id,
  112. ts: new Date(),
  113. },
  114. version: this.version,
  115. projectHistoryId: this.projectHistoryId,
  116. file: this.file_id,
  117. }
  118. return this.ProjectHistoryRedisManager.queueOps
  119. .calledWithExactly(
  120. this.project_id,
  121. JSON.stringify(update),
  122. this.callback
  123. )
  124. .should.equal(true)
  125. })
  126. })
  127. return describe('queueAddEntity', function () {
  128. beforeEach(function () {
  129. this.rclient.rpush = sinon.stub().yields()
  130. this.doc_id = 1234
  131. this.rawUpdate = {
  132. pathname: (this.pathname = '/old'),
  133. docLines: (this.docLines = 'a\nb'),
  134. version: (this.version = 2),
  135. url: (this.url = 'filestore.example.com'),
  136. }
  137. this.ProjectHistoryRedisManager.queueOps = sinon.stub()
  138. return this.ProjectHistoryRedisManager.queueAddEntity(
  139. this.project_id,
  140. this.projectHistoryId,
  141. 'doc',
  142. this.doc_id,
  143. this.user_id,
  144. this.rawUpdate,
  145. this.callback
  146. )
  147. })
  148. it('should queue an update', function () {
  149. const update = {
  150. pathname: this.pathname,
  151. docLines: this.docLines,
  152. url: this.url,
  153. meta: {
  154. user_id: this.user_id,
  155. ts: new Date(),
  156. },
  157. version: this.version,
  158. projectHistoryId: this.projectHistoryId,
  159. doc: this.doc_id,
  160. }
  161. return this.ProjectHistoryRedisManager.queueOps
  162. .calledWithExactly(
  163. this.project_id,
  164. JSON.stringify(update),
  165. this.callback
  166. )
  167. .should.equal(true)
  168. })
  169. describe('queueResyncProjectStructure', function () {
  170. return it('should queue an update', function () {})
  171. })
  172. return describe('queueResyncDocContent', function () {
  173. return it('should queue an update', function () {})
  174. })
  175. })
  176. })