CompileControllerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. },
  125. })
  126. .should.equal(true)
  127. })
  128. })
  129. describe('without a outputUrlPrefix', function () {
  130. beforeEach(function () {
  131. this.Settings.apis.clsi.outputUrlPrefix = ''
  132. this.CompileController.compile(this.req, this.res)
  133. })
  134. it('should return the JSON response with empty outputUrlPrefix', function () {
  135. this.res.status.calledWith(200).should.equal(true)
  136. this.res.send
  137. .calledWith({
  138. compile: {
  139. status: 'success',
  140. error: null,
  141. stats: this.stats,
  142. timings: this.timings,
  143. buildId: this.buildId,
  144. outputUrlPrefix: '',
  145. outputFiles: this.output_files.map(file => ({
  146. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  147. ...file,
  148. })),
  149. },
  150. })
  151. .should.equal(true)
  152. })
  153. })
  154. describe('with user provided fake_output.pdf', function () {
  155. beforeEach(function () {
  156. this.output_files = [
  157. {
  158. path: 'fake_output.pdf',
  159. type: 'pdf',
  160. build: 1234,
  161. },
  162. {
  163. path: 'output.log',
  164. type: 'log',
  165. build: 1234,
  166. },
  167. ]
  168. this.CompileManager.doCompileWithLock = sinon
  169. .stub()
  170. .callsFake((_req, stats, timings, cb) => {
  171. Object.assign(stats, this.stats)
  172. Object.assign(timings, this.timings)
  173. cb(null, {
  174. outputFiles: this.output_files,
  175. buildId: this.buildId,
  176. })
  177. })
  178. this.CompileController.compile(this.req, this.res)
  179. })
  180. it('should return the JSON response with status failure', function () {
  181. this.res.status.calledWith(200).should.equal(true)
  182. this.res.send.should.have.been.calledWith({
  183. compile: {
  184. status: 'failure',
  185. error: null,
  186. stats: this.stats,
  187. timings: this.timings,
  188. outputUrlPrefix: '/zone/b',
  189. buildId: this.buildId,
  190. outputFiles: this.output_files.map(file => ({
  191. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  192. ...file,
  193. })),
  194. },
  195. })
  196. })
  197. })
  198. describe('with an empty output.pdf', function () {
  199. beforeEach(function () {
  200. this.output_files = [
  201. {
  202. path: 'output.pdf',
  203. type: 'pdf',
  204. size: 0,
  205. build: 1234,
  206. },
  207. {
  208. path: 'output.log',
  209. type: 'log',
  210. build: 1234,
  211. },
  212. ]
  213. this.CompileManager.doCompileWithLock = sinon
  214. .stub()
  215. .callsFake((_req, stats, timings, cb) => {
  216. Object.assign(stats, this.stats)
  217. Object.assign(timings, this.timings)
  218. cb(null, {
  219. outputFiles: this.output_files,
  220. buildId: this.buildId,
  221. })
  222. })
  223. this.CompileController.compile(this.req, this.res)
  224. })
  225. it('should return the JSON response with status failure', function () {
  226. this.res.status.calledWith(200).should.equal(true)
  227. this.res.send.should.have.been.calledWith({
  228. compile: {
  229. status: 'failure',
  230. error: null,
  231. stats: this.stats,
  232. buildId: this.buildId,
  233. timings: this.timings,
  234. outputUrlPrefix: '/zone/b',
  235. outputFiles: this.output_files.map(file => ({
  236. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  237. ...file,
  238. })),
  239. },
  240. })
  241. })
  242. })
  243. describe('with an error', function () {
  244. beforeEach(function () {
  245. const error = new Error((this.message = 'error message'))
  246. error.buildId = this.buildId
  247. this.CompileManager.doCompileWithLock = sinon
  248. .stub()
  249. .callsFake((_req, stats, timings, cb) => {
  250. Object.assign(stats, this.stats)
  251. Object.assign(timings, this.timings)
  252. cb(error)
  253. })
  254. this.CompileController.compile(this.req, this.res)
  255. })
  256. it('should return the JSON response with the error', function () {
  257. this.res.status.calledWith(500).should.equal(true)
  258. this.res.send
  259. .calledWith({
  260. compile: {
  261. status: 'error',
  262. error: this.message,
  263. outputUrlPrefix: '/zone/b',
  264. outputFiles: [],
  265. buildId: this.buildId,
  266. stats: this.stats,
  267. timings: this.timings,
  268. },
  269. })
  270. .should.equal(true)
  271. })
  272. })
  273. describe('with too many compile requests error', function () {
  274. beforeEach(function () {
  275. const error = new Errors.TooManyCompileRequestsError(
  276. 'too many concurrent compile requests'
  277. )
  278. this.CompileManager.doCompileWithLock = sinon
  279. .stub()
  280. .callsFake((_req, stats, timings, cb) => {
  281. Object.assign(stats, this.stats)
  282. Object.assign(timings, this.timings)
  283. cb(error)
  284. })
  285. this.CompileController.compile(this.req, this.res)
  286. })
  287. it('should return the JSON response with the error', function () {
  288. this.res.status.calledWith(503).should.equal(true)
  289. this.res.send
  290. .calledWith({
  291. compile: {
  292. status: 'unavailable',
  293. error: 'too many concurrent compile requests',
  294. outputUrlPrefix: '/zone/b',
  295. outputFiles: [],
  296. stats: this.stats,
  297. timings: this.timings,
  298. // JSON.stringify will omit these undefined values
  299. buildId: undefined,
  300. },
  301. })
  302. .should.equal(true)
  303. })
  304. })
  305. describe('when the request times out', function () {
  306. beforeEach(function () {
  307. this.error = new Error((this.message = 'container timed out'))
  308. this.error.timedout = true
  309. this.CompileManager.doCompileWithLock = sinon
  310. .stub()
  311. .callsFake((_req, stats, timings, cb) => {
  312. Object.assign(stats, this.stats)
  313. Object.assign(timings, this.timings)
  314. cb(this.error)
  315. })
  316. this.CompileController.compile(this.req, this.res)
  317. })
  318. it('should return the JSON response with the timeout status', function () {
  319. this.res.status.calledWith(200).should.equal(true)
  320. this.res.send
  321. .calledWith({
  322. compile: {
  323. status: 'timedout',
  324. error: this.message,
  325. outputUrlPrefix: '/zone/b',
  326. outputFiles: [],
  327. stats: this.stats,
  328. timings: this.timings,
  329. // JSON.stringify will omit these undefined values
  330. buildId: undefined,
  331. },
  332. })
  333. .should.equal(true)
  334. })
  335. })
  336. describe('when the request returns no output files', function () {
  337. beforeEach(function () {
  338. this.CompileManager.doCompileWithLock = sinon
  339. .stub()
  340. .callsFake((_req, stats, timings, cb) => {
  341. Object.assign(stats, this.stats)
  342. Object.assign(timings, this.timings)
  343. cb(null, {})
  344. })
  345. this.CompileController.compile(this.req, this.res)
  346. })
  347. it('should return the JSON response with the failure status', function () {
  348. this.res.status.calledWith(200).should.equal(true)
  349. this.res.send
  350. .calledWith({
  351. compile: {
  352. error: null,
  353. status: 'failure',
  354. outputUrlPrefix: '/zone/b',
  355. outputFiles: [],
  356. stats: this.stats,
  357. timings: this.timings,
  358. // JSON.stringify will omit these undefined values
  359. buildId: undefined,
  360. },
  361. })
  362. .should.equal(true)
  363. })
  364. })
  365. })
  366. describe('syncFromCode', function () {
  367. beforeEach(function () {
  368. this.file = 'main.tex'
  369. this.line = 42
  370. this.column = 5
  371. this.project_id = 'mock-project-id'
  372. this.req.params = { project_id: this.project_id }
  373. this.req.query = {
  374. file: this.file,
  375. line: this.line.toString(),
  376. column: this.column.toString(),
  377. }
  378. this.res.json = sinon.stub()
  379. this.CompileManager.syncFromCode = sinon
  380. .stub()
  381. .yields(null, (this.pdfPositions = ['mock-positions']))
  382. this.CompileController.syncFromCode(this.req, this.res, this.next)
  383. })
  384. it('should find the corresponding location in the PDF', function () {
  385. this.CompileManager.syncFromCode
  386. .calledWith(
  387. this.project_id,
  388. undefined,
  389. this.file,
  390. this.line,
  391. this.column
  392. )
  393. .should.equal(true)
  394. })
  395. it('should return the positions', function () {
  396. this.res.json
  397. .calledWith({
  398. pdf: this.pdfPositions,
  399. })
  400. .should.equal(true)
  401. })
  402. })
  403. describe('syncFromPdf', function () {
  404. beforeEach(function () {
  405. this.page = 5
  406. this.h = 100.23
  407. this.v = 45.67
  408. this.project_id = 'mock-project-id'
  409. this.req.params = { project_id: this.project_id }
  410. this.req.query = {
  411. page: this.page.toString(),
  412. h: this.h.toString(),
  413. v: this.v.toString(),
  414. }
  415. this.res.json = sinon.stub()
  416. this.CompileManager.syncFromPdf = sinon
  417. .stub()
  418. .yields(null, (this.codePositions = ['mock-positions']))
  419. this.CompileController.syncFromPdf(this.req, this.res, this.next)
  420. })
  421. it('should find the corresponding location in the code', function () {
  422. this.CompileManager.syncFromPdf
  423. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  424. .should.equal(true)
  425. })
  426. it('should return the positions', function () {
  427. this.res.json
  428. .calledWith({
  429. code: this.codePositions,
  430. })
  431. .should.equal(true)
  432. })
  433. })
  434. describe('wordcount', function () {
  435. beforeEach(function () {
  436. this.file = 'main.tex'
  437. this.project_id = 'mock-project-id'
  438. this.req.params = { project_id: this.project_id }
  439. this.req.query = {
  440. file: this.file,
  441. image: (this.image = 'example.com/image'),
  442. }
  443. this.res.json = sinon.stub()
  444. this.CompileManager.wordcount = sinon
  445. .stub()
  446. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  447. })
  448. it('should return the word count of a file', function () {
  449. this.CompileController.wordcount(this.req, this.res, this.next)
  450. this.CompileManager.wordcount
  451. .calledWith(this.project_id, undefined, this.file, this.image)
  452. .should.equal(true)
  453. })
  454. it('should return the texcount info', function () {
  455. this.CompileController.wordcount(this.req, this.res, this.next)
  456. this.res.json
  457. .calledWith({
  458. texcount: this.texcount,
  459. })
  460. .should.equal(true)
  461. })
  462. })
  463. })