CompileControllerTests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. })
  82. describe('successfully', function () {
  83. beforeEach(function () {
  84. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  85. outputFiles: this.output_files,
  86. stats: this.stats,
  87. timings: this.timings,
  88. buildId: this.buildId,
  89. })
  90. this.CompileController.compile(this.req, this.res)
  91. })
  92. it('should parse the request', function () {
  93. this.RequestParser.parse.calledWith(this.req.body).should.equal(true)
  94. })
  95. it('should run the compile for the specified project', function () {
  96. this.CompileManager.doCompileWithLock
  97. .calledWith(this.request_with_project_id)
  98. .should.equal(true)
  99. })
  100. it('should mark the project as accessed', function () {
  101. this.ProjectPersistenceManager.markProjectAsJustAccessed
  102. .calledWith(this.project_id)
  103. .should.equal(true)
  104. })
  105. it('should return the JSON response', function () {
  106. this.res.status.calledWith(200).should.equal(true)
  107. this.res.send
  108. .calledWith({
  109. compile: {
  110. status: 'success',
  111. error: null,
  112. stats: this.stats,
  113. timings: this.timings,
  114. buildId: this.buildId,
  115. outputUrlPrefix: '/zone/b',
  116. outputFiles: this.output_files.map(file => ({
  117. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  118. ...file,
  119. })),
  120. },
  121. })
  122. .should.equal(true)
  123. })
  124. })
  125. describe('without a outputUrlPrefix', function () {
  126. beforeEach(function () {
  127. this.Settings.apis.clsi.outputUrlPrefix = ''
  128. this.CompileManager.doCompileWithLock = sinon.stub().yields(null, {
  129. outputFiles: this.output_files,
  130. stats: this.stats,
  131. timings: this.timings,
  132. buildId: this.buildId,
  133. })
  134. this.CompileController.compile(this.req, this.res)
  135. })
  136. it('should return the JSON response with empty outputUrlPrefix', function () {
  137. this.res.status.calledWith(200).should.equal(true)
  138. this.res.send
  139. .calledWith({
  140. compile: {
  141. status: 'success',
  142. error: null,
  143. stats: this.stats,
  144. timings: this.timings,
  145. buildId: this.buildId,
  146. outputUrlPrefix: '',
  147. outputFiles: this.output_files.map(file => ({
  148. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  149. ...file,
  150. })),
  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.stub().yields(null, {
  171. outputFiles: this.output_files,
  172. stats: this.stats,
  173. timings: this.timings,
  174. buildId: this.buildId,
  175. })
  176. this.CompileController.compile(this.req, this.res)
  177. })
  178. it('should return the JSON response with status failure', function () {
  179. this.res.status.calledWith(200).should.equal(true)
  180. this.res.send
  181. .calledWith({
  182. compile: {
  183. status: 'failure',
  184. error: null,
  185. stats: this.stats,
  186. timings: this.timings,
  187. outputUrlPrefix: '/zone/b',
  188. buildId: this.buildId,
  189. outputFiles: this.output_files.map(file => ({
  190. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  191. ...file,
  192. })),
  193. },
  194. })
  195. .should.equal(true)
  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.stub().yields(null, {
  214. outputFiles: this.output_files,
  215. stats: this.stats,
  216. timings: this.timings,
  217. buildId: this.buildId,
  218. })
  219. this.CompileController.compile(this.req, this.res)
  220. })
  221. it('should return the JSON response with status failure', function () {
  222. this.res.status.calledWith(200).should.equal(true)
  223. this.res.send
  224. .calledWith({
  225. compile: {
  226. status: 'failure',
  227. error: null,
  228. stats: this.stats,
  229. buildId: this.buildId,
  230. timings: this.timings,
  231. outputUrlPrefix: '/zone/b',
  232. outputFiles: this.output_files.map(file => ({
  233. url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
  234. ...file,
  235. })),
  236. },
  237. })
  238. .should.equal(true)
  239. })
  240. })
  241. describe('with an error', function () {
  242. beforeEach(function () {
  243. const error = new Error((this.message = 'error message'))
  244. error.buildId = this.buildId
  245. this.CompileManager.doCompileWithLock = sinon
  246. .stub()
  247. .callsArgWith(1, error, null)
  248. this.CompileController.compile(this.req, this.res)
  249. })
  250. it('should return the JSON response with the error', function () {
  251. this.res.status.calledWith(500).should.equal(true)
  252. this.res.send
  253. .calledWith({
  254. compile: {
  255. status: 'error',
  256. error: this.message,
  257. outputUrlPrefix: '/zone/b',
  258. outputFiles: [],
  259. buildId: this.buildId,
  260. // JSON.stringify will omit these
  261. stats: undefined,
  262. timings: undefined,
  263. },
  264. })
  265. .should.equal(true)
  266. })
  267. })
  268. describe('with too many compile requests error', function () {
  269. beforeEach(function () {
  270. const error = new Errors.TooManyCompileRequestsError(
  271. 'too many concurrent compile requests'
  272. )
  273. this.CompileManager.doCompileWithLock = sinon
  274. .stub()
  275. .callsArgWith(1, error, null)
  276. this.CompileController.compile(this.req, this.res)
  277. })
  278. it('should return the JSON response with the error', function () {
  279. this.res.status.calledWith(503).should.equal(true)
  280. this.res.send
  281. .calledWith({
  282. compile: {
  283. status: 'unavailable',
  284. error: 'too many concurrent compile requests',
  285. outputUrlPrefix: '/zone/b',
  286. outputFiles: [],
  287. buildId: undefined,
  288. stats: undefined,
  289. timings: undefined,
  290. },
  291. })
  292. .should.equal(true)
  293. })
  294. })
  295. describe('when the request times out', function () {
  296. beforeEach(function () {
  297. this.error = new Error((this.message = 'container timed out'))
  298. this.error.timedout = true
  299. this.CompileManager.doCompileWithLock = sinon
  300. .stub()
  301. .callsArgWith(1, this.error, null)
  302. this.CompileController.compile(this.req, this.res)
  303. })
  304. it('should return the JSON response with the timeout status', function () {
  305. this.res.status.calledWith(200).should.equal(true)
  306. this.res.send
  307. .calledWith({
  308. compile: {
  309. status: 'timedout',
  310. error: this.message,
  311. outputUrlPrefix: '/zone/b',
  312. outputFiles: [],
  313. // JSON.stringify will omit these
  314. buildId: undefined,
  315. stats: undefined,
  316. timings: undefined,
  317. },
  318. })
  319. .should.equal(true)
  320. })
  321. })
  322. describe('when the request returns no output files', function () {
  323. beforeEach(function () {
  324. this.CompileManager.doCompileWithLock = sinon
  325. .stub()
  326. .callsArgWith(1, null, [])
  327. this.CompileController.compile(this.req, this.res)
  328. })
  329. it('should return the JSON response with the failure status', function () {
  330. this.res.status.calledWith(200).should.equal(true)
  331. this.res.send
  332. .calledWith({
  333. compile: {
  334. error: null,
  335. status: 'failure',
  336. outputUrlPrefix: '/zone/b',
  337. outputFiles: [],
  338. // JSON.stringify will omit these
  339. buildId: undefined,
  340. stats: undefined,
  341. timings: undefined,
  342. },
  343. })
  344. .should.equal(true)
  345. })
  346. })
  347. })
  348. describe('syncFromCode', function () {
  349. beforeEach(function () {
  350. this.file = 'main.tex'
  351. this.line = 42
  352. this.column = 5
  353. this.project_id = 'mock-project-id'
  354. this.req.params = { project_id: this.project_id }
  355. this.req.query = {
  356. file: this.file,
  357. line: this.line.toString(),
  358. column: this.column.toString(),
  359. }
  360. this.res.json = sinon.stub()
  361. this.CompileManager.syncFromCode = sinon
  362. .stub()
  363. .yields(null, (this.pdfPositions = ['mock-positions']))
  364. this.CompileController.syncFromCode(this.req, this.res, this.next)
  365. })
  366. it('should find the corresponding location in the PDF', function () {
  367. this.CompileManager.syncFromCode
  368. .calledWith(
  369. this.project_id,
  370. undefined,
  371. this.file,
  372. this.line,
  373. this.column
  374. )
  375. .should.equal(true)
  376. })
  377. it('should return the positions', function () {
  378. this.res.json
  379. .calledWith({
  380. pdf: this.pdfPositions,
  381. })
  382. .should.equal(true)
  383. })
  384. })
  385. describe('syncFromPdf', function () {
  386. beforeEach(function () {
  387. this.page = 5
  388. this.h = 100.23
  389. this.v = 45.67
  390. this.project_id = 'mock-project-id'
  391. this.req.params = { project_id: this.project_id }
  392. this.req.query = {
  393. page: this.page.toString(),
  394. h: this.h.toString(),
  395. v: this.v.toString(),
  396. }
  397. this.res.json = sinon.stub()
  398. this.CompileManager.syncFromPdf = sinon
  399. .stub()
  400. .yields(null, (this.codePositions = ['mock-positions']))
  401. this.CompileController.syncFromPdf(this.req, this.res, this.next)
  402. })
  403. it('should find the corresponding location in the code', function () {
  404. this.CompileManager.syncFromPdf
  405. .calledWith(this.project_id, undefined, this.page, this.h, this.v)
  406. .should.equal(true)
  407. })
  408. it('should return the positions', function () {
  409. this.res.json
  410. .calledWith({
  411. code: this.codePositions,
  412. })
  413. .should.equal(true)
  414. })
  415. })
  416. describe('wordcount', function () {
  417. beforeEach(function () {
  418. this.file = 'main.tex'
  419. this.project_id = 'mock-project-id'
  420. this.req.params = { project_id: this.project_id }
  421. this.req.query = {
  422. file: this.file,
  423. image: (this.image = 'example.com/image'),
  424. }
  425. this.res.json = sinon.stub()
  426. this.CompileManager.wordcount = sinon
  427. .stub()
  428. .callsArgWith(4, null, (this.texcount = ['mock-texcount']))
  429. })
  430. it('should return the word count of a file', function () {
  431. this.CompileController.wordcount(this.req, this.res, this.next)
  432. this.CompileManager.wordcount
  433. .calledWith(this.project_id, undefined, this.file, this.image)
  434. .should.equal(true)
  435. })
  436. it('should return the texcount info', function () {
  437. this.CompileController.wordcount(this.req, this.res, this.next)
  438. this.res.json
  439. .calledWith({
  440. texcount: this.texcount,
  441. })
  442. .should.equal(true)
  443. })
  444. })
  445. })