CompileManagerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS206: Consider reworking classes to avoid initClass
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const sinon = require('sinon')
  16. const chai = require('chai')
  17. const should = chai.should()
  18. const { expect } = chai
  19. const modulePath = '../../../../app/src/Features/Compile/CompileManager.js'
  20. const { assert } = require('chai')
  21. const SandboxedModule = require('sandboxed-module')
  22. describe('CompileManager', function() {
  23. beforeEach(function() {
  24. let Timer
  25. this.rateLimitGetStub = sinon.stub()
  26. const { rateLimitGetStub } = this
  27. this.ratelimiter = { addCount: sinon.stub() }
  28. this.CompileManager = SandboxedModule.require(modulePath, {
  29. globals: {
  30. console: console
  31. },
  32. requires: {
  33. 'settings-sharelatex': (this.settings = {
  34. redis: { web: { host: 'localhost', port: 42 } }
  35. }),
  36. '../../infrastructure/RedisWrapper': {
  37. client: () => {
  38. return (this.rclient = { auth() {} })
  39. }
  40. },
  41. '../Project/ProjectRootDocManager': (this.ProjectRootDocManager = {}),
  42. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  43. '../User/UserGetter': (this.UserGetter = {}),
  44. './ClsiManager': (this.ClsiManager = {}),
  45. '../../infrastructure/RateLimiter': this.ratelimiter,
  46. 'metrics-sharelatex': (this.Metrics = {
  47. Timer: (Timer = (function() {
  48. Timer = class Timer {
  49. static initClass() {
  50. this.prototype.done = sinon.stub()
  51. }
  52. }
  53. Timer.initClass()
  54. return Timer
  55. })()),
  56. inc: sinon.stub()
  57. }),
  58. 'logger-sharelatex': (this.logger = {
  59. log: sinon.stub(),
  60. warn: sinon.stub()
  61. })
  62. }
  63. })
  64. this.project_id = 'mock-project-id-123'
  65. this.user_id = 'mock-user-id-123'
  66. this.callback = sinon.stub()
  67. return (this.limits = {
  68. timeout: 42
  69. })
  70. })
  71. describe('compile', function() {
  72. beforeEach(function() {
  73. this.CompileManager._checkIfRecentlyCompiled = sinon
  74. .stub()
  75. .callsArgWith(2, null, false)
  76. this.ProjectRootDocManager.ensureRootDocumentIsSet = sinon
  77. .stub()
  78. .callsArgWith(1, null)
  79. this.CompileManager.getProjectCompileLimits = sinon
  80. .stub()
  81. .callsArgWith(1, null, this.limits)
  82. return (this.ClsiManager.sendRequest = sinon
  83. .stub()
  84. .callsArgWith(
  85. 3,
  86. null,
  87. (this.status = 'mock-status'),
  88. (this.outputFiles = 'mock output files'),
  89. (this.output = 'mock output')
  90. ))
  91. })
  92. describe('succesfully', function() {
  93. beforeEach(function() {
  94. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = (
  95. isAutoCompile,
  96. compileGroup,
  97. cb
  98. ) => cb(null, true)
  99. return this.CompileManager.compile(
  100. this.project_id,
  101. this.user_id,
  102. {},
  103. this.callback
  104. )
  105. })
  106. it('should check the project has not been recently compiled', function() {
  107. return this.CompileManager._checkIfRecentlyCompiled
  108. .calledWith(this.project_id, this.user_id)
  109. .should.equal(true)
  110. })
  111. it('should ensure that the root document is set', function() {
  112. return this.ProjectRootDocManager.ensureRootDocumentIsSet
  113. .calledWith(this.project_id)
  114. .should.equal(true)
  115. })
  116. it('should get the project compile limits', function() {
  117. return this.CompileManager.getProjectCompileLimits
  118. .calledWith(this.project_id)
  119. .should.equal(true)
  120. })
  121. it('should run the compile with the compile limits', function() {
  122. return this.ClsiManager.sendRequest
  123. .calledWith(this.project_id, this.user_id, {
  124. timeout: this.limits.timeout
  125. })
  126. .should.equal(true)
  127. })
  128. it('should call the callback with the output', function() {
  129. return this.callback
  130. .calledWith(null, this.status, this.outputFiles, this.output)
  131. .should.equal(true)
  132. })
  133. it('should time the compile', function() {
  134. return this.Metrics.Timer.prototype.done.called.should.equal(true)
  135. })
  136. it('should log out the compile', function() {
  137. return this.logger.log
  138. .calledWith(
  139. { project_id: this.project_id, user_id: this.user_id },
  140. 'compiling project'
  141. )
  142. .should.equal(true)
  143. })
  144. })
  145. describe('when the project has been recently compiled', () =>
  146. it('should return', function(done) {
  147. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = (
  148. isAutoCompile,
  149. compileGroup,
  150. cb
  151. ) => cb(null, true)
  152. this.CompileManager._checkIfRecentlyCompiled = sinon
  153. .stub()
  154. .callsArgWith(2, null, true)
  155. return this.CompileManager.compile(
  156. this.project_id,
  157. this.user_id,
  158. {},
  159. function(err, status) {
  160. status.should.equal('too-recently-compiled')
  161. return done()
  162. }
  163. )
  164. }))
  165. describe('should check the rate limit', () =>
  166. it('should return', function(done) {
  167. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = sinon
  168. .stub()
  169. .callsArgWith(2, null, false)
  170. return this.CompileManager.compile(
  171. this.project_id,
  172. this.user_id,
  173. {},
  174. function(err, status) {
  175. status.should.equal('autocompile-backoff')
  176. return done()
  177. }
  178. )
  179. }))
  180. })
  181. describe('getProjectCompileLimits', function() {
  182. beforeEach(function() {
  183. this.features = {
  184. compileTimeout: (this.timeout = 42),
  185. compileGroup: (this.group = 'priority')
  186. }
  187. this.ProjectGetter.getProject = sinon
  188. .stub()
  189. .callsArgWith(
  190. 2,
  191. null,
  192. (this.project = { owner_ref: (this.owner_id = 'owner-id-123') })
  193. )
  194. this.UserGetter.getUser = sinon
  195. .stub()
  196. .callsArgWith(2, null, (this.user = { features: this.features }))
  197. return this.CompileManager.getProjectCompileLimits(
  198. this.project_id,
  199. this.callback
  200. )
  201. })
  202. it('should look up the owner of the project', function() {
  203. return this.ProjectGetter.getProject
  204. .calledWith(this.project_id, { owner_ref: 1 })
  205. .should.equal(true)
  206. })
  207. it("should look up the owner's features", function() {
  208. return this.UserGetter.getUser
  209. .calledWith(this.project.owner_ref, { features: 1 })
  210. .should.equal(true)
  211. })
  212. it('should return the limits', function() {
  213. return this.callback
  214. .calledWith(null, {
  215. timeout: this.timeout,
  216. compileGroup: this.group
  217. })
  218. .should.equal(true)
  219. })
  220. })
  221. describe('deleteAuxFiles', function() {
  222. beforeEach(function() {
  223. this.CompileManager.getProjectCompileLimits = sinon
  224. .stub()
  225. .callsArgWith(
  226. 1,
  227. null,
  228. (this.limits = { compileGroup: 'mock-compile-group' })
  229. )
  230. this.ClsiManager.deleteAuxFiles = sinon.stub().callsArg(3)
  231. return this.CompileManager.deleteAuxFiles(
  232. this.project_id,
  233. this.user_id,
  234. this.callback
  235. )
  236. })
  237. it('should look up the compile group to use', function() {
  238. return this.CompileManager.getProjectCompileLimits
  239. .calledWith(this.project_id)
  240. .should.equal(true)
  241. })
  242. it('should delete the aux files', function() {
  243. return this.ClsiManager.deleteAuxFiles
  244. .calledWith(this.project_id, this.user_id, this.limits)
  245. .should.equal(true)
  246. })
  247. it('should call the callback', function() {
  248. return this.callback.called.should.equal(true)
  249. })
  250. })
  251. describe('_checkIfRecentlyCompiled', function() {
  252. describe('when the key exists in redis', function() {
  253. beforeEach(function() {
  254. this.rclient.set = sinon.stub().callsArgWith(5, null, null)
  255. return this.CompileManager._checkIfRecentlyCompiled(
  256. this.project_id,
  257. this.user_id,
  258. this.callback
  259. )
  260. })
  261. it('should try to set the key', function() {
  262. return this.rclient.set
  263. .calledWith(
  264. `compile:${this.project_id}:${this.user_id}`,
  265. true,
  266. 'EX',
  267. this.CompileManager.COMPILE_DELAY,
  268. 'NX'
  269. )
  270. .should.equal(true)
  271. })
  272. it('should call the callback with true', function() {
  273. return this.callback.calledWith(null, true).should.equal(true)
  274. })
  275. })
  276. describe('when the key does not exist in redis', function() {
  277. beforeEach(function() {
  278. this.rclient.set = sinon.stub().callsArgWith(5, null, 'OK')
  279. return this.CompileManager._checkIfRecentlyCompiled(
  280. this.project_id,
  281. this.user_id,
  282. this.callback
  283. )
  284. })
  285. it('should try to set the key', function() {
  286. return this.rclient.set
  287. .calledWith(
  288. `compile:${this.project_id}:${this.user_id}`,
  289. true,
  290. 'EX',
  291. this.CompileManager.COMPILE_DELAY,
  292. 'NX'
  293. )
  294. .should.equal(true)
  295. })
  296. it('should call the callback with false', function() {
  297. return this.callback.calledWith(null, false).should.equal(true)
  298. })
  299. })
  300. })
  301. describe('_checkIfAutoCompileLimitHasBeenHit', function() {
  302. it('should be able to compile if it is not an autocompile', function(done) {
  303. this.ratelimiter.addCount.callsArgWith(2, null, true)
  304. return this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  305. false,
  306. 'everyone',
  307. (err, canCompile) => {
  308. canCompile.should.equal(true)
  309. return done()
  310. }
  311. )
  312. })
  313. it('should be able to compile if rate limit has remianing', function(done) {
  314. this.ratelimiter.addCount.callsArgWith(1, null, true)
  315. return this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  316. true,
  317. 'everyone',
  318. (err, canCompile) => {
  319. const args = this.ratelimiter.addCount.args[0][0]
  320. args.throttle.should.equal(25)
  321. args.subjectName.should.equal('everyone')
  322. args.timeInterval.should.equal(20)
  323. args.endpointName.should.equal('auto_compile')
  324. canCompile.should.equal(true)
  325. return done()
  326. }
  327. )
  328. })
  329. it('should be not able to compile if rate limit has no remianing', function(done) {
  330. this.ratelimiter.addCount.callsArgWith(1, null, false)
  331. return this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  332. true,
  333. 'everyone',
  334. (err, canCompile) => {
  335. canCompile.should.equal(false)
  336. return done()
  337. }
  338. )
  339. })
  340. it('should return false if there is an error in the rate limit', function(done) {
  341. this.ratelimiter.addCount.callsArgWith(1, 'error')
  342. return this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  343. true,
  344. 'everyone',
  345. (err, canCompile) => {
  346. canCompile.should.equal(false)
  347. return done()
  348. }
  349. )
  350. })
  351. })
  352. describe('wordCount', function() {
  353. beforeEach(function() {
  354. this.CompileManager.getProjectCompileLimits = sinon
  355. .stub()
  356. .callsArgWith(
  357. 1,
  358. null,
  359. (this.limits = { compileGroup: 'mock-compile-group' })
  360. )
  361. this.ClsiManager.wordCount = sinon.stub().callsArg(4)
  362. return this.CompileManager.wordCount(
  363. this.project_id,
  364. this.user_id,
  365. false,
  366. this.callback
  367. )
  368. })
  369. it('should look up the compile group to use', function() {
  370. return this.CompileManager.getProjectCompileLimits
  371. .calledWith(this.project_id)
  372. .should.equal(true)
  373. })
  374. it('should call wordCount for project', function() {
  375. return this.ClsiManager.wordCount
  376. .calledWith(this.project_id, this.user_id, false, this.limits)
  377. .should.equal(true)
  378. })
  379. it('should call the callback', function() {
  380. return this.callback.called.should.equal(true)
  381. })
  382. })
  383. })