getProjectDocsTests.js 7.3 KB

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