mock-compile.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = (delayPromise = Promise.resolve()) =>
  80. fetchMock.delete(
  81. 'express:/project/:projectId/output',
  82. delayPromise.then(() => ({
  83. body: {
  84. status: 204,
  85. },
  86. }))
  87. )
  88. export const mockValidPdf = () => {
  89. nock('https://clsi.test-overleaf.com')
  90. .get(/^\/build\/output\.pdf/)
  91. .replyWithFile(200, examplePDF)
  92. }
  93. export const defaultFileResponses = {
  94. '/build/output.pdf': () => fs.createReadStream(examplePDF),
  95. '/build/output.blg': 'This is BibTeX, Version 4.0', // FIXME
  96. '/build/output.log': `
  97. The LaTeX compiler output
  98. * With a lot of details
  99. Wrapped in an HTML <pre> element with
  100. preformatted text which is to be presented exactly
  101. as written in the HTML file
  102. (whitespace included™)
  103. The text is typically rendered using a non-proportional ("monospace") font.
  104. LaTeX Font Info: External font \`cmex10' loaded for size
  105. (Font) <7> on input line 18.
  106. LaTeX Font Info: External font \`cmex10' loaded for size
  107. (Font) <5> on input line 18.
  108. ! Undefined control sequence.
  109. <recently read> \\Zlpha
  110. main.tex, line 23
  111. `,
  112. }
  113. export const mockBuildFile = (responses = defaultFileResponses) => {
  114. fetchMock.get('begin:https://clsi.test-overleaf.com/', _url => {
  115. const url = new URL(_url, 'https://clsi.test-overleaf.com')
  116. if (url.pathname in responses) {
  117. return responses[url.pathname]
  118. }
  119. return 404
  120. })
  121. fetchMock.get('express:/build/:file', (_url, options, request) => {
  122. const url = new URL(_url, 'https://example.com')
  123. if (url.pathname in responses) {
  124. return responses[url.pathname]
  125. }
  126. return 404
  127. })
  128. }