pdf-preview.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. import localStorage from '@/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 {
  6. IdeLayout,
  7. IdeView,
  8. useLayoutContext,
  9. } from '../../../../frontend/js/shared/context/layout-context'
  10. import { FC, useEffect } from 'react'
  11. const storeAndFireEvent = (win: typeof window, key: string, value: unknown) => {
  12. localStorage.setItem(key, value)
  13. win.dispatchEvent(new StorageEvent('storage', { key }))
  14. }
  15. const Layout: FC<{ layout: IdeLayout; view?: IdeView }> = ({
  16. layout,
  17. view,
  18. }) => {
  19. const { changeLayout } = useLayoutContext()
  20. useEffect(() => {
  21. changeLayout(layout, view)
  22. }, [changeLayout, layout, view])
  23. return null
  24. }
  25. describe('<PdfPreview/>', function () {
  26. let projectId: string
  27. beforeEach(function () {
  28. /**
  29. * There are time sensitive tests in this test suite. They need to wait for a Promise before resolving a request.
  30. *
  31. * Using a promise across the test-env (browser) vs stub-env (server) causes additional latency.
  32. *
  33. * This latency seems to stack up when adding more intercepts for the same path. Using static responses for some of these intercepts does not help.
  34. *
  35. * All of that seems like a bug in Cypress. For now just work around it by using a unique projectId for each intercept.
  36. */
  37. projectId = Math.random().toString().slice(2)
  38. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  39. window.metaAttributesCache.set(
  40. 'ol-compilesUserContentDomain',
  41. 'https://compiles-user.dev-overleaf.com'
  42. )
  43. window.metaAttributesCache.set('ol-splitTestVariants', {
  44. 'populate-clsi-cache': 'enabled',
  45. })
  46. window.metaAttributesCache.set('ol-projectOwnerHasPremiumOnPageLoad', true)
  47. window.metaAttributesCache.set('ol-compileSettings', {
  48. reducedWarningThreshold: 'default',
  49. compileTimeout: 240,
  50. })
  51. cy.interceptEvents()
  52. })
  53. it('renders the PDF preview', function () {
  54. window.metaAttributesCache.set('ol-preventCompileOnLoad', false)
  55. cy.interceptCompile()
  56. const scope = mockScope()
  57. cy.mount(
  58. <EditorProviders scope={scope}>
  59. <div className="pdf-viewer">
  60. <PdfPreview />
  61. </div>
  62. </EditorProviders>
  63. )
  64. // wait for "compile on load" to finish
  65. cy.waitForCompile({ pdf: true })
  66. cy.findByRole('button', { name: 'Recompile' })
  67. })
  68. it('uses the cache when available', function () {
  69. cy.interceptCompile({
  70. prefix: 'compile',
  71. times: 1,
  72. cached: true,
  73. regular: false,
  74. })
  75. const scope = mockScope()
  76. cy.mount(
  77. <EditorProviders scope={scope}>
  78. <div className="pdf-viewer">
  79. <PdfPreview />
  80. </div>
  81. </EditorProviders>
  82. )
  83. // wait for "compile from cache on load" to finish
  84. cy.waitForCompile({ pdf: true, cached: true, regular: false })
  85. cy.contains('Your Paper')
  86. })
  87. it('uses the cache when available then compiles', function () {
  88. cy.interceptCompile({
  89. prefix: 'compile',
  90. times: 1,
  91. cached: true,
  92. regular: false,
  93. })
  94. const scope = mockScope()
  95. cy.mount(
  96. <EditorProviders scope={scope}>
  97. <div className="pdf-viewer">
  98. <PdfPreview />
  99. </div>
  100. </EditorProviders>
  101. )
  102. // wait for "compile from cache on load" to finish
  103. cy.waitForCompile({ pdf: true, cached: true, regular: false })
  104. cy.contains('Your Paper')
  105. // Then trigger a new compile
  106. cy.interceptCompile({
  107. prefix: 'recompile',
  108. times: 1,
  109. cached: false,
  110. outputPDFFixture: 'output-2.pdf',
  111. })
  112. // press the Recompile button => compile
  113. cy.findByRole('button', { name: 'Recompile' }).click()
  114. // wait for compile to finish
  115. cy.waitForCompile({ prefix: 'recompile', pdf: true })
  116. cy.contains('Modern Authoring Tools for Science')
  117. })
  118. describe('racing compile from cache and regular compile trigger', function () {
  119. for (const [timing] of ['before rendering', 'after rendering']) {
  120. it(`replaces the compile from cache with a regular compile - ${timing}`, function () {
  121. const requestedOnce = new Set()
  122. ;['log', 'pdf', 'blg'].forEach(ext => {
  123. cy.intercept({ pathname: `/build/*/output.${ext}` }, req => {
  124. if (requestedOnce.has(ext)) {
  125. throw new Error(
  126. `compile from cache triggered extra ${ext} request: ${req.url}`
  127. )
  128. }
  129. requestedOnce.add(ext)
  130. req.reply({ fixture: `build/output.${ext},null` })
  131. }).as(`compile-${ext}`)
  132. })
  133. const { promise, resolve } = Promise.withResolvers<void>()
  134. cy.interceptCompileFromCacheRequest({
  135. promise,
  136. times: 1,
  137. }).as('cached-compile')
  138. cy.interceptCompileRequest().as('compile')
  139. const scope = mockScope()
  140. cy.mount(
  141. <EditorProviders scope={scope} projectId={projectId}>
  142. <div className="pdf-viewer">
  143. <PdfPreview />
  144. </div>
  145. </EditorProviders>
  146. )
  147. // press the Recompile button => compile
  148. cy.findByRole('button', { name: 'Recompile' }).click()
  149. if (timing === 'before rendering') {
  150. cy.then(() => resolve())
  151. cy.wait('@cached-compile')
  152. }
  153. // wait for rendering to finish
  154. cy.waitForCompile({ pdf: true, cached: false })
  155. if (timing === 'after rendering') {
  156. cy.then(() => resolve())
  157. cy.wait('@cached-compile')
  158. }
  159. cy.contains('Your Paper')
  160. cy.then(() => Array.from(requestedOnce).sort().join(',')).should(
  161. 'equal',
  162. 'blg,log,pdf'
  163. )
  164. })
  165. }
  166. })
  167. describe('clsi-cache project settings validation', function () {
  168. const cases = {
  169. // Flaky, skip for now
  170. 'uses compile from cache when nothing changed': {
  171. cached: true,
  172. setup: () => {},
  173. props: {},
  174. },
  175. 'ignores the compile from cache when imageName changed': {
  176. cached: false,
  177. setup: () => {},
  178. props: {
  179. imageName: 'texlive-full:2025.1',
  180. },
  181. },
  182. 'ignores the compile from cache when compiler changed': {
  183. cached: false,
  184. setup: () => {},
  185. props: {
  186. compiler: 'lualatex',
  187. },
  188. },
  189. 'ignores the compile from cache when draft mode changed': {
  190. cached: false,
  191. setup: () => {
  192. cy.window().then(w =>
  193. w.localStorage.setItem(`draft:${projectId}`, 'true')
  194. )
  195. },
  196. props: {},
  197. },
  198. 'ignores the compile from cache when stopOnFirstError mode changed': {
  199. cached: false,
  200. setup: () => {
  201. cy.window().then(w =>
  202. w.localStorage.setItem(`stop_on_first_error:${projectId}`, 'true')
  203. )
  204. },
  205. props: {},
  206. },
  207. 'ignores the compile from cache when rootDoc changed': {
  208. cached: false,
  209. setup: () => {},
  210. props: {
  211. rootDocId: 'new-root-doc-id',
  212. rootFolder: [
  213. {
  214. _id: 'root-folder-id',
  215. name: 'rootFolder',
  216. docs: [
  217. {
  218. _id: '_root_doc_id',
  219. name: 'main.tex',
  220. },
  221. {
  222. _id: 'new-root-doc-id',
  223. name: 'new-main.tex',
  224. },
  225. ],
  226. folders: [],
  227. fileRefs: [],
  228. },
  229. ],
  230. },
  231. },
  232. }
  233. Object.entries(cases).forEach(([name, { cached, setup, props }]) => {
  234. it(name, function () {
  235. cy.interceptCompile({
  236. cached: true,
  237. regular: !cached,
  238. })
  239. const scope = mockScope()
  240. window.metaAttributesCache.set('ol-preventCompileOnLoad', false)
  241. setup()
  242. cy.mount(
  243. <EditorProviders scope={scope} projectId={projectId} {...props}>
  244. <div className="pdf-viewer">
  245. <PdfPreview />
  246. </div>
  247. </EditorProviders>
  248. )
  249. // wait for compile to finish
  250. cy.waitForCompile({ pdf: true, cached, regular: !cached })
  251. cy.contains('Your Paper')
  252. })
  253. })
  254. })
  255. it('runs a compile when the Recompile button is pressed', function () {
  256. cy.interceptCompile()
  257. const scope = mockScope()
  258. cy.mount(
  259. <EditorProviders scope={scope}>
  260. <div className="pdf-viewer">
  261. <PdfPreview />
  262. </div>
  263. </EditorProviders>
  264. )
  265. // press the Recompile button => compile
  266. cy.findByRole('button', { name: 'Recompile' }).click()
  267. // wait for compile to finish
  268. cy.waitForCompile({ pdf: true })
  269. cy.contains('Your Paper')
  270. })
  271. it('runs a compile on `pdf:recompile` event', function () {
  272. cy.interceptCompile()
  273. const scope = mockScope()
  274. cy.mount(
  275. <EditorProviders scope={scope}>
  276. <div className="pdf-viewer">
  277. <PdfPreview />
  278. </div>
  279. </EditorProviders>
  280. )
  281. cy.window().then(win => {
  282. win.dispatchEvent(new CustomEvent('pdf:recompile'))
  283. })
  284. // wait for compile to finish
  285. cy.waitForCompile({ pdf: true })
  286. cy.contains('Your Paper')
  287. })
  288. it('does not compile while compiling', function () {
  289. let counter = 0
  290. cy.interceptDeferredCompile(() => counter++).then(
  291. resolveDeferredCompile => {
  292. const scope = mockScope()
  293. cy.mount(
  294. <EditorProviders scope={scope}>
  295. <div className="pdf-viewer">
  296. <PdfPreview />
  297. </div>
  298. </EditorProviders>
  299. )
  300. // start compiling
  301. cy.findByRole('button', { name: 'Recompile' }).click()
  302. cy.findByRole('button', { name: 'Compiling…' }).then(() => {
  303. // trigger a recompile
  304. cy.window().then(win => {
  305. win.dispatchEvent(new CustomEvent('pdf:recompile'))
  306. })
  307. // finish the original compile
  308. resolveDeferredCompile()
  309. // wait for the original compile to finish
  310. cy.waitForCompile().then(() => {
  311. // NOTE: difficult to assert that a second request won't be sent, at some point
  312. expect(counter).to.equal(1)
  313. })
  314. })
  315. }
  316. )
  317. })
  318. it('disables compile button while compile is running', function () {
  319. cy.interceptDeferredCompile().then(resolveDeferredCompile => {
  320. const scope = mockScope()
  321. cy.mount(
  322. <EditorProviders scope={scope}>
  323. <div className="pdf-viewer">
  324. <PdfPreview />
  325. </div>
  326. </EditorProviders>
  327. )
  328. cy.findByRole('button', { name: 'Recompile' }).click()
  329. cy.findByRole('button', { name: 'Compiling…' })
  330. .should('be.disabled')
  331. .then(() => resolveDeferredCompile())
  332. cy.waitForCompile()
  333. cy.findByRole('button', { name: 'Recompile' }).should('not.be.disabled')
  334. })
  335. })
  336. it('runs a compile on doc change if autocompile is enabled', function () {
  337. cy.interceptCompile()
  338. const scope = mockScope()
  339. cy.mount(
  340. <EditorProviders scope={scope}>
  341. <div className="pdf-viewer">
  342. <PdfPreview />
  343. </div>
  344. </EditorProviders>
  345. )
  346. cy.window().then(win => {
  347. cy.clock()
  348. // switch on auto compile
  349. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  350. // fire a doc:changed event => compile
  351. win.dispatchEvent(new CustomEvent('doc:changed'))
  352. // wait enough time for the compile to start
  353. cy.tick(6000) // > AUTO_COMPILE_DEBOUNCE
  354. cy.clock().invoke('restore')
  355. })
  356. // wait for compile to finish
  357. cy.waitForCompile({ pdf: true })
  358. cy.findByRole('button', { name: 'Recompile' })
  359. })
  360. it('does not run a compile on doc change if autocompile is disabled', function () {
  361. cy.interceptCompile()
  362. const scope = mockScope()
  363. cy.mount(
  364. <EditorProviders scope={scope}>
  365. <div className="pdf-viewer">
  366. <PdfPreview />
  367. </div>
  368. </EditorProviders>
  369. )
  370. cy.window().then(win => {
  371. cy.clock()
  372. // make sure auto compile is switched off
  373. storeAndFireEvent(win, 'autocompile_enabled:project123', false)
  374. // fire a doc:changed event => no compile
  375. win.dispatchEvent(new CustomEvent('doc:changed'))
  376. // wait enough time for the compile to start
  377. cy.tick(6000) // AUTO_COMPILE_DEBOUNCE
  378. cy.clock().invoke('restore')
  379. })
  380. // NOTE: difficult to assert that a request hasn't been sent
  381. cy.findByRole('button', { name: 'Recompile' })
  382. })
  383. it('does not run a compile on doc change if autocompile is blocked by syntax check', function () {
  384. cy.interceptCompile()
  385. const scope = mockScope()
  386. // enable linting in the editor
  387. const userSettings = { syntaxValidation: true }
  388. // mock a linting error
  389. scope.hasLintingError = true
  390. cy.mount(
  391. <EditorProviders scope={scope} userSettings={userSettings}>
  392. <div className="pdf-viewer">
  393. <PdfPreview />
  394. </div>
  395. </EditorProviders>
  396. )
  397. cy.window().then(win => {
  398. cy.clock()
  399. // switch on auto compile
  400. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  401. // switch on syntax checking
  402. storeAndFireEvent(win, 'stop_on_validation_error', true)
  403. // fire a doc:changed event => no compile
  404. win.dispatchEvent(new CustomEvent('doc:changed'))
  405. // wait enough time for the compile to start
  406. cy.tick(6000) // AUTO_COMPILE_DEBOUNCE
  407. cy.clock().invoke('restore')
  408. })
  409. // NOTE: difficult to assert that a request hasn't been sent
  410. cy.findByRole('button', { name: 'Recompile' })
  411. cy.findByText('Code check failed')
  412. })
  413. it('does not run a compile on doc change if the PDF preview is not open', function () {
  414. cy.interceptCompile()
  415. const scope = mockScope()
  416. cy.mount(
  417. <EditorProviders scope={scope}>
  418. <Layout layout="flat" view="editor" />
  419. <div className="pdf-viewer">
  420. <PdfPreview />
  421. </div>
  422. </EditorProviders>
  423. )
  424. cy.window().then(win => {
  425. cy.clock()
  426. // switch on auto compile
  427. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  428. // fire a doc:changed event => compile
  429. win.dispatchEvent(new CustomEvent('doc:changed'))
  430. // wait enough time for the compile to start
  431. cy.tick(6000) // > AUTO_COMPILE_DEBOUNCE
  432. cy.clock().invoke('restore')
  433. })
  434. // NOTE: difficult to assert that a request hasn't been sent
  435. cy.findByRole('button', { name: 'Recompile' })
  436. })
  437. describe('error messages', function () {
  438. const compileErrorStatuses = {
  439. 'clear-cache':
  440. 'Sorry, something went wrong and your project could not be compiled. Please try again in a few moments.',
  441. 'clsi-maintenance':
  442. 'The compile servers are down for maintenance, and will be back shortly.',
  443. 'compile-in-progress':
  444. 'A previous compile is still running. Please wait a minute and try compiling again.',
  445. exited: 'Server Error',
  446. failure: 'No PDF',
  447. generic: 'Server Error',
  448. 'project-too-large': 'Project too large',
  449. 'rate-limited': 'Compile rate limit hit',
  450. terminated: 'Compilation cancelled',
  451. timedout: 'Timed out',
  452. 'too-recently-compiled':
  453. 'This project was compiled very recently, so this compile has been skipped.',
  454. unavailable:
  455. 'Sorry, the compile server for your project was temporarily unavailable. Please try again in a few moments.',
  456. foo: 'Sorry, something went wrong and your project could not be compiled. Please try again in a few moments.',
  457. }
  458. for (const [status, message] of Object.entries(compileErrorStatuses)) {
  459. it(`displays error message for '${status}' status`, function () {
  460. cy.intercept('POST', '/project/*/compile*', {
  461. body: {
  462. status,
  463. clsiServerId: 'foo',
  464. compileGroup: 'priority',
  465. },
  466. }).as('compile')
  467. const scope = mockScope()
  468. cy.mount(
  469. <EditorProviders scope={scope}>
  470. <div className="pdf-viewer">
  471. <PdfPreview />
  472. </div>
  473. </EditorProviders>
  474. )
  475. cy.findByRole('button', { name: 'Recompile' }).click()
  476. cy.wait('@compile')
  477. cy.findByText(message)
  478. })
  479. }
  480. })
  481. it('displays expandable raw logs', function () {
  482. cy.interceptCompile()
  483. const scope = mockScope()
  484. cy.mount(
  485. <EditorProviders scope={scope}>
  486. <div className="pdf-viewer">
  487. <PdfPreview />
  488. </div>
  489. </EditorProviders>
  490. )
  491. cy.findByRole('button', { name: 'Recompile' }).click()
  492. cy.waitForCompile({ pdf: true })
  493. cy.findByRole('button', { name: 'View logs' }).click()
  494. cy.findByRole('button', { name: 'View PDF' })
  495. cy.findByRole('button', { name: 'Expand' }).click()
  496. cy.findByRole('button', { name: 'Collapse' }).click()
  497. })
  498. it('displays error messages if there were validation problems', function () {
  499. const validationProblems = {
  500. sizeCheck: {
  501. resources: [
  502. { path: 'foo/bar', kbSize: 76221 },
  503. { path: 'bar/baz', kbSize: 2342 },
  504. ],
  505. },
  506. mainFile: true,
  507. conflictedPaths: [
  508. {
  509. path: 'foo/bar',
  510. },
  511. {
  512. path: 'foo/baz',
  513. },
  514. ],
  515. }
  516. cy.intercept('POST', '/project/*/compile*', {
  517. body: {
  518. status: 'validation-problems',
  519. validationProblems,
  520. clsiServerId: 'foo',
  521. compileGroup: 'priority',
  522. },
  523. }).as('compile')
  524. const scope = mockScope()
  525. cy.mount(
  526. <EditorProviders scope={scope}>
  527. <div className="pdf-viewer">
  528. <PdfPreview />
  529. </div>
  530. </EditorProviders>
  531. )
  532. cy.findByRole('button', { name: 'Recompile' }).click()
  533. cy.wait('@compile')
  534. cy.findByText('Project too large')
  535. cy.findByText('Unknown main document')
  536. cy.findByText('Conflicting Paths Found')
  537. })
  538. describe('clear cache', function () {
  539. it('sends a clear cache request when the button is pressed', function () {
  540. cy.interceptCompile()
  541. const scope = mockScope()
  542. cy.mount(
  543. <EditorProviders scope={scope}>
  544. <div className="pdf-viewer">
  545. <PdfPreview />
  546. </div>
  547. </EditorProviders>
  548. )
  549. cy.findByRole('button', { name: 'Recompile' }).click()
  550. cy.waitForCompile({ pdf: true })
  551. cy.findByRole('button', { name: 'View logs' }).click()
  552. cy.findByRole('button', { name: 'Clear cached files' }).should(
  553. 'not.be.disabled'
  554. )
  555. const { promise, resolve } = Promise.withResolvers<void>()
  556. cy.intercept('DELETE', '/project/*/output*', req => {
  557. return promise
  558. .then(() => Cypress.Promise.delay(100))
  559. .then(() => {
  560. req.reply({ statusCode: 204 })
  561. })
  562. }).as('clear-cache')
  563. // click the button
  564. cy.findByRole('button', { name: 'Clear cached files' }).click()
  565. cy.findByRole('button', { name: 'Clear cached files' }).should(
  566. 'be.disabled'
  567. )
  568. cy.then(() => {
  569. resolve()
  570. })
  571. cy.wait('@clear-cache')
  572. cy.findByRole('button', { name: 'Clear cached files' }).should(
  573. 'not.be.disabled'
  574. )
  575. })
  576. it('handle "recompile from scratch"', function () {
  577. cy.interceptCompile()
  578. const scope = mockScope()
  579. cy.mount(
  580. <EditorProviders scope={scope}>
  581. <div className="pdf-viewer">
  582. <PdfPreview />
  583. </div>
  584. </EditorProviders>
  585. )
  586. cy.findByRole('button', { name: 'Recompile' }).click()
  587. cy.waitForCompile({ pdf: true })
  588. cy.interceptCompile({ prefix: 'recompile' })
  589. cy.intercept('DELETE', '/project/*/output*', {
  590. statusCode: 204,
  591. delay: 100,
  592. }).as('clear-cache')
  593. // show the logs UI
  594. cy.findByRole('button', { name: 'View logs' }).click()
  595. cy.findByRole('button', { name: 'Clear cached files' }).should(
  596. 'not.be.disabled'
  597. )
  598. cy.interceptDeferredCompile().then(resolveDeferredCompile => {
  599. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  600. cy.findByRole('menuitem', {
  601. name: 'Recompile from scratch',
  602. }).trigger('click')
  603. cy.findByRole('button', { name: 'Clear cached files' }).should(
  604. 'be.disabled'
  605. )
  606. cy.wait('@clear-cache')
  607. cy.findByRole('button', { name: 'Compiling…' }).then(() =>
  608. resolveDeferredCompile()
  609. )
  610. // wait for recompile from scratch to finish
  611. cy.waitForCompile({ pdf: true })
  612. cy.findByRole('button', { name: 'Recompile' })
  613. })
  614. })
  615. })
  616. describe('invalid URLs and broken PDFs', function () {
  617. it('shows an error for an invalid URL', function () {
  618. cy.interceptCompile()
  619. cy.intercept('/build/*/output.pdf*', {
  620. statusCode: 500,
  621. body: {
  622. message: 'something awful happened',
  623. code: 'AWFUL_ERROR',
  624. },
  625. }).as('compile-pdf-error')
  626. const scope = mockScope()
  627. cy.mount(
  628. <EditorProviders scope={scope}>
  629. <div className="pdf-viewer">
  630. <PdfPreview />
  631. </div>
  632. </EditorProviders>
  633. )
  634. cy.findByRole('button', { name: 'Recompile' }).click()
  635. cy.waitForCompile()
  636. cy.wait('@compile-pdf-error')
  637. cy.contains('Something went wrong while rendering this PDF.')
  638. cy.contains(
  639. 'Please try recompiling the project from scratch, and if that doesn’t help, follow our troubleshooting guide.'
  640. )
  641. cy.findByLabelText('Page 1').should('not.exist')
  642. })
  643. it('shows an error for a corrupt PDF', function () {
  644. cy.interceptCompile()
  645. cy.intercept('/build/*/output.pdf*', {
  646. fixture: 'build/output-corrupt.pdf,null',
  647. }).as('compile-pdf-corrupt')
  648. const scope = mockScope()
  649. cy.mount(
  650. <EditorProviders scope={scope}>
  651. <div className="pdf-viewer">
  652. <PdfPreview />
  653. </div>
  654. </EditorProviders>
  655. )
  656. cy.findByRole('button', { name: 'Recompile' }).click()
  657. cy.waitForCompile()
  658. cy.wait('@compile-pdf-corrupt')
  659. cy.contains('Something went wrong while rendering this PDF.')
  660. cy.contains(
  661. 'Please try recompiling the project from scratch, and if that doesn’t help, follow our troubleshooting guide.'
  662. )
  663. cy.findByLabelText('Page 1').should('not.exist')
  664. })
  665. })
  666. describe('human readable logs', function () {
  667. it('shows human readable hint for undefined reference errors', function () {
  668. cy.interceptCompile()
  669. cy.intercept('/build/*/output.log*', {
  670. fixture: 'build/output-human-readable.log',
  671. }).as('compile-log')
  672. const scope = mockScope()
  673. cy.mount(
  674. <EditorProviders scope={scope}>
  675. <div className="pdf-viewer">
  676. <PdfPreview />
  677. </div>
  678. </EditorProviders>
  679. )
  680. cy.findByRole('button', { name: 'Recompile' }).click()
  681. cy.waitForCompile()
  682. cy.findByRole('button', { name: 'View logs' }).click()
  683. cy.findByText(
  684. "Reference `intorduction' on page 1 undefined on input line 11."
  685. )
  686. cy.findByText(
  687. "Reference `section1' on page 1 undefined on input line 13."
  688. )
  689. cy.findByText('There were undefined references.')
  690. cy.findAllByText(
  691. /You have referenced something which has not yet been labelled/
  692. ).should('have.length', 3)
  693. })
  694. it('does not show human readable hint when no undefined reference errors', function () {
  695. cy.interceptCompile()
  696. cy.intercept('/build/*/output.log?*', {
  697. fixture: 'build/output-undefined-references.log',
  698. }).as('compile-log')
  699. const scope = mockScope()
  700. cy.mount(
  701. <EditorProviders scope={scope}>
  702. <div className="pdf-viewer">
  703. <PdfPreview />
  704. </div>
  705. </EditorProviders>
  706. )
  707. cy.findByRole('button', { name: 'Recompile' }).click()
  708. cy.waitForCompile()
  709. cy.findByRole('button', { name: 'View logs' }).click()
  710. cy.findByText(
  711. "Package rerunfilecheck Warning: File `output.brf' has changed. Rerun to get bibliographical references right."
  712. )
  713. cy.findByText(
  714. /You have referenced something which has not yet been labelled/
  715. ).should('not.exist')
  716. })
  717. })
  718. })