sandboxed-compiles.spec.ts 12 KB

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