CompileControllerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath = require('node:path').join(
  4. __dirname,
  5. '../../../app/js/CompileController'
  6. )
  7. const Errors = require('../../../app/js/Errors')
  8. describe('CompileController', function () {
  9. beforeEach(function () {
  10. this.buildId = 'build-id-123'
  11. this.CompileController = SandboxedModule.require(modulePath, {
  12. requires: {
  13. './CompileManager': (this.CompileManager = {}),
  14. './RequestParser': (this.RequestParser = {}),
  15. '@overleaf/settings': (this.Settings = {
  16. apis: {
  17. clsi: {
  18. url: 'http://clsi.example.com',
  19. outputUrlPrefix: '/zone/b',
  20. downloadHost: 'http://localhost:3013',
  21. },
  22. clsiCache: {
  23. enabled: false,
  24. url: 'http://localhost:3044',
  25. },
  26. },
  27. }),
  28. '@overleaf/metrics': {
  29. Timer: sinon.stub().returns({ done: sinon.stub() }),
  30. },
  31. './ProjectPersistenceManager': (this.ProjectPersistenceManager = {}),
  32. './CLSICacheHandler': {
  33. notifyCLSICacheAboutBuild: sinon.stub(),
  34. downloadLatestCompileCache: sinon.stub().resolves(),
  35. downloadOutputDotSynctexFromCompileCache: sinon.stub().resolves(),
  36. },
  37. './Errors': (this.Erros = Errors),
  38. },
  39. })
  40. this.Settings.externalUrl = 'http://www.example.com'
  41. this.req = {}
  42. this.res = {}
  43. this.next = sinon.stub()
  44. })
  45. describe('compile', function () {
  46. beforeEach(function () {
  47. this.req.body = {
  48. compile: 'mock-body',
  49. }
  50. this.req.params = { project_id: (this.project_id = 'project-id-123') }
  51. this.request = {
  52. compile: 'mock-parsed-request',
  53. }
  54. this.request_with_project_id = {
  55. compile: this.request.compile,
  56. project_id: this.project_id,
  57. }
  58. this.output_files = [
  59. {
  60. path: 'output.pdf',
  61. type: 'pdf',
  62. size: 1337,
  63. build: 1234,
  64. },
  65. {
  66. path: 'output.log',
  67. type: 'log',
  68. build: 1234,
  69. },
  70. ]
  71. this.RequestParser.parse = sinon
  72. .stub()
  73. .callsArgWith(1, null, this.request)
  74. this.ProjectPersistenceManager.markProjectAsJustAccessed = sinon
  75. .stub()
  76. .callsArg(1)
  77. this.stats = { foo: 1 }
  78. this.timings = { bar: 2 }
  79. this.res.status = sinon.stub().returnsThis()
  80. this.res.send = sinon.stub()
  81. this.CompileManager.doCompileWithLock = sinon
  82. .stub()
  83. .callsFake((_req, stats, timings, cb) => {
  84. Object.assign(stats, this.stats)
  85. Object.assign(timings, this.timings)
  86. cb(null, {
  87. outputFiles: this.output_files,
  88. buildId: this.buildId,
  89. })
  90. })
  91. })
  92. describe('successfully', function () {
  93. beforeEach(function () {
  94. this.CompileController.compile(this.req, this.res)
  95. })
  96. it('should parse the request', function () {
  97. this.RequestParser.parse.calledWith(this.req.body).should.equal(true)
  98. })
  99. it('should run the compile for the specified project', function () {
  100. this.CompileManager.doCompileWithLock
  101. .calledWith(this.request_with_project_id)
  102. .should.equal(true)
  103. })
  104. it('should mark the project as accessed', function () {
  105. this.ProjectPersistenceManager.markProjectAsJustAccessed
  106. .calledWith(this.project_id)
  107. .should.equal(true)
  108. })
  109. it('should return the JSON response', function () {
  110. this.res.status.calledWith(200).should.equal(true)
  111. this.res.send
  112. .calledWith({
  113. compile: {
  114. status: 'success',
  115. error: null,
  116. stats: this.stats,
  117. timings: this.timings,
  118. buildId: this.buildId,
  119. outputUrlPrefix: '/zone/b',
  120. outputFiles: this.output_files.map(file => ({
  121. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  122. ...file,
  123. })),
  124. clsiCacheShard: undefined,
  125. },
  126. })
  127. .should.equal(true)
  128. })
  129. })
  130. describe('without a outputUrlPrefix', function () {
  131. beforeEach(function () {
  132. this.Settings.apis.clsi.outputUrlPrefix = ''
  133. this.CompileController.compile(this.req, this.res)
  134. })
  135. it('should return the JSON response with empty outputUrlPrefix', function () {
  136. this.res.status.calledWith(200).should.equal(true)
  137. this.res.send
  138. .calledWith({
  139. compile: {
  140. status: 'success',
  141. error: null,
  142. stats: this.stats,
  143. timings: this.timings,
  144. buildId: this.buildId,
  145. outputUrlPrefix: '',
  146. outputFiles: this.output_files.map(file => ({
  147. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  148. ...file,
  149. })),
  150. clsiCacheShard: undefined,
  151. },
  152. })
  153. .should.equal(true)
  154. })
  155. })
  156. describe('with user provided fake_output.pdf', function () {
  157. beforeEach(function () {
  158. this.output_files = [
  159. {
  160. path: 'fake_output.pdf',
  161. type: 'pdf',
  162. build: 1234,
  163. },
  164. {
  165. path: 'output.log',
  166. type: 'log',
  167. build: 1234,
  168. },
  169. ]
  170. this.CompileManager.doCompileWithLock = sinon
  171. .stub()
  172. .callsFake((_req, stats, timings, cb) => {
  173. Object.assign(stats, this.stats)
  174. Object.assign(timings, this.timings)
  175. cb(null, {
  176. outputFiles: this.output_files,
  177. buildId: this.buildId,
  178. })
  179. })
  180. this.CompileController.compile(this.req, this.res)
  181. })
  182. it('should return the JSON response with status failure', function () {
  183. this.res.status.calledWith(200).should.equal(true)
  184. this.res.send.should.have.been.calledWith({
  185. compile: {
  186. status: 'failure',
  187. error: null,
  188. stats: this.stats,
  189. timings: this.timings,
  190. outputUrlPrefix: '/zone/b',
  191. buildId: this.buildId,
  192. outputFiles: this.output_files.map(file => ({
  193. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  194. ...file,
  195. })),
  196. clsiCacheShard: undefined,
  197. },
  198. })
  199. })
  200. })
  201. describe('with an empty output.pdf', function () {
  202. beforeEach(function () {
  203. this.output_files = [
  204. {
  205. path: 'output.pdf',
  206. type: 'pdf',
  207. size: 0,
  208. build: 1234,
  209. },
  210. {
  211. path: 'output.log',
  212. type: 'log',
  213. build: 1234,
  214. },
  215. ]
  216. this.CompileManager.doCompileWithLock = sinon
  217. .stub()
  218. .callsFake((_req, stats, timings, cb) => {
  219. Object.assign(stats, this.stats)
  220. Object.assign(timings, this.timings)
  221. cb(null, {
  222. outputFiles: this.output_files,
  223. buildId: this.buildId,
  224. })
  225. })
  226. this.CompileController.compile(this.req, this.res)
  227. })
  228. it('should return the JSON response with status failure', function () {
  229. this.res.status.calledWith(200).should.equal(true)
  230. this.res.send.should.have.been.calledWith({
  231. compile: {
  232. status: 'failure',
  233. error: null,
  234. stats: this.stats,
  235. buildId: this.buildId,
  236. timings: this.timings,
  237. outputUrlPrefix: '/zone/b',
  238. outputFiles: this.output_files.map(file => ({
  239. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  240. ...file,
  241. })),
  242. clsiCacheShard: undefined,
  243. },
  244. })
  245. })
  246. })
  247. describe('with an error', function () {
  248. beforeEach(function () {
  249. const error = new Error((this.message = 'error message'))
  250. error.buildId = this.buildId
  251. this.CompileManager.doCompileWithLock = sinon
  252. .stub()
  253. .callsFake((_req, stats, timings, cb) => {
  254. Object.assign(stats, this.stats)
  255. Object.assign(timings, this.timings)
  256. cb(error)
  257. })
  258. this.CompileController.compile(this.req, this.res)
  259. })
  260. it('should return the JSON response with the error', function () {
  261. this.res.status.calledWith(500).should.equal(true)
  262. this.res.send
  263. .calledWith({
  264. compile: {
  265. status: 'error',
  266. error: this.message,
  267. outputUrlPrefix: '/zone/b',
  268. outputFiles: [],
  269. buildId: this.buildId,
  270. stats: this.stats,
  271. timings: this.timings,
  272. clsiCacheShard: undefined,
  273. },
  274. })
  275. .should.equal(true)
  276. })
  277. })
  278. describe('with too many compile requests error', function () {
  279. beforeEach(function () {
  280. const error = new Errors.TooManyCompileRequestsError(
  281. 'too many concurrent compile requests'
  282. )
  283. this.CompileManager.doCompileWithLock = sinon
  284. .stub()
  285. .callsFake((_req, stats, timings, cb) => {
  286. Object.assign(stats, this.stats)
  287. Object.assign(timings, this.timings)
  288. cb(error)
  289. })
  290. this.CompileController.compile(this.req, this.res)
  291. })
  292. it('should return the JSON response with the error', function () {
  293. this.res.status.calledWith(503).should.equal(true)
  294. this.res.send
  295. .calledWith({
  296. compile: {
  297. status: 'unavailable',
  298. error: 'too many concurrent compile requests',
  299. outputUrlPrefix: '/zone/b',
  300. outputFiles: [],
  301. stats: this.stats,
  302. timings: this.timings,
  303. // JSON.stringify will omit these undefined values
  304. buildId: undefined,
  305. clsiCacheShard: undefined,
  306. },
  307. })
  308. .should.equal(true)
  309. })
  310. })
  311. describe('when the request times out', function () {
  312. beforeEach(function () {
  313. this.error = new Error((this.message = 'container timed out'))
  314. this.error.timedout = true
  315. this.CompileManager.doCompileWithLock = sinon
  316. .stub()
  317. .callsFake((_req, stats, timings, cb) => {
  318. Object.assign(stats, this.stats)
  319. Object.assign(timings, this.timings)
  320. cb(this.error)
  321. })
  322. this.CompileController.compile(this.req, this.res)
  323. })
  324. it('should return the JSON response with the timeout status', function () {
  325. this.res.status.calledWith(200).should.equal(true)
  326. this.res.send
  327. .calledWith({
  328. compile: {
  329. status: 'timedout',
  330. error: this.message,
  331. outputUrlPrefix: '/zone/b',
  332. outputFiles: [],
  333. stats: this.stats,
  334. timings: this.timings,
  335. // JSON.stringify will omit these undefined values
  336. buildId: undefined,
  337. clsiCacheShard: undefined,
  338. },
  339. })
  340. .should.equal(true)
  341. })
  342. })
  343. describe('when the request returns no output files', function () {
  344. beforeEach(function () {
  345. this.CompileManager.doCompileWithLock = sinon
  346. .stub()
  347. .callsFake((_req, stats, timings, cb) => {
  348. Object.assign(stats, this.stats)
  349. Object.assign(timings, this.timings)
  350. cb(null, {})
  351. })
  352. this.CompileController.compile(this.req, this.res)
  353. })
  354. it('should return the JSON response with the failure status', function () {
  355. this.res.status.calledWith(200).should.equal(true)
  356. this.res.send
  357. .calledWith({
  358. compile: {
  359. error: null,
  360. status: 'failure',
  361. outputUrlPrefix: '/zone/b',
  362. outputFiles: [],
  363. stats: this.stats,
  364. timings: this.timings,
  365. // JSON.stringify will omit these undefined values
  366. buildId: undefined,
  367. clsiCacheShard: undefined,
  368. },
  369. })
  370. .should.equal(true)
  371. })
  372. })
  373. })
  374. describe('syncFromCode', function () {
  375. beforeEach(function () {
  376. this.file = 'main.tex'
  377. this.line = 42
  378. this.column = 5
  379. this.project_id = 'mock-project-id'
  380. this.req.params = { project_id: this.project_id }
  381. this.req.query = {
  382. file: this.file,
  383. line: this.line.toString(),
  384. column: this.column.toString(),
  385. }
  386. this.res.json = sinon.stub()
  387. this.CompileManager.syncFromCode = sinon
  388. .stub()
  389. .yields(null, (this.pdfPositions = ['mock-positions']))
  390. this.CompileController.syncFromCode(this.req, this.res, this.next)
  391. })
  392. it('should find the corresponding location in the PDF', function () {
  393. this.CompileManager.syncFromCode
  394. .calledWith(
  395. this.project_id,
  396. undefined,
  397. this.file,
  398. this.line,
  399. this.column
  400. )
  401. .should.equal(true)
  402. })
  403. it('should return the positions', function () {
  404. this.res.json
  405. .calledWith({
  406. pdf: this.pdfPositions,
  407. })
  408. .should.equal(true)
  409. })
  410. })
  411. describe('syncFromPdf', function () {
  412. beforeEach(function () {
  413. this.page = 5
  414. this.h = 100.23
  415. this.v = 45.67
  416. this.project_id = 'mock-project-id'
  417. this.req.params = { project_id: this.project_id }
  418. this.req.query = {
  419. page: this.page.toString(),
  420. h: this.h.toString(),
  421. v: this.v.toString(),
  422. }
  423. this.res.json = sinon.stub()
  424. this.CompileManager.syncFromPdf = sinon
  425. .stub()
  426. .yields(null, (this.codePositions = ['mock-positions']))
  427. this.CompileController.syncFromPdf(this.req, this.res, this.next)
  428. })
  429. it('should find the corresponding location in the code', function () {
  430. this.CompileManager.syncFromPdf
  431. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  432. .should.equal(true)
  433. })
  434. it('should return the positions', function () {
  435. this.res.json
  436. .calledWith({
  437. code: this.codePositions,
  438. })
  439. .should.equal(true)
  440. })
  441. })
  442. describe('wordcount', function () {
  443. beforeEach(function () {
  444. this.file = 'main.tex'
  445. this.project_id = 'mock-project-id'
  446. this.req.params = { project_id: this.project_id }
  447. this.req.query = {
  448. file: this.file,
  449. image: (this.image = 'example.com/image'),
  450. }
  451. this.res.json = sinon.stub()
  452. this.CompileManager.wordcount = sinon
  453. .stub()
  454. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  455. })
  456. it('should return the word count of a file', function () {
  457. this.CompileController.wordcount(this.req, this.res, this.next)
  458. this.CompileManager.wordcount
  459. .calledWith(this.project_id, undefined, this.file, this.image)
  460. .should.equal(true)
  461. })
  462. it('should return the texcount info', function () {
  463. this.CompileController.wordcount(this.req, this.res, this.next)
  464. this.res.json
  465. .calledWith({
  466. texcount: this.texcount,
  467. })
  468. .should.equal(true)
  469. })
  470. })
  471. })