mock-compile.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import fetchMock from 'fetch-mock'
  2. import { cloneDeep } from 'lodash'
  3. import nock from 'nock'
  4. import fs from 'fs'
  5. import path from 'path'
  6. export const examplePDF = path.join(__dirname, '../fixtures/test-example.pdf')
  7. export const corruptPDF = path.join(
  8. __dirname,
  9. '../fixtures/test-example-corrupt.pdf'
  10. )
  11. const outputFiles = [
  12. {
  13. path: 'output.pdf',
  14. build: '123',
  15. url: '/build/output.pdf',
  16. type: 'pdf',
  17. },
  18. {
  19. path: 'output.bbl',
  20. build: '123',
  21. url: '/build/output.bbl',
  22. type: 'bbl',
  23. },
  24. {
  25. path: 'output.bib',
  26. build: '123',
  27. url: '/build/output.bib',
  28. type: 'bib',
  29. },
  30. {
  31. path: 'example.txt',
  32. build: '123',
  33. url: '/build/example.txt',
  34. type: 'txt',
  35. },
  36. {
  37. path: 'output.log',
  38. build: '123',
  39. url: '/build/output.log',
  40. type: 'log',
  41. },
  42. {
  43. path: 'output.blg',
  44. build: '123',
  45. url: '/build/output.blg',
  46. type: 'blg',
  47. },
  48. ]
  49. export const mockCompile = (delayPromise = Promise.resolve()) =>
  50. fetchMock.post(
  51. 'express:/project/:projectId/compile',
  52. delayPromise.then(() => ({
  53. body: {
  54. status: 'success',
  55. clsiServerId: 'foo',
  56. compileGroup: 'priority',
  57. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  58. outputFiles: cloneDeep(outputFiles),
  59. },
  60. }))
  61. )
  62. export const mockCompileError = status =>
  63. fetchMock.post('express:/project/:projectId/compile', {
  64. body: {
  65. status,
  66. clsiServerId: 'foo',
  67. compileGroup: 'priority',
  68. },
  69. })
  70. export const mockValidationProblems = validationProblems =>
  71. fetchMock.post('express:/project/:projectId/compile', {
  72. body: {
  73. status: 'validation-problems',
  74. validationProblems,
  75. clsiServerId: 'foo',
  76. compileGroup: 'priority',
  77. },
  78. })
  79. export const mockClearCache = () =>
  80. fetchMock.delete('express:/project/:projectId/output', 204)
  81. export const mockValidPdf = () => {
  82. nock('https://clsi.test-overleaf.com')
  83. .get(/^\/build\/output\.pdf/)
  84. .replyWithFile(200, examplePDF)
  85. }
  86. export const defaultFileResponses = {
  87. '/build/output.pdf': () => fs.createReadStream(examplePDF),
  88. '/build/output.blg': 'This is BibTeX, Version 4.0', // FIXME
  89. '/build/output.log': `
  90. The LaTeX compiler output
  91. * With a lot of details
  92. Wrapped in an HTML <pre> element with
  93. preformatted text which is to be presented exactly
  94. as written in the HTML file
  95. (whitespace included™)
  96. The text is typically rendered using a non-proportional ("monospace") font.
  97. LaTeX Font Info: External font \`cmex10' loaded for size
  98. (Font) <7> on input line 18.
  99. LaTeX Font Info: External font \`cmex10' loaded for size
  100. (Font) <5> on input line 18.
  101. ! Undefined control sequence.
  102. <recently read> \\Zlpha
  103. main.tex, line 23
  104. `,
  105. }
  106. export const mockBuildFile = (responses = defaultFileResponses) => {
  107. fetchMock.get('begin:https://clsi.test-overleaf.com/', _url => {
  108. const url = new URL(_url, 'https://clsi.test-overleaf.com')
  109. if (url.pathname in responses) {
  110. return responses[url.pathname]
  111. }
  112. return 404
  113. })
  114. fetchMock.get('express:/build/:file', (_url, options, request) => {
  115. const url = new URL(_url, 'https://example.com')
  116. if (url.pathname in responses) {
  117. return responses[url.pathname]
  118. }
  119. return 404
  120. })
  121. }