compile.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * Helper function for throttling clicks on the recompile button to avoid hitting server side rate limits.
  3. * The naive approach is waiting a fixed a mount of time (3s) just before clicking the button.
  4. * This helper takes into account that other UI interactions take time. We can deduce that latency from the fixed delay (3s minus other latency). This can bring down the effective waiting time to 0s.
  5. */
  6. export function stopCompile(options: { delay?: number } = {}) {
  7. const { delay = 0 } = options
  8. cy.wait(delay)
  9. cy.log('Stop compile')
  10. cy.findByRole('button', { name: 'Toggle compile options menu' }).click()
  11. cy.findByRole('menuitem', { name: 'Stop compilation' })
  12. .should('not.have.class', 'disabled')
  13. .and('not.have.attr', 'aria-disabled', 'true')
  14. .click()
  15. }
  16. export function prepareWaitForNextCompileSlot() {
  17. let lastCompile = 0
  18. function queueReset() {
  19. cy.then(() => {
  20. lastCompile = Date.now()
  21. })
  22. }
  23. function waitForCompileRateLimitCoolOff() {
  24. cy.then(() => {
  25. cy.log('Wait for recompile rate-limit to cool off')
  26. const msSinceLastCompile = Date.now() - lastCompile
  27. cy.wait(Math.max(0, 1_000 - msSinceLastCompile))
  28. queueReset()
  29. })
  30. }
  31. function waitForCompile(triggerCompile: () => void) {
  32. waitForCompileRateLimitCoolOff()
  33. cy.then(() => {
  34. let compilingVisible: () => void
  35. const waitForCompilingVisible = new Promise<void>(resolve => {
  36. compilingVisible = resolve
  37. })
  38. cy.intercept(
  39. {
  40. method: 'POST',
  41. pathname: /\/project\/[a-fA-F0-9]{24}\/compile$/,
  42. times: 1,
  43. },
  44. async req => {
  45. await waitForCompilingVisible
  46. req.continue()
  47. }
  48. ).as('recompile')
  49. triggerCompile()
  50. cy.log('Wait for compile to finish')
  51. cy.findByRole('button', { name: 'Compiling…' }).then(() =>
  52. compilingVisible()
  53. )
  54. cy.wait('@recompile')
  55. cy.findByRole('button', { name: 'Compiling…' }).should('not.exist')
  56. cy.findByRole('button', { name: 'Recompile' }).should('be.visible')
  57. })
  58. }
  59. function recompile() {
  60. waitForCompile(() => {
  61. cy.findByRole('button', { name: 'Recompile' }).click()
  62. })
  63. }
  64. return {
  65. queueReset,
  66. waitForCompileRateLimitCoolOff,
  67. waitForCompile,
  68. recompile,
  69. }
  70. }