From b5eb9ea552db16d5ef83b79151b9cda305e6fb3f Mon Sep 17 00:00:00 2001 From: Shakker Date: Sat, 9 May 2026 00:38:40 +0100 Subject: [PATCH] test: tighten qa lab server polling --- extensions/qa-lab/src/lab-server.test.ts | 62 +++++++++++++++--------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/extensions/qa-lab/src/lab-server.test.ts b/extensions/qa-lab/src/lab-server.test.ts index e46adbdf335..250d1ca7a16 100644 --- a/extensions/qa-lab/src/lab-server.test.ts +++ b/extensions/qa-lab/src/lab-server.test.ts @@ -189,39 +189,55 @@ async function fetchWithRetry(input: string, init?: RequestInit, attempts = 3) { } async function waitForRunnerCatalog(baseUrl: string, timeoutMs = 5_000) { - const startedAt = Date.now(); - while (Date.now() - startedAt < timeoutMs) { - const response = await fetchWithRetry(`${baseUrl}/api/bootstrap`); - const bootstrap = (await response.json()) as { - runnerCatalog: { + let catalog: + | { status: "loading" | "ready" | "failed"; real: Array<{ key: string; name: string }>; + } + | undefined; + await vi.waitFor( + async () => { + const response = await fetchWithRetry(`${baseUrl}/api/bootstrap`); + const bootstrap = (await response.json()) as { + runnerCatalog: { + status: "loading" | "ready" | "failed"; + real: Array<{ key: string; name: string }>; + }; }; - }; - if (bootstrap.runnerCatalog.status !== "loading") { - return bootstrap.runnerCatalog; - } - await sleep(10); + if (bootstrap.runnerCatalog.status === "loading") { + throw new Error("runner catalog still loading"); + } + catalog = bootstrap.runnerCatalog; + }, + { interval: 1, timeout: timeoutMs }, + ); + if (!catalog) { + throw new Error("runner catalog stayed loading"); } - throw new Error("runner catalog stayed loading"); + return catalog; } async function waitForFileContent(filePath: string, expected: string, timeoutMs = 5_000) { - const startedAt = Date.now(); - while (Date.now() - startedAt < timeoutMs) { - try { - const content = await readFile(filePath, "utf8"); - if (content === expected) { - return content; + let content: string | undefined; + await vi.waitFor( + async () => { + try { + content = await readFile(filePath, "utf8"); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") { + throw error; + } } - } catch (error) { - if ((error as NodeJS.ErrnoException).code !== "ENOENT") { - throw error; + if (content !== expected) { + throw new Error(`file did not reach expected content: ${filePath}`); } - } - await sleep(10); + }, + { interval: 1, timeout: timeoutMs }, + ); + if (content === undefined) { + throw new Error(`file did not reach expected content: ${filePath}`); } - throw new Error(`file did not reach expected content: ${filePath}`); + return content; } async function createQaLabRepoRootFixture(params?: {