mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:11:23 +00:00
test: dedupe and optimize test suites
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { slackPlugin } from "../../../extensions/slack/src/channel.js";
|
||||
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
|
||||
import { whatsappPlugin } from "../../../extensions/whatsapp/src/channel.js";
|
||||
@@ -86,16 +86,32 @@ function createAlwaysConfiguredPluginConfig(account: Record<string, unknown> = {
|
||||
};
|
||||
}
|
||||
|
||||
describe("runMessageAction context isolation", () => {
|
||||
beforeEach(async () => {
|
||||
const { createPluginRuntime } = await import("../../plugins/runtime/index.js");
|
||||
const { setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js");
|
||||
const { setTelegramRuntime } = await import("../../../extensions/telegram/src/runtime.js");
|
||||
const { setWhatsAppRuntime } = await import("../../../extensions/whatsapp/src/runtime.js");
|
||||
const runtime = createPluginRuntime();
|
||||
setSlackRuntime(runtime);
|
||||
let createPluginRuntime: typeof import("../../plugins/runtime/index.js").createPluginRuntime;
|
||||
let setSlackRuntime: typeof import("../../../extensions/slack/src/runtime.js").setSlackRuntime;
|
||||
let setTelegramRuntime: typeof import("../../../extensions/telegram/src/runtime.js").setTelegramRuntime;
|
||||
let setWhatsAppRuntime: typeof import("../../../extensions/whatsapp/src/runtime.js").setWhatsAppRuntime;
|
||||
|
||||
function installChannelRuntimes(params?: { includeTelegram?: boolean; includeWhatsApp?: boolean }) {
|
||||
const runtime = createPluginRuntime();
|
||||
setSlackRuntime(runtime);
|
||||
if (params?.includeTelegram !== false) {
|
||||
setTelegramRuntime(runtime);
|
||||
}
|
||||
if (params?.includeWhatsApp !== false) {
|
||||
setWhatsAppRuntime(runtime);
|
||||
}
|
||||
}
|
||||
|
||||
describe("runMessageAction context isolation", () => {
|
||||
beforeAll(async () => {
|
||||
({ createPluginRuntime } = await import("../../plugins/runtime/index.js"));
|
||||
({ setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js"));
|
||||
({ setTelegramRuntime } = await import("../../../extensions/telegram/src/runtime.js"));
|
||||
({ setWhatsAppRuntime } = await import("../../../extensions/whatsapp/src/runtime.js"));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
installChannelRuntimes();
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
@@ -222,59 +238,59 @@ describe("runMessageAction context isolation", () => {
|
||||
expect(result.kind).toBe("action");
|
||||
});
|
||||
|
||||
it("allows WhatsApp send when target matches current chat", async () => {
|
||||
it.each([
|
||||
{
|
||||
name: "whatsapp",
|
||||
channel: "whatsapp",
|
||||
target: "123@g.us",
|
||||
currentChannelId: "123@g.us",
|
||||
},
|
||||
{
|
||||
name: "imessage",
|
||||
channel: "imessage",
|
||||
target: "imessage:+15551234567",
|
||||
currentChannelId: "imessage:+15551234567",
|
||||
},
|
||||
] as const)("allows $name send when target matches current context", async (testCase) => {
|
||||
const result = await runDrySend({
|
||||
cfg: whatsappConfig,
|
||||
actionParams: {
|
||||
channel: "whatsapp",
|
||||
target: "123@g.us",
|
||||
channel: testCase.channel,
|
||||
target: testCase.target,
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: { currentChannelId: "123@g.us" },
|
||||
toolContext: { currentChannelId: testCase.currentChannelId },
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("send");
|
||||
});
|
||||
|
||||
it("blocks WhatsApp send when target differs from current chat", async () => {
|
||||
it.each([
|
||||
{
|
||||
name: "whatsapp",
|
||||
channel: "whatsapp",
|
||||
target: "456@g.us",
|
||||
currentChannelId: "123@g.us",
|
||||
currentChannelProvider: "whatsapp",
|
||||
},
|
||||
{
|
||||
name: "imessage",
|
||||
channel: "imessage",
|
||||
target: "imessage:+15551230000",
|
||||
currentChannelId: "imessage:+15551234567",
|
||||
currentChannelProvider: "imessage",
|
||||
},
|
||||
] as const)("blocks $name send when target differs from current context", async (testCase) => {
|
||||
const result = await runDrySend({
|
||||
cfg: whatsappConfig,
|
||||
actionParams: {
|
||||
channel: "whatsapp",
|
||||
target: "456@g.us",
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: { currentChannelId: "123@g.us", currentChannelProvider: "whatsapp" },
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("send");
|
||||
});
|
||||
|
||||
it("allows iMessage send when target matches current handle", async () => {
|
||||
const result = await runDrySend({
|
||||
cfg: whatsappConfig,
|
||||
actionParams: {
|
||||
channel: "imessage",
|
||||
target: "imessage:+15551234567",
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: { currentChannelId: "imessage:+15551234567" },
|
||||
});
|
||||
|
||||
expect(result.kind).toBe("send");
|
||||
});
|
||||
|
||||
it("blocks iMessage send when target differs from current handle", async () => {
|
||||
const result = await runDrySend({
|
||||
cfg: whatsappConfig,
|
||||
actionParams: {
|
||||
channel: "imessage",
|
||||
target: "imessage:+15551230000",
|
||||
channel: testCase.channel,
|
||||
target: testCase.target,
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: {
|
||||
currentChannelId: "imessage:+15551234567",
|
||||
currentChannelProvider: "imessage",
|
||||
currentChannelId: testCase.currentChannelId,
|
||||
currentChannelProvider: testCase.currentChannelProvider,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -498,11 +514,8 @@ describe("runMessageAction sendAttachment hydration", () => {
|
||||
});
|
||||
|
||||
describe("runMessageAction sandboxed media validation", () => {
|
||||
beforeEach(async () => {
|
||||
const { createPluginRuntime } = await import("../../plugins/runtime/index.js");
|
||||
const { setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js");
|
||||
const runtime = createPluginRuntime();
|
||||
setSlackRuntime(runtime);
|
||||
beforeEach(() => {
|
||||
installChannelRuntimes({ includeTelegram: false, includeWhatsApp: false });
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
@@ -518,38 +531,38 @@ describe("runMessageAction sandboxed media validation", () => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
it("rejects media outside the sandbox root", async () => {
|
||||
await withSandbox(async (sandboxDir) => {
|
||||
await expect(
|
||||
runDrySend({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "#C12345678",
|
||||
media: "/etc/passwd",
|
||||
message: "",
|
||||
},
|
||||
sandboxRoot: sandboxDir,
|
||||
}),
|
||||
).rejects.toThrow(/sandbox/i);
|
||||
});
|
||||
});
|
||||
it.each(["/etc/passwd", "file:///etc/passwd"])(
|
||||
"rejects out-of-sandbox media reference: %s",
|
||||
async (media) => {
|
||||
await withSandbox(async (sandboxDir) => {
|
||||
await expect(
|
||||
runDrySend({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "#C12345678",
|
||||
media,
|
||||
message: "",
|
||||
},
|
||||
sandboxRoot: sandboxDir,
|
||||
}),
|
||||
).rejects.toThrow(/sandbox/i);
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("rejects file:// media outside the sandbox root", async () => {
|
||||
await withSandbox(async (sandboxDir) => {
|
||||
await expect(
|
||||
runDrySend({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "#C12345678",
|
||||
media: "file:///etc/passwd",
|
||||
message: "",
|
||||
},
|
||||
sandboxRoot: sandboxDir,
|
||||
}),
|
||||
).rejects.toThrow(/sandbox/i);
|
||||
});
|
||||
it("rejects data URLs in media params", async () => {
|
||||
await expect(
|
||||
runDrySend({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "#C12345678",
|
||||
media: "data:image/png;base64,abcd",
|
||||
message: "",
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow(/data:/i);
|
||||
});
|
||||
|
||||
it("rewrites sandbox-relative media paths", async () => {
|
||||
@@ -592,20 +605,6 @@ describe("runMessageAction sandboxed media validation", () => {
|
||||
expect(result.sendResult?.mediaUrl).toBe(path.join(sandboxDir, "data", "note.ogg"));
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects data URLs in media params", async () => {
|
||||
await expect(
|
||||
runDrySend({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "#C12345678",
|
||||
media: "data:image/png;base64,abcd",
|
||||
message: "",
|
||||
},
|
||||
}),
|
||||
).rejects.toThrow(/data:/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("runMessageAction media caption behavior", () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { slackPlugin } from "../../../extensions/slack/src/channel.js";
|
||||
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
@@ -80,11 +80,18 @@ const defaultTelegramToolContext = {
|
||||
currentThreadTs: "42",
|
||||
} as const;
|
||||
|
||||
let createPluginRuntime: typeof import("../../plugins/runtime/index.js").createPluginRuntime;
|
||||
let setSlackRuntime: typeof import("../../../extensions/slack/src/runtime.js").setSlackRuntime;
|
||||
let setTelegramRuntime: typeof import("../../../extensions/telegram/src/runtime.js").setTelegramRuntime;
|
||||
|
||||
describe("runMessageAction threading auto-injection", () => {
|
||||
beforeEach(async () => {
|
||||
const { createPluginRuntime } = await import("../../plugins/runtime/index.js");
|
||||
const { setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js");
|
||||
const { setTelegramRuntime } = await import("../../../extensions/telegram/src/runtime.js");
|
||||
beforeAll(async () => {
|
||||
({ createPluginRuntime } = await import("../../plugins/runtime/index.js"));
|
||||
({ setSlackRuntime } = await import("../../../extensions/slack/src/runtime.js"));
|
||||
({ setTelegramRuntime } = await import("../../../extensions/telegram/src/runtime.js"));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
const runtime = createPluginRuntime();
|
||||
setSlackRuntime(runtime);
|
||||
setTelegramRuntime(runtime);
|
||||
@@ -110,94 +117,73 @@ describe("runMessageAction threading auto-injection", () => {
|
||||
mocks.recordSessionMetaFromInbound.mockReset();
|
||||
});
|
||||
|
||||
it("uses toolContext thread when auto-threading is active", async () => {
|
||||
it.each([
|
||||
{
|
||||
name: "exact channel id",
|
||||
target: "channel:C123",
|
||||
threadTs: "111.222",
|
||||
expectedSessionKey: "agent:main:slack:channel:c123:thread:111.222",
|
||||
},
|
||||
{
|
||||
name: "case-insensitive channel id",
|
||||
target: "channel:c123",
|
||||
threadTs: "333.444",
|
||||
expectedSessionKey: "agent:main:slack:channel:c123:thread:333.444",
|
||||
},
|
||||
] as const)("auto-threads slack using $name", async (testCase) => {
|
||||
mockHandledSendAction();
|
||||
|
||||
const call = await runThreadingAction({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "channel:C123",
|
||||
target: testCase.target,
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: {
|
||||
currentChannelId: "C123",
|
||||
currentThreadTs: "111.222",
|
||||
currentThreadTs: testCase.threadTs,
|
||||
replyToMode: "all",
|
||||
},
|
||||
});
|
||||
|
||||
expect(call?.ctx?.agentId).toBe("main");
|
||||
expect(call?.ctx?.mirror?.sessionKey).toBe("agent:main:slack:channel:c123:thread:111.222");
|
||||
expect(call?.ctx?.mirror?.sessionKey).toBe(testCase.expectedSessionKey);
|
||||
});
|
||||
|
||||
it("matches auto-threading when channel ids differ in case", async () => {
|
||||
mockHandledSendAction();
|
||||
|
||||
const call = await runThreadingAction({
|
||||
cfg: slackConfig,
|
||||
actionParams: {
|
||||
channel: "slack",
|
||||
target: "channel:c123",
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: {
|
||||
currentChannelId: "C123",
|
||||
currentThreadTs: "333.444",
|
||||
replyToMode: "all",
|
||||
},
|
||||
});
|
||||
|
||||
expect(call?.ctx?.mirror?.sessionKey).toBe("agent:main:slack:channel:c123:thread:333.444");
|
||||
});
|
||||
|
||||
it("auto-injects telegram threadId from toolContext when omitted", async () => {
|
||||
it.each([
|
||||
{
|
||||
name: "injects threadId for matching target",
|
||||
target: "telegram:123",
|
||||
expectedThreadId: "42",
|
||||
},
|
||||
{
|
||||
name: "injects threadId for prefixed group target",
|
||||
target: "telegram:group:123",
|
||||
expectedThreadId: "42",
|
||||
},
|
||||
{
|
||||
name: "skips threadId when target chat differs",
|
||||
target: "telegram:999",
|
||||
expectedThreadId: undefined,
|
||||
},
|
||||
] as const)("telegram auto-threading: $name", async (testCase) => {
|
||||
mockHandledSendAction();
|
||||
|
||||
const call = await runThreadingAction({
|
||||
cfg: telegramConfig,
|
||||
actionParams: {
|
||||
channel: "telegram",
|
||||
target: "telegram:123",
|
||||
target: testCase.target,
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: defaultTelegramToolContext,
|
||||
});
|
||||
|
||||
expect(call?.threadId).toBe("42");
|
||||
expect(call?.ctx?.params?.threadId).toBe("42");
|
||||
});
|
||||
|
||||
it("skips telegram auto-threading when target chat differs", async () => {
|
||||
mockHandledSendAction();
|
||||
|
||||
const call = await runThreadingAction({
|
||||
cfg: telegramConfig,
|
||||
actionParams: {
|
||||
channel: "telegram",
|
||||
target: "telegram:999",
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: defaultTelegramToolContext,
|
||||
});
|
||||
|
||||
expect(call?.ctx?.params?.threadId).toBeUndefined();
|
||||
});
|
||||
|
||||
it("matches telegram target with internal prefix variations", async () => {
|
||||
mockHandledSendAction();
|
||||
|
||||
const call = await runThreadingAction({
|
||||
cfg: telegramConfig,
|
||||
actionParams: {
|
||||
channel: "telegram",
|
||||
target: "telegram:group:123",
|
||||
message: "hi",
|
||||
},
|
||||
toolContext: defaultTelegramToolContext,
|
||||
});
|
||||
|
||||
expect(call?.ctx?.params?.threadId).toBe("42");
|
||||
expect(call?.ctx?.params?.threadId).toBe(testCase.expectedThreadId);
|
||||
if (testCase.expectedThreadId !== undefined) {
|
||||
expect(call?.threadId).toBe(testCase.expectedThreadId);
|
||||
}
|
||||
});
|
||||
|
||||
it("uses explicit telegram threadId when provided", async () => {
|
||||
|
||||
Reference in New Issue
Block a user