Skip to content

Commit e38b8f6

Browse files
committed
fix(test): reject malformed cron cleanup limits
1 parent 646974b commit e38b8f6

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

scripts/e2e/cron-mcp-cleanup-docker-client.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import { setTimeout as delay } from "node:timers/promises";
77
import { pathToFileURL } from "node:url";
88
import { promisify } from "node:util";
99
import type { GatewayRpcClient } from "./mcp-channels-harness.ts";
10+
import { readPositiveIntEnv } from "./lib/env-limits.mjs";
1011

1112
const execFileAsync = promisify(execFile);
12-
const PROBE_PID_WAIT_MS = readPositiveInt(
13-
process.env.OPENCLAW_CRON_MCP_CLEANUP_PID_WAIT_MS,
14-
120_000,
15-
);
13+
const PROBE_PID_WAIT_MS = readCronMcpCleanupProbePidWaitMs();
1614
type McpChannelsHarness = typeof import("./mcp-channels-harness.ts");
1715
let mcpChannelsHarness: McpChannelsHarness | undefined;
1816

@@ -25,13 +23,8 @@ async function loadMcpChannelsHarness(): Promise<McpChannelsHarness> {
2523
return mcpChannelsHarness;
2624
}
2725

28-
function readPositiveInt(raw: string | undefined, fallback: number): number {
29-
const text = (raw ?? "").trim();
30-
if (!/^\d+$/u.test(text)) {
31-
return fallback;
32-
}
33-
const parsed = Number(text);
34-
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
26+
export function readCronMcpCleanupProbePidWaitMs(env: NodeJS.ProcessEnv = process.env): number {
27+
return readPositiveIntEnv("OPENCLAW_CRON_MCP_CLEANUP_PID_WAIT_MS", 120_000, env);
3528
}
3629

3730
async function readProbePid(pidPath: string): Promise<number | undefined> {

test/scripts/cron-mcp-cleanup-docker-client.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,26 @@ import fs from "node:fs";
22
import os from "node:os";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
5-
import { waitForProbePid } from "../../scripts/e2e/cron-mcp-cleanup-docker-client.ts";
5+
import {
6+
readCronMcpCleanupProbePidWaitMs,
7+
waitForProbePid,
8+
} from "../../scripts/e2e/cron-mcp-cleanup-docker-client.ts";
69

710
describe("cron MCP cleanup docker client", () => {
11+
it("rejects malformed probe pid wait limits", () => {
12+
expect(readCronMcpCleanupProbePidWaitMs({})).toBe(120_000);
13+
expect(
14+
readCronMcpCleanupProbePidWaitMs({ OPENCLAW_CRON_MCP_CLEANUP_PID_WAIT_MS: "250" }),
15+
).toBe(250);
16+
for (const value of ["1.5", "1e3", "10ms", "0"]) {
17+
expect(() =>
18+
readCronMcpCleanupProbePidWaitMs({
19+
OPENCLAW_CRON_MCP_CLEANUP_PID_WAIT_MS: value,
20+
}),
21+
).toThrow("invalid OPENCLAW_CRON_MCP_CLEANUP_PID_WAIT_MS");
22+
}
23+
});
24+
825
it("bounds missing probe pid waits", async () => {
926
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-cron-mcp-client-"));
1027
try {

0 commit comments

Comments
 (0)