fix(live): reject loose heartbeat intervals · openclaw/openclaw@d717ff7
vincentkoc
·
2026-05-30
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -69,12 +69,20 @@ export function buildTestLiveEnv(args, baseEnv = process.env) {
|
69 | 69 | }; |
70 | 70 | } |
71 | 71 | |
72 | | -function parsePositiveInt(value, fallback) { |
73 | | -if (!value) { |
74 | | -return fallback; |
| 72 | +export function resolveTestLiveHeartbeatMs(baseEnv = process.env) { |
| 73 | +const value = baseEnv.OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS; |
| 74 | +if (value === undefined || value === "") { |
| 75 | +return 20_000; |
75 | 76 | } |
76 | | -const parsed = Number.parseInt(value, 10); |
77 | | -return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback; |
| 77 | +const text = value.trim(); |
| 78 | +if (!/^\d+$/u.test(text)) { |
| 79 | +throw new Error(`invalid OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: ${text}`); |
| 80 | +} |
| 81 | +const parsed = Number(text); |
| 82 | +if (!Number.isSafeInteger(parsed) || parsed <= 0) { |
| 83 | +throw new Error(`invalid OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: ${text}`); |
| 84 | +} |
| 85 | +return parsed; |
78 | 86 | } |
79 | 87 | |
80 | 88 | export function buildTestLivePnpmArgs(args) { |
@@ -96,7 +104,7 @@ export function main(argv = process.argv.slice(2), baseEnv = process.env) {
|
96 | 104 | } |
97 | 105 | |
98 | 106 | const env = buildTestLiveEnv(args, baseEnv); |
99 | | -const heartbeatMs = parsePositiveInt(baseEnv.OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS, 20_000); |
| 107 | +const heartbeatMs = resolveTestLiveHeartbeatMs(baseEnv); |
100 | 108 | const startedAt = Date.now(); |
101 | 109 | let lastOutputAt = startedAt; |
102 | 110 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import {
|
4 | 4 | buildTestLiveEnv, |
5 | 5 | buildTestLivePnpmArgs, |
6 | 6 | parseTestLiveArgs, |
| 7 | +resolveTestLiveHeartbeatMs, |
7 | 8 | } from "../../scripts/test-live.mjs"; |
8 | 9 | |
9 | 10 | describe("scripts/test-live", () => { |
@@ -66,6 +67,22 @@ describe("scripts/test-live", () => {
|
66 | 67 | }); |
67 | 68 | }); |
68 | 69 | |
| 70 | +it("rejects loose heartbeat intervals instead of parsing prefixes", () => { |
| 71 | +expect(resolveTestLiveHeartbeatMs({})).toBe(20_000); |
| 72 | +expect(resolveTestLiveHeartbeatMs({ OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: "2500" })).toBe( |
| 73 | +2500, |
| 74 | +); |
| 75 | +expect(() => |
| 76 | +resolveTestLiveHeartbeatMs({ OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: "1e3" }), |
| 77 | +).toThrow("invalid OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: 1e3"); |
| 78 | +expect(() => |
| 79 | +resolveTestLiveHeartbeatMs({ OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: "1000ms" }), |
| 80 | +).toThrow("invalid OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: 1000ms"); |
| 81 | +expect(() => |
| 82 | +resolveTestLiveHeartbeatMs({ OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: "0" }), |
| 83 | +).toThrow("invalid OPENCLAW_LIVE_WRAPPER_HEARTBEAT_MS: 0"); |
| 84 | +}); |
| 85 | + |
69 | 86 | it("prints help without spawning live Vitest", () => { |
70 | 87 | const result = spawnSync(process.execPath, ["scripts/test-live.mjs", "--help"], { |
71 | 88 | cwd: process.cwd(), |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。