pdf-preview.spec.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. const storeAndFireEvent = (win: typeof window, key: string, value: unknown) => {
  6. localStorage.setItem(key, value)
  7. win.dispatchEvent(new StorageEvent('storage', { key }))
  8. }
  9. describe('<PdfPreview/>', function () {
  10. beforeEach(function () {
  11. cy.interceptCompile()
  12. cy.interceptEvents()
  13. })
  14. it('renders the PDF preview', function () {
  15. const scope = mockScope()
  16. cy.mount(
  17. <EditorProviders scope={scope}>
  18. <div className="pdf-viewer">
  19. <PdfPreview />
  20. </div>
  21. </EditorProviders>
  22. )
  23. // wait for "compile on load" to finish
  24. cy.findByRole('button', { name: 'Compiling…' })
  25. cy.wait('@compile')
  26. cy.findByRole('button', { name: 'Recompile' })
  27. cy.wait('@compile-pdf')
  28. })
  29. it('runs a compile when the Recompile button is pressed', function () {
  30. const scope = mockScope()
  31. cy.mount(
  32. <EditorProviders scope={scope}>
  33. <div className="pdf-viewer">
  34. <PdfPreview />
  35. </div>
  36. </EditorProviders>
  37. )
  38. // wait for "compile on load" to finish
  39. cy.findByRole('button', { name: 'Compiling…' })
  40. cy.wait('@compile')
  41. cy.wait('@compile-pdf')
  42. cy.interceptCompile('recompile')
  43. // press the Recompile button => compile
  44. cy.findByRole('button', { name: 'Recompile' }).click()
  45. // wait for "recompile" to finish
  46. // cy.findByRole('button', { name: 'Compiling…' })
  47. cy.wait('@recompile-pdf')
  48. cy.wait('@recompile-log')
  49. cy.wait('@recompile-blg')
  50. cy.findByRole('button', { name: 'Recompile' })
  51. cy.contains('Your Paper')
  52. })
  53. it('runs a compile on `pdf:recompile` event', function () {
  54. const scope = mockScope()
  55. cy.mount(
  56. <EditorProviders scope={scope}>
  57. <div className="pdf-viewer">
  58. <PdfPreview />
  59. </div>
  60. </EditorProviders>
  61. )
  62. // wait for "compile on load" to finish
  63. cy.findByRole('button', { name: 'Compiling…' })
  64. cy.wait('@compile')
  65. cy.wait('@compile-pdf')
  66. cy.interceptCompile('recompile')
  67. cy.window().then(win => {
  68. win.dispatchEvent(new CustomEvent('pdf:recompile'))
  69. })
  70. // wait for "recompile" to finish
  71. // cy.findByRole('button', { name: 'Compiling…' })
  72. cy.wait('@recompile')
  73. cy.findByRole('button', { name: 'Recompile' })
  74. cy.wait('@recompile-pdf')
  75. cy.contains('Your Paper')
  76. })
  77. it('does not compile while compiling', function () {
  78. let compileResolve: (value?: unknown) => void
  79. let counter = 0
  80. const promise = new Promise(resolve => {
  81. compileResolve = resolve
  82. })
  83. cy.intercept(
  84. 'POST',
  85. '/project/project123/compile?auto_compile=true',
  86. req => {
  87. counter++
  88. promise.then(() => {
  89. req.reply({
  90. body: {
  91. status: 'success',
  92. clsiServerId: 'foo',
  93. compileGroup: 'priority',
  94. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  95. outputFiles: [
  96. {
  97. path: 'output.pdf',
  98. build: '123',
  99. url: '/build/123/output.pdf',
  100. type: 'pdf',
  101. },
  102. {
  103. path: 'output.log',
  104. build: '123',
  105. url: '/build/123/output.log',
  106. type: 'log',
  107. },
  108. ],
  109. },
  110. })
  111. })
  112. return promise
  113. }
  114. ).as('compile')
  115. const scope = mockScope()
  116. cy.mount(
  117. <EditorProviders scope={scope}>
  118. <div className="pdf-viewer">
  119. <PdfPreview />
  120. </div>
  121. </EditorProviders>
  122. ).then(() => {
  123. cy.findByRole('button', { name: 'Compiling…' })
  124. cy.window().then(win => {
  125. win.dispatchEvent(new CustomEvent('pdf:recompile'))
  126. })
  127. compileResolve()
  128. cy.findByRole('button', { name: 'Recompile' })
  129. cy.contains('Your Paper').should(() => {
  130. expect(counter).to.equal(1)
  131. })
  132. })
  133. })
  134. it('disables compile button while compile is running', function () {
  135. const scope = mockScope()
  136. cy.mount(
  137. <EditorProviders scope={scope}>
  138. <div className="pdf-viewer">
  139. <PdfPreview />
  140. </div>
  141. </EditorProviders>
  142. )
  143. cy.findByRole('button', { name: 'Compiling…' }).should('be.disabled')
  144. cy.findByRole('button', { name: 'Recompile' }).should('not.be.disabled')
  145. })
  146. it('runs a compile on doc change if autocompile is enabled', function () {
  147. const scope = mockScope()
  148. cy.mount(
  149. <EditorProviders scope={scope}>
  150. <div className="pdf-viewer">
  151. <PdfPreview />
  152. </div>
  153. </EditorProviders>
  154. )
  155. // wait for "compile on load" to finish
  156. cy.findByRole('button', { name: 'Compiling…' })
  157. cy.wait('@compile')
  158. cy.findByRole('button', { name: 'Recompile' })
  159. cy.window().then(win => {
  160. cy.clock()
  161. // switch on auto compile
  162. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  163. // fire a doc:changed event => compile
  164. win.dispatchEvent(new CustomEvent('doc:changed'))
  165. cy.tick(5000) // AUTO_COMPILE_DEBOUNCE
  166. cy.clock().invoke('restore')
  167. })
  168. cy.findByRole('button', { name: 'Compiling…' })
  169. cy.findByRole('button', { name: 'Recompile' })
  170. })
  171. it('does not run a compile on doc change if autocompile is disabled', function () {
  172. const scope = mockScope()
  173. cy.mount(
  174. <EditorProviders scope={scope}>
  175. <div className="pdf-viewer">
  176. <PdfPreview />
  177. </div>
  178. </EditorProviders>
  179. )
  180. // wait for "compile on load" to finish
  181. cy.findByRole('button', { name: 'Compiling…' })
  182. cy.findByRole('button', { name: 'Recompile' })
  183. cy.window().then(win => {
  184. cy.clock()
  185. // make sure auto compile is switched off
  186. storeAndFireEvent(win, 'autocompile_enabled:project123', false)
  187. // fire a doc:changed event => no compile
  188. win.dispatchEvent(new CustomEvent('doc:changed'))
  189. cy.tick(5000) // AUTO_COMPILE_DEBOUNCE
  190. cy.clock().invoke('restore')
  191. })
  192. cy.findByRole('button', { name: 'Recompile' })
  193. })
  194. it('does not run a compile on doc change if autocompile is blocked by syntax check', function () {
  195. const scope = mockScope()
  196. // enable linting in the editor
  197. scope.settings.syntaxValidation = true
  198. // mock a linting error
  199. scope.hasLintingError = true
  200. cy.mount(
  201. <EditorProviders scope={scope}>
  202. <div className="pdf-viewer">
  203. <PdfPreview />
  204. </div>
  205. </EditorProviders>
  206. )
  207. // wait for "compile on load" to finish
  208. cy.findByRole('button', { name: 'Compiling…' })
  209. cy.findByRole('button', { name: 'Recompile' })
  210. cy.window().then(win => {
  211. cy.clock()
  212. // switch on auto compile
  213. storeAndFireEvent(win, 'autocompile_enabled:project123', true)
  214. // switch on syntax checking
  215. storeAndFireEvent(win, 'stop_on_validation_error', true)
  216. // fire a doc:changed event => no compile
  217. win.dispatchEvent(new CustomEvent('doc:changed'))
  218. cy.tick(5000) // AUTO_COMPILE_DEBOUNCE
  219. cy.clock().invoke('restore')
  220. })
  221. cy.findByRole('button', { name: 'Recompile' })
  222. cy.findByText('Code check failed')
  223. })
  224. describe('displays 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. // wait for "compile on load" to finish
  263. cy.findByRole('button', { name: 'Compiling…' })
  264. cy.findByRole('button', { name: 'Recompile' })
  265. cy.findByText(message)
  266. })
  267. }
  268. it('displays expandable raw logs', function () {
  269. const scope = mockScope()
  270. cy.mount(
  271. <EditorProviders scope={scope}>
  272. <div className="pdf-viewer">
  273. <PdfPreview />
  274. </div>
  275. </EditorProviders>
  276. )
  277. // wait for "compile on load" to finish
  278. cy.findByRole('button', { name: 'Compiling…' })
  279. cy.findByRole('button', { name: 'Recompile' })
  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. // wait for "compile on load" to finish
  320. cy.findByRole('button', { name: 'Compiling…' })
  321. cy.findByRole('button', { name: 'Recompile' })
  322. cy.wait('@compile')
  323. cy.findByText('Project too large')
  324. cy.findByText('Unknown main document')
  325. cy.findByText('Conflicting Paths Found')
  326. })
  327. it('sends a clear cache request when the button is pressed', function () {
  328. const scope = mockScope()
  329. cy.mount(
  330. <EditorProviders scope={scope}>
  331. <div className="pdf-viewer">
  332. <PdfPreview />
  333. </div>
  334. </EditorProviders>
  335. )
  336. // wait for "compile on load" to finish
  337. cy.findByRole('button', { name: 'Compiling…' })
  338. cy.findByRole('button', { name: 'Recompile' })
  339. cy.findByRole('button', { name: 'View logs' }).click()
  340. cy.findByRole('button', { name: 'Clear cached files' }).should(
  341. 'not.be.disabled'
  342. )
  343. cy.intercept('DELETE', 'project/*/output?*', {
  344. statusCode: 204,
  345. delay: 100,
  346. }).as('clear-cache')
  347. // click the button
  348. cy.findByRole('button', { name: 'Clear cached files' }).click()
  349. cy.findByRole('button', { name: 'Clear cached files' }).should(
  350. 'be.disabled'
  351. )
  352. cy.wait('@clear-cache')
  353. cy.findByRole('button', { name: 'Clear cached files' }).should(
  354. 'not.be.disabled'
  355. )
  356. })
  357. it('handle "recompile from scratch"', function () {
  358. const scope = mockScope()
  359. cy.mount(
  360. <EditorProviders scope={scope}>
  361. <div className="pdf-viewer">
  362. <PdfPreview />
  363. </div>
  364. </EditorProviders>
  365. )
  366. // wait for "compile on load" to finish
  367. cy.findByRole('button', { name: 'Compiling…' })
  368. cy.findByRole('button', { name: 'Recompile' })
  369. // show the logs UI
  370. cy.findByRole('button', { name: 'View logs' }).click()
  371. cy.findByRole('button', { name: 'Clear cached files' }).should(
  372. 'not.be.disabled'
  373. )
  374. cy.interceptCompile()
  375. cy.intercept('DELETE', 'project/*/output?*', {
  376. statusCode: 204,
  377. delay: 100,
  378. }).as('clear-cache')
  379. // TODO: open the menu?
  380. cy.findByRole('menuitem', {
  381. name: 'Recompile from scratch',
  382. hidden: true,
  383. }).trigger('click', { force: true })
  384. cy.findByRole('button', { name: 'Clear cached files' }).should(
  385. 'be.disabled'
  386. )
  387. cy.findByRole('button', { name: 'Compiling…' })
  388. cy.wait('@clear-cache')
  389. cy.findByRole('button', { name: 'Recompile' })
  390. cy.wait('@compile')
  391. cy.wait('@compile-pdf')
  392. })
  393. it('shows an error for an invalid URL', function () {
  394. cy.intercept('/build/*/output.pdf?*', {
  395. statusCode: 500,
  396. body: {
  397. message: 'something awful happened',
  398. code: 'AWFUL_ERROR',
  399. },
  400. }).as('compile-pdf-error')
  401. const scope = mockScope()
  402. cy.mount(
  403. <EditorProviders scope={scope}>
  404. <div className="pdf-viewer">
  405. <PdfPreview />
  406. </div>
  407. </EditorProviders>
  408. )
  409. cy.wait('@compile-pdf-error')
  410. cy.findByText('Something went wrong while rendering this PDF.')
  411. cy.findByLabelText('Page 1').should('not.exist')
  412. })
  413. it('shows an error for a corrupt PDF', function () {
  414. cy.intercept('/build/*/output.pdf?*', {
  415. fixture: 'build/output-corrupt.pdf,null',
  416. }).as('compile-pdf-corrupt')
  417. const scope = mockScope()
  418. cy.mount(
  419. <EditorProviders scope={scope}>
  420. <div className="pdf-viewer">
  421. <PdfPreview />
  422. </div>
  423. </EditorProviders>
  424. )
  425. cy.wait('@compile-pdf-corrupt')
  426. cy.findByText('Something went wrong while rendering this PDF.')
  427. cy.findByLabelText('Page 1').should('not.exist')
  428. })
  429. })
  430. describe('human readable logs', function () {
  431. it('shows human readable hint for undefined reference errors', function () {
  432. cy.intercept('/build/*/output.log?*', {
  433. fixture: 'build/output-human-readable.log',
  434. }).as('log')
  435. const scope = mockScope()
  436. cy.mount(
  437. <EditorProviders scope={scope}>
  438. <div className="pdf-viewer">
  439. <PdfPreview />
  440. </div>
  441. </EditorProviders>
  442. )
  443. cy.wait('@log')
  444. cy.findByRole('button', { name: 'View logs' }).click()
  445. cy.findByText(
  446. "Reference `intorduction' on page 1 undefined on input line 11."
  447. )
  448. cy.findByText(
  449. "Reference `section1' on page 1 undefined on input line 13."
  450. )
  451. cy.findByText('There were undefined references.')
  452. cy.findAllByText(
  453. /You have referenced something which has not yet been labelled/
  454. ).should('have.length', 3)
  455. })
  456. it('does not show human readable hint when no undefined reference errors', function () {
  457. cy.intercept('/build/*/output.log?*', {
  458. fixture: 'build/output-undefined-references.log',
  459. }).as('log')
  460. const scope = mockScope()
  461. cy.mount(
  462. <EditorProviders scope={scope}>
  463. <div className="pdf-viewer">
  464. <PdfPreview />
  465. </div>
  466. </EditorProviders>
  467. )
  468. cy.wait('@log')
  469. cy.findByRole('button', { name: 'View logs' }).click()
  470. cy.findByText(
  471. "Package rerunfilecheck Warning: File `output.brf' has changed. Rerun to get bibliographical references right."
  472. )
  473. cy.findByText(
  474. /You have referenced something which has not yet been labelled/
  475. ).should('not.exist')
  476. })
  477. })
  478. })