CompileManager.test.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. import { vi, expect, describe, beforeEach, it } from 'vitest'
  2. import Path from 'node:path'
  3. import sinon from 'sinon'
  4. import Metrics from '../../../app/js/Metrics.js'
  5. const MODULE_PATH = Path.join(
  6. import.meta.dirname,
  7. '../../../app/js/CompileManager'
  8. )
  9. describe('CompileManager', () => {
  10. beforeEach(async ctx => {
  11. ctx.projectId = 'project-id-123'
  12. ctx.userId = '1234'
  13. ctx.resources = 'mock-resources'
  14. ctx.outputFiles = [
  15. {
  16. path: 'output.log',
  17. type: 'log',
  18. },
  19. {
  20. path: 'output.pdf',
  21. type: 'pdf',
  22. },
  23. ]
  24. ctx.buildFiles = [
  25. {
  26. path: 'output.log',
  27. type: 'log',
  28. build: '1234-5678',
  29. },
  30. {
  31. path: 'output.pdf',
  32. type: 'pdf',
  33. build: '1234-5678',
  34. },
  35. ]
  36. ctx.buildId = '00000000000-0000000000000000'
  37. ctx.commandOutput = 'Dummy output'
  38. ctx.compileBaseDir = '/compile/dir'
  39. ctx.outputBaseDir = '/output/dir'
  40. ctx.compileDir = `${ctx.compileBaseDir}/${ctx.projectId}-${ctx.userId}`
  41. ctx.outputDir = `${ctx.outputBaseDir}/${ctx.projectId}-${ctx.userId}`
  42. ctx.LatexRunner = {
  43. promises: {
  44. runLatex: sinon.stub().resolves({}),
  45. },
  46. }
  47. ctx.ResourceWriter = {
  48. promises: {
  49. syncResourcesToDisk: sinon.stub().resolves(ctx.resources),
  50. },
  51. }
  52. ctx.OutputFileFinder = {
  53. promises: {
  54. findOutputFiles: sinon.stub().resolves({
  55. outputFiles: ctx.outputFiles,
  56. allEntries: ctx.outputFiles.map(f => f.path).concat(['main.tex']),
  57. }),
  58. },
  59. }
  60. ctx.OutputCacheManager = {
  61. BUILD_REGEX: /^[0-9a-f]+-[0-9a-f]+$/,
  62. CACHE_SUBDIR: 'generated-files',
  63. promises: {
  64. queueDirOperation: sinon.stub().callsArg(1),
  65. saveOutputFiles: sinon
  66. .stub()
  67. .resolves({ outputFiles: ctx.buildFiles, buildId: ctx.buildId }),
  68. },
  69. }
  70. ctx.Settings = {
  71. path: {
  72. compilesDir: ctx.compileBaseDir,
  73. outputDir: ctx.outputBaseDir,
  74. synctexBaseDir: sinon.stub(),
  75. },
  76. clsi: {
  77. docker: {
  78. image: 'SOMEIMAGE',
  79. },
  80. },
  81. }
  82. ctx.Settings.path.synctexBaseDir
  83. .withArgs(`${ctx.projectId}-${ctx.userId}`)
  84. .returns(ctx.compileDir)
  85. ctx.child_process = {
  86. exec: sinon.stub(),
  87. execFile: sinon.stub().yields(),
  88. }
  89. ctx.CommandRunner = {
  90. canRunSyncTeXInOutputDir: sinon.stub().returns(false),
  91. promises: {
  92. run: sinon.stub().callsFake((_1, _2, _3, _4, _5, _6, compileGroup) => {
  93. if (compileGroup === 'synctex' || compileGroup === 'synctex-output') {
  94. return Promise.resolve({ stdout: ctx.commandOutput })
  95. } else {
  96. return Promise.resolve({
  97. stdout: 'Encoding: ascii\nWords in text: 2',
  98. })
  99. }
  100. }),
  101. },
  102. }
  103. ctx.DraftModeManager = {
  104. promises: {
  105. injectDraftMode: sinon.stub().resolves(),
  106. },
  107. }
  108. ctx.TikzManager = {
  109. promises: {
  110. checkMainFile: sinon.stub().resolves(false),
  111. },
  112. }
  113. ctx.lock = {
  114. release: sinon.stub(),
  115. }
  116. ctx.LockManager = {
  117. acquire: sinon.stub().returns(ctx.lock),
  118. }
  119. ctx.SynctexOutputParser = {
  120. parseViewOutput: sinon.stub(),
  121. parseEditOutput: sinon.stub(),
  122. }
  123. ctx.dirStats = {
  124. isDirectory: sinon.stub().returns(true),
  125. }
  126. ctx.fileStats = {
  127. isFile: sinon.stub().returns(true),
  128. }
  129. ctx.fsPromises = {
  130. lstat: sinon.stub(),
  131. stat: sinon.stub(),
  132. readFile: sinon.stub(),
  133. mkdir: sinon.stub().resolves(),
  134. rm: sinon.stub().resolves(),
  135. unlink: sinon.stub().resolves(),
  136. rmdir: sinon.stub().resolves(),
  137. }
  138. ctx.fsPromises.lstat.withArgs(ctx.compileDir).resolves(ctx.dirStats)
  139. ctx.fsPromises.stat
  140. .withArgs(Path.join(ctx.compileDir, 'output.synctex.gz'))
  141. .resolves(ctx.fileStats)
  142. ctx.CLSICacheHandler = {
  143. notifyCLSICacheAboutBuild: sinon.stub(),
  144. downloadLatestCompileCache: sinon.stub().resolves(),
  145. downloadOutputDotSynctexFromCompileCache: sinon.stub().resolves(),
  146. }
  147. ctx.LatexMetrics = { enableLatexMkMetrics: sinon.stub() }
  148. ctx.StatsManager = { sampleRequest: sinon.stub().returns(false) }
  149. vi.doMock('../../../app/js/LatexRunner', () => ({
  150. default: ctx.LatexRunner,
  151. }))
  152. vi.doMock('../../../app/js/ResourceWriter', () => ({
  153. default: ctx.ResourceWriter,
  154. }))
  155. vi.doMock('../../../app/js/OutputFileFinder', () => ({
  156. default: ctx.OutputFileFinder,
  157. }))
  158. vi.doMock('../../../app/js/OutputCacheManager', () => ({
  159. default: ctx.OutputCacheManager,
  160. }))
  161. vi.doMock('@overleaf/settings', () => ({
  162. default: ctx.Settings,
  163. }))
  164. vi.doMock('@overleaf/metrics', () => ({
  165. default: {
  166. inc: sinon.stub(),
  167. timing: sinon.stub(),
  168. gauge: sinon.stub(),
  169. Timer: sinon.stub().returns({ done: sinon.stub() }),
  170. },
  171. }))
  172. vi.doMock('child_process', () => ({
  173. default: ctx.child_process,
  174. }))
  175. vi.doMock('../../../app/js/CommandRunner', () => ({
  176. default: ctx.CommandRunner,
  177. }))
  178. vi.doMock('../../../app/js/DraftModeManager', () => ({
  179. default: ctx.DraftModeManager,
  180. }))
  181. vi.doMock('../../../app/js/TikzManager', () => ({
  182. default: ctx.TikzManager,
  183. }))
  184. vi.doMock('../../../app/js/LockManager', () => ({
  185. default: ctx.LockManager,
  186. }))
  187. vi.doMock('../../../app/js/SynctexOutputParser', () => ({
  188. default: ctx.SynctexOutputParser,
  189. }))
  190. vi.doMock('fs/promises', () => ({
  191. default: ctx.fsPromises,
  192. }))
  193. vi.doMock('../../../app/js/CLSICacheHandler', () => ({
  194. default: ctx.CLSICacheHandler,
  195. }))
  196. vi.doMock('../../../app/js/LatexMetrics', () => ({
  197. default: ctx.LatexMetrics,
  198. }))
  199. vi.doMock('../../../app/js/StatsManager', () => ({
  200. default: ctx.StatsManager,
  201. }))
  202. vi.doMock('../../../app/js/Metrics', () => ({
  203. default: Metrics,
  204. }))
  205. ctx.CompileManager = (await import(MODULE_PATH)).default
  206. })
  207. describe('doCompileWithLock', () => {
  208. beforeEach(ctx => {
  209. ctx.request = {
  210. resources: ctx.resources,
  211. rootResourcePath: (ctx.rootResourcePath = 'main.tex'),
  212. project_id: ctx.projectId,
  213. user_id: ctx.userId,
  214. compiler: (ctx.compiler = 'pdflatex'),
  215. timeout: (ctx.timeout = 42000),
  216. imageName: (ctx.image = 'example.com/image'),
  217. flags: (ctx.flags = ['-file-line-error']),
  218. compileGroup: (ctx.compileGroup = 'compile-group'),
  219. stopOnFirstError: false,
  220. metricsOpts: {
  221. path: 'clsi-perf',
  222. method: 'minimal',
  223. compile: 'initial',
  224. },
  225. }
  226. ctx.env = {
  227. OVERLEAF_PROJECT_ID: ctx.projectId,
  228. }
  229. })
  230. describe('when the project is locked', () => {
  231. beforeEach(async ctx => {
  232. const error = new Error('locked')
  233. ctx.LockManager.acquire.throws(error)
  234. await expect(
  235. ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  236. ).to.be.rejectedWith(error)
  237. })
  238. it('should ensure that the compile directory exists', ctx => {
  239. expect(ctx.fsPromises.mkdir).to.have.been.calledWith(ctx.compileDir, {
  240. recursive: true,
  241. })
  242. })
  243. it('should not run LaTeX', ctx => {
  244. expect(ctx.LatexRunner.promises.runLatex).not.to.have.been.called
  245. })
  246. })
  247. describe('normally', () => {
  248. beforeEach(async ctx => {
  249. ctx.result = await ctx.CompileManager.promises.doCompileWithLock(
  250. ctx.request,
  251. {},
  252. {}
  253. )
  254. })
  255. it('should ensure that the compile directory exists', ctx => {
  256. expect(ctx.fsPromises.mkdir).to.have.been.calledWith(ctx.compileDir, {
  257. recursive: true,
  258. })
  259. })
  260. it('should write the resources to disk', ctx => {
  261. expect(
  262. ctx.ResourceWriter.promises.syncResourcesToDisk
  263. ).to.have.been.calledWith(ctx.request, ctx.compileDir)
  264. })
  265. it('should run LaTeX', ctx => {
  266. expect(ctx.LatexRunner.promises.runLatex).to.have.been.calledWith(
  267. `${ctx.projectId}-${ctx.userId}`,
  268. {
  269. directory: ctx.compileDir,
  270. mainFile: ctx.rootResourcePath,
  271. compiler: ctx.compiler,
  272. timeout: ctx.timeout,
  273. image: ctx.image,
  274. flags: ctx.flags,
  275. environment: ctx.env,
  276. compileGroup: ctx.compileGroup,
  277. stopOnFirstError: ctx.request.stopOnFirstError,
  278. stats: sinon.match.object,
  279. timings: sinon.match.object,
  280. }
  281. )
  282. })
  283. it('should find the output files', ctx => {
  284. expect(
  285. ctx.OutputFileFinder.promises.findOutputFiles
  286. ).to.have.been.calledWith(ctx.resources, ctx.compileDir)
  287. })
  288. it('should return the output files', ctx => {
  289. expect(ctx.result.outputFiles).to.equal(ctx.buildFiles)
  290. })
  291. it('should not inject draft mode by default', ctx => {
  292. expect(ctx.DraftModeManager.promises.injectDraftMode).not.to.have.been
  293. .called
  294. })
  295. })
  296. describe('with performance metric collection', () => {
  297. it('should enable latexmk metrics when sampleRequest returns true', async ctx => {
  298. ctx.StatsManager.sampleRequest.returns(true)
  299. await ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  300. expect(ctx.LatexMetrics.enableLatexMkMetrics).to.have.been.calledWith(
  301. sinon.match.object
  302. )
  303. })
  304. it('should enable latexmk metrics when sampleRequest returns false', async ctx => {
  305. ctx.StatsManager.sampleRequest.returns(false)
  306. await ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  307. expect(ctx.LatexMetrics.enableLatexMkMetrics).to.have.been.calledWith(
  308. sinon.match.object
  309. )
  310. })
  311. it('should enable latexmk metrics when sampleRequest returns undefined', async ctx => {
  312. ctx.StatsManager.sampleRequest.returns(undefined)
  313. await ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  314. expect(ctx.LatexMetrics.enableLatexMkMetrics).to.have.been.calledWith(
  315. sinon.match.object
  316. )
  317. })
  318. })
  319. describe('with draft mode', () => {
  320. beforeEach(async ctx => {
  321. ctx.request.draft = true
  322. await ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  323. })
  324. it('should inject the draft mode header', ctx => {
  325. expect(
  326. ctx.DraftModeManager.promises.injectDraftMode
  327. ).to.have.been.calledWith(ctx.compileDir + '/' + ctx.rootResourcePath)
  328. })
  329. })
  330. describe('with a check option', () => {
  331. beforeEach(async ctx => {
  332. ctx.request.check = 'error'
  333. await ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  334. })
  335. it('should run chktex', ctx => {
  336. expect(ctx.LatexRunner.promises.runLatex).to.have.been.calledWith(
  337. `${ctx.projectId}-${ctx.userId}`,
  338. {
  339. directory: ctx.compileDir,
  340. mainFile: ctx.rootResourcePath,
  341. compiler: ctx.compiler,
  342. timeout: ctx.timeout,
  343. image: ctx.image,
  344. flags: ctx.flags,
  345. environment: {
  346. CHKTEX_OPTIONS: '-nall -e9 -e10 -w15 -w16',
  347. CHKTEX_EXIT_ON_ERROR: 1,
  348. CHKTEX_ULIMIT_OPTIONS: '-t 5 -v 64000',
  349. OVERLEAF_PROJECT_ID: ctx.projectId,
  350. },
  351. compileGroup: ctx.compileGroup,
  352. stopOnFirstError: ctx.request.stopOnFirstError,
  353. stats: sinon.match.object,
  354. timings: sinon.match.object,
  355. }
  356. )
  357. })
  358. })
  359. describe('with a knitr file and check options', () => {
  360. beforeEach(async ctx => {
  361. ctx.request.rootResourcePath = 'main.Rtex'
  362. ctx.request.check = 'error'
  363. await ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  364. })
  365. it('should not run chktex', ctx => {
  366. expect(ctx.LatexRunner.promises.runLatex).to.have.been.calledWith(
  367. `${ctx.projectId}-${ctx.userId}`,
  368. {
  369. directory: ctx.compileDir,
  370. mainFile: 'main.Rtex',
  371. compiler: ctx.compiler,
  372. timeout: ctx.timeout,
  373. image: ctx.image,
  374. flags: ctx.flags,
  375. environment: ctx.env,
  376. compileGroup: ctx.compileGroup,
  377. stopOnFirstError: ctx.request.stopOnFirstError,
  378. stats: sinon.match.object,
  379. timings: sinon.match.object,
  380. }
  381. )
  382. })
  383. })
  384. describe('when the compile times out', () => {
  385. beforeEach(async ctx => {
  386. const error = new Error('timed out!')
  387. error.timedout = true
  388. ctx.LatexRunner.promises.runLatex.rejects(error)
  389. await expect(
  390. ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  391. ).to.be.rejected
  392. })
  393. it('should clear the compile directory', ctx => {
  394. for (const { path } of ctx.buildFiles) {
  395. expect(ctx.fsPromises.unlink).to.have.been.calledWith(
  396. ctx.compileDir + '/' + path
  397. )
  398. }
  399. expect(ctx.fsPromises.unlink).to.have.been.calledWith(
  400. ctx.compileDir + '/main.tex'
  401. )
  402. expect(ctx.fsPromises.rmdir).to.have.been.calledWith(ctx.compileDir)
  403. })
  404. })
  405. describe('when the compile is manually stopped', () => {
  406. beforeEach(async ctx => {
  407. const error = new Error('terminated!')
  408. error.terminated = true
  409. ctx.LatexRunner.promises.runLatex.rejects(error)
  410. await expect(
  411. ctx.CompileManager.promises.doCompileWithLock(ctx.request, {}, {})
  412. ).to.be.rejected
  413. })
  414. it('should clear the compile directory', ctx => {
  415. for (const { path } of ctx.buildFiles) {
  416. expect(ctx.fsPromises.unlink).to.have.been.calledWith(
  417. ctx.compileDir + '/' + path
  418. )
  419. }
  420. expect(ctx.fsPromises.unlink).to.have.been.calledWith(
  421. ctx.compileDir + '/main.tex'
  422. )
  423. expect(ctx.fsPromises.rmdir).to.have.been.calledWith(ctx.compileDir)
  424. })
  425. })
  426. })
  427. describe('clearProject', () => {
  428. it('should clear the compile directory', async ctx => {
  429. await ctx.CompileManager.promises.clearProject(ctx.projectId, ctx.userId)
  430. expect(ctx.fsPromises.rm).to.have.been.calledWith(ctx.compileDir, {
  431. force: true,
  432. recursive: true,
  433. })
  434. })
  435. })
  436. describe('syncing', () => {
  437. beforeEach(ctx => {
  438. ctx.page = 1
  439. ctx.h = 42.23
  440. ctx.v = 87.56
  441. ctx.width = 100.01
  442. ctx.height = 234.56
  443. ctx.line = 5
  444. ctx.column = 3
  445. ctx.filename = 'main.tex'
  446. })
  447. describe('syncFromCode', () => {
  448. beforeEach(ctx => {
  449. ctx.records = [{ page: 1, h: 2, v: 3, width: 4, height: 5 }]
  450. ctx.SynctexOutputParser.parseViewOutput
  451. .withArgs(ctx.commandOutput)
  452. .returns(ctx.records)
  453. })
  454. describe('normal case', () => {
  455. beforeEach(async ctx => {
  456. ctx.result = await ctx.CompileManager.promises.syncFromCode(
  457. ctx.projectId,
  458. ctx.userId,
  459. ctx.filename,
  460. ctx.line,
  461. ctx.column,
  462. ''
  463. )
  464. })
  465. it('should execute the synctex binary', ctx => {
  466. const outputFilePath = `${ctx.compileDir}/output.pdf`
  467. const inputFilePath = `${ctx.compileDir}/${ctx.filename}`
  468. expect(ctx.CommandRunner.promises.run).to.have.been.calledWith(
  469. `${ctx.projectId}-${ctx.userId}`,
  470. [
  471. 'synctex',
  472. 'view',
  473. '-i',
  474. `${ctx.line}:${ctx.column}:${inputFilePath}`,
  475. '-o',
  476. outputFilePath,
  477. ],
  478. ctx.compileDir,
  479. ctx.Settings.clsi.docker.image,
  480. 60000,
  481. {},
  482. 'synctex'
  483. )
  484. })
  485. it('should return the parsed output', ctx => {
  486. expect(ctx.result).to.deep.equal({
  487. codePositions: ctx.records,
  488. downloadedFromCache: false,
  489. })
  490. })
  491. })
  492. describe('from cache in docker', () => {
  493. beforeEach(async ctx => {
  494. ctx.CommandRunner.canRunSyncTeXInOutputDir.returns(true)
  495. ctx.Settings.path.synctexBaseDir
  496. .withArgs(`${ctx.projectId}-${ctx.userId}`)
  497. .returns('/compile')
  498. const errNotFound = new Error()
  499. errNotFound.code = 'ENOENT'
  500. ctx.outputDir = `${ctx.outputBaseDir}/${ctx.projectId}-${ctx.userId}/${ctx.OutputCacheManager.CACHE_SUBDIR}/${ctx.buildId}`
  501. const filename = Path.join(ctx.outputDir, 'output.synctex.gz')
  502. ctx.fsPromises.stat
  503. .withArgs(ctx.outputDir)
  504. .onFirstCall()
  505. .rejects(errNotFound)
  506. ctx.fsPromises.stat
  507. .withArgs(ctx.outputDir)
  508. .onSecondCall()
  509. .resolves(ctx.dirStats)
  510. ctx.fsPromises.stat.withArgs(filename).resolves(ctx.fileStats)
  511. ctx.CLSICacheHandler.downloadOutputDotSynctexFromCompileCache.resolves(
  512. true
  513. )
  514. ctx.result = await ctx.CompileManager.promises.syncFromCode(
  515. ctx.projectId,
  516. ctx.userId,
  517. ctx.filename,
  518. ctx.line,
  519. ctx.column,
  520. {
  521. imageName: 'image',
  522. editorId: '00000000-0000-0000-0000-000000000000',
  523. buildId: ctx.buildId,
  524. compileFromClsiCache: true,
  525. }
  526. )
  527. })
  528. it('should run in output dir', ctx => {
  529. const outputFilePath = '/compile/output.pdf'
  530. const inputFilePath = `/compile/${ctx.filename}`
  531. expect(ctx.CommandRunner.promises.run).to.have.been.calledWith(
  532. `${ctx.projectId}-${ctx.userId}`,
  533. [
  534. 'synctex',
  535. 'view',
  536. '-i',
  537. `${ctx.line}:${ctx.column}:${inputFilePath}`,
  538. '-o',
  539. outputFilePath,
  540. ],
  541. ctx.outputDir,
  542. 'image',
  543. 60000,
  544. {},
  545. 'synctex-output'
  546. )
  547. })
  548. it('should return the parsed output', ctx => {
  549. expect(ctx.result).to.deep.equal({
  550. codePositions: ctx.records,
  551. downloadedFromCache: true,
  552. })
  553. })
  554. })
  555. describe('with a custom imageName', () => {
  556. const customImageName = 'foo/bar:tag-0'
  557. beforeEach(async ctx => {
  558. await ctx.CompileManager.promises.syncFromCode(
  559. ctx.projectId,
  560. ctx.userId,
  561. ctx.filename,
  562. ctx.line,
  563. ctx.column,
  564. { imageName: customImageName }
  565. )
  566. })
  567. it('should execute the synctex binary in a custom docker image', ctx => {
  568. const outputFilePath = `${ctx.compileDir}/output.pdf`
  569. const inputFilePath = `${ctx.compileDir}/${ctx.filename}`
  570. expect(ctx.CommandRunner.promises.run).to.have.been.calledWith(
  571. `${ctx.projectId}-${ctx.userId}`,
  572. [
  573. 'synctex',
  574. 'view',
  575. '-i',
  576. `${ctx.line}:${ctx.column}:${inputFilePath}`,
  577. '-o',
  578. outputFilePath,
  579. ],
  580. ctx.compileDir,
  581. customImageName,
  582. 60000,
  583. {},
  584. 'synctex'
  585. )
  586. })
  587. })
  588. })
  589. describe('syncFromPdf', () => {
  590. beforeEach(ctx => {
  591. ctx.records = [{ file: 'main.tex', line: 1, column: 1 }]
  592. ctx.SynctexOutputParser.parseEditOutput
  593. .withArgs(ctx.commandOutput, ctx.compileDir)
  594. .returns(ctx.records)
  595. })
  596. describe('normal case', () => {
  597. beforeEach(async ctx => {
  598. ctx.result = await ctx.CompileManager.promises.syncFromPdf(
  599. ctx.projectId,
  600. ctx.userId,
  601. ctx.page,
  602. ctx.h,
  603. ctx.v,
  604. { imageName: '' }
  605. )
  606. })
  607. it('should execute the synctex binary', ctx => {
  608. const outputFilePath = `${ctx.compileDir}/output.pdf`
  609. expect(ctx.CommandRunner.promises.run).to.have.been.calledWith(
  610. `${ctx.projectId}-${ctx.userId}`,
  611. [
  612. 'synctex',
  613. 'edit',
  614. '-o',
  615. `${ctx.page}:${ctx.h}:${ctx.v}:${outputFilePath}`,
  616. ],
  617. ctx.compileDir,
  618. ctx.Settings.clsi.docker.image,
  619. 60000,
  620. {}
  621. )
  622. })
  623. it('should return the parsed output', ctx => {
  624. expect(ctx.result).to.deep.equal({
  625. pdfPositions: ctx.records,
  626. downloadedFromCache: false,
  627. })
  628. })
  629. })
  630. describe('with a custom imageName', () => {
  631. const customImageName = 'foo/bar:tag-1'
  632. beforeEach(async ctx => {
  633. await ctx.CompileManager.promises.syncFromPdf(
  634. ctx.projectId,
  635. ctx.userId,
  636. ctx.page,
  637. ctx.h,
  638. ctx.v,
  639. { imageName: customImageName }
  640. )
  641. })
  642. it('should execute the synctex binary in a custom docker image', ctx => {
  643. const outputFilePath = `${ctx.compileDir}/output.pdf`
  644. expect(ctx.CommandRunner.promises.run).to.have.been.calledWith(
  645. `${ctx.projectId}-${ctx.userId}`,
  646. [
  647. 'synctex',
  648. 'edit',
  649. '-o',
  650. `${ctx.page}:${ctx.h}:${ctx.v}:${outputFilePath}`,
  651. ],
  652. ctx.compileDir,
  653. customImageName,
  654. 60000,
  655. {}
  656. )
  657. })
  658. })
  659. })
  660. })
  661. describe('wordcount', () => {
  662. beforeEach(async ctx => {
  663. ctx.timeout = 60 * 1000
  664. ctx.filename = 'main.tex'
  665. ctx.image = 'example.com/image'
  666. ctx.result = await ctx.CompileManager.promises.wordcount(
  667. ctx.projectId,
  668. ctx.userId,
  669. ctx.filename,
  670. ctx.image
  671. )
  672. })
  673. it('should run the texcount command', ctx => {
  674. ctx.filePath = `$COMPILE_DIR/${ctx.filename}`
  675. ctx.command = ['texcount', '-nocol', '-inc', ctx.filePath]
  676. expect(ctx.CommandRunner.promises.run).to.have.been.calledWith(
  677. `${ctx.projectId}-${ctx.userId}`,
  678. ctx.command,
  679. ctx.compileDir,
  680. ctx.image,
  681. ctx.timeout,
  682. {}
  683. )
  684. })
  685. it('should return the parsed output', ctx => {
  686. expect(ctx.result).to.deep.equal({
  687. encode: 'ascii',
  688. textWords: 2,
  689. headWords: 0,
  690. outside: 0,
  691. headers: 0,
  692. elements: 0,
  693. mathInline: 0,
  694. mathDisplay: 0,
  695. errors: 0,
  696. messages: '',
  697. })
  698. })
  699. })
  700. })