Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
test(js-sdk): wait for workers.dev propagation in the CF deploy suite (#1592)
Mish Ushakov4a2571d32187
diff --git a/packages/js-sdk/tests/runtimes/cloudflare-deploy/run.test.ts b/packages/js-sdk/tests/runtimes/cloudflare-deploy/run.test.tsindex 1ef567415..da8ade1c7 100644--- a/packages/js-sdk/tests/runtimes/cloudflare-deploy/run.test.ts+++ b/packages/js-sdk/tests/runtimes/cloudflare-deploy/run.test.ts@@ -4,15 +4,15 @@ import { template } from '../../template'test('sandbox lifecycle inside a deployed Cloudflare Worker',- // Retry the whole test while the fresh workers.dev subdomain propagates:- // Cloudflare serves an HTML error page until the route is live ("non-JSON- // response"), and fetch itself can fail at the DNS/connect level first- // (undici throws "fetch failed").+ // setup.mts waits for the fresh workers.dev subdomain to propagate before+ // tests run; these retries only absorb transient blips — a stray+ // propagation 404 or an undici DNS/connect error ("fetch failed"). Any+ // other Cloudflare error page is a real failure and propagates.{retry: {count: 10,delay: 3_000,- condition: /non-JSON response|fetch failed/,+ condition: /non-JSON response \(404|fetch failed/,},},async () => {@@ -35,10 +35,14 @@ test(try {body = JSON.parse(text)} catch {- // Cloudflare edge error page (e.g. DNS still propagating) — fail and- // let the retry kick in.+ // Cloudflare edge error page — fail and let the retry kick in. The+ // page's <title> says which error it is (the truncated body is just+ // boilerplate shared by every Cloudflare error page).+ const title = text.match(/<title>(.*?)<\/title>/is)?.[1]?.trim()throw new Error(- `non-JSON response (${response.status}): ${text.slice(0, 200)}`+ `non-JSON response (${response.status}${+ title ? `, "${title}"` : ''+ }): ${text.slice(0, 200)}`)}diff --git a/packages/js-sdk/tests/runtimes/cloudflare-deploy/setup.mts b/packages/js-sdk/tests/runtimes/cloudflare-deploy/setup.mtsindex 43642e088..ea5c9e84f 100644--- a/packages/js-sdk/tests/runtimes/cloudflare-deploy/setup.mts+++ b/packages/js-sdk/tests/runtimes/cloudflare-deploy/setup.mts@@ -30,7 +30,47 @@ function wrangler(args: string[]) {return { ...result, output: `${result.stdout ?? ''}\n${result.stderr ?? ''}` }}-export default function setup(project: TestProject) {+// A temporary deploy lands on a brand-new account subdomain+// (<worker>.<random-name>.workers.dev), and Cloudflare serves its HTML 404+// page ("nothing is here yet") until the route propagates to the edge —+// usually seconds, occasionally minutes. Wait for the worker itself to+// answer (405 to GET, worker.mjs is POST-only) so the test's own retries+// only have to absorb transient network failures.+async function waitUntilLive(workerUrl: string, timeoutMs = 240_000) {+ const deadline = Date.now() + timeoutMs+ for (;;) {+ // Thrown fetch errors (DNS/connect for the fresh subdomain) are also+ // transient — keep waiting on those, but fail fast on any status other+ // than the propagation 404 so real errors surface immediately.+ const response = await fetch(workerUrl).catch((err) => {+ console.log(`Worker not reachable yet (${err}), waiting...`)+ return undefined+ })+ if (response) {+ if (response.status === 405) {+ return+ }+ if (response.status !== 404) {+ const text = await response.text()+ const title = text.match(/<title>(.*?)<\/title>/is)?.[1]?.trim()+ throw new Error(+ `Worker at ${workerUrl} answered ${response.status}${+ title ? ` ("${title}")` : ''+ } — not the propagation 404: ${text.slice(0, 200)}`+ )+ }+ console.log('Worker route not live yet (404), waiting...')+ }+ if (Date.now() >= deadline) {+ throw new Error(+ `Worker at ${workerUrl} did not come up within ${timeoutMs / 1000}s of deploy`+ )+ }+ await new Promise((resolve) => setTimeout(resolve, 3_000))+ }+}++export default async function setup(project: TestProject) {rmSync(outputFile, { force: true })console.log('Deploying worker to a temporary Cloudflare preview account...')@@ -58,6 +98,8 @@ export default function setup(project: TestProject) {}console.log(`Deployed: ${workerUrl}`)+ await waitUntilLive(workerUrl)+ console.log('Worker is live.')project.provide('cfWorkerUrl', workerUrl)return function teardown() {