mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 20:44:31 +00:00
refactor(test): share command handler params
This commit is contained in:
@@ -1,52 +1,13 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import type { MsgContext } from "../templating.js";
|
|
||||||
import { callGateway } from "../../gateway/call.js";
|
import { callGateway } from "../../gateway/call.js";
|
||||||
import { buildCommandContext, handleCommands } from "./commands.js";
|
import { handleCommands } from "./commands.js";
|
||||||
import { parseInlineDirectives } from "./directive-handling.js";
|
import { buildCommandTestParams } from "./commands.test-harness.js";
|
||||||
|
|
||||||
vi.mock("../../gateway/call.js", () => ({
|
vi.mock("../../gateway/call.js", () => ({
|
||||||
callGateway: vi.fn(),
|
callGateway: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
function buildParams(commandBody: string, cfg: OpenClawConfig, ctxOverrides?: Partial<MsgContext>) {
|
|
||||||
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: "/tmp",
|
|
||||||
defaultGroupActivation: () => "mention",
|
|
||||||
resolvedVerboseLevel: "off" as const,
|
|
||||||
resolvedReasoningLevel: "off" as const,
|
|
||||||
resolveDefaultThinkingLevel: async () => undefined,
|
|
||||||
provider: "whatsapp",
|
|
||||||
model: "test-model",
|
|
||||||
contextTokens: 0,
|
|
||||||
isGroup: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("/approve command", () => {
|
describe("/approve command", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
@@ -57,7 +18,7 @@ describe("/approve command", () => {
|
|||||||
commands: { text: true },
|
commands: { text: true },
|
||||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
const params = buildParams("/approve", cfg);
|
const params = buildCommandTestParams("/approve", cfg);
|
||||||
const result = await handleCommands(params);
|
const result = await handleCommands(params);
|
||||||
expect(result.shouldContinue).toBe(false);
|
expect(result.shouldContinue).toBe(false);
|
||||||
expect(result.reply?.text).toContain("Usage: /approve");
|
expect(result.reply?.text).toContain("Usage: /approve");
|
||||||
@@ -68,7 +29,7 @@ describe("/approve command", () => {
|
|||||||
commands: { text: true },
|
commands: { text: true },
|
||||||
channels: { whatsapp: { allowFrom: ["*"] } },
|
channels: { whatsapp: { allowFrom: ["*"] } },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
const params = buildParams("/approve abc allow-once", cfg, { SenderId: "123" });
|
const params = buildCommandTestParams("/approve abc allow-once", cfg, { SenderId: "123" });
|
||||||
|
|
||||||
const mockCallGateway = vi.mocked(callGateway);
|
const mockCallGateway = vi.mocked(callGateway);
|
||||||
mockCallGateway.mockResolvedValueOnce({ ok: true });
|
mockCallGateway.mockResolvedValueOnce({ ok: true });
|
||||||
@@ -88,7 +49,7 @@ describe("/approve command", () => {
|
|||||||
const cfg = {
|
const cfg = {
|
||||||
commands: { text: true },
|
commands: { text: true },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
const params = buildParams("/approve abc allow-once", cfg, {
|
const params = buildCommandTestParams("/approve abc allow-once", cfg, {
|
||||||
Provider: "webchat",
|
Provider: "webchat",
|
||||||
Surface: "webchat",
|
Surface: "webchat",
|
||||||
GatewayClientScopes: ["operator.write"],
|
GatewayClientScopes: ["operator.write"],
|
||||||
@@ -107,7 +68,7 @@ describe("/approve command", () => {
|
|||||||
const cfg = {
|
const cfg = {
|
||||||
commands: { text: true },
|
commands: { text: true },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
const params = buildParams("/approve abc allow-once", cfg, {
|
const params = buildCommandTestParams("/approve abc allow-once", cfg, {
|
||||||
Provider: "webchat",
|
Provider: "webchat",
|
||||||
Surface: "webchat",
|
Surface: "webchat",
|
||||||
GatewayClientScopes: ["operator.approvals"],
|
GatewayClientScopes: ["operator.approvals"],
|
||||||
@@ -131,7 +92,7 @@ describe("/approve command", () => {
|
|||||||
const cfg = {
|
const cfg = {
|
||||||
commands: { text: true },
|
commands: { text: true },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
const params = buildParams("/approve abc allow-once", cfg, {
|
const params = buildCommandTestParams("/approve abc allow-once", cfg, {
|
||||||
Provider: "webchat",
|
Provider: "webchat",
|
||||||
Surface: "webchat",
|
Surface: "webchat",
|
||||||
GatewayClientScopes: ["operator.admin"],
|
GatewayClientScopes: ["operator.admin"],
|
||||||
|
|||||||
@@ -1,49 +1,10 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import type { OpenClawConfig } from "../../config/config.js";
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
import type { MsgContext } from "../templating.js";
|
|
||||||
import { extractMessageText } from "./commands-subagents.js";
|
import { extractMessageText } from "./commands-subagents.js";
|
||||||
import { buildCommandContext, handleCommands } from "./commands.js";
|
import { handleCommands } from "./commands.js";
|
||||||
|
import { buildCommandTestParams } from "./commands.test-harness.js";
|
||||||
import { parseConfigCommand } from "./config-commands.js";
|
import { parseConfigCommand } from "./config-commands.js";
|
||||||
import { parseDebugCommand } from "./debug-commands.js";
|
import { parseDebugCommand } from "./debug-commands.js";
|
||||||
import { parseInlineDirectives } from "./directive-handling.js";
|
|
||||||
|
|
||||||
function buildParams(commandBody: string, cfg: OpenClawConfig, ctxOverrides?: Partial<MsgContext>) {
|
|
||||||
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: "/tmp",
|
|
||||||
defaultGroupActivation: () => "mention",
|
|
||||||
resolvedVerboseLevel: "off" as const,
|
|
||||||
resolvedReasoningLevel: "off" as const,
|
|
||||||
resolveDefaultThinkingLevel: async () => undefined,
|
|
||||||
provider: "whatsapp",
|
|
||||||
model: "test-model",
|
|
||||||
contextTokens: 0,
|
|
||||||
isGroup: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("parseConfigCommand", () => {
|
describe("parseConfigCommand", () => {
|
||||||
it("parses show/unset", () => {
|
it("parses show/unset", () => {
|
||||||
@@ -116,7 +77,7 @@ describe("handleCommands /config configWrites gating", () => {
|
|||||||
commands: { config: true, text: true },
|
commands: { config: true, text: true },
|
||||||
channels: { whatsapp: { allowFrom: ["*"], configWrites: false } },
|
channels: { whatsapp: { allowFrom: ["*"], configWrites: false } },
|
||||||
} as OpenClawConfig;
|
} as OpenClawConfig;
|
||||||
const params = buildParams('/config set messages.ackReaction=":)"', cfg);
|
const params = buildCommandTestParams('/config set messages.ackReaction=":)"', cfg);
|
||||||
const result = await handleCommands(params);
|
const result = await handleCommands(params);
|
||||||
expect(result.shouldContinue).toBe(false);
|
expect(result.shouldContinue).toBe(false);
|
||||||
expect(result.reply?.text).toContain("Config writes are disabled");
|
expect(result.reply?.text).toContain("Config writes are disabled");
|
||||||
|
|||||||
46
src/auto-reply/reply/commands.test-harness.ts
Normal file
46
src/auto-reply/reply/commands.test-harness.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import type { OpenClawConfig } from "../../config/config.js";
|
||||||
|
import type { MsgContext } from "../templating.js";
|
||||||
|
import { buildCommandContext } from "./commands.js";
|
||||||
|
import { parseInlineDirectives } from "./directive-handling.js";
|
||||||
|
|
||||||
|
export function buildCommandTestParams(
|
||||||
|
commandBody: string,
|
||||||
|
cfg: OpenClawConfig,
|
||||||
|
ctxOverrides?: Partial<MsgContext>,
|
||||||
|
) {
|
||||||
|
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: "/tmp",
|
||||||
|
defaultGroupActivation: () => "mention",
|
||||||
|
resolvedVerboseLevel: "off" as const,
|
||||||
|
resolvedReasoningLevel: "off" as const,
|
||||||
|
resolveDefaultThinkingLevel: async () => undefined,
|
||||||
|
provider: "whatsapp",
|
||||||
|
model: "test-model",
|
||||||
|
contextTokens: 0,
|
||||||
|
isGroup: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user