compile.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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: '123',
  12. url: '/build/output.pdf',
  13. type: 'pdf',
  14. },
  15. {
  16. path: 'output.bbl',
  17. build: '123',
  18. url: '/build/output.bbl',
  19. type: 'bbl',
  20. },
  21. {
  22. path: 'output.bib',
  23. build: '123',
  24. url: '/build/output.bib',
  25. type: 'bib',
  26. },
  27. {
  28. path: 'example.txt',
  29. build: '123',
  30. url: '/build/example.txt',
  31. type: 'txt',
  32. },
  33. {
  34. path: 'output.log',
  35. build: '123',
  36. url: '/build/output.log',
  37. type: 'log',
  38. },
  39. {
  40. path: 'output.blg',
  41. build: '123',
  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, overwriteRoutes: true }
  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(
  97. 'express:/build/:file',
  98. (url, options, request) => {
  99. const { pathname } = new URL(url, 'https://example.com')
  100. switch (pathname) {
  101. case '/build/output.blg':
  102. return 'This is BibTeX, Version 4.0' // FIXME
  103. case '/build/output.log':
  104. return `
  105. The LaTeX compiler output
  106. * With a lot of details
  107. Wrapped in an HTML <pre> element with
  108. preformatted text which is to be presented exactly
  109. as written in the HTML file
  110. (whitespace included™)
  111. The text is typically rendered using a non-proportional ("monospace") font.
  112. LaTeX Font Info: External font \`cmex10' loaded for size
  113. (Font) <7> on input line 18.
  114. LaTeX Font Info: External font \`cmex10' loaded for size
  115. (Font) <5> on input line 18.
  116. ! Undefined control sequence.
  117. <recently read> \\Zlpha
  118. main.tex, line 23
  119. `
  120. case '/build/output.pdf':
  121. return new Promise(resolve => {
  122. const xhr = new XMLHttpRequest()
  123. xhr.addEventListener('load', () => {
  124. resolve({
  125. status: 200,
  126. headers: {
  127. 'Content-Length': xhr.getResponseHeader('Content-Length'),
  128. 'Content-Type': xhr.getResponseHeader('Content-Type'),
  129. },
  130. body: xhr.response,
  131. })
  132. })
  133. xhr.open('GET', examplePdf)
  134. xhr.responseType = 'arraybuffer'
  135. xhr.send()
  136. })
  137. default:
  138. console.log(pathname)
  139. return 404
  140. }
  141. },
  142. { sendAsJson: false }
  143. )
  144. const mockHighlights = [
  145. {
  146. page: 1,
  147. h: 85.03936,
  148. v: 509.999878,
  149. width: 441.921265,
  150. height: 8.855677,
  151. },
  152. {
  153. page: 1,
  154. h: 85.03936,
  155. v: 486.089539,
  156. width: 441.921265,
  157. height: 8.855677,
  158. },
  159. {
  160. page: 1,
  161. h: 85.03936,
  162. v: 498.044708,
  163. width: 441.921265,
  164. height: 8.855677,
  165. },
  166. {
  167. page: 1,
  168. h: 85.03936,
  169. v: 521.955078,
  170. width: 441.921265,
  171. height: 8.855677,
  172. },
  173. ]
  174. export const mockEventTracking = fetchMock =>
  175. fetchMock.get('express:/event/:event', 204)
  176. export const mockValidPdf = fetchMock =>
  177. fetchMock.get(
  178. 'express:/build/output.pdf',
  179. (url, options, request) => {
  180. return new Promise(resolve => {
  181. const xhr = new XMLHttpRequest()
  182. xhr.addEventListener('load', () => {
  183. resolve({
  184. status: 200,
  185. headers: {
  186. 'Content-Length': xhr.getResponseHeader('Content-Length'),
  187. 'Content-Type': xhr.getResponseHeader('Content-Type'),
  188. 'Accept-Ranges': 'bytes',
  189. },
  190. body: xhr.response,
  191. })
  192. })
  193. xhr.open('GET', examplePdf)
  194. xhr.responseType = 'arraybuffer'
  195. xhr.send()
  196. })
  197. },
  198. { sendAsJson: false }
  199. )
  200. export const mockSynctex = fetchMock =>
  201. fetchMock
  202. .get('express:/project/:projectId/sync/code', () => {
  203. return { pdf: cloneDeep(mockHighlights) }
  204. })
  205. .get('express:/project/:projectId/sync/pdf', () => {
  206. return { code: [{ file: 'main.tex', line: 100 }] }
  207. })