compile.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { v4 as uuid } from 'uuid'
  2. const outputFiles = () => {
  3. const build = uuid()
  4. return [
  5. {
  6. path: 'output.pdf',
  7. build,
  8. url: `/build/${build}/output.pdf`,
  9. type: 'pdf',
  10. },
  11. {
  12. path: 'output.bbl',
  13. build,
  14. url: `/build/${build}/output.bbl`,
  15. type: 'bbl',
  16. },
  17. {
  18. path: 'output.bib',
  19. build,
  20. url: `/build/${build}/output.bib`,
  21. type: 'bib',
  22. },
  23. {
  24. path: 'example.txt',
  25. build,
  26. url: `/build/${build}/example.txt`,
  27. type: 'txt',
  28. },
  29. {
  30. path: 'output.log',
  31. build,
  32. url: `/build/${build}/output.log`,
  33. type: 'log',
  34. },
  35. {
  36. path: 'output.blg',
  37. build,
  38. url: `/build/${build}/output.blg`,
  39. type: 'blg',
  40. },
  41. ]
  42. }
  43. export const interceptCompile = ({
  44. prefix = 'compile',
  45. times = 1,
  46. cached = false,
  47. outputPDFFixture = 'output.pdf',
  48. } = {}) => {
  49. if (cached) {
  50. cy.intercept(
  51. { pathname: '/project/*/output/cached/output.overleaf.json', times },
  52. {
  53. body: {
  54. fromCache: true,
  55. status: 'success',
  56. clsiServerId: 'foo',
  57. compileGroup: 'priority',
  58. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  59. outputFiles: outputFiles(),
  60. },
  61. }
  62. ).as(`${prefix}-cached`)
  63. cy.intercept(
  64. { method: 'POST', pathname: '/project/*/compile', times },
  65. {
  66. body: {
  67. status: 'unavailable',
  68. clsiServerId: 'foo',
  69. compileGroup: 'priority',
  70. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  71. outputFiles: [],
  72. },
  73. }
  74. ).as(`${prefix}`)
  75. } else {
  76. cy.intercept(
  77. { pathname: '/project/*/output/cached/output.overleaf.json', times },
  78. { statusCode: 404 }
  79. ).as(`${prefix}-cached`)
  80. cy.intercept(
  81. { method: 'POST', pathname: '/project/*/compile', times },
  82. {
  83. body: {
  84. status: 'success',
  85. clsiServerId: 'foo',
  86. compileGroup: 'priority',
  87. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  88. outputFiles: outputFiles(),
  89. },
  90. }
  91. ).as(`${prefix}`)
  92. }
  93. cy.intercept(
  94. { pathname: '/build/*/output.pdf', times },
  95. { fixture: `build/${outputPDFFixture},null` }
  96. ).as(`${prefix}-pdf`)
  97. cy.intercept(
  98. { pathname: '/build/*/output.log', times },
  99. { fixture: 'build/output.log' }
  100. ).as(`${prefix}-log`)
  101. cy.intercept(
  102. { pathname: '/build/*/output.blg', times },
  103. { fixture: 'build/output.blg' }
  104. ).as(`${prefix}-blg`)
  105. }
  106. export const waitForCompile = ({
  107. prefix = 'compile',
  108. pdf = false,
  109. cached = false,
  110. } = {}) => {
  111. if (cached) {
  112. cy.wait(`@${prefix}-cached`)
  113. } else {
  114. cy.wait(`@${prefix}`)
  115. }
  116. cy.wait(`@${prefix}-log`)
  117. .its('request.query.clsiserverid')
  118. .should('eq', cached ? 'cache' : 'foo') // straight from cache if cached
  119. cy.wait(`@${prefix}-blg`)
  120. .its('request.query.clsiserverid')
  121. .should('eq', cached ? 'cache' : 'foo') // straight from cache if cached
  122. if (pdf) {
  123. cy.wait(`@${prefix}-pdf`)
  124. .its('request.query.clsiserverid')
  125. .should('eq', 'foo') // always from VM first
  126. }
  127. return cy.wrap(null)
  128. }
  129. export const interceptDeferredCompile = (beforeResponse?: () => void) => {
  130. const { promise, resolve } = Promise.withResolvers<void>()
  131. cy.intercept(
  132. { method: 'POST', url: '/project/*/compile*', times: 1 },
  133. req => {
  134. if (beforeResponse) {
  135. beforeResponse()
  136. }
  137. // only reply once the Promise is resolved
  138. promise.then(() => {
  139. req.reply({
  140. body: {
  141. status: 'success',
  142. clsiServerId: 'foo',
  143. compileGroup: 'priority',
  144. pdfDownloadDomain: 'https://clsi.test-overleaf.com',
  145. outputFiles: [
  146. {
  147. path: 'output.pdf',
  148. build: '123',
  149. url: '/build/123/output.pdf',
  150. type: 'pdf',
  151. },
  152. {
  153. path: 'output.log',
  154. build: '123',
  155. url: '/build/123/output.log',
  156. type: 'log',
  157. },
  158. {
  159. path: 'output.blg',
  160. build: '123',
  161. url: '/build/123/output.blg',
  162. type: 'log',
  163. },
  164. ],
  165. },
  166. })
  167. })
  168. return promise
  169. }
  170. ).as('compile')
  171. cy.intercept(
  172. { pathname: '/build/*/output.pdf', times: 1 },
  173. { fixture: 'build/output.pdf,null' }
  174. ).as(`compile-pdf`)
  175. cy.intercept(
  176. { pathname: '/build/*/output.log', times: 1 },
  177. { fixture: 'build/output.log' }
  178. ).as(`compile-log`)
  179. cy.intercept(
  180. { pathname: '/build/*/output.blg', times: 1 },
  181. { fixture: 'build/output.blg' }
  182. ).as(`compile-blg`)
  183. return cy.wrap(resolve)
  184. }