sandboxed-compiles.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { ensureUserExists, login } from './helpers/login'
  2. import { createProject } from './helpers/project'
  3. import { isExcludedBySharding, startWith } from './helpers/config'
  4. import { throttledRecompile, stopCompile } from './helpers/compile'
  5. import { v4 as uuid } from 'uuid'
  6. import { waitUntilScrollingFinished } from './helpers/waitUntilScrollingFinished'
  7. import { beforeWithReRunOnTestRetry } from './helpers/beforeWithReRunOnTestRetry'
  8. const LABEL_TEX_LIVE_VERSION = 'TeX Live version'
  9. describe('SandboxedCompiles', function () {
  10. const enabledVars = {
  11. SANDBOXED_COMPILES: 'true',
  12. ALL_TEX_LIVE_DOCKER_IMAGE_NAMES: '2023,2022',
  13. }
  14. describe('enabled in Server Pro', function () {
  15. if (isExcludedBySharding('PRO_CUSTOM_2')) return
  16. startWith({
  17. pro: true,
  18. vars: enabledVars,
  19. resetData: true,
  20. })
  21. ensureUserExists({ email: 'user@example.com' })
  22. beforeEach(function () {
  23. login('user@example.com')
  24. })
  25. it('should offer TexLive images and switch the compiler', function () {
  26. createProject('sandboxed')
  27. const recompile = throttledRecompile()
  28. cy.log('wait for compile')
  29. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  30. 'contain.text',
  31. 'sandboxed'
  32. )
  33. cy.log('Check which compiler version was used, expect 2023')
  34. cy.findByRole('button', { name: 'View logs' }).click()
  35. cy.findByText(/This is pdfTeX, Version .+ \(TeX Live 2023\) /)
  36. cy.log('Switch TeXLive version from 2023 to 2022')
  37. cy.findByRole('navigation', {
  38. name: 'Project actions',
  39. })
  40. .findByRole('button', { name: 'Menu' })
  41. .click()
  42. cy.findByRole('dialog').within(() => {
  43. cy.findByRole('option', { name: '2023' }).should('be.selected')
  44. cy.findByRole('combobox', { name: LABEL_TEX_LIVE_VERSION }).select(
  45. '2022'
  46. )
  47. })
  48. cy.get('body').type('{esc}')
  49. cy.findByRole('dialog').should('not.exist')
  50. cy.log('Trigger compile with other TeX Live version')
  51. recompile()
  52. cy.log('Check which compiler version was used, expect 2022')
  53. cy.findByRole('button', { name: 'View logs' }).click()
  54. cy.findByText(/This is pdfTeX, Version .+ \(TeX Live 2022\) /)
  55. })
  56. checkSyncTeX()
  57. checkXeTeX()
  58. checkRecompilesAfterErrors()
  59. checkStopCompile()
  60. })
  61. function checkStopCompile() {
  62. it('users can stop a running compile', function () {
  63. login('user@example.com')
  64. createProject('test-project')
  65. // create an infinite loop in the main document
  66. // this will cause the compile to run indefinitely
  67. cy.findByText('\\maketitle').parent().click()
  68. cy.findByText('\\maketitle')
  69. .parent()
  70. .type('\n\\def\\x{{}Hello!\\par\\x}\\x')
  71. cy.log('Start compile')
  72. // We need to start the compile manually because we do not want to wait for it to finish
  73. cy.findByText('Recompile').click()
  74. // Now stop the compile and kill the latex process
  75. stopCompile({ delay: 1000 })
  76. cy.get('.logs-pane')
  77. .invoke('text')
  78. .should('match', /PDF Rendering Error|Compilation cancelled/)
  79. // Check that the previous compile is not running in the background by
  80. // disabling the infinite loop and recompiling
  81. cy.findByText('\\def').parent().click()
  82. cy.findByText('\\def').parent().type('{home}disabled loop% ')
  83. cy.findByText('Recompile').click()
  84. cy.get('.pdf-viewer').should('contain.text', 'disabled loop')
  85. cy.get('.logs-pane').should(
  86. 'not.contain.text',
  87. 'A previous compile is still running'
  88. )
  89. })
  90. }
  91. function checkSyncTeX() {
  92. describe('SyncTeX', function () {
  93. let projectName: string
  94. beforeEach(function () {
  95. projectName = `Project ${uuid()}`
  96. createProject(projectName)
  97. const recompile = throttledRecompile()
  98. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(
  99. () => {
  100. cy.findByText('\\maketitle').parent().click()
  101. cy.findByText('\\maketitle')
  102. .parent()
  103. .type(
  104. `\n\\pagebreak\n\\section{{}Section A}\n\\pagebreak\n\\section{{}Section B}\n\\pagebreak`
  105. )
  106. }
  107. )
  108. recompile()
  109. cy.log('wait for pdf-rendering')
  110. cy.findByRole('region', { name: 'PDF preview and logs' }).findByText(
  111. projectName
  112. )
  113. })
  114. it('should sync to code', function () {
  115. cy.log('navigate to \\maketitle using double click in PDF')
  116. cy.findByRole('region', { name: 'PDF preview and logs' })
  117. .findByText(projectName)
  118. .dblclick()
  119. cy.get('.cm-activeLine').should('have.text', '\\maketitle')
  120. cy.log('navigate to Section A using double click in PDF')
  121. cy.findByRole('region', { name: 'PDF preview and logs' })
  122. .findByText('Section A')
  123. .dblclick()
  124. cy.get('.cm-activeLine').should('have.text', '\\section{Section A}')
  125. cy.log('navigate to Section B using arrow button')
  126. cy.findByTestId('pdfjs-viewer-inner')
  127. .should('have.prop', 'scrollTop')
  128. .as('start')
  129. cy.findByRole('region', { name: 'PDF preview and logs' })
  130. .findByText('Section B')
  131. .scrollIntoView()
  132. cy.get('@start').then((start: any) => {
  133. waitUntilScrollingFinished('.pdfjs-viewer-inner', start)
  134. })
  135. // The sync button is swapped as the position in the PDF changes.
  136. // Cypress appears to click on a button that references a stale position.
  137. // Adding a cy.wait() statement is the most reliable "fix" so far :/
  138. cy.wait(1000)
  139. cy.findByRole('button', {
  140. name: 'Go to PDF location in code (Tip: double click on the PDF for best results)',
  141. }).click()
  142. cy.get('.cm-activeLine').should('have.text', '\\section{Section B}')
  143. })
  144. it('should sync to pdf', function () {
  145. cy.log('zoom in')
  146. cy.findByRole('button', { name: /^\d+%$/ }).click() // TODO: ARIA label
  147. cy.findByRole('menuitem', { name: '400%' }).click()
  148. cy.log('scroll to top')
  149. cy.findByTestId('pdfjs-viewer-inner').scrollTo('top')
  150. waitUntilScrollingFinished('.pdfjs-viewer-inner', -1).as('start')
  151. cy.log('navigate to title')
  152. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(
  153. () => {
  154. cy.findByText('\\maketitle').parent().click()
  155. }
  156. )
  157. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  158. cy.get('@start').then((start: any) => {
  159. waitUntilScrollingFinished('.pdfjs-viewer-inner', start)
  160. .as('title')
  161. .should('be.greaterThan', start)
  162. })
  163. cy.log('navigate to Section A')
  164. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() =>
  165. cy.findByText('Section A').click()
  166. )
  167. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  168. cy.get('@title').then((title: any) => {
  169. waitUntilScrollingFinished('.pdfjs-viewer-inner', title)
  170. .as('sectionA')
  171. .should('be.greaterThan', title)
  172. })
  173. cy.log('navigate to Section B')
  174. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() =>
  175. cy.findByText('Section B').click()
  176. )
  177. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  178. cy.get('@sectionA').then((title: any) => {
  179. waitUntilScrollingFinished('.pdfjs-viewer-inner', title)
  180. .as('sectionB')
  181. .should('be.greaterThan', title)
  182. })
  183. })
  184. })
  185. }
  186. function checkRecompilesAfterErrors() {
  187. it('recompiles even if there are Latex errors', function () {
  188. login('user@example.com')
  189. createProject('test-project')
  190. const recompile = throttledRecompile()
  191. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() => {
  192. cy.findByText('\\maketitle').parent().click()
  193. cy.findByText('\\maketitle')
  194. .parent()
  195. .type('\n\\fakeCommand{} \n\\section{{}Test Section}')
  196. })
  197. recompile()
  198. recompile()
  199. cy.findByRole('region', { name: 'PDF preview and logs' }).within(() => {
  200. cy.findByText('Test Section').should('contain.text', 'Test Section')
  201. })
  202. cy.findByTestId('logs-pane').should('not.contain.text', 'No PDF')
  203. })
  204. }
  205. function checkXeTeX() {
  206. it('should be able to use XeLaTeX', function () {
  207. createProject('XeLaTeX')
  208. const recompile = throttledRecompile()
  209. cy.log('wait for compile')
  210. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  211. 'contain.text',
  212. 'XeLaTeX'
  213. )
  214. cy.log('Check which compiler was used, expect pdfLaTeX')
  215. cy.findByRole('button', { name: 'View logs' }).click()
  216. cy.findByText(/This is pdfTeX/)
  217. cy.log('Switch compiler to from pdfLaTeX to XeLaTeX')
  218. cy.findByRole('navigation', {
  219. name: 'Project actions',
  220. })
  221. .findByRole('button', { name: 'Menu' })
  222. .click()
  223. cy.findByRole('dialog').within(() => {
  224. cy.findByRole('option', { name: 'pdfLaTeX' }).should('be.selected')
  225. cy.findByRole('combobox', { name: 'Compiler' }).select('XeLaTeX')
  226. })
  227. cy.get('body').type('{esc}')
  228. cy.findByRole('dialog').should('not.exist')
  229. cy.log('Trigger compile with other compiler')
  230. recompile()
  231. cy.log('Check which compiler was used, expect XeLaTeX')
  232. cy.findByRole('button', { name: 'View logs' }).click()
  233. cy.findByText(/This is XeTeX/)
  234. })
  235. }
  236. function checkUsesDefaultCompiler() {
  237. beforeEach(function () {
  238. login('user@example.com')
  239. })
  240. it('should not offer TexLive images and use default compiler', function () {
  241. createProject('sandboxed')
  242. cy.log('wait for compile')
  243. cy.get('.pdf-viewer').should('contain.text', 'sandboxed')
  244. cy.log('Check which compiler version was used, expect 2025')
  245. cy.get('[aria-label="View logs"]').click()
  246. cy.findByText(/This is pdfTeX, Version .+ \(TeX Live 2025\) /)
  247. cy.log('Check that there is no TeX Live version toggle')
  248. cy.findByRole('navigation', {
  249. name: 'Project actions',
  250. })
  251. .findByRole('button', { name: 'Menu' })
  252. .click()
  253. cy.findByText('Word Count') // wait for lazy loading
  254. cy.findByText(LABEL_TEX_LIVE_VERSION).should('not.exist')
  255. })
  256. }
  257. describe('disabled in Server Pro', function () {
  258. if (isExcludedBySharding('PRO_DEFAULT_2')) return
  259. startWith({ pro: true })
  260. ensureUserExists({ email: 'user@example.com' })
  261. beforeEach(function () {
  262. login('user@example.com')
  263. })
  264. checkUsesDefaultCompiler()
  265. checkSyncTeX()
  266. checkXeTeX()
  267. checkRecompilesAfterErrors()
  268. checkStopCompile()
  269. })
  270. // https://github.com/overleaf/internal/issues/20216
  271. describe.skip('unavailable in CE', function () {
  272. if (isExcludedBySharding('CE_CUSTOM_1')) return
  273. startWith({ pro: false, vars: enabledVars, resetData: true })
  274. ensureUserExists({ email: 'user@example.com' })
  275. beforeEach(function () {
  276. login('user@example.com')
  277. })
  278. checkUsesDefaultCompiler()
  279. checkSyncTeX()
  280. checkXeTeX()
  281. checkRecompilesAfterErrors()
  282. checkStopCompile()
  283. })
  284. })