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 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. }
  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(
  141. new Errors.ProjectStateChangedError('project state changed')
  142. )
  143. .should.equal(true)
  144. })
  145. return it('should time the execution', function () {
  146. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  147. })
  148. })
  149. describe('when a doc errors', function () {
  150. beforeEach(function (done) {
  151. this.doc_ids = ['doc-id-1', 'doc-id-2', 'doc-id-3']
  152. this.RedisManager.checkOrSetProjectState = sinon
  153. .stub()
  154. .callsArgWith(2, null)
  155. this.RedisManager.getDocIdsInProject = sinon
  156. .stub()
  157. .callsArgWith(1, null, this.doc_ids)
  158. this.DocumentManager.getDocAndFlushIfOldWithLock = sinon.stub()
  159. this.DocumentManager.getDocAndFlushIfOldWithLock
  160. .withArgs(this.project_id, 'doc-id-1')
  161. .callsArgWith(2, null, ['test doc content'], this.doc_versions[1])
  162. this.DocumentManager.getDocAndFlushIfOldWithLock
  163. .withArgs(this.project_id, 'doc-id-2')
  164. .callsArgWith(2, (this.error = new Error('oops'))) // trigger an error
  165. return this.ProjectManager.getProjectDocsAndFlushIfOld(
  166. this.project_id,
  167. this.projectStateHash,
  168. this.excludeVersions,
  169. (error, docs) => {
  170. this.callback(error)
  171. return done()
  172. }
  173. )
  174. })
  175. it('should record the error', function () {
  176. return this.logger.error
  177. .calledWith(
  178. { err: this.error, projectId: this.project_id, docId: 'doc-id-2' },
  179. 'error getting project doc lines in getProjectDocsAndFlushIfOld'
  180. )
  181. .should.equal(true)
  182. })
  183. it('should call the callback with an error', function () {
  184. return this.callback.calledWith(new Error('oops')).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. })