CompileManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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: { clsi: { defaultBackendClass: 'e2' } },
  24. redis: { web: { host: 'localhost', port: 42 } },
  25. rateLimit: { autoCompile: {} },
  26. }),
  27. '../../infrastructure/RedisWrapper': {
  28. client: () => (this.rclient = { auth() {} }),
  29. },
  30. '../Project/ProjectRootDocManager': (this.ProjectRootDocManager = {}),
  31. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  32. '../User/UserGetter': (this.UserGetter = {}),
  33. './ClsiManager': (this.ClsiManager = {}),
  34. '../../infrastructure/RateLimiter': this.RateLimiter,
  35. '@overleaf/metrics': this.Metrics,
  36. '../SplitTests/SplitTestHandler': {
  37. getAssignmentForMongoUser: (this.getAssignmentForMongoUser = sinon
  38. .stub()
  39. .yields(null, {
  40. variant: 'default',
  41. })),
  42. },
  43. },
  44. })
  45. this.project_id = 'mock-project-id-123'
  46. this.user_id = 'mock-user-id-123'
  47. this.callback = sinon.stub()
  48. this.limits = {
  49. timeout: 42,
  50. compileGroup: 'standard',
  51. }
  52. })
  53. describe('compile', function () {
  54. beforeEach(function () {
  55. this.CompileManager._checkIfRecentlyCompiled = sinon
  56. .stub()
  57. .callsArgWith(2, null, false)
  58. this.ProjectRootDocManager.ensureRootDocumentIsSet = sinon
  59. .stub()
  60. .callsArgWith(1, null)
  61. this.CompileManager.getProjectCompileLimits = sinon
  62. .stub()
  63. .callsArgWith(1, null, this.limits)
  64. this.ClsiManager.sendRequest = sinon
  65. .stub()
  66. .callsArgWith(
  67. 3,
  68. null,
  69. (this.status = 'mock-status'),
  70. (this.outputFiles = 'mock output files'),
  71. (this.output = 'mock output')
  72. )
  73. })
  74. describe('succesfully', function () {
  75. beforeEach(function () {
  76. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = (
  77. isAutoCompile,
  78. compileGroup,
  79. cb
  80. ) => cb(null, true)
  81. this.CompileManager.compile(
  82. this.project_id,
  83. this.user_id,
  84. {},
  85. this.callback
  86. )
  87. })
  88. it('should check the project has not been recently compiled', function () {
  89. this.CompileManager._checkIfRecentlyCompiled
  90. .calledWith(this.project_id, this.user_id)
  91. .should.equal(true)
  92. })
  93. it('should ensure that the root document is set', function () {
  94. this.ProjectRootDocManager.ensureRootDocumentIsSet
  95. .calledWith(this.project_id)
  96. .should.equal(true)
  97. })
  98. it('should get the project compile limits', function () {
  99. this.CompileManager.getProjectCompileLimits
  100. .calledWith(this.project_id)
  101. .should.equal(true)
  102. })
  103. it('should run the compile with the compile limits', function () {
  104. this.ClsiManager.sendRequest
  105. .calledWith(this.project_id, this.user_id, {
  106. timeout: this.limits.timeout,
  107. compileGroup: 'standard',
  108. })
  109. .should.equal(true)
  110. })
  111. it('should call the callback with the output', function () {
  112. this.callback
  113. .calledWith(null, this.status, this.outputFiles, this.output)
  114. .should.equal(true)
  115. })
  116. it('should time the compile', function () {
  117. this.timer.done.called.should.equal(true)
  118. })
  119. })
  120. describe('when the project has been recently compiled', function () {
  121. it('should return', function (done) {
  122. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = (
  123. isAutoCompile,
  124. compileGroup,
  125. cb
  126. ) => cb(null, true)
  127. this.CompileManager._checkIfRecentlyCompiled = sinon
  128. .stub()
  129. .callsArgWith(2, null, true)
  130. this.CompileManager.compile(
  131. this.project_id,
  132. this.user_id,
  133. {},
  134. (err, status) => {
  135. if (err) {
  136. return done(err)
  137. }
  138. status.should.equal('too-recently-compiled')
  139. done()
  140. }
  141. )
  142. })
  143. })
  144. describe('should check the rate limit', function () {
  145. it('should return', function (done) {
  146. this.CompileManager._checkIfAutoCompileLimitHasBeenHit = sinon
  147. .stub()
  148. .callsArgWith(2, null, false)
  149. this.CompileManager.compile(
  150. this.project_id,
  151. this.user_id,
  152. {},
  153. (err, status) => {
  154. if (err) {
  155. return done(err)
  156. }
  157. status.should.equal('autocompile-backoff')
  158. done()
  159. }
  160. )
  161. })
  162. })
  163. })
  164. describe('getProjectCompileLimits', function () {
  165. beforeEach(function () {
  166. this.features = {
  167. compileTimeout: (this.timeout = 42),
  168. compileGroup: (this.group = 'priority'),
  169. }
  170. this.ProjectGetter.getProject = sinon
  171. .stub()
  172. .callsArgWith(
  173. 2,
  174. null,
  175. (this.project = { owner_ref: (this.owner_id = 'owner-id-123') })
  176. )
  177. this.UserGetter.getUser = sinon
  178. .stub()
  179. .callsArgWith(
  180. 2,
  181. null,
  182. (this.user = { features: this.features, analyticsId: 'abc' })
  183. )
  184. this.CompileManager.getProjectCompileLimits(
  185. this.project_id,
  186. this.callback
  187. )
  188. })
  189. it('should look up the owner of the project', function () {
  190. this.ProjectGetter.getProject
  191. .calledWith(this.project_id, { owner_ref: 1 })
  192. .should.equal(true)
  193. })
  194. it("should look up the owner's features", function () {
  195. this.UserGetter.getUser
  196. .calledWith(this.project.owner_ref, {
  197. _id: 1,
  198. alphaProgram: 1,
  199. analyticsId: 1,
  200. betaProgram: 1,
  201. features: 1,
  202. splitTests: 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: 'e2',
  212. ownerAnalyticsId: 'abc',
  213. showFasterCompilesFeedbackUI: false,
  214. })
  215. .should.equal(true)
  216. })
  217. })
  218. describe('compileBackendClass', function () {
  219. beforeEach(function () {
  220. this.features = {
  221. compileTimeout: 42,
  222. compileGroup: 'standard',
  223. }
  224. this.ProjectGetter.getProject = sinon
  225. .stub()
  226. .yields(null, { owner_ref: 'owner-id-123' })
  227. this.UserGetter.getUser = sinon
  228. .stub()
  229. .yields(null, { features: this.features, analyticsId: 'abc' })
  230. })
  231. describe('with standard compile', function () {
  232. beforeEach(function () {
  233. this.features.compileGroup = 'standard'
  234. })
  235. it('should return the default class and disable ui', function (done) {
  236. this.CompileManager.getProjectCompileLimits(
  237. this.project_id,
  238. (err, { compileBackendClass, showFasterCompilesFeedbackUI }) => {
  239. if (err) return done(err)
  240. expect(compileBackendClass).to.equal('e2')
  241. expect(showFasterCompilesFeedbackUI).to.equal(false)
  242. done()
  243. }
  244. )
  245. })
  246. })
  247. describe('with priority compile', function () {
  248. beforeEach(function () {
  249. this.features.compileGroup = 'priority'
  250. })
  251. describe('split test not active', function () {
  252. beforeEach(function () {
  253. this.getAssignmentForMongoUser.yields(null, {
  254. analytics: { segmentation: {} },
  255. variant: 'default',
  256. })
  257. })
  258. it('should return the default class and disable ui', function (done) {
  259. this.CompileManager.getProjectCompileLimits(
  260. this.project_id,
  261. (err, { compileBackendClass, showFasterCompilesFeedbackUI }) => {
  262. if (err) return done(err)
  263. expect(compileBackendClass).to.equal('e2')
  264. expect(showFasterCompilesFeedbackUI).to.equal(false)
  265. done()
  266. }
  267. )
  268. })
  269. })
  270. describe('split test active', function () {
  271. describe('default variant', function () {
  272. beforeEach(function () {
  273. this.getAssignmentForMongoUser.yields(null, {
  274. analytics: { segmentation: { splitTest: 'foo' } },
  275. variant: 'default',
  276. })
  277. })
  278. it('should return the default class and enable ui', function (done) {
  279. this.CompileManager.getProjectCompileLimits(
  280. this.project_id,
  281. (err, { compileBackendClass, showFasterCompilesFeedbackUI }) => {
  282. if (err) return done(err)
  283. expect(compileBackendClass).to.equal('e2')
  284. expect(showFasterCompilesFeedbackUI).to.equal(true)
  285. done()
  286. }
  287. )
  288. })
  289. })
  290. describe('c2d variant', function () {
  291. beforeEach(function () {
  292. this.getAssignmentForMongoUser.yields(null, {
  293. analytics: { segmentation: { splitTest: 'foo' } },
  294. variant: 'c2d',
  295. })
  296. })
  297. it('should return the c2d class and enable ui', function (done) {
  298. this.CompileManager.getProjectCompileLimits(
  299. this.project_id,
  300. (err, { compileBackendClass, showFasterCompilesFeedbackUI }) => {
  301. if (err) return done(err)
  302. expect(compileBackendClass).to.equal('c2d')
  303. expect(showFasterCompilesFeedbackUI).to.equal(true)
  304. done()
  305. }
  306. )
  307. })
  308. })
  309. })
  310. })
  311. })
  312. describe('deleteAuxFiles', function () {
  313. beforeEach(function () {
  314. this.CompileManager.getProjectCompileLimits = sinon
  315. .stub()
  316. .callsArgWith(
  317. 1,
  318. null,
  319. (this.limits = { compileGroup: 'mock-compile-group' })
  320. )
  321. this.ClsiManager.deleteAuxFiles = sinon.stub().callsArg(3)
  322. this.CompileManager.deleteAuxFiles(
  323. this.project_id,
  324. this.user_id,
  325. this.callback
  326. )
  327. })
  328. it('should look up the compile group to use', function () {
  329. this.CompileManager.getProjectCompileLimits
  330. .calledWith(this.project_id)
  331. .should.equal(true)
  332. })
  333. it('should delete the aux files', function () {
  334. this.ClsiManager.deleteAuxFiles
  335. .calledWith(this.project_id, this.user_id, this.limits)
  336. .should.equal(true)
  337. })
  338. it('should call the callback', function () {
  339. this.callback.called.should.equal(true)
  340. })
  341. })
  342. describe('_checkIfRecentlyCompiled', function () {
  343. describe('when the key exists in redis', function () {
  344. beforeEach(function () {
  345. this.rclient.set = sinon.stub().callsArgWith(5, null, null)
  346. this.CompileManager._checkIfRecentlyCompiled(
  347. this.project_id,
  348. this.user_id,
  349. this.callback
  350. )
  351. })
  352. it('should try to set the key', function () {
  353. this.rclient.set
  354. .calledWith(
  355. `compile:${this.project_id}:${this.user_id}`,
  356. true,
  357. 'EX',
  358. this.CompileManager.COMPILE_DELAY,
  359. 'NX'
  360. )
  361. .should.equal(true)
  362. })
  363. it('should call the callback with true', function () {
  364. this.callback.calledWith(null, true).should.equal(true)
  365. })
  366. })
  367. describe('when the key does not exist in redis', function () {
  368. beforeEach(function () {
  369. this.rclient.set = sinon.stub().callsArgWith(5, null, 'OK')
  370. this.CompileManager._checkIfRecentlyCompiled(
  371. this.project_id,
  372. this.user_id,
  373. this.callback
  374. )
  375. })
  376. it('should try to set the key', function () {
  377. this.rclient.set
  378. .calledWith(
  379. `compile:${this.project_id}:${this.user_id}`,
  380. true,
  381. 'EX',
  382. this.CompileManager.COMPILE_DELAY,
  383. 'NX'
  384. )
  385. .should.equal(true)
  386. })
  387. it('should call the callback with false', function () {
  388. this.callback.calledWith(null, false).should.equal(true)
  389. })
  390. })
  391. })
  392. describe('_checkIfAutoCompileLimitHasBeenHit', function () {
  393. it('should be able to compile if it is not an autocompile', function (done) {
  394. this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  395. false,
  396. 'everyone',
  397. (err, canCompile) => {
  398. if (err) {
  399. return done(err)
  400. }
  401. canCompile.should.equal(true)
  402. done()
  403. }
  404. )
  405. })
  406. it('should be able to compile if rate limit has remaining', function (done) {
  407. this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  408. true,
  409. 'everyone',
  410. (err, canCompile) => {
  411. if (err) {
  412. return done(err)
  413. }
  414. expect(this.rateLimiter.consume).to.have.been.calledWith('global')
  415. canCompile.should.equal(true)
  416. done()
  417. }
  418. )
  419. })
  420. it('should be not able to compile if rate limit has no remianing', function (done) {
  421. this.rateLimiter.consume.rejects({ remainingPoints: 0 })
  422. this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  423. true,
  424. 'everyone',
  425. (err, canCompile) => {
  426. if (err) {
  427. return done(err)
  428. }
  429. canCompile.should.equal(false)
  430. done()
  431. }
  432. )
  433. })
  434. it('should return false if there is an error in the rate limit', function (done) {
  435. this.rateLimiter.consume.rejects(new Error('BOOM!'))
  436. this.CompileManager._checkIfAutoCompileLimitHasBeenHit(
  437. true,
  438. 'everyone',
  439. (err, canCompile) => {
  440. if (err) {
  441. return done(err)
  442. }
  443. canCompile.should.equal(false)
  444. done()
  445. }
  446. )
  447. })
  448. })
  449. describe('wordCount', function () {
  450. beforeEach(function () {
  451. this.CompileManager.getProjectCompileLimits = sinon
  452. .stub()
  453. .callsArgWith(
  454. 1,
  455. null,
  456. (this.limits = { compileGroup: 'mock-compile-group' })
  457. )
  458. this.ClsiManager.wordCount = sinon.stub().callsArg(4)
  459. this.CompileManager.wordCount(
  460. this.project_id,
  461. this.user_id,
  462. false,
  463. this.callback
  464. )
  465. })
  466. it('should look up the compile group to use', function () {
  467. this.CompileManager.getProjectCompileLimits
  468. .calledWith(this.project_id)
  469. .should.equal(true)
  470. })
  471. it('should call wordCount for project', function () {
  472. this.ClsiManager.wordCount
  473. .calledWith(this.project_id, this.user_id, false, this.limits)
  474. .should.equal(true)
  475. })
  476. it('should call the callback', function () {
  477. this.callback.called.should.equal(true)
  478. })
  479. })
  480. })