compile.js 5.2 KB

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