import { describe, it, expect } from "vitest"; import { htmlToPlainText, renderEmail } from "./render.js"; import { WelcomeEmail } from "./sequences/welcome.js"; import { TrialReminderEmail } from "./sequences/trial-reminder.js"; import { ReActivationEmail } from "./sequences/re-activation.js"; import { GenericStepEmail } from "./sequences/generic-step.js"; const COMMON = { productName: "ToolCtrl", unsubscribeUrl: "https://app.example.com/unsubscribe/tok", } as const; describe("renderEmail", () => { it("renders a welcome email with html + text", () => { const { html, text } = renderEmail( WelcomeEmail({ productName: COMMON.productName, ctaUrl: "https://app.example.com/onboard", unsubscribeUrl: COMMON.unsubscribeUrl, recipientName: "Ada", }) ); expect(html.startsWith("")).toBe(true); expect(html).toContain("Welcome to ToolCtrl"); expect(html).toContain("Ada"); expect(html).toContain("https://app.example.com/onboard"); expect(text).toContain("Welcome to ToolCtrl"); expect(text).toContain("Hi Ada"); }); it("allows welcome chrome copy to be supplied by the caller", () => { const { html, text } = renderEmail( WelcomeEmail({ productName: COMMON.productName, ctaUrl: "https://app.example.com/onboard", unsubscribeUrl: COMMON.unsubscribeUrl, copy: { defaultCta: "Loslegen", fallbackRecipient: "du", preview: (productName) => `Willkommen bei ${productName}`, title: (productName) => `Willkommen bei ${productName}`, footerReason: (productName) => `Du bekommst diese E-Mail wegen deiner Registrierung bei ${productName}.`, unsubscribe: "Abmelden", heading: (productName) => `Willkommen bei ${productName}`, greeting: (recipientName) => `Hallo ${recipientName},`, intro: (productName) => `${productName} hilft deinem Team beim Automatisieren.`, replyPrompt: "Antworte auf diese E-Mail, wenn du Fragen hast.", signoff: (_senderName, productName) => `Dein ${productName} Team`, }, }) ); expect(html).toContain("Loslegen"); expect(html).toContain("Abmelden"); expect(text).toContain("Hallo du,"); expect(text).toContain("Dein ToolCtrl Team"); }); it("renders trial-reminder with correct day-pluralisation", () => { const single = renderEmail( TrialReminderEmail({ productName: COMMON.productName, daysRemaining: 1, upgradeUrl: "https://app.example.com/upgrade", unsubscribeUrl: COMMON.unsubscribeUrl, }) ); expect(single.html).toContain("1 day left"); const multi = renderEmail( TrialReminderEmail({ productName: COMMON.productName, daysRemaining: 3, upgradeUrl: "https://app.example.com/upgrade", unsubscribeUrl: COMMON.unsubscribeUrl, }) ); expect(multi.html).toContain("3 days left"); }); it("allows trial-reminder copy to be supplied by the caller", () => { const { html, text } = renderEmail( TrialReminderEmail({ productName: COMMON.productName, daysRemaining: 2, upgradeUrl: "https://app.example.com/upgrade", unsubscribeUrl: COMMON.unsubscribeUrl, copy: { daySingular: "Tag", dayPlural: "Tage", fallbackRecipient: "du", preview: (productName, daysRemaining, dayWord) => `${productName} endet in ${daysRemaining} ${dayWord}`, title: (daysRemaining, dayWord) => `${daysRemaining} ${dayWord} verbleiben`, footerReason: (productName) => `Trial-Erinnerung von ${productName}.`, unsubscribe: "Abmelden", heading: (daysRemaining, dayWord) => `${daysRemaining} ${dayWord} im Trial`, greeting: (recipientName) => `Hallo ${recipientName},`, intro: (productName, daysRemaining, dayWord) => `Dein ${productName} Trial endet in ${daysRemaining} ${dayWord}.`, defaultCta: "Plan upgraden", extensionPrompt: "Antworte auf diese E-Mail, wenn du mehr Zeit brauchst.", }, }) ); expect(html).toContain("Plan upgraden"); expect(html).toContain("Abmelden"); expect(text).toContain("Hallo du,"); expect(text).toContain("2 Tage im Trial"); }); it("renders re-activation with optional highlight", () => { const withHighlight = renderEmail( ReActivationEmail({ productName: COMMON.productName, returnUrl: "https://app.example.com/back", highlight: "we shipped Plugin v2", unsubscribeUrl: COMMON.unsubscribeUrl, }) ); expect(withHighlight.html).toContain("we shipped Plugin v2"); const noHighlight = renderEmail( ReActivationEmail({ productName: COMMON.productName, returnUrl: "https://app.example.com/back", unsubscribeUrl: COMMON.unsubscribeUrl, }) ); expect(noHighlight.html).not.toContain("What is new"); }); it("allows re-activation copy to be supplied by the caller", () => { const { html, text } = renderEmail( ReActivationEmail({ productName: COMMON.productName, returnUrl: "https://app.example.com/back", highlight: "wir haben Automationen verbessert", unsubscribeUrl: COMMON.unsubscribeUrl, copy: { fallbackRecipient: "du", preview: (productName) => `Zurueck zu ${productName}`, title: (productName) => `Zurueck zu ${productName}`, footerReason: (productName) => `Du hattest dich bei ${productName} registriert.`, unsubscribe: "Abmelden", heading: (productName) => `Zurueck zu ${productName}`, greeting: (recipientName) => `Hallo ${recipientName},`, intro: (productName) => `Seit deinem letzten Besuch bei ${productName} hat sich viel getan.`, highlightLabel: "Neu:", defaultCta: "Nochmal ansehen", replyPrompt: "Antworte mit deinem wichtigsten Wunsch.", }, }) ); expect(html).toContain("Nochmal ansehen"); expect(html).toContain("Abmelden"); expect(text).toContain("Hallo du,"); expect(text).toContain("Neu: wir haben Automationen verbessert"); }); it("renders generic-step with multiple body paragraphs and CTA", () => { const { html, text } = renderEmail( GenericStepEmail({ productName: COMMON.productName, headline: "Day 3: your first automation", bodyParagraphs: ["First paragraph here.", "Second paragraph follows."], ctaUrl: "https://app.example.com/setup", unsubscribeUrl: COMMON.unsubscribeUrl, }) ); expect(html).toContain("Day 3: your first automation"); expect(html).toContain("First paragraph here."); expect(html).toContain("Second paragraph follows."); expect(text).toContain("First paragraph here."); expect(text).toContain("Second paragraph follows."); }); it("allows generic-step chrome copy to be supplied by the caller", () => { const { html, text } = renderEmail( GenericStepEmail({ productName: COMMON.productName, headline: "Day 4", bodyParagraphs: ["Localized body."], ctaUrl: "https://app.example.com/setup", unsubscribeUrl: COMMON.unsubscribeUrl, copy: { defaultCta: "Zum Dashboard", fallbackRecipient: "du", footerReason: (productName) => `Du bekommst diese E-Mail wegen deiner Registrierung bei ${productName}.`, unsubscribe: "Abmelden", greeting: (recipientName) => `Hallo ${recipientName},`, signoff: (productName) => `Dein ${productName} Team`, }, }) ); expect(html).toContain("Zum Dashboard"); expect(html).toContain("Abmelden"); expect(text).toContain("Hallo du,"); expect(text).toContain("Dein ToolCtrl Team"); }); it("escapes user-provided variables (no XSS via recipientName)", () => { const { html } = renderEmail( WelcomeEmail({ productName: COMMON.productName, ctaUrl: "https://app.example.com/onboard", unsubscribeUrl: COMMON.unsubscribeUrl, recipientName: "", }) ); expect(html).not.toContain(""); expect(html).toContain("<script>alert(1)</script>"); }); it("emits unsubscribe URL in every sequence template", () => { const url = "https://app.example.com/unsubscribe/abc"; for (const tpl of [ WelcomeEmail({ productName: "X", ctaUrl: "https://e.com/a", unsubscribeUrl: url }), TrialReminderEmail({ productName: "X", daysRemaining: 7, upgradeUrl: "https://e.com/u", unsubscribeUrl: url }), ReActivationEmail({ productName: "X", returnUrl: "https://e.com/r", unsubscribeUrl: url }), GenericStepEmail({ productName: "X", headline: "h", bodyParagraphs: ["p"], unsubscribeUrl: url }), ]) { const { html } = renderEmail(tpl); expect(html).toContain(url); } }); }); describe("htmlToPlainText", () => { it("preserves anchor URLs as text", () => { const text = htmlToPlainText('

click here now

'); expect(text).toContain("here (https://x.example.com/y)"); }); it("collapses excess whitespace and strips scripts/styles", () => { const text = htmlToPlainText("

A

B

"); expect(text).toContain("A"); expect(text).toContain("B"); expect(text).not.toContain("body{}"); expect(text).not.toContain("<"); }); });