test: let the deadline policy own test waits

Vitest's expect.poll and vi.waitFor stop after one second by default, a
host-speed guess that the project's deadline policy does not see. Under
hosted-CI load, kunaProjectConcurrency failed that way twice on unrelated
PRs. Another 256 waits relied on the default, and 97 passed literals
picked to fit some host.

Waits now take as long as their test may run, less the reporting reserve
that children already keep. Each Vitest project sets expect.poll's
default, and the new waitFor in tests/support/testWait.ts replaces
vi.waitFor. Fixture readiness waits (awaitAcknowledgement and the provider
process waits) default to the same deadline, and their host-speed literal
arguments are removed. One remainingTestDeadline owns the arithmetic for
both children and waits.

Ten waits whose short timeout was the real check now assert it directly
(Kuna version probe, Cutter discovery, doctor and Inspector cancellation,
apktool fixture teardown, JADX disconnect): settlement before the
production deadline that would otherwise also satisfy the wait, or a
deadline pinned beyond every test deadline.

The rea/project-wait-deadline lint rule rejects vi.waitFor and explicit
expect.poll timeouts. The testing guide and the CI tests audit skill now
document the policy.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
gensokyo-devandgensokyo-dev authored and gensokyo-dev committed 2026-11-14 19:28:38 +08:00
1 parent 054fbc4c26
commit 07e4b700ac
119 files changed
+818 -648

No files matched your search

