mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 16:14:58 +00:00
refactor: migrate messaging plugins to sdk
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { afterAll, 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 { messageCommand } from "./message.js";
|
||||
import { createTestRegistry } from "../test-utils/channel-plugins.js";
|
||||
const loadMessageCommand = async () => await import("./message.js");
|
||||
|
||||
let testConfig: Record<string, unknown> = {};
|
||||
vi.mock("../config/config.js", async (importOriginal) => {
|
||||
@@ -47,10 +53,17 @@ vi.mock("../agents/tools/whatsapp-actions.js", () => ({
|
||||
const originalTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
const originalDiscordToken = process.env.DISCORD_BOT_TOKEN;
|
||||
|
||||
beforeEach(() => {
|
||||
const setRegistry = async (registry: ReturnType<typeof createTestRegistry>) => {
|
||||
const { setActivePluginRegistry } = await import("../plugins/runtime.js");
|
||||
setActivePluginRegistry(registry);
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
||||
process.env.DISCORD_BOT_TOKEN = "";
|
||||
testConfig = {};
|
||||
vi.resetModules();
|
||||
await setRegistry(createTestRegistry([]));
|
||||
callGatewayMock.mockReset();
|
||||
webAuthExists.mockReset().mockResolvedValue(false);
|
||||
handleDiscordAction.mockReset();
|
||||
@@ -82,10 +95,55 @@ const makeDeps = (overrides: Partial<CliDeps> = {}): CliDeps => ({
|
||||
...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,
|
||||
});
|
||||
|
||||
describe("messageCommand", () => {
|
||||
it("defaults channel when only one configured", async () => {
|
||||
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
|
||||
await setRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "telegram",
|
||||
source: "test",
|
||||
plugin: createStubPlugin({
|
||||
id: "telegram",
|
||||
label: "Telegram",
|
||||
actions: {
|
||||
listActions: () => ["send"],
|
||||
handleAction: async ({ action, params, cfg, accountId }) =>
|
||||
await handleTelegramAction(
|
||||
{ action, to: params.to, accountId: accountId ?? undefined },
|
||||
cfg,
|
||||
),
|
||||
},
|
||||
}),
|
||||
},
|
||||
]),
|
||||
);
|
||||
const deps = makeDeps();
|
||||
const { messageCommand } = await loadMessageCommand();
|
||||
await messageCommand(
|
||||
{
|
||||
target: "123456",
|
||||
@@ -100,7 +158,44 @@ describe("messageCommand", () => {
|
||||
it("requires channel when multiple configured", async () => {
|
||||
process.env.TELEGRAM_BOT_TOKEN = "token-abc";
|
||||
process.env.DISCORD_BOT_TOKEN = "token-discord";
|
||||
await setRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "telegram",
|
||||
source: "test",
|
||||
plugin: createStubPlugin({
|
||||
id: "telegram",
|
||||
label: "Telegram",
|
||||
actions: {
|
||||
listActions: () => ["send"],
|
||||
handleAction: async ({ action, params, cfg, accountId }) =>
|
||||
await handleTelegramAction(
|
||||
{ action, to: params.to, accountId: accountId ?? undefined },
|
||||
cfg,
|
||||
),
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
pluginId: "discord",
|
||||
source: "test",
|
||||
plugin: createStubPlugin({
|
||||
id: "discord",
|
||||
label: "Discord",
|
||||
actions: {
|
||||
listActions: () => ["poll"],
|
||||
handleAction: async ({ action, params, cfg, accountId }) =>
|
||||
await handleDiscordAction(
|
||||
{ action, to: params.to, accountId: accountId ?? undefined },
|
||||
cfg,
|
||||
),
|
||||
},
|
||||
}),
|
||||
},
|
||||
]),
|
||||
);
|
||||
const deps = makeDeps();
|
||||
const { messageCommand } = await loadMessageCommand();
|
||||
await expect(
|
||||
messageCommand(
|
||||
{
|
||||
@@ -115,7 +210,23 @@ describe("messageCommand", () => {
|
||||
|
||||
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();
|
||||
const { messageCommand } = await loadMessageCommand();
|
||||
await messageCommand(
|
||||
{
|
||||
action: "send",
|
||||
@@ -130,7 +241,28 @@ describe("messageCommand", () => {
|
||||
});
|
||||
|
||||
it("routes discord polls through message action", async () => {
|
||||
await setRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "discord",
|
||||
source: "test",
|
||||
plugin: createStubPlugin({
|
||||
id: "discord",
|
||||
label: "Discord",
|
||||
actions: {
|
||||
listActions: () => ["poll"],
|
||||
handleAction: async ({ action, params, cfg, accountId }) =>
|
||||
await handleDiscordAction(
|
||||
{ action, to: params.to, accountId: accountId ?? undefined },
|
||||
cfg,
|
||||
),
|
||||
},
|
||||
}),
|
||||
},
|
||||
]),
|
||||
);
|
||||
const deps = makeDeps();
|
||||
const { messageCommand } = await loadMessageCommand();
|
||||
await messageCommand(
|
||||
{
|
||||
action: "poll",
|
||||
|
||||
Reference in New Issue
Block a user