overleaf/server-ce/test/helpers/compile.ts
Jakob Ackermann f9846699cb Merge pull request #18994 from overleaf/jpa-sandboxed-compiles-logs
[server-pro] migrate comments to cy.log for sandboxed-compiles tests

GitOrigin-RevId: 2e66089b43c2f37af6e3cd56273df2b3c976a407
2024-06-20 08:05:32 +00:00

23 lines
845 B
TypeScript

/**
* Helper function for throttling clicks on the recompile button to avoid hitting server side rate limits.
* The naive approach is waiting a fixed a mount of time (3s) just before clicking the button.
* 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.
*/
export function throttledRecompile() {
let lastCompile = 0
function queueReset() {
cy.then(() => {
lastCompile = Date.now()
})
}
queueReset()
return () =>
cy.then(() => {
cy.log('Recompile without hitting rate-limit')
const msSinceLastCompile = Date.now() - lastCompile
cy.wait(Math.max(0, 1_000 - msSinceLastCompile))
cy.findByText('Recompile').click()
queueReset()
})
}