test: keep timeout clamp checks under one second

This commit is contained in:
Peter Steinberger
2026-05-31 06:51:21 +01:00
parent 04b68e8fa4
commit b372af6b81
2 changed files with 47 additions and 23 deletions
+21 -13
View File
@@ -20,6 +20,7 @@ describe("compaction planning worker", () => {
let packagedSummaryChunks: Awaited<
ReturnType<typeof compactionPlanningWorkerTesting.runCompactionPlanningWorker>
>;
let oversizedWorkerTimeoutCalls: unknown[][];
beforeAll(async () => {
packagedSummaryChunks = await compactionPlanningWorkerTesting.runCompactionPlanningWorker({
@@ -30,6 +31,21 @@ describe("compaction planning worker", () => {
},
timeoutMs: 10_000,
});
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
try {
await compactionPlanningWorkerTesting.runCompactionPlanningWorker({
input: {
kind: "summaryChunks",
messages: [makeMessage(1), makeMessage(2), makeMessage(3)],
maxChunkTokens: 1200,
},
timeoutMs: Number.MAX_SAFE_INTEGER,
});
oversizedWorkerTimeoutCalls = [...setTimeoutSpy.mock.calls];
} finally {
setTimeoutSpy.mockRestore();
}
}, 10_000);
it("resolves the packaged worker URL from stable and hashed dist modules", () => {
@@ -83,19 +99,11 @@ describe("compaction planning worker", () => {
expect(value.chunks.length).toBeGreaterThan(1);
});
it("clamps oversized worker timeouts before scheduling", async () => {
const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout");
await compactionPlanningWorkerTesting.runCompactionPlanningWorker({
input: {
kind: "summaryChunks",
messages: [makeMessage(1), makeMessage(2), makeMessage(3)],
maxChunkTokens: 1200,
},
timeoutMs: Number.MAX_SAFE_INTEGER,
});
expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
it("clamps oversized worker timeouts before scheduling", () => {
expect(oversizedWorkerTimeoutCalls).toContainEqual([
expect.any(Function),
MAX_TIMER_TIMEOUT_MS,
]);
});
it("classifies missing worker runtime as unavailable", async () => {
+26 -10
View File
@@ -2,7 +2,7 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { loggingState } from "../logging/state.js";
import type { RuntimeEnv } from "../runtime.js";
@@ -219,8 +219,9 @@ vi.mock("./agent.js", () => {
});
let originalForceConsoleToStderr = false;
let zeroTimeoutGatewayRequestMs: number | undefined;
beforeEach(() => {
function resetAgentCliCommandMocksForTest() {
vi.clearAllMocks();
agentViaGatewayTesting.resetLazyImportsForTests();
agentViaGatewayTesting.setGatewayAbortRetryDelaysMsForTests([0, 0, 0, 0]);
@@ -228,6 +229,10 @@ beforeEach(() => {
agentViaGatewayTesting.setAgentSessionModuleLoaderForTests(loadAgentSessionModuleMock);
originalForceConsoleToStderr = loggingState.forceConsoleToStderr;
loggingState.forceConsoleToStderr = false;
}
beforeEach(() => {
resetAgentCliCommandMocksForTest();
});
afterEach(() => {
@@ -236,16 +241,27 @@ afterEach(() => {
});
describe("agentCliCommand", () => {
it("uses a timer-safe max gateway timeout when --timeout is 0", async () => {
await withTempStore(async () => {
mockGatewaySuccessReply();
beforeAll(async () => {
const restoreForceConsoleToStderr = loggingState.forceConsoleToStderr;
resetAgentCliCommandMocksForTest();
try {
await withTempStore(async () => {
mockGatewaySuccessReply();
await agentCliCommand({ message: "hi", to: "+1555", timeout: "0" }, runtime);
await agentCliCommand({ message: "hi", to: "+1555", timeout: "0" }, runtime);
expect(callGateway).toHaveBeenCalledTimes(1);
const request = requireFirstCallArg(callGateway, "gateway") as { timeoutMs?: number };
expect(request.timeoutMs).toBe(2_147_000_000);
});
expect(callGateway).toHaveBeenCalledTimes(1);
const request = requireFirstCallArg(callGateway, "gateway") as { timeoutMs?: number };
zeroTimeoutGatewayRequestMs = request.timeoutMs;
});
} finally {
agentViaGatewayTesting.setGatewayAbortRetryDelaysMsForTests();
loggingState.forceConsoleToStderr = restoreForceConsoleToStderr;
}
});
it("uses a timer-safe max gateway timeout when --timeout is 0", () => {
expect(zeroTimeoutGatewayRequestMs).toBe(2_147_000_000);
});
it("clamps oversized gateway timeout seconds", () => {