refactor(test): dedupe gateway env restores

This commit is contained in:
Peter Steinberger
2026-02-15 23:18:16 +00:00
parent 70f86e326d
commit 31980bcaf1
4 changed files with 14 additions and 33 deletions

View File

@@ -4,6 +4,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { createOpenClawTools } from "../agents/openclaw-tools.js";
import { resolveSessionTranscriptPath } from "../config/sessions.js";
import { emitAgentEvent } from "../infra/agent-events.js";
import { captureEnv } from "../test-utils/env.js";
import {
agentCommand,
getFreePort,
@@ -16,13 +17,11 @@ installGatewayTestHooks({ scope: "suite" });
let server: Awaited<ReturnType<typeof startGatewayServer>>;
let gatewayPort: number;
let prevGatewayPort: string | undefined;
let prevGatewayToken: string | undefined;
const gatewayToken = "test-token";
let envSnapshot: ReturnType<typeof captureEnv>;
beforeAll(async () => {
prevGatewayPort = process.env.OPENCLAW_GATEWAY_PORT;
prevGatewayToken = process.env.OPENCLAW_GATEWAY_TOKEN;
envSnapshot = captureEnv(["OPENCLAW_GATEWAY_PORT", "OPENCLAW_GATEWAY_TOKEN"]);
gatewayPort = await getFreePort();
testState.gatewayAuth = { mode: "token", token: gatewayToken };
process.env.OPENCLAW_GATEWAY_PORT = String(gatewayPort);
@@ -32,16 +31,7 @@ beforeAll(async () => {
afterAll(async () => {
await server.close();
if (prevGatewayPort === undefined) {
delete process.env.OPENCLAW_GATEWAY_PORT;
} else {
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
}
if (prevGatewayToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN;
} else {
process.env.OPENCLAW_GATEWAY_TOKEN = prevGatewayToken;
}
envSnapshot.restore();
});
describe("sessions_send gateway loopback", () => {

View File

@@ -1,5 +1,6 @@
import path from "node:path";
import { describe, expect, it } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import {
connectOk,
installGatewayTestHooks,
@@ -12,23 +13,19 @@ installGatewayTestHooks({ scope: "suite" });
async function withServer<T>(
run: (ws: Awaited<ReturnType<typeof startServerWithClient>>["ws"]) => Promise<T>,
) {
const { server, ws, prevToken } = await startServerWithClient("secret");
const { server, ws, envSnapshot } = await startServerWithClient("secret");
try {
return await run(ws);
} finally {
ws.close();
await server.close();
if (prevToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN;
} else {
process.env.OPENCLAW_GATEWAY_TOKEN = prevToken;
}
envSnapshot.restore();
}
}
describe("gateway skills.status", () => {
it("does not expose raw config values to operator.read clients", async () => {
const prevBundledSkillsDir = process.env.OPENCLAW_BUNDLED_SKILLS_DIR;
const envSnapshot = captureEnv(["OPENCLAW_BUNDLED_SKILLS_DIR"]);
process.env.OPENCLAW_BUNDLED_SKILLS_DIR = path.join(process.cwd(), "skills");
const secret = "discord-token-secret-abc";
const { writeConfigFile } = await import("../config/config.js");
@@ -62,11 +59,7 @@ describe("gateway skills.status", () => {
expect(check && "value" in check).toBe(false);
});
} finally {
if (prevBundledSkillsDir === undefined) {
delete process.env.OPENCLAW_BUNDLED_SKILLS_DIR;
} else {
process.env.OPENCLAW_BUNDLED_SKILLS_DIR = prevBundledSkillsDir;
}
envSnapshot.restore();
}
});
});

View File

@@ -11,17 +11,13 @@ installGatewayTestHooks({ scope: "suite" });
async function withServer<T>(
run: (ws: Awaited<ReturnType<typeof startServerWithClient>>["ws"]) => Promise<T>,
) {
const { server, ws, prevToken } = await startServerWithClient("secret");
const { server, ws, envSnapshot } = await startServerWithClient("secret");
try {
return await run(ws);
} finally {
ws.close();
await server.close();
if (prevToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN;
} else {
process.env.OPENCLAW_GATEWAY_TOKEN = prevToken;
}
envSnapshot.restore();
}
}

View File

@@ -16,6 +16,7 @@ import { drainSystemEvents, peekSystemEvents } from "../infra/system-events.js";
import { rawDataToString } from "../infra/ws.js";
import { resetLogger, setLoggerOverride } from "../logging.js";
import { DEFAULT_AGENT_ID, toAgentStoreSessionKey } from "../routing/session-key.js";
import { captureEnv } from "../test-utils/env.js";
import { getDeterministicFreePortBlock } from "../test-utils/ports.js";
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js";
import { buildDeviceAuthPayload } from "./device-auth.js";
@@ -374,6 +375,7 @@ export async function startServerWithClient(
) {
const { wsHeaders, ...gatewayOpts } = opts ?? {};
let port = await getFreePort();
const envSnapshot = captureEnv(["OPENCLAW_GATEWAY_TOKEN"]);
const prev = process.env.OPENCLAW_GATEWAY_TOKEN;
if (typeof token === "string") {
testState.gatewayAuth = { mode: "token", token };
@@ -421,7 +423,7 @@ export async function startServerWithClient(
ws.once("error", onError);
ws.once("close", onClose);
});
return { server, ws, port, prevToken: prev };
return { server, ws, port, prevToken: prev, envSnapshot };
}
type ConnectResponse = {