compile.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import examplePdf from './storybook-example.pdf'
  2. import { cloneDeep } from 'lodash'
  3. export const dispatchDocChanged = () => {
  4. window.dispatchEvent(
  5. new CustomEvent('doc:changed', { detail: { doc_id: 'foo' } })
  6. )
  7. }
  8. export const outputFiles = [
  9. {
  10. path: 'output.pdf',
  11. build: '1234-5678',
  12. url: '/build/output.pdf',
  13. type: 'pdf',
  14. },
  15. {
  16. path: 'output.bbl',
  17. build: '1234-5678',
  18. url: '/build/output.bbl',
  19. type: 'bbl',
  20. },
  21. {
  22. path: 'output.bib',
  23. build: '1234-5678',
  24. url: '/build/output.bib',
  25. type: 'bib',
  26. },
  27. {
  28. path: 'example.txt',
  29. build: '1234-5678',
  30. url: '/build/example.txt',
  31. type: 'txt',
  32. },
  33. {
  34. path: 'output.log',
  35. build: '1234-5678',
  36. url: '/build/output.log',
  37. type: 'log',
  38. },
  39. {
  40. path: 'output.blg',
  41. build: '1234-5678',
  42. url: '/build/output.blg',
  43. type: 'blg',
  44. },
  45. ]
  46. export const mockCompile = (fetchMock, delay = 1000) =>
  47. fetchMock.post(
  48. 'express:/project/:projectId/compile',
  49. {
  50. body: {
  51. status: 'success',
  52. clsiServerId: 'foo',
  53. compileGroup: 'priority',
  54. pdfDownloadDomain: '',
  55. outputFiles: cloneDeep(outputFiles),
  56. },
  57. },
  58. { delay }
  59. )
  60. export const mockCompileError = (fetchMock, status = 'success', delay = 1000) =>
  61. fetchMock.post(
  62. 'express:/project/:projectId/compile',
  63. {
  64. body: {
  65. status,
  66. clsiServerId: 'foo',
  67. compileGroup: 'priority',
  68. },
  69. },
  70. { delay, overwriteRoutes: true }
  71. )
  72. export const mockCompileValidationIssues = (
  73. fetchMock,
  74. validationProblems,
  75. delay = 1000
  76. ) =>
  77. fetchMock.post(
  78. 'express:/project/:projectId/compile',
  79. () => {
  80. return {
  81. body: {
  82. status: 'validation-problems',
  83. validationProblems,
  84. clsiServerId: 'foo',
  85. compileGroup: 'priority',
  86. },
  87. }
  88. },
  89. { delay }
  90. )
  91. export const mockClearCache = fetchMock =>
  92. fetchMock.delete('express:/project/:projectId/output', 204, {
  93. delay: 1000,
  94. })
  95. export const mockBuildFile = fetchMock =>
  96. fetchMock.get('express:/build/:file', ({ url }) => {
  97. const { pathname } = new URL(url, 'https://example.com')
  98. switch (pathname) {
  99. case '/build/output.blg':
  100. return 'This is BibTeX, Version 4.0' // FIXME
  101. case '/build/output.log':
  102. return `
  103. The LaTeX compiler output
  104. * With a lot of details
  105. Wrapped in an HTML <pre> element with
  106. preformatted text which is to be presented exactly
  107. as written in the HTML file
  108. (whitespace included™)
  109. The text is typically rendered using a non-proportional ("monospace") font.
  110. LaTeX Font Info: External font \`cmex10' loaded for size
  111. (Font) <7> on input line 18.
  112. LaTeX Font Info: External font \`cmex10' loaded for size
  113. (Font) <5> on input line 18.
  114. ! Undefined control sequence.
  115. <recently read> \\Zlpha
  116. main.tex, line 23
  117. `
  118. case '/build/output.pdf':
  119. return new Promise(resolve => {
  120. const xhr = new XMLHttpRequest()
  121. xhr.addEventListener('load', () => {
  122. resolve({
  123. status: 200,
  124. headers: {
  125. 'Content-Length': xhr.getResponseHeader('Content-Length'),
  126. 'Content-Type': xhr.getResponseHeader('Content-Type'),
  127. },
  128. body: xhr.response,
  129. })
  130. })
  131. xhr.open('GET', examplePdf)
  132. xhr.responseType = 'arraybuffer'
  133. xhr.send()
  134. })
  135. default:
  136. console.log(pathname)
  137. return 404
  138. }
  139. })
  140. const mockHighlights = [
  141. {
  142. page: 1,
  143. h: 85.03936,
  144. v: 509.999878,
  145. width: 441.921265,
  146. height: 8.855677,
  147. },
  148. {
  149. page: 1,
  150. h: 85.03936,
  151. v: 486.089539,
  152. width: 441.921265,
  153. height: 8.855677,
  154. },
  155. {
  156. page: 1,
  157. h: 85.03936,
  158. v: 498.044708,
  159. width: 441.921265,
  160. height: 8.855677,
  161. },
  162. {
  163. page: 1,
  164. h: 85.03936,
  165. v: 521.955078,
  166. width: 441.921265,
  167. height: 8.855677,
  168. },
  169. ]
  170. export const mockEventTracking = fetchMock =>
  171. fetchMock.get('express:/event/:event', 204)
  172. export const mockValidPdf = fetchMock =>
  173. fetchMock.get('express:/build/output.pdf', () => {
  174. return new Promise(resolve => {
  175. const xhr = new XMLHttpRequest()
  176. xhr.addEventListener('load', () => {
  177. resolve({
  178. status: 200,
  179. headers: {
  180. 'Content-Length': xhr.getResponseHeader('Content-Length'),
  181. 'Content-Type': xhr.getResponseHeader('Content-Type'),
  182. 'Accept-Ranges': 'bytes',
  183. },
  184. body: xhr.response,
  185. })
  186. })
  187. xhr.open('GET', examplePdf)
  188. xhr.responseType = 'arraybuffer'
  189. xhr.send()
  190. })
  191. })
  192. export const mockSynctex = fetchMock =>
  193. fetchMock
  194. .get('express:/project/:projectId/sync/code', () => {
  195. return { pdf: cloneDeep(mockHighlights) }
  196. })
  197. .get('express:/project/:projectId/sync/pdf', () => {
  198. return { code: [{ file: 'main.tex', line: 100 }] }
  199. })