CompileManagerTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. buildId: sinon.match(/[a-f0-9]+-[a-f0-9]+/),
  118. })
  119. .should.equal(true)
  120. })
  121. it('should resolve with the output', function () {
  122. expect(result).to.haveOwnProperty('status', this.status)
  123. expect(result).to.haveOwnProperty('clsiServerId', this.output)
  124. expect(result).to.haveOwnProperty('outputFiles', this.outputFiles)
  125. })
  126. it('should time the compile', function () {
  127. this.timer.done.called.should.equal(true)
  128. })
  129. })
  130. describe('when the project has been recently compiled', function () {
  131. it('should return', async function () {
  132. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = async (
  133. isAutoCompile,
  134. compileGroup
  135. ) => true
  136. this.CompileManager._checkIfRecentlyCompiled = sinon
  137. .stub()
  138. .resolves(true)
  139. const { status } = await this.CompileManager.promises.compile(
  140. this.project_id,
  141. this.user_id,
  142. {}
  143. )
  144. status.should.equal('too-recently-compiled')
  145. })
  146. })
  147. describe('should check the rate limit', function () {
  148. it('should return', async function () {
  149. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = sinon
  150. .stub()
  151. .resolves(false)
  152. const { status } = await this.CompileManager.promises.compile(
  153. this.project_id,
  154. this.user_id,
  155. {}
  156. )
  157. expect(status).to.equal('autocompile-backoff')
  158. })
  159. })
  160. })
  161. describe('getProjectCompileLimits', function () {
  162. beforeEach(async function () {
  163. this.features = {
  164. compileTimeout: (this.timeout = 42),
  165. compileGroup: (this.group = 'priority'),
  166. }
  167. this.ProjectGetter.promises.getProject = sinon
  168. .stub()
  169. .resolves(
  170. (this.project = { owner_ref: (this.owner_id = 'owner-id-123') })
  171. )
  172. this.UserGetter.promises.getUser = sinon
  173. .stub()
  174. .resolves((this.user = { features: this.features, analyticsId: 'abc' }))
  175. try {
  176. const result =
  177. await this.CompileManager.promises.getProjectCompileLimits(
  178. this.project_id
  179. )
  180. this.callback(null, result)
  181. } catch (error) {
  182. this.callback(error)
  183. }
  184. })
  185. it('should look up the owner of the project', function () {
  186. this.ProjectGetter.promises.getProject
  187. .calledWith(this.project_id, { owner_ref: 1 })
  188. .should.equal(true)
  189. })
  190. it("should look up the owner's features", function () {
  191. this.UserGetter.promises.getUser
  192. .calledWith(this.project.owner_ref, {
  193. _id: 1,
  194. alphaProgram: 1,
  195. analyticsId: 1,
  196. betaProgram: 1,
  197. features: 1,
  198. })
  199. .should.equal(true)
  200. })
  201. it('should return the limits', function () {
  202. this.callback
  203. .calledWith(null, {
  204. timeout: this.timeout,
  205. compileGroup: this.group,
  206. compileBackendClass: 'c2d',
  207. ownerAnalyticsId: 'abc',
  208. })
  209. .should.equal(true)
  210. })
  211. })
  212. describe('compileBackendClass', function () {
  213. beforeEach(function () {
  214. this.features = {
  215. compileTimeout: 42,
  216. compileGroup: 'standard',
  217. }
  218. this.ProjectGetter.promises.getProject = sinon
  219. .stub()
  220. .resolves({ owner_ref: 'owner-id-123' })
  221. this.UserGetter.promises.getUser = sinon
  222. .stub()
  223. .resolves({ features: this.features, analyticsId: 'abc' })
  224. })
  225. describe('with priority compile', function () {
  226. beforeEach(function () {
  227. this.features.compileGroup = 'priority'
  228. })
  229. it('should return the default class', async function () {
  230. const { compileBackendClass } =
  231. await this.CompileManager.promises.getProjectCompileLimits(
  232. this.project_id
  233. )
  234. expect(compileBackendClass).to.equal('c2d')
  235. })
  236. })
  237. })
  238. describe('deleteAuxFiles', function () {
  239. let result
  240. beforeEach(async function () {
  241. this.CompileManager.promises.getProjectCompileLimits = sinon
  242. .stub()
  243. .resolves((this.limits = { compileGroup: 'mock-compile-group' }))
  244. this.ClsiManager.promises.deleteAuxFiles = sinon.stub().resolves('test')
  245. result = await this.CompileManager.promises.deleteAuxFiles(
  246. this.project_id,
  247. this.user_id
  248. )
  249. })
  250. it('should look up the compile group to use', function () {
  251. this.CompileManager.promises.getProjectCompileLimits
  252. .calledWith(this.project_id)
  253. .should.equal(true)
  254. })
  255. it('should delete the aux files', function () {
  256. this.ClsiManager.promises.deleteAuxFiles
  257. .calledWith(this.project_id, this.user_id, this.limits)
  258. .should.equal(true)
  259. })
  260. it('should resolve', function () {
  261. expect(result).not.to.be.undefined
  262. })
  263. })
  264. describe('_checkIfRecentlyCompiled', function () {
  265. describe('when the key exists in redis', function () {
  266. let result
  267. beforeEach(async function () {
  268. this.rclient.set = sinon.stub().resolves(null)
  269. result = await this.CompileManager._checkIfRecentlyCompiled(
  270. this.project_id,
  271. this.user_id
  272. )
  273. })
  274. it('should try to set the key', function () {
  275. this.rclient.set
  276. .calledWith(
  277. `compile:${this.project_id}:${this.user_id}`,
  278. true,
  279. 'EX',
  280. this.CompileManager.COMPILE_DELAY,
  281. 'NX'
  282. )
  283. .should.equal(true)
  284. })
  285. it('should resolve with true', function () {
  286. result.should.equal(true)
  287. })
  288. })
  289. describe('when the key does not exist in redis', function () {
  290. let result
  291. beforeEach(async function () {
  292. this.rclient.set = sinon.stub().resolves('OK')
  293. result = await this.CompileManager._checkIfRecentlyCompiled(
  294. this.project_id,
  295. this.user_id
  296. )
  297. })
  298. it('should try to set the key', function () {
  299. this.rclient.set
  300. .calledWith(
  301. `compile:${this.project_id}:${this.user_id}`,
  302. true,
  303. 'EX',
  304. this.CompileManager.COMPILE_DELAY,
  305. 'NX'
  306. )
  307. .should.equal(true)
  308. })
  309. it('should resolve with false', function () {
  310. result.should.equal(false)
  311. })
  312. })
  313. })
  314. describe('_checkIfAutoCompileLimitHasBeenHit', function () {
  315. it('should be able to compile if it is not an autocompile', async function () {
  316. const canCompile =
  317. await this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  318. false,
  319. 'everyone'
  320. )
  321. expect(canCompile).to.equal(true)
  322. })
  323. it('should be able to compile if rate limit has remaining', async function () {
  324. const canCompile =
  325. await this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  326. true,
  327. 'everyone'
  328. )
  329. expect(this.rateLimiter.consume).to.have.been.calledWith('global')
  330. expect(canCompile).to.equal(true)
  331. })
  332. it('should be not able to compile if rate limit has no remianing', async function () {
  333. this.rateLimiter.consume.rejects({ remainingPoints: 0 })
  334. const canCompile =
  335. await this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  336. true,
  337. 'everyone'
  338. )
  339. expect(canCompile).to.equal(false)
  340. })
  341. it('should return false if there is an error in the rate limit', async function () {
  342. this.rateLimiter.consume.rejects(new Error('BOOM!'))
  343. const canCompile =
  344. await this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  345. true,
  346. 'everyone'
  347. )
  348. expect(canCompile).to.equal(false)
  349. })
  350. })
  351. describe('wordCount', function () {
  352. let result
  353. const wordCount = 1
  354. beforeEach(async function () {
  355. this.CompileManager.promises.getProjectCompileLimits = sinon
  356. .stub()
  357. .resolves((this.limits = { compileGroup: 'mock-compile-group' }))
  358. this.ClsiManager.promises.wordCount = sinon.stub().resolves(wordCount)
  359. result = await this.CompileManager.promises.wordCount(
  360. this.project_id,
  361. this.user_id,
  362. false
  363. )
  364. })
  365. it('should look up the compile group to use', function () {
  366. this.CompileManager.promises.getProjectCompileLimits
  367. .calledWith(this.project_id)
  368. .should.equal(true)
  369. })
  370. it('should call wordCount for project', function () {
  371. this.ClsiManager.promises.wordCount
  372. .calledWith(this.project_id, this.user_id, false, this.limits)
  373. .should.equal(true)
  374. })
  375. it('should resolve with the wordCount from the ClsiManager', function () {
  376. expect(result).to.equal(wordCount)
  377. })
  378. })
  379. })