sandboxed-compiles.spec.ts 13 KB

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