+3 -4
View File
@@ -72,6 +72,7 @@ import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { parseEvidence } from "../domain/evidence.js";
import type { AdbRequest } from "../domain/adb/adbDeviceAnalysis.js";
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import { waitFor } from "../../tests/support/testWait.js";
const it = test.skipIf(process.platform === "win32");
@@ -166,13 +167,11 @@ fi`,
let closing: Promise<void> | undefined;
try {
await awaitAcknowledgement(
vi.waitFor(
async () => expect(await lstat(join(root, "started"))).toBeDefined(),
{ timeout: 10_000 },
waitFor(async () =>
expect(await lstat(join(root, "started"))).toBeDefined(),
),
inspection,
"ADB version probe",
10_000,
);
let closed = false;
closing = provider.close().then(() => {
@@ -36,7 +36,7 @@ it.skipIf(process.platform === "win32")(
({ kind }) => kind === "running",
);
},
{ timeout: 10_000, interval: 25 },
{ interval: 25 },
)
.toBe(true);
controller.abort();
@@ -50,7 +50,7 @@ it.skipIf(process.platform === "win32")(
(await Promise.all(launched.map(observePosixProcess))).every(
({ kind }) => kind !== "running",
),
{ timeout: 5_000, interval: 25 },
{ interval: 25 },
)
.toBe(true);
} finally {
+5 -7
View File
@@ -10,6 +10,7 @@ import {
execFileOutput,
} from "../process/ExecFileOutput.js";
import { inspectLinuxHopperPythonRuntime } from "./LinuxHopperPython.js";
import { waitFor } from "../../tests/support/testWait.js";
const response = (value: unknown) => {
const stdout = JSON.stringify(value);
@@ -157,8 +158,7 @@ setInterval(() => {}, 1000);
execFileOutput(process.execPath, ["-e", program], options),
);
try {
await vi.waitFor(() => access(stopping), {
timeout: 5_000,
await waitFor(() => access(stopping), {
interval: 10,
});
controller.abort({ request: "cancel during failed command cleanup" });
@@ -209,10 +209,9 @@ while True:
return result;
});
try {
await vi.waitFor(() => access(ready), { timeout: 5_000, interval: 10 });
await waitFor(() => access(ready), { interval: 10 });
controller.abort(reason);
await vi.waitFor(() => access(stopping), {
timeout: 3_000,
await waitFor(() => access(stopping), {
interval: 10,
});
// The child cannot exit until released; early settlement loses ownership.
@@ -224,8 +223,7 @@ while True:
controller.abort(reason);
await execution;
}
await vi.waitFor(() => access(stopped), {
timeout: 5_000,
await waitFor(() => access(stopped), {
interval: 10,
});
}
+10 -15
View File
@@ -32,6 +32,7 @@ import { hashReadable } from "./ArtifactHash.js";
import { translateArtifactFailure } from "./ArtifactOperationFailure.js";
import { analysisErrorUserMessage } from "../domain/analysisErrorPresentation.js";
import { waitFor } from "../../tests/support/testWait.js";
const MINIMUM_ARTIFACT_EXPANDED_BYTES = 256 * 1024 * 1024;
@@ -398,27 +399,21 @@ describe("ZIP Zstandard member streams", () => {
expect(decoders).toHaveLength(1);
// A fresh decoder can succeed while an abandoned producer is stuck.
// Verify the first actual getData and native close before starting it.
await vi.waitFor(
() => {
expect(producers[0]?.settled).toBe(true);
expect(decoders[0]?.closed).toBe(true);
expect(decoders[0]?.destroyed).toBe(true);
},
{ timeout: 5_000 },
);
await waitFor(() => {
expect(producers[0]?.settled).toBe(true);
expect(decoders[0]?.closed).toBe(true);
expect(decoders[0]?.destroyed).toBe(true);
});
const chunks: Buffer[] = [];
for await (const chunk of await reader.open(entry))
chunks.push(Buffer.from(chunk));
expect(Buffer.concat(chunks).equals(content)).toBe(true);
expect(producers).toHaveLength(2);
expect(decoders).toHaveLength(2);
await vi.waitFor(
() => {
expect(producers.every(({ settled }) => settled)).toBe(true);
expect(decoders.every(({ closed }) => closed)).toBe(true);
},
{ timeout: 5_000 },
);
await waitFor(() => {
expect(producers.every(({ settled }) => settled)).toBe(true);
expect(decoders.every(({ closed }) => closed)).toBe(true);
});
} finally {
controller.abort();
await reader.close();
@@ -15,6 +15,7 @@ import {
analyzeAppleAssetCatalogs,
collectInterfaceBuilderResourceKeys,
} from "./AppleAssetCatalogAnalysis.js";
import { waitFor } from "../../../tests/support/testWait.js";
describe("Apple asset catalog traversal coverage", () => {
it.for([false, true])(
@@ -412,7 +413,7 @@ describe("Apple asset catalog and Interface Builder concurrency", () => {
settled = true;
});
analysis.catch(() => undefined);
await vi.waitFor(() => expect(catalogStarted).toBe(true));
await waitFor(() => expect(catalogStarted).toBe(true));
await new Promise((resolve) => setImmediate(resolve));
expect(settled).toBe(false);
finishCatalog("[]");
@@ -452,7 +453,7 @@ describe("Apple asset catalog and Interface Builder concurrency", () => {
signal: controller.signal,
analyzeInterfaceBuilder: async () => emptyGraph,
}).catch((cause: unknown) => cause);
await vi.waitFor(() => expect(access(ready)).resolves.toBeUndefined());
await waitFor(() => expect(access(ready)).resolves.toBeUndefined());
controller.abort("stop asset inspection");
expect(await settled).toMatchObject({ reason: "cancelled" });
await expect(access(exited)).resolves.toBeUndefined();
@@ -746,7 +747,7 @@ it.skipIf(process.platform === "win32")(
settled = true;
});
try {
await vi.waitFor(() => expect(access(exited)).resolves.toBeUndefined());
await waitFor(() => expect(access(exited)).resolves.toBeUndefined());
expect(settled).toBe(false);
controller.abort("late cancellation");
expect(settled).toBe(false);
@@ -32,6 +32,7 @@ import {
PlaywrightElectronActiveProvider,
safeElectronEnvironment,
} from "./PlaywrightElectronActiveProvider.js";
import { waitFor } from "../../tests/support/testWait.js";
afterEach(() => {
vi.restoreAllMocks();
@@ -1051,7 +1052,7 @@ it("reports an expired final state grace period as the caller's cancellation", a
};
const capture = captureWithController(controller, application, []);
await vi.waitFor(() => expect(evaluate).toHaveBeenCalled());
await waitFor(() => expect(evaluate).toHaveBeenCalled());
await vi.advanceTimersByTimeAsync(5_000);
const result = await capture;
+3 -2
View File
@@ -4,6 +4,7 @@ import type { BrowserContext, Page, Request, Response } from "playwright-core";
import { browserScenarioSchema } from "../domain/browserScenario.js";
import { resolveScenarioSecrets } from "../../tests/fixtures/browserScenarioSecrets.js";
import { PlaywrightScenarioEvents } from "./PlaywrightScenarioEvents.js";
import { waitFor } from "../../tests/support/testWait.js";
describe("declared secrets in scenario events", () => {
it("redacts encoded declared secrets from text evidence and fingerprints", () => {
@@ -544,7 +545,7 @@ const sourceTransactionCapture = async (
};
listeners.get("request")!(request);
if (!options.metadataOnly)
await vi.waitFor(() =>
await waitFor(() =>
expect(capture.result().items).toContainEqual(
expect.objectContaining({ kind: "network-content", phase: "request" }),
),
@@ -592,7 +593,7 @@ const sourceTransactionCapture = async (
});
listeners.get("requestfinished")!(followup);
if (!options.metadataOnly)
await vi.waitFor(() =>
await waitFor(() =>
expect(
capture
.result()
@@ -6,6 +6,7 @@ import type { BrowserScenarioAction } from "../domain/browserScenarioValues.js";
import { BrowserScenarioSecrets } from "./BrowserScenarioSecrets.js";
import { responsePointerValue } from "./BrowserScenarioResponseValues.js";
import { performPlaywrightScenarioAction } from "./PlaywrightScenarioActions.js";
import { waitFor } from "../../tests/support/testWait.js";
const ORIGIN = "https://app.example.test";
@@ -1614,7 +1615,7 @@ describe("ambiguous saved response observations", () => {
]);
const running = perform(page, parsed.actions[0]!, secrets);
const rejected = expect(running).rejects.toThrow("ambiguous");
await vi.waitFor(() => expect(reads.count).toBe(1));
await waitFor(() => expect(reads.count).toBe(1));
for (const listener of listeners)
listener({ method: () => "POST", url: () => `${ORIGIN}/api/items` });
release('{"id":"wrong-object"}');
@@ -121,6 +121,7 @@ vi.mock("./PlaywrightScenarioEvents.js", () => ({
import { browserScenarioSchema } from "../domain/browserScenario.js";
import { PlaywrightScenarioSession } from "./PlaywrightScenarioSession.js";
import { PlaywrightScenarioBrowserCleanupOwner } from "./PlaywrightScenarioBrowser.js";
import { waitFor } from "../../tests/support/testWait.js";
const scenario = browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: process.execPath },
@@ -1304,7 +1305,7 @@ it("can retry cleanup of a browser that opened after request cancellation", asyn
controller.abort();
await cancelled;
resolveOpening?.(lateOpened);
await vi.waitFor(() => expect(closeBrowser).toHaveBeenCalledTimes(1));
await waitFor(() => expect(closeBrowser).toHaveBeenCalledTimes(1));
expect(retained).toHaveLength(1);
await expect(retained[0]?.release()).resolves.toBeUndefined();
expect(closeBrowser).toHaveBeenCalledTimes(2);
+1 -1
View File
@@ -70,7 +70,7 @@ describe("runSolveHelper", () => {
// agentenv keeps a sandbox's folder until its teardown is confirmed, so the
// emptied private root shows nothing is left up: only the cancellation remains.
try {
await awaitAcknowledgement(started, solved, "solve helper start", 10_000);
await awaitAcknowledgement(started, solved, "solve helper start");
stop.abort(new Error("cancelled by the test"));
const failure: unknown = await solved.then(
() => undefined,
@@ -1,9 +1,10 @@
import { execFileSync } from "node:child_process";
import { chmod, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { expect, it } from "vitest";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { ScreenJudgeProcess } from "./ScreenJudgeProcess.js";
import { waitFor } from "../../tests/support/testWait.js";
const check = { id: "x", kind: "screen_judge", question: "ready?" } as const;
@@ -68,13 +69,10 @@ it.skipIf(process.platform === "win32")(
void running.catch(() => undefined);
let pid = 0;
try {
await vi.waitFor(
async () => {
pid = Number(await readFile(marker, "utf8"));
expect(pid).toBeGreaterThan(0);
},
{ timeout: 10000 },
);
await waitFor(async () => {
pid = Number(await readFile(marker, "utf8"));
expect(pid).toBeGreaterThan(0);
});
controller.abort(new Error("cancelled by caller"));
await expect(running).rejects.toThrow();
let state = "";
+2 -1
View File
@@ -14,6 +14,7 @@ import type {
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { ScreenJudgeProcess } from "./ScreenJudgeProcess.js";
import { waitFor } from "../../tests/support/testWait.js";
vi.mock("./AgentEnvComputerHelper.js", async (importOriginal) => ({
...(await importOriginal<typeof import("./AgentEnvComputerHelper.js")>()),
@@ -261,7 +262,7 @@ it("waits for final process cleanup after cancellation and keeps its owner", asy
settled = true;
});
// Wait for the mocked command to receive control, not just for the judge to start.
await vi.waitFor(() =>
await waitFor(() =>
expect(vi.mocked(runOwnedCommand).mock.calls.at(-1)?.[2]?.signal).toBe(
controller.signal,
),
@@ -12,6 +12,7 @@ import {
} from "./ProcessCaptureError.js";
import { ProcessCaptureResourceScope } from "./ProcessCaptureLifecycle.js";
import { startProcessSampler } from "./ProcessSampling.js";
import { waitFor } from "../../../tests/support/testWait.js";
vi.mock("./LinuxProcessSampling.js", async (original) => ({
...(await original<typeof import("./LinuxProcessSampling.js")>()),
@@ -135,7 +136,7 @@ it.each(["plain", "all-cancel", "mixed-fd", "opaque-peer"] as const)(
Object.defineProperty(process, "platform", platform);
}
});
await vi.waitFor(() => expect(environment).toHaveBeenCalledOnce());
await waitFor(() => expect(environment).toHaveBeenCalledOnce());
const outcome = stop();
if (kind === "opaque-peer") {
const failure: unknown = await outcome.catch((cause: unknown) => cause);
+8 -7
View File
@@ -2,13 +2,14 @@ import { once } from "node:events";
import { createServer, type Server } from "node:http";
import { connect } from "node:net";
import type { AddressInfo } from "node:net";
import { afterEach, expect, it, vi } from "vitest";
import { afterEach, expect, it } from "vitest";
import {
toWebRequest,
writeWebResponse,
type WebResponseOptions,
} from "./nodeHttpBridge.js";
import { waitFor } from "../../../tests/support/testWait.js";
const servers: Server[] = [];
const encoder = new TextEncoder();
@@ -66,7 +67,7 @@ it("ends a HEAD reply after the headers and cancels a body the handler attached"
const response = await fetch(`${origin}/events`, { method: "HEAD" });
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toBe("text/event-stream");
await vi.waitFor(() => {
await waitFor(() => {
expect(cancelled).toBe(true);
});
expect(pulls).toBeLessThanOrEqual(1);
@@ -93,7 +94,7 @@ it("finishes a HEAD reply without waiting on a teed body whose sibling stays liv
method: "HEAD",
});
expect(response.status).toBe(200);
await vi.waitFor(() => {
await waitFor(() => {
expect(finished).toBe(true);
});
});
@@ -126,11 +127,11 @@ it("cancels the body when an asynchronous route resolves after its client left",
const socket = connect(port, "127.0.0.1");
await once(socket, "connect");
socket.write("GET /late HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
await vi.waitFor(() => {
await waitFor(() => {
expect(received).toBe(true);
});
socket.destroy();
await vi.waitFor(() => {
await waitFor(() => {
expect(settled).toBe(true);
expect(cancelled).toBe(true);
});
@@ -165,7 +166,7 @@ it("returns when a client leaves a teed streaming body whose sibling stays live"
const first = await response.body.getReader().read();
expect(decoder.decode(first.value)).toBe("data: first\n\n");
leave.abort();
await vi.waitFor(() => {
await waitFor(() => {
expect(settled).toBe(true);
});
});
@@ -266,7 +267,7 @@ it("aborts the web Request and cancels its response body when the client leaves"
if (reader === undefined) throw new Error("Response body missing");
await reader.read();
controller.abort();
await vi.waitFor(() => {
await waitFor(() => {
expect(aborted).toBe(true);
expect(cancelled).toBe(true);
});
+11 -10
View File
@@ -25,6 +25,7 @@ import { StreamableHttpListenError } from "./streamableHttpListenError.js";
import { mcpAllowedHostsTextSchema } from "../../config/mcpHttpEndpoint.js";
import { parseMcpHttpArguments } from "../../main/mcpHttpArguments.js";
import { RequestTrackingMcpServer } from "../RequestTrackingMcpServer.js";
import { waitFor } from "../../../tests/support/testWait.js";
const servers: StreamableHttpServer[] = [];
@@ -1318,7 +1319,7 @@ it.each(["chunk", "headers"])(
.catch(() => undefined);
let closing: Promise<void> | undefined;
try {
await vi.waitFor(() => {
await waitFor(() => {
expect(cancelled).toBe(1);
});
expect((await fetch(new URL("/health", served.url))).status).toBe(503);
@@ -1403,7 +1404,7 @@ it("retains failed response cleanup and blocks provider teardown and further adm
const response = await fetch(new URL("/failed", served.url));
try {
await response.body?.cancel();
await vi.waitFor(async () => {
await waitFor(async () => {
expect((await fetch(new URL("/health", served.url))).status).toBe(503);
});
await expect(served.prepareShutdown()).rejects.toMatchObject({
@@ -1459,7 +1460,7 @@ it("retains plain-response admission until asynchronous cancellation settles", a
expect((await fetch(new URL("/health", served.url))).status).toBe(503);
expect(health).not.toHaveBeenCalled();
cleanup.release();
await vi.waitFor(async () => {
await waitFor(async () => {
expect((await fetch(new URL("/health", served.url))).status).toBe(200);
});
} finally {
@@ -1562,10 +1563,10 @@ it.each(["pending handler", "open response"])(
await first.promise;
socket.write(request("/second"));
socket.write(request("/health").repeat(32));
await vi.waitFor(() => {
await waitFor(() => {
expect(socket.destroyed).toBe(true);
});
await vi.waitFor(() => {
await waitFor(() => {
expect(finalized).toBe(mode === "pending handler" ? 1 : 0);
});
expect(secondCalled).toBe(0);
@@ -1687,12 +1688,12 @@ it("retains admission for an unread upload attached to a long response", async (
upload.once("error", () => undefined);
upload.write("prefix");
try {
await vi.waitFor(() => {
await waitFor(() => {
expect(received).toBe(true);
});
expect((await fetch(new URL("/health", served.url))).status).toBe(503);
upload.destroy();
await vi.waitFor(async () => {
await waitFor(async () => {
expect((await fetch(new URL("/health", served.url))).status).toBe(200);
});
} finally {
@@ -1737,7 +1738,7 @@ it("bounds HTTP input dispatch globally and releases admission when an upload le
}
// A plain route observes the same budget while both sessionless MCP probes
// wait for the rest of their bodies. Idle SSE connections consume no permit.
await vi.waitFor(async () => {
await waitFor(async () => {
expect((await fetch(new URL("/health", served.url))).status).toBe(503);
});
const refused = await rawHttpRequest(new URL(served.url), {
@@ -1752,7 +1753,7 @@ it("bounds HTTP input dispatch globally and releases admission when an upload le
expect(refused.headers.connection).toBe("close");
uploads[0]?.destroy();
await pending[0];
await vi.waitFor(async () => {
await waitFor(async () => {
expect((await fetch(new URL("/health", served.url))).status).toBe(200);
});
expect(await echo(first.client, "kept during overload")).toEqual([
@@ -1862,7 +1863,7 @@ it.each(["DELETE", "shutdown"])(
upload.write('{"jsonrpc":"2.0","id":2,"method":"tools/call",');
});
try {
await vi.waitFor(() => {
await waitFor(() => {
expect(
entered.mock.calls.some(
([request]) =>