ProjectHistoryRedisManagerTests.js 5.4 KB

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