sandboxed-compiles.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. cy.wait(1000)
  147. cy.findByRole('button', {
  148. name: 'Go to PDF location in code (Tip: double click on the PDF for best results)',
  149. }).click()
  150. cy.get('.cm-activeLine').should('have.text', '\\section{Section B}')
  151. })
  152. it('should sync to pdf', function () {
  153. cy.log('zoom in')
  154. cy.findByRole('button', { name: /^\d+%$/ }).click() // TODO: ARIA label
  155. cy.findByRole('menuitem', { name: '400%' }).click()
  156. cy.log('scroll to top')
  157. cy.findByTestId('pdfjs-viewer-inner').scrollTo('top')
  158. waitUntilScrollingFinished('.pdfjs-viewer-inner', -1).as('start')
  159. cy.log('navigate to title')
  160. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(
  161. () => {
  162. cy.findByText('\\maketitle').parent().click()
  163. }
  164. )
  165. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  166. cy.get('@start').then((start: any) => {
  167. waitUntilScrollingFinished('.pdfjs-viewer-inner', start)
  168. .as('title')
  169. .should('be.greaterThan', start)
  170. })
  171. cy.log('navigate to Section A')
  172. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() =>
  173. cy.findByText('Section A').click()
  174. )
  175. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  176. cy.get('@title').then((title: any) => {
  177. waitUntilScrollingFinished('.pdfjs-viewer-inner', title)
  178. .as('sectionA')
  179. .should('be.greaterThan', title)
  180. })
  181. cy.log('navigate to Section B')
  182. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() =>
  183. cy.findByText('Section B').click()
  184. )
  185. cy.findByRole('button', { name: 'Go to code location in PDF' }).click()
  186. cy.get('@sectionA').then((title: any) => {
  187. waitUntilScrollingFinished('.pdfjs-viewer-inner', title)
  188. .as('sectionB')
  189. .should('be.greaterThan', title)
  190. })
  191. })
  192. })
  193. }
  194. function checkRecompilesAfterErrors() {
  195. it('recompiles even if there are Latex errors', function () {
  196. login('user@example.com')
  197. const { recompile, waitForCompile } = prepareWaitForNextCompileSlot()
  198. waitForCompile(() => {
  199. createProject('test-project')
  200. })
  201. cy.findByRole('textbox', { name: 'Source Editor editing' }).within(() => {
  202. cy.findByText('\\maketitle').parent().click()
  203. cy.findByText('\\maketitle')
  204. .parent()
  205. .type('\n\\fakeCommand{} \n\\section{{}Test Section}')
  206. })
  207. recompile()
  208. recompile()
  209. cy.findByRole('region', { name: 'PDF preview and logs' }).within(() => {
  210. cy.findByText('Test Section').should('contain.text', 'Test Section')
  211. })
  212. cy.findByTestId('logs-pane').should('not.contain.text', 'No PDF')
  213. })
  214. }
  215. function checkXeTeX() {
  216. it('should be able to use XeLaTeX', function () {
  217. const { recompile, waitForCompile } = prepareWaitForNextCompileSlot()
  218. waitForCompile(() => {
  219. createProject('XeLaTeX')
  220. })
  221. cy.log('wait for compile')
  222. cy.findByRole('region', { name: 'PDF preview and logs' }).should(
  223. 'contain.text',
  224. 'XeLaTeX'
  225. )
  226. cy.log('Check which compiler was used, expect pdfLaTeX')
  227. cy.findByRole('button', { name: 'View logs' }).click()
  228. cy.findByText(/This is pdfTeX/)
  229. cy.log('Switch compiler to from pdfLaTeX to XeLaTeX')
  230. cy.findByRole('navigation', {
  231. name: 'Project actions',
  232. })
  233. .findByRole('button', { name: 'Menu' })
  234. .click()
  235. cy.findByRole('dialog').within(() => {
  236. cy.findByRole('option', { name: 'pdfLaTeX' }).should('be.selected')
  237. cy.findByRole('combobox', { name: 'Compiler' }).select('XeLaTeX')
  238. })
  239. cy.get('body').type('{esc}')
  240. cy.findByRole('dialog').should('not.exist')
  241. cy.log('Trigger compile with other compiler')
  242. recompile()
  243. cy.log('Check which compiler was used, expect XeLaTeX')
  244. cy.findByRole('button', { name: 'View logs' }).click()
  245. cy.findByText(/This is XeTeX/)
  246. })
  247. }
  248. function checkUsesDefaultCompiler() {
  249. beforeEach(function () {
  250. login('user@example.com')
  251. })
  252. it('should not offer TexLive images and use default compiler', function () {
  253. createProject('sandboxed')
  254. cy.log('wait for compile')
  255. cy.get('.pdf-viewer').should('contain.text', 'sandboxed')
  256. cy.log('Check which compiler version was used, expect 2025')
  257. cy.get('[aria-label="View logs"]').click()
  258. cy.findByText(/This is pdfTeX, Version .+ \(TeX Live 2025\) /)
  259. cy.log('Check that there is no TeX Live version toggle')
  260. cy.findByRole('navigation', {
  261. name: 'Project actions',
  262. })
  263. .findByRole('button', { name: 'Menu' })
  264. .click()
  265. cy.findByText('Word Count') // wait for lazy loading
  266. cy.findByText(LABEL_TEX_LIVE_VERSION).should('not.exist')
  267. })
  268. }
  269. describe('disabled in Server Pro', function () {
  270. if (isExcludedBySharding('PRO_DEFAULT_2')) return
  271. startWith({ pro: true })
  272. ensureUserExists({ email: 'user@example.com' })
  273. beforeEach(function () {
  274. login('user@example.com')
  275. })
  276. checkUsesDefaultCompiler()
  277. checkSyncTeX()
  278. checkXeTeX()
  279. checkRecompilesAfterErrors()
  280. checkStopCompile()
  281. })
  282. // https://github.com/overleaf/internal/issues/20216
  283. describe.skip('unavailable in CE', function () {
  284. if (isExcludedBySharding('CE_CUSTOM_1')) return
  285. startWith({ pro: false, vars: enabledVars, resetData: true })
  286. ensureUserExists({ email: 'user@example.com' })
  287. beforeEach(function () {
  288. login('user@example.com')
  289. })
  290. checkUsesDefaultCompiler()
  291. checkSyncTeX()
  292. checkXeTeX()
  293. checkRecompilesAfterErrors()
  294. checkStopCompile()
  295. })
  296. })