getProjectDocsTests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS206: Consider reworking classes to avoid initClass
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const modulePath = '../../../../app/js/ProjectManager.js'
  15. const SandboxedModule = require('sandboxed-module')
  16. const Errors = require('../../../../app/js/Errors.js')
  17. describe('ProjectManager - getProjectDocsAndFlushIfOld', function () {
  18. beforeEach(function () {
  19. let Timer
  20. this.ProjectManager = SandboxedModule.require(modulePath, {
  21. requires: {
  22. './RedisManager': (this.RedisManager = {}),
  23. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  24. './DocumentManager': (this.DocumentManager = {}),
  25. './HistoryManager': (this.HistoryManager = {}),
  26. './Metrics': (this.Metrics = {
  27. Timer: (Timer = (function () {
  28. Timer = class Timer {
  29. static initClass() {
  30. this.prototype.done = sinon.stub()
  31. }
  32. }
  33. Timer.initClass()
  34. return Timer
  35. })())
  36. }),
  37. './Errors': Errors
  38. }
  39. })
  40. this.project_id = 'project-id-123'
  41. this.callback = sinon.stub()
  42. return (this.doc_versions = [111, 222, 333])
  43. })
  44. describe('successfully', function () {
  45. beforeEach(function (done) {
  46. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  47. this.doc_lines = [
  48. ['aaa', 'aaa'],
  49. ['bbb', 'bbb'],
  50. ['ccc', 'ccc']
  51. ]
  52. this.docs = [
  53. {
  54. _id: this.doc_ids[0],
  55. lines: this.doc_lines[0],
  56. v: this.doc_versions[0]
  57. },
  58. {
  59. _id: this.doc_ids[1],
  60. lines: this.doc_lines[1],
  61. v: this.doc_versions[1]
  62. },
  63. {
  64. _id: this.doc_ids[2],
  65. lines: this.doc_lines[2],
  66. v: this.doc_versions[2]
  67. }
  68. ]
  69. this.RedisManager.checkOrSetProjectState = sinon
  70. .stub()
  71. .callsArgWith(2, null)
  72. this.RedisManager.getDocIdsInProject = sinon
  73. .stub()
  74. .callsArgWith(1, null, this.doc_ids)
  75. this.DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub()
  76. this.DocumentManager.getDocAndFlushIfOldWithLock
  77. .withArgs(this.project_id, this.doc_ids[0])
  78. .callsArgWith(2, null, this.doc_lines[0], this.doc_versions[0])
  79. this.DocumentManager.getDocAndFlushIfOldWithLock
  80. .withArgs(this.project_id, this.doc_ids[1])
  81. .callsArgWith(2, null, this.doc_lines[1], this.doc_versions[1])
  82. this.DocumentManager.getDocAndFlushIfOldWithLock
  83. .withArgs(this.project_id, this.doc_ids[2])
  84. .callsArgWith(2, null, this.doc_lines[2], this.doc_versions[2])
  85. return this.ProjectManager.getProjectDocsAndFlushIfOld(
  86. this.project_id,
  87. this.projectStateHash,
  88. this.excludeVersions,
  89. (error, docs) => {
  90. this.callback(error, docs)
  91. return done()
  92. }
  93. )
  94. })
  95. it('should check the project state', function () {
  96. return this.RedisManager.checkOrSetProjectState
  97. .calledWith(this.project_id, this.projectStateHash)
  98. .should.equal(true)
  99. })
  100. it('should get the doc ids in the project', function () {
  101. return this.RedisManager.getDocIdsInProject
  102. .calledWith(this.project_id)
  103. .should.equal(true)
  104. })
  105. it('should call the callback without error', function () {
  106. return this.callback.calledWith(null, this.docs).should.equal(true)
  107. })
  108. return it('should time the execution', function () {
  109. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  110. })
  111. })
  112. describe('when the state does not match', function () {
  113. beforeEach(function (done) {
  114. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  115. this.RedisManager.checkOrSetProjectState = sinon
  116. .stub()
  117. .callsArgWith(2, null, true)
  118. return this.ProjectManager.getProjectDocsAndFlushIfOld(
  119. this.project_id,
  120. this.projectStateHash,
  121. this.excludeVersions,
  122. (error, docs) => {
  123. this.callback(error, docs)
  124. return done()
  125. }
  126. )
  127. })
  128. it('should check the project state', function () {
  129. return this.RedisManager.checkOrSetProjectState
  130. .calledWith(this.project_id, this.projectStateHash)
  131. .should.equal(true)
  132. })
  133. it('should call the callback with an error', function () {
  134. return this.callback
  135. .calledWith(sinon.match.instanceOf(Errors.ProjectStateChangedError))
  136. .should.equal(true)
  137. })
  138. return it('should time the execution', function () {
  139. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  140. })
  141. })
  142. describe('when a doc errors', function () {
  143. beforeEach(function (done) {
  144. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  145. this.RedisManager.checkOrSetProjectState = sinon
  146. .stub()
  147. .callsArgWith(2, null)
  148. this.RedisManager.getDocIdsInProject = sinon
  149. .stub()
  150. .callsArgWith(1, null, this.doc_ids)
  151. this.DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub()
  152. this.DocumentManager.getDocAndFlushIfOldWithLock
  153. .withArgs(this.project_id, 'doc-id-1')
  154. .callsArgWith(2, null, ['test doc content'], this.doc_versions[1])
  155. this.DocumentManager.getDocAndFlushIfOldWithLock
  156. .withArgs(this.project_id, 'doc-id-2')
  157. .callsArgWith(2, (this.error = new Error('oops'))) // trigger an error
  158. return this.ProjectManager.getProjectDocsAndFlushIfOld(
  159. this.project_id,
  160. this.projectStateHash,
  161. this.excludeVersions,
  162. (error, docs) => {
  163. this.callback(error)
  164. return done()
  165. }
  166. )
  167. })
  168. it('should record the error', function () {
  169. return this.logger.error
  170. .calledWith(
  171. { err: this.error, projectId: this.project_id, docId: 'doc-id-2' },
  172. 'error getting project doc lines in getProjectDocsAndFlushIfOld'
  173. )
  174. .should.equal(true)
  175. })
  176. it('should call the callback with an error', function () {
  177. return this.callback
  178. .calledWith(sinon.match.instanceOf(Error))
  179. .should.equal(true)
  180. })
  181. return it('should time the execution', function () {
  182. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  183. })
  184. })
  185. return describe('clearing the project state with clearProjectState', function () {
  186. beforeEach(function (done) {
  187. this.RedisManager.clearProjectState = sinon.stub().callsArg(1)
  188. return this.ProjectManager.clearProjectState(this.project_id, (error) => {
  189. this.callback(error)
  190. return done()
  191. })
  192. })
  193. it('should clear the project state', function () {
  194. return this.RedisManager.clearProjectState
  195. .calledWith(this.project_id)
  196. .should.equal(true)
  197. })
  198. return it('should call the callback', function () {
  199. return this.callback.called.should.equal(true)
  200. })
  201. })
  202. })