test(core): dedupe command gating and trim announce reset overhead

This commit is contained in:
Peter Steinberger
2026-02-22 08:54:06 +00:00
parent a4c107ee11
commit d625f888a9
2 changed files with 88 additions and 54 deletions

View File

@@ -143,10 +143,10 @@ vi.mock("../config/config.js", async (importOriginal) => {
describe("subagent announce formatting", () => { describe("subagent announce formatting", () => {
beforeEach(() => { beforeEach(() => {
agentSpy agentSpy
.mockReset() .mockClear()
.mockImplementation(async (_req: AgentCallRequest) => ({ runId: "run-main", status: "ok" })); .mockImplementation(async (_req: AgentCallRequest) => ({ runId: "run-main", status: "ok" }));
sendSpy sendSpy
.mockReset() .mockClear()
.mockImplementation(async (_req: AgentCallRequest) => ({ runId: "send-main", status: "ok" })); .mockImplementation(async (_req: AgentCallRequest) => ({ runId: "send-main", status: "ok" }));
sessionsDeleteSpy.mockClear().mockImplementation((_req: AgentCallRequest) => undefined); sessionsDeleteSpy.mockClear().mockImplementation((_req: AgentCallRequest) => undefined);
embeddedRunMock.isEmbeddedPiRunActive.mockReset().mockReturnValue(false); embeddedRunMock.isEmbeddedPiRunActive.mockReset().mockReturnValue(false);

View File

@@ -137,28 +137,32 @@ function buildParams(commandBody: string, cfg: OpenClawConfig, ctxOverrides?: Pa
} }
describe("handleCommands gating", () => { describe("handleCommands gating", () => {
it("blocks /bash when disabled or not elevated-allowlisted", async () => { it("blocks gated commands when disabled or not elevated-allowlisted", async () => {
resetBashChatCommandForTests();
const cases = typedCases<{ const cases = typedCases<{
name: string; name: string;
cfg: OpenClawConfig; commandBody: string;
makeCfg: () => OpenClawConfig;
applyParams?: (params: ReturnType<typeof buildParams>) => void; applyParams?: (params: ReturnType<typeof buildParams>) => void;
expectedText: string; expectedText: string;
}>([ }>([
{ {
name: "disabled bash command", name: "disabled bash command",
cfg: { commandBody: "/bash echo hi",
commands: { bash: false, text: true }, makeCfg: () =>
whatsapp: { allowFrom: ["*"] }, ({
} as OpenClawConfig, commands: { bash: false, text: true },
whatsapp: { allowFrom: ["*"] },
}) as OpenClawConfig,
expectedText: "bash is disabled", expectedText: "bash is disabled",
}, },
{ {
name: "missing elevated allowlist", name: "missing elevated allowlist",
cfg: { commandBody: "/bash echo hi",
commands: { bash: true, text: true }, makeCfg: () =>
whatsapp: { allowFrom: ["*"] }, ({
} as OpenClawConfig, commands: { bash: true, text: true },
whatsapp: { allowFrom: ["*"] },
}) as OpenClawConfig,
applyParams: (params: ReturnType<typeof buildParams>) => { applyParams: (params: ReturnType<typeof buildParams>) => {
params.elevated = { params.elevated = {
enabled: true, enabled: true,
@@ -168,55 +172,85 @@ describe("handleCommands gating", () => {
}, },
expectedText: "elevated is not available", expectedText: "elevated is not available",
}, },
{
name: "disabled config command",
commandBody: "/config show",
makeCfg: () =>
({
commands: { config: false, debug: false, text: true },
channels: { whatsapp: { allowFrom: ["*"] } },
}) as OpenClawConfig,
expectedText: "/config is disabled",
},
{
name: "disabled debug command",
commandBody: "/debug show",
makeCfg: () =>
({
commands: { config: false, debug: false, text: true },
channels: { whatsapp: { allowFrom: ["*"] } },
}) as OpenClawConfig,
expectedText: "/debug is disabled",
},
{
name: "inherited bash flag does not enable command",
commandBody: "/bash echo hi",
makeCfg: () => {
const inheritedCommands = Object.create({
bash: true,
config: true,
debug: true,
}) as Record<string, unknown>;
return {
commands: inheritedCommands as never,
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig;
},
expectedText: "bash is disabled",
},
{
name: "inherited config flag does not enable command",
commandBody: "/config show",
makeCfg: () => {
const inheritedCommands = Object.create({
bash: true,
config: true,
debug: true,
}) as Record<string, unknown>;
return {
commands: inheritedCommands as never,
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig;
},
expectedText: "/config is disabled",
},
{
name: "inherited debug flag does not enable command",
commandBody: "/debug show",
makeCfg: () => {
const inheritedCommands = Object.create({
bash: true,
config: true,
debug: true,
}) as Record<string, unknown>;
return {
commands: inheritedCommands as never,
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig;
},
expectedText: "/debug is disabled",
},
]); ]);
for (const testCase of cases) { for (const testCase of cases) {
const params = buildParams("/bash echo hi", testCase.cfg); resetBashChatCommandForTests();
const params = buildParams(testCase.commandBody, testCase.makeCfg());
testCase.applyParams?.(params); testCase.applyParams?.(params);
const result = await handleCommands(params); const result = await handleCommands(params);
expect(result.shouldContinue, testCase.name).toBe(false); expect(result.shouldContinue, testCase.name).toBe(false);
expect(result.reply?.text, testCase.name).toContain(testCase.expectedText); expect(result.reply?.text, testCase.name).toContain(testCase.expectedText);
} }
}); });
it("blocks /config and /debug when disabled", async () => {
const cfg = {
commands: { config: false, debug: false, text: true },
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig;
const cases = [
{ commandBody: "/config show", expectedText: "/config is disabled" },
{ commandBody: "/debug show", expectedText: "/debug is disabled" },
] as const;
for (const testCase of cases) {
const params = buildParams(testCase.commandBody, cfg);
const result = await handleCommands(params);
expect(result.shouldContinue).toBe(false);
expect(result.reply?.text).toContain(testCase.expectedText);
}
});
it("does not enable gated commands from inherited command flags", async () => {
const inheritedCommands = Object.create({
bash: true,
config: true,
debug: true,
}) as Record<string, unknown>;
const cfg = {
commands: inheritedCommands as never,
channels: { whatsapp: { allowFrom: ["*"] } },
} as OpenClawConfig;
const cases = [
{ commandBody: "/bash echo hi", expectedText: "bash is disabled" },
{ commandBody: "/config show", expectedText: "/config is disabled" },
{ commandBody: "/debug show", expectedText: "/debug is disabled" },
] as const;
for (const testCase of cases) {
const result = await handleCommands(buildParams(testCase.commandBody, cfg));
expect(result.shouldContinue, testCase.commandBody).toBe(false);
expect(result.reply?.text, testCase.commandBody).toContain(testCase.expectedText);
}
});
}); });
describe("/approve command", () => { describe("/approve command", () => {