From a410dad60228231eb58a5aa52e30c2b7cfc0aefa Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 18:20:31 +0000 Subject: [PATCH] refactor(test): simplify env setup in safe bins and skills status --- src/agents/pi-tools.safe-bins.e2e.test.ts | 18 ++--- src/gateway/server.skills-status.e2e.test.ts | 73 ++++++++++---------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/src/agents/pi-tools.safe-bins.e2e.test.ts b/src/agents/pi-tools.safe-bins.e2e.test.ts index 0892246be..7ccd4ad7b 100644 --- a/src/agents/pi-tools.safe-bins.e2e.test.ts +++ b/src/agents/pi-tools.safe-bins.e2e.test.ts @@ -4,7 +4,7 @@ import path from "node:path"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../config/config.js"; import type { ExecApprovalsResolved } from "../infra/exec-approvals.js"; -import { captureEnv } from "../test-utils/env.js"; +import { captureEnv, withEnvAsync } from "../test-utils/env.js"; const bundledPluginsDirSnapshot = captureEnv(["OPENCLAW_BUNDLED_PLUGINS_DIR"]); @@ -130,18 +130,14 @@ describe("createOpenClawCodingTools safeBins", () => { }); const marker = `safe-bins-${Date.now()}`; - const envSnapshot = captureEnv(["OPENCLAW_SHELL_ENV_TIMEOUT_MS"]); - const result = await (async () => { - try { - process.env.OPENCLAW_SHELL_ENV_TIMEOUT_MS = "1000"; - return await execTool.execute("call1", { + const result = await withEnvAsync( + { OPENCLAW_SHELL_ENV_TIMEOUT_MS: "1000" }, + async () => + await execTool.execute("call1", { command: `echo ${marker}`, workdir: tmpDir, - }); - } finally { - envSnapshot.restore(); - } - })(); + }), + ); const text = result.content.find((content) => content.type === "text")?.text ?? ""; const resultDetails = result.details as { status?: string }; diff --git a/src/gateway/server.skills-status.e2e.test.ts b/src/gateway/server.skills-status.e2e.test.ts index 9cf05ffac..746574dc9 100644 --- a/src/gateway/server.skills-status.e2e.test.ts +++ b/src/gateway/server.skills-status.e2e.test.ts @@ -1,6 +1,6 @@ import path from "node:path"; import { describe, expect, it } from "vitest"; -import { captureEnv } from "../test-utils/env.js"; +import { withEnvAsync } from "../test-utils/env.js"; import { connectOk, installGatewayTestHooks, rpcReq } from "./test-helpers.js"; import { withServer } from "./test-with-server.js"; @@ -8,41 +8,42 @@ installGatewayTestHooks({ scope: "suite" }); describe("gateway skills.status", () => { it("does not expose raw config values to operator.read clients", async () => { - 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"); - await writeConfigFile({ - session: { mainKey: "main-test" }, - channels: { - discord: { - token: secret, - }, + await withEnvAsync( + { OPENCLAW_BUNDLED_SKILLS_DIR: path.join(process.cwd(), "skills") }, + async () => { + const secret = "discord-token-secret-abc"; + const { writeConfigFile } = await import("../config/config.js"); + await writeConfigFile({ + session: { mainKey: "main-test" }, + channels: { + discord: { + token: secret, + }, + }, + }); + + await withServer(async (ws) => { + await connectOk(ws, { token: "secret", scopes: ["operator.read"] }); + const res = await rpcReq<{ + skills?: Array<{ + name?: string; + configChecks?: Array< + { path?: string; satisfied?: boolean } & Record + >; + }>; + }>(ws, "skills.status", {}); + + expect(res.ok).toBe(true); + expect(JSON.stringify(res.payload)).not.toContain(secret); + + const discord = res.payload?.skills?.find((s) => s.name === "discord"); + expect(discord).toBeTruthy(); + const check = discord?.configChecks?.find((c) => c.path === "channels.discord.token"); + expect(check).toBeTruthy(); + expect(check?.satisfied).toBe(true); + expect(check && "value" in check).toBe(false); + }); }, - }); - - try { - await withServer(async (ws) => { - await connectOk(ws, { token: "secret", scopes: ["operator.read"] }); - const res = await rpcReq<{ - skills?: Array<{ - name?: string; - configChecks?: Array<{ path?: string; satisfied?: boolean } & Record>; - }>; - }>(ws, "skills.status", {}); - - expect(res.ok).toBe(true); - expect(JSON.stringify(res.payload)).not.toContain(secret); - - const discord = res.payload?.skills?.find((s) => s.name === "discord"); - expect(discord).toBeTruthy(); - const check = discord?.configChecks?.find((c) => c.path === "channels.discord.token"); - expect(check).toBeTruthy(); - expect(check?.satisfied).toBe(true); - expect(check && "value" in check).toBe(false); - }); - } finally { - envSnapshot.restore(); - } + ); }); });