sandboxed-compiles.spec.ts 12 KB

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