mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-29 18:12:52 +00:00
refactor: trim outbound target test imports
This commit is contained in:
@@ -1,145 +1,21 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
isWhatsAppGroupJid,
|
||||
normalizeWhatsAppTarget,
|
||||
} from "../../channels/plugins/normalize/whatsapp.js";
|
||||
import type { ChannelOutboundAdapter } from "../../channels/plugins/types.js";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { setActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
|
||||
import { resolveOutboundTarget } from "./targets.js";
|
||||
|
||||
const createOutboundStub = (channel: "telegram" | "whatsapp"): ChannelOutboundAdapter => ({
|
||||
deliveryMode: channel === "whatsapp" ? "gateway" : "direct",
|
||||
resolveTarget:
|
||||
channel === "whatsapp"
|
||||
? ({ to }) => {
|
||||
const normalized = to ? normalizeWhatsAppTarget(to) : null;
|
||||
return normalized
|
||||
? { ok: true, to: normalized }
|
||||
: { ok: false, error: new Error("WhatsApp target required") };
|
||||
}
|
||||
: ({ to }) =>
|
||||
typeof to === "string" && to.trim()
|
||||
? { ok: true, to: to.trim() }
|
||||
: { ok: false, error: new Error("Telegram target required") },
|
||||
sendText: async () => ({ channel, messageId: `${channel}-msg` }),
|
||||
});
|
||||
|
||||
const telegramOutbound = createOutboundStub("telegram");
|
||||
const whatsappOutbound = createOutboundStub("whatsapp");
|
||||
|
||||
function parseTelegramTargetForTest(raw: string): {
|
||||
chatId: string;
|
||||
messageThreadId?: number;
|
||||
chatType: "direct" | "group" | "unknown";
|
||||
} {
|
||||
const trimmed = raw
|
||||
.trim()
|
||||
.replace(/^telegram:/i, "")
|
||||
.replace(/^tg:/i, "");
|
||||
const prefixedTopic = /^group:([^:]+):topic:(\d+)$/i.exec(trimmed);
|
||||
if (prefixedTopic) {
|
||||
return {
|
||||
chatId: prefixedTopic[1],
|
||||
messageThreadId: Number.parseInt(prefixedTopic[2], 10),
|
||||
chatType: "group",
|
||||
};
|
||||
}
|
||||
const topic = /^([^:]+):topic:(\d+)$/i.exec(trimmed);
|
||||
if (topic) {
|
||||
return {
|
||||
chatId: topic[1],
|
||||
messageThreadId: Number.parseInt(topic[2], 10),
|
||||
chatType: topic[1].startsWith("-") ? "group" : "direct",
|
||||
};
|
||||
}
|
||||
const colonPair = /^([^:]+):(\d+)$/i.exec(trimmed);
|
||||
if (colonPair && colonPair[1].startsWith("-")) {
|
||||
return {
|
||||
chatId: colonPair[1],
|
||||
messageThreadId: Number.parseInt(colonPair[2], 10),
|
||||
chatType: "group",
|
||||
};
|
||||
}
|
||||
return {
|
||||
chatId: trimmed,
|
||||
chatType: trimmed.startsWith("-") ? "group" : "unknown",
|
||||
};
|
||||
}
|
||||
|
||||
const telegramMessaging = {
|
||||
parseExplicitTarget: ({ raw }: { raw: string }) => {
|
||||
const target = parseTelegramTargetForTest(raw);
|
||||
return {
|
||||
to: target.chatId,
|
||||
threadId: target.messageThreadId,
|
||||
chatType: target.chatType === "unknown" ? undefined : target.chatType,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const whatsappMessaging = {
|
||||
inferTargetChatType: ({ to }: { to: string }) => {
|
||||
const normalized = normalizeWhatsAppTarget(to);
|
||||
if (!normalized) {
|
||||
return undefined;
|
||||
}
|
||||
return isWhatsAppGroupJid(normalized) ? ("group" as const) : ("direct" as const);
|
||||
},
|
||||
targetResolver: {
|
||||
hint: "<E.164|group JID>",
|
||||
},
|
||||
};
|
||||
import {
|
||||
createTargetsTestRegistry,
|
||||
createTelegramTestPlugin,
|
||||
createWhatsAppTestPlugin,
|
||||
} from "./targets.test-helpers.js";
|
||||
|
||||
export function installResolveOutboundTargetPluginRegistryHooks(): void {
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "whatsapp",
|
||||
plugin: {
|
||||
...createOutboundTestPlugin({
|
||||
id: "whatsapp",
|
||||
label: "WhatsApp",
|
||||
outbound: whatsappOutbound,
|
||||
messaging: whatsappMessaging,
|
||||
}),
|
||||
config: {
|
||||
listAccountIds: () => [],
|
||||
resolveDefaultTo: ({ cfg }: { cfg: OpenClawConfig }) =>
|
||||
typeof cfg.channels?.whatsapp?.defaultTo === "string"
|
||||
? cfg.channels.whatsapp.defaultTo
|
||||
: undefined,
|
||||
},
|
||||
},
|
||||
source: "test",
|
||||
},
|
||||
{
|
||||
pluginId: "telegram",
|
||||
plugin: {
|
||||
...createOutboundTestPlugin({
|
||||
id: "telegram",
|
||||
label: "Telegram",
|
||||
outbound: telegramOutbound,
|
||||
messaging: telegramMessaging,
|
||||
}),
|
||||
config: {
|
||||
listAccountIds: () => [],
|
||||
resolveDefaultTo: ({ cfg }: { cfg: OpenClawConfig }) =>
|
||||
typeof cfg.channels?.telegram?.defaultTo === "string"
|
||||
? cfg.channels.telegram.defaultTo
|
||||
: undefined,
|
||||
},
|
||||
},
|
||||
source: "test",
|
||||
},
|
||||
]),
|
||||
createTargetsTestRegistry([createWhatsAppTestPlugin(), createTelegramTestPlugin()]),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry());
|
||||
setActivePluginRegistry(createTargetsTestRegistry([]));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
isWhatsAppGroupJid,
|
||||
normalizeWhatsAppTarget,
|
||||
} from "../../channels/plugins/normalize/whatsapp.js";
|
||||
import type { ChannelOutboundAdapter } from "../../channels/plugins/types.js";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import type { SessionEntry } from "../../config/sessions/types.js";
|
||||
import { setActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
|
||||
import {
|
||||
resolveHeartbeatDeliveryTarget,
|
||||
resolveOutboundTarget,
|
||||
@@ -18,81 +12,23 @@ import {
|
||||
installResolveOutboundTargetPluginRegistryHooks,
|
||||
runResolveOutboundTargetCoreTests,
|
||||
} from "./targets.shared-test.js";
|
||||
import { telegramMessagingForTest } from "./targets.test-helpers.js";
|
||||
|
||||
const createOutboundStub = (channel: "telegram" | "whatsapp"): ChannelOutboundAdapter => ({
|
||||
deliveryMode: channel === "whatsapp" ? "gateway" : "direct",
|
||||
resolveTarget:
|
||||
channel === "whatsapp"
|
||||
? ({ to }) => {
|
||||
const normalized = to ? normalizeWhatsAppTarget(to) : null;
|
||||
return normalized
|
||||
? { ok: true, to: normalized }
|
||||
: { ok: false, error: new Error("WhatsApp target required") };
|
||||
}
|
||||
: ({ to }) =>
|
||||
typeof to === "string" && to.trim()
|
||||
? { ok: true, to: to.trim() }
|
||||
: { ok: false, error: new Error("Telegram target required") },
|
||||
sendText: async () => ({ channel, messageId: `${channel}-msg` }),
|
||||
});
|
||||
|
||||
const telegramOutbound = createOutboundStub("telegram");
|
||||
const whatsappOutbound = createOutboundStub("whatsapp");
|
||||
import {
|
||||
createNoopOutboundChannelPlugin,
|
||||
createTargetsTestRegistry,
|
||||
createTelegramTestPlugin,
|
||||
createWhatsAppTestPlugin,
|
||||
} from "./targets.test-helpers.js";
|
||||
|
||||
runResolveOutboundTargetCoreTests();
|
||||
|
||||
const whatsappMessaging = {
|
||||
inferTargetChatType: ({ to }: { to: string }) => {
|
||||
const normalized = normalizeWhatsAppTarget(to);
|
||||
if (!normalized) {
|
||||
return undefined;
|
||||
}
|
||||
return isWhatsAppGroupJid(normalized) ? ("group" as const) : ("direct" as const);
|
||||
},
|
||||
};
|
||||
|
||||
const noopOutbound = (channel: "discord" | "imessage" | "slack"): ChannelOutboundAdapter => ({
|
||||
deliveryMode: "direct",
|
||||
sendText: async () => ({ channel, messageId: `${channel}-msg` }),
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "discord",
|
||||
plugin: createOutboundTestPlugin({ id: "discord", outbound: noopOutbound("discord") }),
|
||||
source: "test",
|
||||
},
|
||||
{
|
||||
pluginId: "imessage",
|
||||
plugin: createOutboundTestPlugin({ id: "imessage", outbound: noopOutbound("imessage") }),
|
||||
source: "test",
|
||||
},
|
||||
{
|
||||
pluginId: "slack",
|
||||
plugin: createOutboundTestPlugin({ id: "slack", outbound: noopOutbound("slack") }),
|
||||
source: "test",
|
||||
},
|
||||
{
|
||||
pluginId: "telegram",
|
||||
plugin: createOutboundTestPlugin({
|
||||
id: "telegram",
|
||||
outbound: telegramOutbound,
|
||||
messaging: telegramMessagingForTest,
|
||||
}),
|
||||
source: "test",
|
||||
},
|
||||
{
|
||||
pluginId: "whatsapp",
|
||||
plugin: createOutboundTestPlugin({
|
||||
id: "whatsapp",
|
||||
outbound: whatsappOutbound,
|
||||
messaging: whatsappMessaging,
|
||||
}),
|
||||
source: "test",
|
||||
},
|
||||
createTargetsTestRegistry([
|
||||
createNoopOutboundChannelPlugin("discord"),
|
||||
createNoopOutboundChannelPlugin("imessage"),
|
||||
createNoopOutboundChannelPlugin("slack"),
|
||||
createTelegramTestPlugin(),
|
||||
createWhatsAppTestPlugin(),
|
||||
]),
|
||||
);
|
||||
});
|
||||
@@ -150,7 +86,7 @@ describe("resolveOutboundTarget defaultTo config fallback", () => {
|
||||
});
|
||||
|
||||
it("falls back to the active registry when the cached channel map is stale", () => {
|
||||
const registry = createTestRegistry([]);
|
||||
const registry = createTargetsTestRegistry([]);
|
||||
setActivePluginRegistry(registry, "stale-registry-test");
|
||||
|
||||
// Warm the cached channel map before mutating the registry in place.
|
||||
@@ -160,11 +96,7 @@ describe("resolveOutboundTarget defaultTo config fallback", () => {
|
||||
|
||||
registry.channels.push({
|
||||
pluginId: "telegram",
|
||||
plugin: createOutboundTestPlugin({
|
||||
id: "telegram",
|
||||
outbound: telegramOutbound,
|
||||
messaging: telegramMessagingForTest,
|
||||
}),
|
||||
plugin: createTelegramTestPlugin(),
|
||||
source: "test",
|
||||
});
|
||||
|
||||
@@ -395,7 +327,7 @@ describe("resolveSessionDeliveryTarget", () => {
|
||||
});
|
||||
|
||||
it("keeps raw :topic: targets when the telegram plugin registry is unavailable", () => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
setActivePluginRegistry(createTargetsTestRegistry([]));
|
||||
|
||||
const resolved = resolveSessionDeliveryTarget({
|
||||
entry: {
|
||||
|
||||
Reference in New Issue
Block a user