From 8b2a5672be568ff83bd0588cedb6c119573bae0b Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 15:01:00 +0000 Subject: [PATCH] refactor(test): reuse command test harness --- src/auto-reply/reply/commands.test-harness.ts | 5 ++- src/auto-reply/reply/commands.test.ts | 40 ++----------------- 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/auto-reply/reply/commands.test-harness.ts b/src/auto-reply/reply/commands.test-harness.ts index 12b598c19..4cda5199f 100644 --- a/src/auto-reply/reply/commands.test-harness.ts +++ b/src/auto-reply/reply/commands.test-harness.ts @@ -7,6 +7,9 @@ export function buildCommandTestParams( commandBody: string, cfg: OpenClawConfig, ctxOverrides?: Partial, + options?: { + workspaceDir?: string; + }, ) { const ctx = { Body: commandBody, @@ -33,7 +36,7 @@ export function buildCommandTestParams( directives: parseInlineDirectives(commandBody), elevated: { enabled: true, allowed: true, failures: [] }, sessionKey: "agent:main:main", - workspaceDir: "/tmp", + workspaceDir: options?.workspaceDir ?? "/tmp", defaultGroupActivation: () => "mention", resolvedVerboseLevel: "off" as const, resolvedReasoningLevel: "off" as const, diff --git a/src/auto-reply/reply/commands.test.ts b/src/auto-reply/reply/commands.test.ts index c0f5a7be0..431755561 100644 --- a/src/auto-reply/reply/commands.test.ts +++ b/src/auto-reply/reply/commands.test.ts @@ -13,14 +13,14 @@ import { updateSessionStore } from "../../config/sessions.js"; import * as internalHooks from "../../hooks/internal-hooks.js"; import { clearPluginCommands, registerPluginCommand } from "../../plugins/commands.js"; import { resetBashChatCommandForTests } from "./bash-command.js"; -import { parseInlineDirectives } from "./directive-handling.js"; +import { buildCommandTestParams } from "./commands.test-harness.js"; const callGatewayMock = vi.fn(); vi.mock("../../gateway/call.js", () => ({ callGateway: (opts: unknown) => callGatewayMock(opts), })); -import { buildCommandContext, handleCommands } from "./commands.js"; +import { handleCommands } from "./commands.js"; // Avoid expensive workspace scans during /context tests. vi.mock("./commands-context-report.js", () => ({ @@ -48,41 +48,7 @@ afterAll(async () => { }); function buildParams(commandBody: string, cfg: OpenClawConfig, ctxOverrides?: Partial) { - const ctx = { - Body: commandBody, - CommandBody: commandBody, - CommandSource: "text", - CommandAuthorized: true, - Provider: "whatsapp", - Surface: "whatsapp", - ...ctxOverrides, - } as MsgContext; - - const command = buildCommandContext({ - ctx, - cfg, - isGroup: false, - triggerBodyNormalized: commandBody.trim().toLowerCase(), - commandAuthorized: true, - }); - - return { - ctx, - cfg, - command, - directives: parseInlineDirectives(commandBody), - elevated: { enabled: true, allowed: true, failures: [] }, - sessionKey: "agent:main:main", - workspaceDir: testWorkspaceDir, - defaultGroupActivation: () => "mention", - resolvedVerboseLevel: "off" as const, - resolvedReasoningLevel: "off" as const, - resolveDefaultThinkingLevel: async () => undefined, - provider: "whatsapp", - model: "test-model", - contextTokens: 0, - isGroup: false, - }; + return buildCommandTestParams(commandBody, cfg, ctxOverrides, { workspaceDir: testWorkspaceDir }); } describe("handleCommands gating", () => {