diff --git a/extensions/whatsapp/src/resolve-target.test.ts b/extensions/whatsapp/src/resolve-target.test.ts index f779a8e07b6..b0ed25e4dc9 100644 --- a/extensions/whatsapp/src/resolve-target.test.ts +++ b/extensions/whatsapp/src/resolve-target.test.ts @@ -1,82 +1,60 @@ import { describe, expect, it, vi } from "vitest"; import { installCommonResolveTargetErrorCases } from "../../shared/resolve-target-test-helpers.js"; -vi.mock("openclaw/plugin-sdk/whatsapp", () => ({ - getChatChannelMeta: () => ({ id: "whatsapp", label: "WhatsApp" }), - normalizeWhatsAppTarget: (value: string) => { +vi.mock("openclaw/plugin-sdk/whatsapp", async () => { + const actual = await vi.importActual( + "openclaw/plugin-sdk/whatsapp", + ); + const normalizeWhatsAppTarget = (value: string) => { if (value === "invalid-target") return null; - // Simulate E.164 normalization: strip leading + and whatsapp: prefix + // Simulate E.164 normalization: strip leading + and whatsapp: prefix. const stripped = value.replace(/^whatsapp:/i, "").replace(/^\+/, ""); return stripped.includes("@g.us") ? stripped : `${stripped}@s.whatsapp.net`; - }, - isWhatsAppGroupJid: (value: string) => value.endsWith("@g.us"), - resolveWhatsAppOutboundTarget: ({ - to, - allowFrom, - mode, - }: { - to?: string; - allowFrom: string[]; - mode: "explicit" | "implicit"; - }) => { - const raw = typeof to === "string" ? to.trim() : ""; - if (!raw) { - return { ok: false, error: new Error("missing target") }; - } - const normalizeWhatsAppTarget = (value: string) => { - if (value === "invalid-target") return null; - const stripped = value.replace(/^whatsapp:/i, "").replace(/^\+/, ""); - return stripped.includes("@g.us") ? stripped : `${stripped}@s.whatsapp.net`; - }; - const normalized = normalizeWhatsAppTarget(raw); - if (!normalized) { - return { ok: false, error: new Error("invalid target") }; - } + }; - if (mode === "implicit" && !normalized.endsWith("@g.us")) { - const allowAll = allowFrom.includes("*"); - const allowExact = allowFrom.some((entry) => { - if (!entry) { - return false; - } - const normalizedEntry = normalizeWhatsAppTarget(entry.trim()); - return normalizedEntry?.toLowerCase() === normalized.toLowerCase(); - }); - if (!allowAll && !allowExact) { - return { ok: false, error: new Error("target not allowlisted") }; + return { + ...actual, + getChatChannelMeta: () => ({ id: "whatsapp", label: "WhatsApp" }), + normalizeWhatsAppTarget, + isWhatsAppGroupJid: (value: string) => value.endsWith("@g.us"), + resolveWhatsAppOutboundTarget: ({ + to, + allowFrom, + mode, + }: { + to?: string; + allowFrom: string[]; + mode: "explicit" | "implicit"; + }) => { + const raw = typeof to === "string" ? to.trim() : ""; + if (!raw) { + return { ok: false, error: new Error("missing target") }; + } + const normalized = normalizeWhatsAppTarget(raw); + if (!normalized) { + return { ok: false, error: new Error("invalid target") }; } - } - return { ok: true, to: normalized }; - }, - missingTargetError: (provider: string, hint: string) => - new Error(`Delivering to ${provider} requires target ${hint}`), - WhatsAppConfigSchema: {}, - whatsappOnboardingAdapter: {}, - resolveWhatsAppHeartbeatRecipients: vi.fn(), - buildChannelConfigSchema: vi.fn(), - collectWhatsAppStatusIssues: vi.fn(), - createActionGate: vi.fn(), - DEFAULT_ACCOUNT_ID: "default", - escapeRegExp: vi.fn(), - formatPairingApproveHint: vi.fn(), - listWhatsAppAccountIds: vi.fn(), - listWhatsAppDirectoryGroupsFromConfig: vi.fn(), - listWhatsAppDirectoryPeersFromConfig: vi.fn(), - looksLikeWhatsAppTargetId: vi.fn(), - migrateBaseNameToDefaultAccount: vi.fn(), - normalizeAccountId: vi.fn(), - normalizeE164: vi.fn(), - normalizeWhatsAppMessagingTarget: vi.fn(), - readStringParam: vi.fn(), - resolveDefaultWhatsAppAccountId: vi.fn(), - resolveWhatsAppAccount: vi.fn(), - resolveWhatsAppGroupIntroHint: vi.fn(), - resolveWhatsAppGroupRequireMention: vi.fn(), - resolveWhatsAppGroupToolPolicy: vi.fn(), - resolveWhatsAppMentionStripPatterns: vi.fn(() => []), - applyAccountNameToChannelSection: vi.fn(), -})); + if (mode === "implicit" && !normalized.endsWith("@g.us")) { + const allowAll = allowFrom.includes("*"); + const allowExact = allowFrom.some((entry) => { + if (!entry) { + return false; + } + const normalizedEntry = normalizeWhatsAppTarget(entry.trim()); + return normalizedEntry?.toLowerCase() === normalized.toLowerCase(); + }); + if (!allowAll && !allowExact) { + return { ok: false, error: new Error("target not allowlisted") }; + } + } + + return { ok: true, to: normalized }; + }, + missingTargetError: (provider: string, hint: string) => + new Error(`Delivering to ${provider} requires target ${hint}`), + }; +}); vi.mock("./runtime.js", () => ({ getWhatsAppRuntime: vi.fn(() => ({