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 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 };

View File

@@ -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<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();
}
);
});
});