test(outbound): share resolveOutboundTarget test suite

This commit is contained in:
Peter Steinberger
2026-02-21 22:38:38 +00:00
parent b6ce5e06cd
commit fd8b7b5c4a
3 changed files with 170 additions and 250 deletions

View File

@@ -2,8 +2,6 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { telegramPlugin } from "../../../extensions/telegram/src/channel.js";
import { whatsappPlugin } from "../../../extensions/whatsapp/src/channel.js";
import type { ReplyPayload } from "../../auto-reply/types.js";
import type { OpenClawConfig } from "../../config/config.js";
import { setActivePluginRegistry } from "../../plugins/runtime.js";
@@ -39,7 +37,7 @@ import {
normalizeOutboundPayloads,
normalizeOutboundPayloadsForJson,
} from "./payloads.js";
import { resolveOutboundTarget } from "./targets.js";
import { runResolveOutboundTargetCoreTests } from "./targets.shared-test.js";
describe("delivery-queue", () => {
let tmpDir: string;
@@ -914,102 +912,4 @@ describe("formatOutboundPayloadLog", () => {
});
});
describe("resolveOutboundTarget", () => {
beforeEach(() => {
setActivePluginRegistry(
createTestRegistry([
{ pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" },
{ pluginId: "telegram", plugin: telegramPlugin, source: "test" },
]),
);
});
afterEach(() => {
setActivePluginRegistry(createTestRegistry());
});
it.each([
{
name: "normalizes whatsapp target when provided",
input: { channel: "whatsapp" as const, to: " (555) 123-4567 " },
expected: { ok: true as const, to: "+5551234567" },
},
{
name: "keeps whatsapp group targets",
input: { channel: "whatsapp" as const, to: "120363401234567890@g.us" },
expected: { ok: true as const, to: "120363401234567890@g.us" },
},
{
name: "normalizes prefixed/uppercase whatsapp group targets",
input: {
channel: "whatsapp" as const,
to: " WhatsApp:120363401234567890@G.US ",
},
expected: { ok: true as const, to: "120363401234567890@g.us" },
},
{
name: "rejects whatsapp with empty target in explicit mode even with cfg allowFrom",
input: {
channel: "whatsapp" as const,
to: "",
cfg: { channels: { whatsapp: { allowFrom: ["+1555"] } } } as OpenClawConfig,
mode: "explicit" as const,
},
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects whatsapp with empty target and allowFrom (no silent fallback)",
input: { channel: "whatsapp" as const, to: "", allowFrom: ["+1555"] },
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects whatsapp with empty target and prefixed allowFrom (no silent fallback)",
input: {
channel: "whatsapp" as const,
to: "",
allowFrom: ["whatsapp:(555) 123-4567"],
},
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects invalid whatsapp target",
input: { channel: "whatsapp" as const, to: "wat" },
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects whatsapp without to when allowFrom missing",
input: { channel: "whatsapp" as const, to: " " },
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects whatsapp allowFrom fallback when invalid",
input: { channel: "whatsapp" as const, to: "", allowFrom: ["wat"] },
expectedErrorIncludes: "WhatsApp",
},
])("$name", ({ input, expected, expectedErrorIncludes }) => {
const res = resolveOutboundTarget(input);
if (expected) {
expect(res).toEqual(expected);
return;
}
expect(res.ok).toBe(false);
if (!res.ok) {
expect(res.error.message).toContain(expectedErrorIncludes);
}
});
it("rejects invalid non-whatsapp targets", () => {
const cases = [
{ input: { channel: "telegram" as const, to: " " }, expectedErrorIncludes: "Telegram" },
{ input: { channel: "webchat" as const, to: "x" }, expectedErrorIncludes: "WebChat" },
] as const;
for (const testCase of cases) {
const res = resolveOutboundTarget(testCase.input);
expect(res.ok).toBe(false);
if (!res.ok) {
expect(res.error.message).toContain(testCase.expectedErrorIncludes);
}
}
});
});
runResolveOutboundTargetCoreTests();