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

View File

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

View File

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

View File

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