Files
openclaw/src/commands/message.test.ts

270 lines
7.1 KiB
TypeScript
Raw Normal View History

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type {
ChannelMessageActionAdapter,
ChannelOutboundAdapter,
ChannelPlugin,
} from "../channels/plugins/types.js";
import type { CliDeps } from "../cli/deps.js";
import type { RuntimeEnv } from "../runtime.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { captureEnv } from "../test-utils/env.js";
2026-01-01 21:22:59 +01:00
let testConfig: Record<string, unknown> = {};
vi.mock("../config/config.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../config/config.js")>();
return {
...actual,
loadConfig: () => testConfig,
};
});
2026-01-01 21:22:59 +01:00
2025-12-09 20:18:50 +00:00
const callGatewayMock = vi.fn();
vi.mock("../gateway/call.js", () => ({
2026-02-17 11:59:29 +09:00
callGateway: callGatewayMock,
callGatewayLeastPrivilege: callGatewayMock,
2025-12-09 20:18:50 +00:00
randomIdempotencyKey: () => "idem-1",
}));
2026-01-09 08:27:17 +01:00
const webAuthExists = vi.fn(async () => false);
vi.mock("../web/session.js", () => ({
2026-02-17 11:59:29 +09:00
webAuthExists,
2026-01-09 08:27:17 +01:00
}));
2026-02-17 14:32:43 +09:00
const handleDiscordAction = vi.fn(async (..._args: unknown[]) => ({ details: { ok: true } }));
2026-01-09 08:27:17 +01:00
vi.mock("../agents/tools/discord-actions.js", () => ({
2026-02-17 11:59:29 +09:00
handleDiscordAction,
2026-01-09 08:27:17 +01:00
}));
2026-02-17 14:32:43 +09:00
const handleSlackAction = vi.fn(async (..._args: unknown[]) => ({ details: { ok: true } }));
2026-01-09 08:27:17 +01:00
vi.mock("../agents/tools/slack-actions.js", () => ({
2026-02-17 11:59:29 +09:00
handleSlackAction,
2026-01-09 08:27:17 +01:00
}));
2026-02-17 14:32:43 +09:00
const handleTelegramAction = vi.fn(async (..._args: unknown[]) => ({ details: { ok: true } }));
2026-01-09 08:27:17 +01:00
vi.mock("../agents/tools/telegram-actions.js", () => ({
2026-02-17 11:59:29 +09:00
handleTelegramAction,
2026-01-09 08:27:17 +01:00
}));
2026-02-17 14:32:43 +09:00
const handleWhatsAppAction = vi.fn(async (..._args: unknown[]) => ({ details: { ok: true } }));
2026-01-09 08:27:17 +01:00
vi.mock("../agents/tools/whatsapp-actions.js", () => ({
2026-02-17 11:59:29 +09:00
handleWhatsAppAction,
2026-01-09 08:27:17 +01:00
}));
let envSnapshot: ReturnType<typeof captureEnv>;
const setRegistry = async (registry: ReturnType<typeof createTestRegistry>) => {
const { setActivePluginRegistry } = await import("../plugins/runtime.js");
setActivePluginRegistry(registry);
};
beforeEach(async () => {
envSnapshot = captureEnv(["TELEGRAM_BOT_TOKEN", "DISCORD_BOT_TOKEN"]);
2026-01-09 08:27:17 +01:00
process.env.TELEGRAM_BOT_TOKEN = "";
process.env.DISCORD_BOT_TOKEN = "";
2026-01-01 21:22:59 +01:00
testConfig = {};
await setRegistry(createTestRegistry([]));
callGatewayMock.mockClear();
webAuthExists.mockClear().mockResolvedValue(false);
handleDiscordAction.mockClear();
handleSlackAction.mockClear();
handleTelegramAction.mockClear();
handleWhatsAppAction.mockClear();
});
afterEach(() => {
envSnapshot.restore();
});
const runtime: RuntimeEnv = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(() => {
throw new Error("exit");
}),
};
2025-12-05 19:03:59 +00:00
const makeDeps = (overrides: Partial<CliDeps> = {}): CliDeps => ({
sendMessageWhatsApp: vi.fn(),
sendMessageTelegram: vi.fn(),
2025-12-15 10:11:18 -06:00
sendMessageDiscord: vi.fn(),
sendMessageSlack: vi.fn(),
2026-01-01 15:43:15 +01:00
sendMessageSignal: vi.fn(),
2026-01-02 01:19:13 +01:00
sendMessageIMessage: vi.fn(),
2025-12-05 19:03:59 +00:00
...overrides,
});
const createStubPlugin = (params: {
id: ChannelPlugin["id"];
label?: string;
actions?: ChannelMessageActionAdapter;
outbound?: ChannelOutboundAdapter;
}): ChannelPlugin => ({
id: params.id,
meta: {
id: params.id,
label: params.label ?? String(params.id),
selectionLabel: params.label ?? String(params.id),
docsPath: `/channels/${params.id}`,
blurb: "test stub.",
},
capabilities: { chatTypes: ["direct"] },
config: {
listAccountIds: () => ["default"],
resolveAccount: () => ({}),
isConfigured: async () => true,
},
actions: params.actions,
outbound: params.outbound,
});
2026-02-17 15:48:16 +09:00
type ChannelActionParams = Parameters<
NonNullable<NonNullable<ChannelPlugin["actions"]>["handleAction"]>
>[0];
const createDiscordPollPluginRegistration = () => ({
pluginId: "discord",
source: "test",
plugin: createStubPlugin({
id: "discord",
label: "Discord",
actions: {
listActions: () => ["poll"],
2026-02-17 15:48:16 +09:00
handleAction: (async ({ action, params, cfg, accountId }: ChannelActionParams) => {
return await handleDiscordAction(
{ action, to: params.to, accountId: accountId ?? undefined },
cfg,
2026-02-17 15:48:16 +09:00
);
}) as unknown as NonNullable<ChannelPlugin["actions"]>["handleAction"],
},
}),
});
const createTelegramSendPluginRegistration = () => ({
pluginId: "telegram",
source: "test",
plugin: createStubPlugin({
id: "telegram",
label: "Telegram",
actions: {
listActions: () => ["send"],
2026-02-17 15:48:16 +09:00
handleAction: (async ({ action, params, cfg, accountId }: ChannelActionParams) => {
return await handleTelegramAction(
{ action, to: params.to, accountId: accountId ?? undefined },
cfg,
2026-02-17 15:48:16 +09:00
);
}) as unknown as NonNullable<ChannelPlugin["actions"]>["handleAction"],
},
}),
});
const { messageCommand } = await import("./message.js");
2026-01-09 08:27:17 +01:00
describe("messageCommand", () => {
it("defaults channel when only one configured", async () => {
2026-01-09 08:27:17 +01:00
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
await setRegistry(
createTestRegistry([
{
...createTelegramSendPluginRegistration(),
},
]),
);
2025-12-05 19:03:59 +00:00
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await messageCommand(
{
target: "123456",
message: "hi",
},
deps,
runtime,
);
2026-01-09 08:27:17 +01:00
expect(handleTelegramAction).toHaveBeenCalled();
});
it("requires channel when multiple configured", async () => {
2026-01-09 08:27:17 +01:00
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
process.env.DISCORD_BOT_TOKEN = "token-discord";
await setRegistry(
createTestRegistry([
{
...createTelegramSendPluginRegistration(),
},
{
...createDiscordPollPluginRegistration(),
},
]),
);
2025-12-05 19:03:59 +00:00
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await expect(
messageCommand(
{
target: "123",
2026-01-09 08:27:17 +01:00
message: "hi",
},
deps,
runtime,
),
).rejects.toThrow(/Channel is required/);
});
2026-01-09 08:27:17 +01:00
it("sends via gateway for WhatsApp", async () => {
callGatewayMock.mockResolvedValueOnce({ messageId: "g1" });
await setRegistry(
createTestRegistry([
{
pluginId: "whatsapp",
source: "test",
plugin: createStubPlugin({
id: "whatsapp",
label: "WhatsApp",
outbound: {
deliveryMode: "gateway",
},
}),
},
]),
);
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await messageCommand(
{
2026-01-09 08:27:17 +01:00
action: "send",
channel: "whatsapp",
target: "+15551234567",
message: "hi",
},
deps,
runtime,
);
2026-01-09 08:27:17 +01:00
expect(callGatewayMock).toHaveBeenCalled();
});
2026-01-09 08:27:17 +01:00
it("routes discord polls through message action", async () => {
await setRegistry(
createTestRegistry([
{
...createDiscordPollPluginRegistration(),
},
]),
);
2026-01-03 23:57:43 +00:00
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await messageCommand(
2026-01-03 23:57:43 +00:00
{
2026-01-09 08:27:17 +01:00
action: "poll",
channel: "discord",
target: "channel:123456789",
2026-01-09 08:27:17 +01:00
pollQuestion: "Snack?",
pollOption: ["Pizza", "Sushi"],
2026-01-03 23:57:43 +00:00
},
deps,
runtime,
);
2026-01-09 08:27:17 +01:00
expect(handleDiscordAction).toHaveBeenCalledWith(
2026-01-03 23:57:43 +00:00
expect.objectContaining({
2026-01-09 08:27:17 +01:00
action: "poll",
to: "channel:123456789",
2026-01-03 23:57:43 +00:00
}),
2026-01-09 08:27:17 +01:00
expect.any(Object),
2026-01-03 23:57:43 +00:00
);
});
2026-01-09 06:43:40 +01:00
});