248 lines
6.4 KiB
TypeScript
248 lines
6.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
|
|
import type { ClawdbotConfig } from "../../config/config.js";
|
|
import { setActivePluginRegistry } from "../../plugins/runtime.js";
|
|
import { createIMessageTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
|
|
import { slackPlugin } from "../../../extensions/slack/src/channel.js";
|
|
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
|
|
import { whatsappPlugin } from "../../../extensions/whatsapp/src/channel.js";
|
|
import { runMessageAction } from "./message-action-runner.js";
|
|
|
|
const slackConfig = {
|
|
channels: {
|
|
slack: {
|
|
botToken: "xoxb-test",
|
|
appToken: "xapp-test",
|
|
},
|
|
},
|
|
} as ClawdbotConfig;
|
|
|
|
const whatsappConfig = {
|
|
channels: {
|
|
whatsapp: {
|
|
allowFrom: ["*"],
|
|
},
|
|
},
|
|
} as ClawdbotConfig;
|
|
|
|
describe("runMessageAction context isolation", () => {
|
|
beforeEach(() => {
|
|
setActivePluginRegistry(
|
|
createTestRegistry([
|
|
{
|
|
pluginId: "slack",
|
|
source: "test",
|
|
plugin: slackPlugin,
|
|
},
|
|
{
|
|
pluginId: "whatsapp",
|
|
source: "test",
|
|
plugin: whatsappPlugin,
|
|
},
|
|
{
|
|
pluginId: "telegram",
|
|
source: "test",
|
|
plugin: telegramPlugin,
|
|
},
|
|
{
|
|
pluginId: "imessage",
|
|
source: "test",
|
|
plugin: createIMessageTestPlugin(),
|
|
},
|
|
]),
|
|
);
|
|
});
|
|
|
|
afterEach(() => {
|
|
setActivePluginRegistry(createTestRegistry([]));
|
|
});
|
|
it("allows send when target matches current channel", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: slackConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "slack",
|
|
target: "#C12345678",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("allows media-only send when target matches current channel", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: slackConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "slack",
|
|
target: "#C12345678",
|
|
media: "https://example.com/note.ogg",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("requires message when no media hint is provided", async () => {
|
|
await expect(
|
|
runMessageAction({
|
|
cfg: slackConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "slack",
|
|
target: "#C12345678",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678" },
|
|
dryRun: true,
|
|
}),
|
|
).rejects.toThrow(/message required/i);
|
|
});
|
|
|
|
it("blocks send when target differs from current channel", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: slackConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "slack",
|
|
target: "channel:C99999999",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("blocks thread-reply when channelId differs from current channel", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: slackConfig,
|
|
action: "thread-reply",
|
|
params: {
|
|
channel: "slack",
|
|
target: "C99999999",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("action");
|
|
});
|
|
|
|
it("allows WhatsApp send when target matches current chat", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: whatsappConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "whatsapp",
|
|
target: "123@g.us",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "123@g.us" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("blocks WhatsApp send when target differs from current chat", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: whatsappConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "whatsapp",
|
|
target: "456@g.us",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "123@g.us", currentChannelProvider: "whatsapp" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("allows iMessage send when target matches current handle", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: whatsappConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "imessage",
|
|
target: "imessage:+15551234567",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "imessage:+15551234567" },
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("blocks iMessage send when target differs from current handle", async () => {
|
|
const result = await runMessageAction({
|
|
cfg: whatsappConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "imessage",
|
|
target: "imessage:+15551230000",
|
|
message: "hi",
|
|
},
|
|
toolContext: {
|
|
currentChannelId: "imessage:+15551234567",
|
|
currentChannelProvider: "imessage",
|
|
},
|
|
dryRun: true,
|
|
});
|
|
|
|
expect(result.kind).toBe("send");
|
|
});
|
|
|
|
it("blocks cross-provider sends by default", async () => {
|
|
await expect(
|
|
runMessageAction({
|
|
cfg: slackConfig,
|
|
action: "send",
|
|
params: {
|
|
channel: "telegram",
|
|
target: "telegram:@ops",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
|
dryRun: true,
|
|
}),
|
|
).rejects.toThrow(/Cross-context messaging denied/);
|
|
});
|
|
|
|
it("blocks same-provider cross-context when disabled", async () => {
|
|
const cfg = {
|
|
...slackConfig,
|
|
tools: {
|
|
message: {
|
|
crossContext: {
|
|
allowWithinProvider: false,
|
|
},
|
|
},
|
|
},
|
|
} as ClawdbotConfig;
|
|
|
|
await expect(
|
|
runMessageAction({
|
|
cfg,
|
|
action: "send",
|
|
params: {
|
|
channel: "slack",
|
|
target: "channel:C99999999",
|
|
message: "hi",
|
|
},
|
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
|
dryRun: true,
|
|
}),
|
|
).rejects.toThrow(/Cross-context messaging denied/);
|
|
});
|
|
});
|