getProjectDocsTests.js 7.3 KB

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