CompileManagerTests.js 13 KB

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