refactor(test): simplify env setup in safe bins and skills status

This commit is contained in:
Peter Steinberger
2026-02-21 18:20:31 +00:00
parent 8fd8988ff7
commit a410dad602
2 changed files with 44 additions and 47 deletions

View File

@@ -4,7 +4,7 @@ import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js"; import type { OpenClawConfig } from "../config/config.js";
import type { ExecApprovalsResolved } from "../infra/exec-approvals.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"]); const bundledPluginsDirSnapshot = captureEnv(["OPENCLAW_BUNDLED_PLUGINS_DIR"]);
@@ -130,18 +130,14 @@ describe("createOpenClawCodingTools safeBins", () => {
}); });
const marker = `safe-bins-${Date.now()}`; const marker = `safe-bins-${Date.now()}`;
const envSnapshot = captureEnv(["OPENCLAW_SHELL_ENV_TIMEOUT_MS"]); const result = await withEnvAsync(
const result = await (async () => { { OPENCLAW_SHELL_ENV_TIMEOUT_MS: "1000" },
try { async () =>
process.env.OPENCLAW_SHELL_ENV_TIMEOUT_MS = "1000"; await execTool.execute("call1", {
return await execTool.execute("call1", {
command: `echo ${marker}`, command: `echo ${marker}`,
workdir: tmpDir, workdir: tmpDir,
}); }),
} finally { );
envSnapshot.restore();
}
})();
const text = result.content.find((content) => content.type === "text")?.text ?? ""; const text = result.content.find((content) => content.type === "text")?.text ?? "";
const resultDetails = result.details as { status?: string }; const resultDetails = result.details as { status?: string };

View File

@@ -1,6 +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 { withEnvAsync } from "../test-utils/env.js";
import { connectOk, installGatewayTestHooks, rpcReq } from "./test-helpers.js"; import { connectOk, installGatewayTestHooks, rpcReq } from "./test-helpers.js";
import { withServer } from "./test-with-server.js"; import { withServer } from "./test-with-server.js";
@@ -8,8 +8,9 @@ installGatewayTestHooks({ scope: "suite" });
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 envSnapshot = captureEnv(["OPENCLAW_BUNDLED_SKILLS_DIR"]); await withEnvAsync(
process.env.OPENCLAW_BUNDLED_SKILLS_DIR = path.join(process.cwd(), "skills"); { OPENCLAW_BUNDLED_SKILLS_DIR: path.join(process.cwd(), "skills") },
async () => {
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");
await writeConfigFile({ await writeConfigFile({
@@ -21,13 +22,14 @@ describe("gateway skills.status", () => {
}, },
}); });
try {
await withServer(async (ws) => { await withServer(async (ws) => {
await connectOk(ws, { token: "secret", scopes: ["operator.read"] }); await connectOk(ws, { token: "secret", scopes: ["operator.read"] });
const res = await rpcReq<{ const res = await rpcReq<{
skills?: Array<{ skills?: Array<{
name?: string; name?: string;
configChecks?: Array<{ path?: string; satisfied?: boolean } & Record<string, unknown>>; configChecks?: Array<
{ path?: string; satisfied?: boolean } & Record<string, unknown>
>;
}>; }>;
}>(ws, "skills.status", {}); }>(ws, "skills.status", {});
@@ -41,8 +43,7 @@ describe("gateway skills.status", () => {
expect(check?.satisfied).toBe(true); expect(check?.satisfied).toBe(true);
expect(check && "value" in check).toBe(false); expect(check && "value" in check).toBe(false);
}); });
} finally { },
envSnapshot.restore(); );
}
}); });
}); });