fix(scripts): parse startup bench gateway ports

This commit is contained in:
Vincent Koc
2026-05-29 04:39:34 +02:00
parent 564ccf1faa
commit 9ca791288c
2 changed files with 93 additions and 14 deletions

View File

@@ -429,6 +429,26 @@ function parseNonNegativeInt(raw: string | undefined, fallback: number, label =
return parseStrictIntegerOption({ fallback, label, min: 0, raw });
}
function parseGatewayPortEnv(raw: string | undefined): number {
const value = raw?.trim();
if (!value) {
return 32123;
}
const bracketHostMatch = /^\[[^\]]+\]:(\d+)$/u.exec(value);
if (bracketHostMatch) {
return parsePositiveInt(bracketHostMatch[1], 32123, "OPENCLAW_GATEWAY_PORT");
}
if (value.startsWith("[") && value.endsWith("]")) {
return 32123;
}
const colonCount = value.split(":").length - 1;
if (colonCount > 1) {
return 32123;
}
const portRaw = colonCount === 1 ? value.split(":")[1] : value;
return parsePositiveInt(portRaw, 32123, "OPENCLAW_GATEWAY_PORT");
}
function parsePresets(raw: string | undefined): string[] {
if (!raw) {
return ["startup"];
@@ -535,8 +555,7 @@ function buildConfigFixture(commandCase: CommandCase): Record<string, unknown> |
if (commandCase.id !== "configGetGatewayPort" && commandCase.id !== "gatewayHealthJson") {
return null;
}
const envPort = Number.parseInt(process.env.OPENCLAW_GATEWAY_PORT ?? "", 10);
const port = Number.isFinite(envPort) && envPort > 0 ? envPort : 32123;
const port = parseGatewayPortEnv(process.env.OPENCLAW_GATEWAY_PORT);
return {
gateway: {
auth: { mode: "none" },
@@ -1004,6 +1023,7 @@ async function main(): Promise<void> {
export const testing = {
buildConfigFixture,
collectFailedSamples,
parseGatewayPortEnv,
parseNonNegativeInt,
parsePositiveInt,
};

View File

@@ -1,6 +1,29 @@
import { describe, expect, it } from "vitest";
import { testing } from "../../scripts/bench-cli-startup.ts";
function withEnv<T>(env: Record<string, string | undefined>, callback: () => T): T {
const previous = new Map<string, string | undefined>();
for (const [key, value] of Object.entries(env)) {
previous.set(key, process.env[key]);
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
try {
return callback();
} finally {
for (const [key, value] of previous) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
}
}
describe("bench-cli-startup", () => {
it("fails reports with no measured samples", () => {
expect(
@@ -165,12 +188,14 @@ describe("bench-cli-startup", () => {
it("writes a config fixture for config get benchmarks", () => {
expect(
testing.buildConfigFixture({
id: "configGetGatewayPort",
name: "config get gateway.port",
args: ["config", "get", "gateway.port"],
presets: ["real"],
}),
withEnv({ OPENCLAW_GATEWAY_PORT: undefined }, () =>
testing.buildConfigFixture({
id: "configGetGatewayPort",
name: "config get gateway.port",
args: ["config", "get", "gateway.port"],
presets: ["real"],
}),
),
).toEqual({
gateway: {
auth: { mode: "none" },
@@ -180,12 +205,14 @@ describe("bench-cli-startup", () => {
},
});
expect(
testing.buildConfigFixture({
id: "gatewayHealthJson",
name: "gateway health --json",
args: ["gateway", "health", "--json"],
presets: ["real"],
}),
withEnv({ OPENCLAW_GATEWAY_PORT: undefined }, () =>
testing.buildConfigFixture({
id: "gatewayHealthJson",
name: "gateway health --json",
args: ["gateway", "health", "--json"],
presets: ["real"],
}),
),
).toEqual({
gateway: {
auth: { mode: "none" },
@@ -195,4 +222,36 @@ describe("bench-cli-startup", () => {
},
});
});
it("parses config fixture gateway ports strictly from env", () => {
expect(testing.parseGatewayPortEnv(undefined)).toBe(32123);
expect(testing.parseGatewayPortEnv("127.0.0.1:45678")).toBe(45678);
expect(testing.parseGatewayPortEnv("[::1]:45679")).toBe(45679);
expect(testing.parseGatewayPortEnv("::1")).toBe(32123);
expect(testing.parseGatewayPortEnv("[::1]")).toBe(32123);
expect(
withEnv({ OPENCLAW_GATEWAY_PORT: "45678" }, () =>
testing.buildConfigFixture({
id: "gatewayHealthJson",
name: "gateway health --json",
args: ["gateway", "health", "--json"],
presets: ["real"],
}),
),
).toMatchObject({ gateway: { port: 45678 } });
for (const invalid of ["45678abc", "127.0.0.1:45678abc"]) {
expect(() =>
withEnv({ OPENCLAW_GATEWAY_PORT: invalid }, () =>
testing.buildConfigFixture({
id: "gatewayHealthJson",
name: "gateway health --json",
args: ["gateway", "health", "--json"],
presets: ["real"],
}),
),
).toThrow("OPENCLAW_GATEWAY_PORT must be an integer >= 1");
}
});
});