sandboxed-compiles.spec.ts 13 KB

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