pdf-preview.spec.tsx 16 KB

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