pdf-preview.spec.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import localStorage from '../../../../frontend/js/infrastructure/local-storage'
  2. import PdfPreview from '../../../../frontend/js/features/pdf-preview/components/pdf-preview'
  3. import { EditorProviders } from '../../helpers/editor-providers'
  4. import { mockScope } from './scope'
  5. import { useLayoutContext } from '../../../../frontend/js/shared/context/layout-context'
  6. import { FC, useEffect } from 'react'
  7. const storeAndFireEvent = (win: typeof window, key: string, value: unknown) => {
  8. localStorage.setItem(key, value)
  9. win.dispatchEvent(new StorageEvent('storage', { key }))
  10. }
  11. const Layout: FC<{ layout: string; view?: string }> = ({ layout, view }) => {
  12. const { changeLayout } = useLayoutContext()
  13. useEffect(() => {
  14. changeLayout(layout, view)
  15. }, [changeLayout, layout, view])
  16. return null
  17. }
  18. describe('<PdfPreview/>', function () {
  19. beforeEach(function () {
  20. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  21. cy.interceptEvents()
  22. })
  23. afterEach(function () {
  24. window.metaAttributesCache = new Map()
  25. })
  26. it('renders the PDF preview', function () {
  27. window.metaAttributesCache.set('ol-preventCompileOnLoad', false)
  28. cy.interceptCompile('compile')
  29. const scope = mockScope()
  30. cy.mount(
  31. <EditorProviders scope={scope}>
  32. <div className="pdf-viewer">
  33. <PdfPreview />
  34. </div>
  35. </EditorProviders>
  36. )
  37. // wait for "compile on load" to finish
  38. cy.waitForCompile({ pdf: true })
  39. cy.findByRole('button', { name: 'Recompile' })
  40. })
  41. it('runs a compile when the Recompile button is pressed', function () {
  42. cy.interceptCompile()
  43. const scope = mockScope()
  44. cy.mount(
  45. <EditorProviders scope={scope}>
  46. <div className="pdf-viewer">
  47. <PdfPreview />
  48. </div>
  49. </EditorProviders>
  50. )
  51. // press the Recompile button => compile
  52. cy.findByRole('button', { name: 'Recompile' }).click()
  53. // wait for compile to finish
  54. cy.waitForCompile({ pdf: true })
  55. cy.contains('Your Paper')
  56. })
  57. it('runs a compile on `pdf:recompile` event', function () {
  58. cy.interceptCompile()
  59. const scope = mockScope()
  60. cy.mount(
  61. <EditorProviders scope={scope}>
  62. <div className="pdf-viewer">
  63. <PdfPreview />
  64. </div>
  65. </EditorProviders>
  66. )
  67. cy.window().then(win => {
  68. win.dispatchEvent(new CustomEvent('pdf:recompile'))
  69. })
  70. // wait for compile to finish
  71. cy.waitForCompile({ pdf: true })
  72. cy.contains('Your Paper')
  73. })
  74. it('does not compile while compiling', function () {
  75. let counter = 0
  76. cy.interceptDeferredCompile(() => counter++).then(
  77. resolveDeferredCompile => {
  78. const scope = mockScope()
  79. cy.mount(
  80. <EditorProviders scope={scope}>
  81. <div className="pdf-viewer">
  82. <PdfPreview />
  83. </div>
  84. </EditorProviders>
  85. )
  86. // start compiling
  87. cy.findByRole('button', { name: 'Recompile' })
  88. .click()
  89. .then(() => {
  90. cy.findByRole('button', { name: 'Compiling…' })
  91. // trigger a recompile
  92. cy.window().then(win => {
  93. win.dispatchEvent(new CustomEvent('pdf:recompile'))
  94. })
  95. // finish the original compile
  96. resolveDeferredCompile()
  97. // wait for the original compile to finish
  98. cy.waitForCompile({ pdf: true })
  99. // NOTE: difficult to assert that a second request won't be sent, at some point
  100. expect(counter).to.equal(1)
  101. })
  102. }
  103. )
  104. })
  105. it('disables compile button while compile is running', function () {
  106. cy.interceptDeferredCompile().then(resolveDeferredCompile => {
  107. const scope = mockScope()
  108. cy.mount(
  109. <EditorProviders scope={scope}>
  110. <div className="pdf-viewer">
  111. <PdfPreview />
  112. </div>
  113. </EditorProviders>
  114. )
  115. cy.findByRole('button', { name: 'Recompile' }).click()
  116. cy.findByRole('button', { name: 'Compiling…' })
  117. .should('be.disabled')
  118. .then(resolveDeferredCompile)
  119. cy.waitForCompile()
  120. cy.findByRole('button', { name: 'Recompile' }).should('not.be.disabled')
  121. })
  122. })
  123. it('runs a compile on doc change if autocompile is enabled', function () {
  124. cy.interceptCompile()
  125. const scope = mockScope()
  126. cy.mount(
  127. <EditorProviders scope={scope}>
  128. <div className="pdf-viewer">
  129. <PdfPreview />
  130. </div>
  131. </EditorProviders>
  132. )
  133. cy.window().then(win => {
  134. cy.clock()
  135. // switch on auto compile
  136. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  137. // fire a doc:changed event => compile
  138. win.dispatchEvent(new CustomEvent('doc:changed'))
  139. // wait enough time for the compile to start
  140. cy.tick(6000) // > AUTO_COMPILE_DEBOUNCE
  141. cy.clock().invoke('restore')
  142. })
  143. // wait for compile to finish
  144. cy.waitForCompile({ pdf: true })
  145. cy.findByRole('button', { name: 'Recompile' })
  146. })
  147. it('does not run a compile on doc change if autocompile is disabled', function () {
  148. cy.interceptCompile()
  149. const scope = mockScope()
  150. cy.mount(
  151. <EditorProviders scope={scope}>
  152. <div className="pdf-viewer">
  153. <PdfPreview />
  154. </div>
  155. </EditorProviders>
  156. )
  157. cy.window().then(win => {
  158. cy.clock()
  159. // make sure auto compile is switched off
  160. storeAndFireEvent(win, 'autocompile_enabled:project123', false)
  161. // fire a doc:changed event => no compile
  162. win.dispatchEvent(new CustomEvent('doc:changed'))
  163. // wait enough time for the compile to start
  164. cy.tick(6000) // AUTO_COMPILE_DEBOUNCE
  165. cy.clock().invoke('restore')
  166. })
  167. // NOTE: difficult to assert that a request hasn't been sent
  168. cy.findByRole('button', { name: 'Recompile' })
  169. })
  170. it('does not run a compile on doc change if autocompile is blocked by syntax check', function () {
  171. cy.interceptCompile()
  172. const scope = mockScope()
  173. // enable linting in the editor
  174. scope.settings.syntaxValidation = true
  175. // mock a linting error
  176. scope.hasLintingError = true
  177. cy.mount(
  178. <EditorProviders scope={scope}>
  179. <div className="pdf-viewer">
  180. <PdfPreview />
  181. </div>
  182. </EditorProviders>
  183. )
  184. cy.window().then(win => {
  185. cy.clock()
  186. // switch on auto compile
  187. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  188. // switch on syntax checking
  189. storeAndFireEvent(win, 'stop_on_validation_error', true)
  190. // fire a doc:changed event => no compile
  191. win.dispatchEvent(new CustomEvent('doc:changed'))
  192. // wait enough time for the compile to start
  193. cy.tick(6000) // AUTO_COMPILE_DEBOUNCE
  194. cy.clock().invoke('restore')
  195. })
  196. // NOTE: difficult to assert that a request hasn't been sent
  197. cy.findByRole('button', { name: 'Recompile' })
  198. cy.findByText('Code check failed')
  199. })
  200. it('does not run a compile on doc change if the PDF preview is not open', function () {
  201. cy.interceptCompile()
  202. const scope = mockScope()
  203. cy.mount(
  204. <EditorProviders scope={scope}>
  205. <Layout layout="flat" view="editor" />
  206. <div className="pdf-viewer">
  207. <PdfPreview />
  208. </div>
  209. </EditorProviders>
  210. )
  211. cy.window().then(win => {
  212. cy.clock()
  213. // switch on auto compile
  214. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  215. // fire a doc:changed event => compile
  216. win.dispatchEvent(new CustomEvent('doc:changed'))
  217. // wait enough time for the compile to start
  218. cy.tick(6000) // > AUTO_COMPILE_DEBOUNCE
  219. cy.clock().invoke('restore')
  220. })
  221. // NOTE: difficult to assert that a request hasn't been sent
  222. cy.findByRole('button', { name: 'Recompile' })
  223. })
  224. describe('error messages', function () {
  225. const compileErrorStatuses = {
  226. 'clear-cache':
  227. 'Sorry, something went wrong and your project could not be compiled. Please try again in a few moments.',
  228. 'clsi-maintenance':
  229. 'The compile servers are down for maintenance, and will be back shortly.',
  230. 'compile-in-progress':
  231. 'A previous compile is still running. Please wait a minute and try compiling again.',
  232. exited: 'Server Error',
  233. failure: 'No PDF',
  234. generic: 'Server Error',
  235. 'project-too-large': 'Project too large',
  236. 'rate-limited': 'Compile rate limit hit',
  237. terminated: 'Compilation cancelled',
  238. timedout: 'Timed out',
  239. 'too-recently-compiled':
  240. 'This project was compiled very recently, so this compile has been skipped.',
  241. unavailable:
  242. 'Sorry, the compile server for your project was temporarily unavailable. Please try again in a few moments.',
  243. foo: 'Sorry, something went wrong and your project could not be compiled. Please try again in a few moments.',
  244. }
  245. for (const [status, message] of Object.entries(compileErrorStatuses)) {
  246. it(`displays error message for '${status}' status`, function () {
  247. cy.intercept('POST', '/project/*/compile*', {
  248. body: {
  249. status,
  250. clsiServerId: 'foo',
  251. compileGroup: 'priority',
  252. },
  253. }).as('compile')
  254. const scope = mockScope()
  255. cy.mount(
  256. <EditorProviders scope={scope}>
  257. <div className="pdf-viewer">
  258. <PdfPreview />
  259. </div>
  260. </EditorProviders>
  261. )
  262. cy.findByRole('button', { name: 'Recompile' }).click()
  263. cy.wait('@compile')
  264. cy.findByText(message)
  265. })
  266. }
  267. })
  268. it('displays expandable raw logs', function () {
  269. cy.interceptCompile()
  270. const scope = mockScope()
  271. cy.mount(
  272. <EditorProviders scope={scope}>
  273. <div className="pdf-viewer">
  274. <PdfPreview />
  275. </div>
  276. </EditorProviders>
  277. )
  278. cy.findByRole('button', { name: 'Recompile' }).click()
  279. cy.waitForCompile({ pdf: true })
  280. cy.findByRole('button', { name: 'View logs' }).click()
  281. cy.findByRole('button', { name: 'View PDF' })
  282. cy.findByRole('button', { name: 'Expand' }).click()
  283. cy.findByRole('button', { name: 'Collapse' }).click()
  284. })
  285. it('displays error messages if there were validation problems', function () {
  286. const validationProblems = {
  287. sizeCheck: {
  288. resources: [
  289. { path: 'foo/bar', kbSize: 76221 },
  290. { path: 'bar/baz', kbSize: 2342 },
  291. ],
  292. },
  293. mainFile: true,
  294. conflictedPaths: [
  295. {
  296. path: 'foo/bar',
  297. },
  298. {
  299. path: 'foo/baz',
  300. },
  301. ],
  302. }
  303. cy.intercept('POST', '/project/*/compile*', {
  304. body: {
  305. status: 'validation-problems',
  306. validationProblems,
  307. clsiServerId: 'foo',
  308. compileGroup: 'priority',
  309. },
  310. }).as('compile')
  311. const scope = mockScope()
  312. cy.mount(
  313. <EditorProviders scope={scope}>
  314. <div className="pdf-viewer">
  315. <PdfPreview />
  316. </div>
  317. </EditorProviders>
  318. )
  319. cy.findByRole('button', { name: 'Recompile' }).click()
  320. cy.wait('@compile')
  321. cy.findByText('Project too large')
  322. cy.findByText('Unknown main document')
  323. cy.findByText('Conflicting Paths Found')
  324. })
  325. describe('clear cache', function () {
  326. it('sends a clear cache request when the button is pressed', function () {
  327. cy.interceptCompile()
  328. const scope = mockScope()
  329. cy.mount(
  330. <EditorProviders scope={scope}>
  331. <div className="pdf-viewer">
  332. <PdfPreview />
  333. </div>
  334. </EditorProviders>
  335. )
  336. cy.findByRole('button', { name: 'Recompile' }).click()
  337. cy.waitForCompile({ pdf: true })
  338. cy.findByRole('button', { name: 'View logs' }).click()
  339. cy.findByRole('button', { name: 'Clear cached files' }).should(
  340. 'not.be.disabled'
  341. )
  342. cy.intercept('DELETE', '/project/*/output*', {
  343. statusCode: 204,
  344. delay: 100,
  345. }).as('clear-cache')
  346. // click the button
  347. cy.findByRole('button', { name: 'Clear cached files' }).click()
  348. cy.findByRole('button', { name: 'Clear cached files' }).should(
  349. 'be.disabled'
  350. )
  351. cy.wait('@clear-cache')
  352. cy.findByRole('button', { name: 'Clear cached files' }).should(
  353. 'not.be.disabled'
  354. )
  355. })
  356. it('handle "recompile from scratch"', function () {
  357. cy.interceptCompile()
  358. const scope = mockScope()
  359. cy.mount(
  360. <EditorProviders scope={scope}>
  361. <div className="pdf-viewer">
  362. <PdfPreview />
  363. </div>
  364. </EditorProviders>
  365. )
  366. cy.findByRole('button', { name: 'Recompile' }).click()
  367. cy.waitForCompile({ pdf: true })
  368. cy.interceptCompile('recompile')
  369. cy.intercept('DELETE', '/project/*/output*', {
  370. statusCode: 204,
  371. delay: 100,
  372. }).as('clear-cache')
  373. // show the logs UI
  374. cy.findByRole('button', { name: 'View logs' }).click()
  375. cy.findByRole('button', { name: 'Clear cached files' }).should(
  376. 'not.be.disabled'
  377. )
  378. // TODO: open the menu?
  379. cy.findByRole('menuitem', {
  380. name: 'Recompile from scratch',
  381. hidden: true,
  382. }).trigger('click', { force: true })
  383. cy.findByRole('button', { name: 'Clear cached files' }).should(
  384. 'be.disabled'
  385. )
  386. cy.findByRole('button', { name: 'Compiling…' })
  387. cy.wait('@clear-cache')
  388. // wait for recompile from scratch to finish
  389. cy.waitForCompile({ pdf: true, prefix: 'recompile' })
  390. cy.findByRole('button', { name: 'Recompile' })
  391. })
  392. })
  393. describe('invalid URLs and broken PDFs', function () {
  394. it('shows an error for an invalid URL', function () {
  395. cy.interceptCompile()
  396. cy.intercept('/build/*/output.pdf*', {
  397. statusCode: 500,
  398. body: {
  399. message: 'something awful happened',
  400. code: 'AWFUL_ERROR',
  401. },
  402. }).as('compile-pdf-error')
  403. const scope = mockScope()
  404. cy.mount(
  405. <EditorProviders scope={scope}>
  406. <div className="pdf-viewer">
  407. <PdfPreview />
  408. </div>
  409. </EditorProviders>
  410. )
  411. cy.findByRole('button', { name: 'Recompile' }).click()
  412. cy.waitForCompile()
  413. cy.wait('@compile-pdf-error')
  414. cy.contains('Something went wrong while rendering this PDF.')
  415. cy.contains(
  416. 'Please try recompiling the project from scratch, and if that doesn’t help, follow our troubleshooting guide.'
  417. )
  418. cy.findByLabelText('Page 1').should('not.exist')
  419. })
  420. it('shows an error for a corrupt PDF', function () {
  421. cy.interceptCompile()
  422. cy.intercept('/build/*/output.pdf*', {
  423. fixture: 'build/output-corrupt.pdf,null',
  424. }).as('compile-pdf-corrupt')
  425. const scope = mockScope()
  426. cy.mount(
  427. <EditorProviders scope={scope}>
  428. <div className="pdf-viewer">
  429. <PdfPreview />
  430. </div>
  431. </EditorProviders>
  432. )
  433. cy.findByRole('button', { name: 'Recompile' }).click()
  434. cy.waitForCompile()
  435. cy.wait('@compile-pdf-corrupt')
  436. cy.contains('Something went wrong while rendering this PDF.')
  437. cy.contains(
  438. 'Please try recompiling the project from scratch, and if that doesn’t help, follow our troubleshooting guide.'
  439. )
  440. cy.findByLabelText('Page 1').should('not.exist')
  441. })
  442. })
  443. describe('human readable logs', function () {
  444. it('shows human readable hint for undefined reference errors', function () {
  445. cy.interceptCompile()
  446. cy.intercept('/build/*/output.log*', {
  447. fixture: 'build/output-human-readable.log',
  448. }).as('compile-log')
  449. const scope = mockScope()
  450. cy.mount(
  451. <EditorProviders scope={scope}>
  452. <div className="pdf-viewer">
  453. <PdfPreview />
  454. </div>
  455. </EditorProviders>
  456. )
  457. cy.findByRole('button', { name: 'Recompile' }).click()
  458. cy.waitForCompile()
  459. cy.findByRole('button', { name: 'View logs' }).click()
  460. cy.findByText(
  461. "Reference `intorduction' on page 1 undefined on input line 11."
  462. )
  463. cy.findByText(
  464. "Reference `section1' on page 1 undefined on input line 13."
  465. )
  466. cy.findByText('There were undefined references.')
  467. cy.findAllByText(
  468. /You have referenced something which has not yet been labelled/
  469. ).should('have.length', 3)
  470. })
  471. it('does not show human readable hint when no undefined reference errors', function () {
  472. cy.interceptCompile()
  473. cy.intercept('/build/*/output.log?*', {
  474. fixture: 'build/output-undefined-references.log',
  475. }).as('compile-log')
  476. const scope = mockScope()
  477. cy.mount(
  478. <EditorProviders scope={scope}>
  479. <div className="pdf-viewer">
  480. <PdfPreview />
  481. </div>
  482. </EditorProviders>
  483. )
  484. cy.findByRole('button', { name: 'Recompile' }).click()
  485. cy.waitForCompile()
  486. cy.findByRole('button', { name: 'View logs' }).click()
  487. cy.findByText(
  488. "Package rerunfilecheck Warning: File `output.brf' has changed. Rerun to get bibliographical references right."
  489. )
  490. cy.findByText(
  491. /You have referenced something which has not yet been labelled/
  492. ).should('not.exist')
  493. })
  494. })
  495. })