Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/integrations/email/campaignSendPlan.test.ts
T
2026-07-08 16:15:37 +02:00

94 lines
4.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { buildTokenMap, hasFooter, injectFooter, replaceTokens } from "./personalization";
import { assignVariants, planCampaignSend, sendAtForIndex, type PlanRecipient, type PlanVariant } from "./campaignSendPlan";
describe("personalization", () => {
it("builds standard + custom-field tokens", () => {
const map = buildTokenMap({ vorname: "Ada", nachname: "Lovelace", email: "ada@x.de", orgName: "ACME", customFields: { sprache: "DE" } });
expect(map).toMatchObject({ firstName: "Ada", lastName: "Lovelace", email: "ada@x.de", company_name: "ACME", "cf.sprache": "DE" });
});
it("replaces known tokens and collapses unknown ones", () => {
expect(replaceTokens("Hi {{firstName}} <{{email}}> {{missing}}", { firstName: "Ada", email: "ada@x.de" })).toBe("Hi Ada <ada@x.de> ");
});
it("injects the footer once (idempotent) and fills the unsubscribe url", () => {
const footer = '<a href="{{unsubscribeUrl}}">Abmelden</a> · Impressum';
const once = injectFooter("<p>Body</p>", footer, "https://t.de/unsubscribe/tok");
expect(hasFooter(once)).toBe(true);
expect(once).toContain("https://t.de/unsubscribe/tok");
const twice = injectFooter(once, footer, "https://t.de/unsubscribe/tok2");
// footer block not duplicated; url refreshed
expect(twice.match(/data-hg-footer/g)?.length).toBe(1);
});
});
describe("assignVariants", () => {
it("returns all null with no variants", () => {
expect(assignVariants(3, [])).toEqual([null, null, null]);
});
it("interleaves 50/50 as a,b,a,b", () => {
const variants: PlanVariant[] = [
{ label: "a", subject: "A", htmlBody: "<p>A</p>", weightPercent: 50 },
{ label: "b", subject: "B", htmlBody: "<p>B</p>", weightPercent: 50 },
];
expect(assignVariants(4, variants).map((v) => v?.label)).toEqual(["a", "b", "a", "b"]);
});
it("respects 60/40 weights over 5 (3:2, interleaved)", () => {
const variants: PlanVariant[] = [
{ label: "a", subject: "A", htmlBody: "<p>A</p>", weightPercent: 60 },
{ label: "b", subject: "B", htmlBody: "<p>B</p>", weightPercent: 40 },
];
const labels = assignVariants(5, variants).map((v) => v?.label);
expect(labels).toEqual(["a", "b", "a", "a", "b"]);
expect(labels.filter((l) => l === "a")).toHaveLength(3);
expect(labels.filter((l) => l === "b")).toHaveLength(2);
});
});
describe("sendAtForIndex", () => {
it("returns startAt with no throttle", () => {
expect(sendAtForIndex(500, 1000, 0)).toBe(1000);
expect(sendAtForIndex(500, 1000)).toBe(1000);
});
it("spreads by minute buckets at the configured rate", () => {
expect(sendAtForIndex(0, 1000, 100)).toBe(1000);
expect(sendAtForIndex(99, 1000, 100)).toBe(1000);
expect(sendAtForIndex(100, 1000, 100)).toBe(1000 + 60_000);
expect(sendAtForIndex(250, 1000, 100)).toBe(1000 + 2 * 60_000);
});
});
describe("planCampaignSend", () => {
const recipients: PlanRecipient[] = [
{ outboundEmailId: "o1", kontaktId: "k1", email: "a@x.de", unsubscribeToken: "t1", tokens: { vorname: "Ada", email: "a@x.de" } },
{ outboundEmailId: "o2", kontaktId: "k2", email: "b@x.de", unsubscribeToken: "t2", tokens: { vorname: "Bo", email: "b@x.de" } },
];
const base = { subject: "Hi {{firstName}}", htmlBody: "<p>Hello {{firstName}}</p>" };
const footer = '<a href="{{unsubscribeUrl}}">Abmelden</a>';
it("personalizes, injects footer, builds list-unsubscribe + sendAt", () => {
const plan = planCampaignSend({ recipients, base, footerHtml: footer, unsubscribeBaseUrl: "https://t.de/", startAt: 5000, ratePerMinute: 1 });
expect(plan).toHaveLength(2);
expect(plan[0].subject).toBe("Hi Ada");
expect(plan[0].html).toContain("Hello Ada");
expect(plan[0].html).toContain("https://t.de/unsubscribe/t1");
expect(plan[0].listUnsubscribe).toBe("<https://t.de/unsubscribe/t1>");
expect(plan[0].variantLabel).toBeNull();
expect(plan[0].sendAt).toBe(5000);
expect(plan[1].sendAt).toBe(5000 + 60_000); // rate 1/min
});
it("assigns A/B variants by weight", () => {
const variants: PlanVariant[] = [
{ label: "a", subject: "A {{firstName}}", htmlBody: "<p>A</p>", weightPercent: 50 },
{ label: "b", subject: "B {{firstName}}", htmlBody: "<p>B</p>", weightPercent: 50 },
];
const plan = planCampaignSend({ recipients, base, variants, footerHtml: footer, unsubscribeBaseUrl: "https://t.de", startAt: 0 });
expect(plan[0].variantLabel).toBe("a");
expect(plan[0].subject).toBe("A Ada");
expect(plan[1].variantLabel).toBe("b");
expect(plan[1].subject).toBe("B Bo");
});
});