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,41 +8,42 @@ 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") },
const secret = "discord-token-secret-abc"; async () => {
const { writeConfigFile } = await import("../config/config.js"); const secret = "discord-token-secret-abc";
await writeConfigFile({ const { writeConfigFile } = await import("../config/config.js");
session: { mainKey: "main-test" }, await writeConfigFile({
channels: { session: { mainKey: "main-test" },
discord: { channels: {
token: secret, 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<string, unknown>
>;
}>;
}>(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<string, unknown>>;
}>;
}>(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();
}
}); });
}); });