refactor: centralize WhatsApp target normalization

This commit is contained in:
Peter Steinberger
2026-01-10 02:39:50 +01:00
parent 8f8caa8d89
commit 9cd2662a86
9 changed files with 191 additions and 130 deletions

View File

@@ -3,39 +3,64 @@ import { describe, expect, it } from "vitest";
import { resolveOutboundTarget } from "./targets.js";
describe("resolveOutboundTarget", () => {
it("falls back to whatsapp allowFrom", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
to: "",
allowFrom: ["+1555"],
});
expect(res).toEqual({ ok: true, to: "+1555" });
});
it("normalizes whatsapp allowFrom fallback targets", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
to: "",
allowFrom: ["whatsapp:(555) 123-4567"],
});
expect(res).toEqual({ ok: true, to: "+5551234567" });
});
it("normalizes whatsapp target when provided", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
to: " (555) 123-4567 ",
});
if (!res.ok) throw res.error;
expect(res.to).toBe("+5551234567");
});
it("keeps whatsapp group targets", () => {
const res = resolveOutboundTarget({
provider: "whatsapp",
to: "120363401234567890@g.us",
});
expect(res).toEqual({ ok: true, to: "120363401234567890@g.us" });
it.each([
{
name: "normalizes whatsapp target when provided",
input: { provider: "whatsapp" as const, to: " (555) 123-4567 " },
expected: { ok: true as const, to: "+5551234567" },
},
{
name: "keeps whatsapp group targets",
input: { provider: "whatsapp" as const, to: "120363401234567890@g.us" },
expected: { ok: true as const, to: "120363401234567890@g.us" },
},
{
name: "normalizes prefixed/uppercase whatsapp group targets",
input: {
provider: "whatsapp" as const,
to: " WhatsApp:Group:120363401234567890@G.US ",
},
expected: { ok: true as const, to: "120363401234567890@g.us" },
},
{
name: "falls back to whatsapp allowFrom",
input: { provider: "whatsapp" as const, to: "", allowFrom: ["+1555"] },
expected: { ok: true as const, to: "+1555" },
},
{
name: "normalizes whatsapp allowFrom fallback targets",
input: {
provider: "whatsapp" as const,
to: "",
allowFrom: ["whatsapp:(555) 123-4567"],
},
expected: { ok: true as const, to: "+5551234567" },
},
{
name: "rejects invalid whatsapp target",
input: { provider: "whatsapp" as const, to: "wat" },
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects whatsapp without to when allowFrom missing",
input: { provider: "whatsapp" as const, to: " " },
expectedErrorIncludes: "WhatsApp",
},
{
name: "rejects whatsapp allowFrom fallback when invalid",
input: { provider: "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 telegram with missing target", () => {