openclaw/src/commands/message.test.ts

154 lines
4.2 KiB
TypeScript
Raw Normal View History

import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { CliDeps } from "../cli/deps.js";
import type { RuntimeEnv } from "../runtime.js";
2026-01-09 08:27:17 +01:00
import { messageCommand } from "./message.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", () => ({
callGateway: (...args: unknown[]) => callGatewayMock(...args),
randomIdempotencyKey: () => "idem-1",
}));
2026-01-09 08:27:17 +01:00
const webAuthExists = vi.fn(async () => false);
vi.mock("../web/session.js", () => ({
webAuthExists: (...args: unknown[]) => webAuthExists(...args),
}));
const handleDiscordAction = vi.fn(async () => ({ details: { ok: true } }));
vi.mock("../agents/tools/discord-actions.js", () => ({
handleDiscordAction: (...args: unknown[]) => handleDiscordAction(...args),
}));
const handleSlackAction = vi.fn(async () => ({ details: { ok: true } }));
vi.mock("../agents/tools/slack-actions.js", () => ({
handleSlackAction: (...args: unknown[]) => handleSlackAction(...args),
}));
const handleTelegramAction = vi.fn(async () => ({ details: { ok: true } }));
vi.mock("../agents/tools/telegram-actions.js", () => ({
handleTelegramAction: (...args: unknown[]) => handleTelegramAction(...args),
}));
const handleWhatsAppAction = vi.fn(async () => ({ details: { ok: true } }));
vi.mock("../agents/tools/whatsapp-actions.js", () => ({
handleWhatsAppAction: (...args: unknown[]) => handleWhatsAppAction(...args),
}));
const originalTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
2025-12-15 10:11:18 -06:00
const originalDiscordToken = process.env.DISCORD_BOT_TOKEN;
beforeEach(() => {
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 = {};
2026-01-09 08:27:17 +01:00
callGatewayMock.mockReset();
webAuthExists.mockReset().mockResolvedValue(false);
handleDiscordAction.mockReset();
handleSlackAction.mockReset();
handleTelegramAction.mockReset();
handleWhatsAppAction.mockReset();
});
afterAll(() => {
process.env.TELEGRAM_BOT_TOKEN = originalTelegramToken;
2025-12-15 10:11:18 -06:00
process.env.DISCORD_BOT_TOKEN = originalDiscordToken;
});
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,
});
2026-01-09 08:27:17 +01:00
describe("messageCommand", () => {
it("defaults provider when only one configured", async () => {
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
2025-12-05 19:03:59 +00:00
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await messageCommand(
{
2026-01-09 08:27:17 +01:00
to: "123",
message: "hi",
},
deps,
runtime,
);
2026-01-09 08:27:17 +01:00
expect(handleTelegramAction).toHaveBeenCalled();
});
2026-01-09 08:27:17 +01:00
it("requires provider when multiple configured", async () => {
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
process.env.DISCORD_BOT_TOKEN = "token-discord";
2025-12-05 19:03:59 +00:00
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await expect(
messageCommand(
{
to: "123",
message: "hi",
},
deps,
runtime,
),
).rejects.toThrow(/Provider is required/);
});
2026-01-09 08:27:17 +01:00
it("sends via gateway for WhatsApp", async () => {
callGatewayMock.mockResolvedValueOnce({ messageId: "g1" });
const deps = makeDeps();
2026-01-09 08:27:17 +01:00
await messageCommand(
{
2026-01-09 08:27:17 +01:00
action: "send",
provider: "whatsapp",
to: "+1",
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 () => {
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",
provider: "discord",
to: "channel:123",
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:123",
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
});