test: dedupe command gating coverage tables

This commit is contained in:
Peter Steinberger
2026-02-21 22:47:07 +00:00
parent b2de8719ad
commit 0e39371dc4

View File

@@ -136,33 +136,40 @@ function buildParams(commandBody: string, cfg: OpenClawConfig, ctxOverrides?: Pa
} }
describe("handleCommands gating", () => { describe("handleCommands gating", () => {
it("blocks /bash when disabled", async () => { it("blocks /bash when disabled or not elevated-allowlisted", async () => {
resetBashChatCommandForTests(); resetBashChatCommandForTests();
const cfg = { const cases = [
commands: { bash: false, text: true }, {
whatsapp: { allowFrom: ["*"] }, name: "disabled bash command",
} as OpenClawConfig; cfg: {
const params = buildParams("/bash echo hi", cfg); commands: { bash: false, text: true },
const result = await handleCommands(params); whatsapp: { allowFrom: ["*"] },
expect(result.shouldContinue).toBe(false); } as OpenClawConfig,
expect(result.reply?.text).toContain("bash is disabled"); expectedText: "bash is disabled",
}); },
{
it("blocks /bash when elevated is not allowlisted", async () => { name: "missing elevated allowlist",
resetBashChatCommandForTests(); cfg: {
const cfg = { commands: { bash: true, text: true },
commands: { bash: true, text: true }, whatsapp: { allowFrom: ["*"] },
whatsapp: { allowFrom: ["*"] }, } as OpenClawConfig,
} as OpenClawConfig; applyParams: (params: ReturnType<typeof buildParams>) => {
const params = buildParams("/bash echo hi", cfg); params.elevated = {
params.elevated = { enabled: true,
enabled: true, allowed: false,
allowed: false, failures: [{ gate: "allowFrom", key: "tools.elevated.allowFrom.whatsapp" }],
failures: [{ gate: "allowFrom", key: "tools.elevated.allowFrom.whatsapp" }], };
}; },
const result = await handleCommands(params); expectedText: "elevated is not available",
expect(result.shouldContinue).toBe(false); },
expect(result.reply?.text).toContain("elevated is not available"); ] as const;
for (const testCase of cases) {
const params = buildParams("/bash echo hi", testCase.cfg);
testCase.applyParams?.(params);
const result = await handleCommands(params);
expect(result.shouldContinue, testCase.name).toBe(false);
expect(result.reply?.text, testCase.name).toContain(testCase.expectedText);
}
}); });
it("blocks /config and /debug when disabled", async () => { it("blocks /config and /debug when disabled", async () => {
@@ -193,17 +200,16 @@ describe("handleCommands gating", () => {
channels: { whatsapp: { allowFrom: ["*"] } }, channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig; } as OpenClawConfig;
const bashResult = await handleCommands(buildParams("/bash echo hi", cfg)); const cases = [
expect(bashResult.shouldContinue).toBe(false); { commandBody: "/bash echo hi", expectedText: "bash is disabled" },
expect(bashResult.reply?.text).toContain("bash is disabled"); { commandBody: "/config show", expectedText: "/config is disabled" },
{ commandBody: "/debug show", expectedText: "/debug is disabled" },
const configResult = await handleCommands(buildParams("/config show", cfg)); ] as const;
expect(configResult.shouldContinue).toBe(false); for (const testCase of cases) {
expect(configResult.reply?.text).toContain("/config is disabled"); const result = await handleCommands(buildParams(testCase.commandBody, cfg));
expect(result.shouldContinue, testCase.commandBody).toBe(false);
const debugResult = await handleCommands(buildParams("/debug show", cfg)); expect(result.reply?.text, testCase.commandBody).toContain(testCase.expectedText);
expect(debugResult.shouldContinue).toBe(false); }
expect(debugResult.reply?.text).toContain("/debug is disabled");
}); });
}); });