Files
WRNexusJS/packages/email-renderer/test/email.test.ts
T
Clintchiz 2cd8f8ea14
Quality / quality (ubuntu-latest) (push) Failing after 10m16s
Quality / quality (windows-latest) (push) Canceled after 0s
fix(email-renderer): preserve section ancestry in parsed HTML
2026-09-05 12:06:48 +05:30

198 lines
7.0 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { parse, type DefaultTreeAdapterMap } from "parse5";
import {
emailCoreBlocks,
renderEmail,
validateAmpEmail,
validateMjml,
importMjml,
type EmailRenderBundle,
} from "../src/index.ts";
import type { BlockDocument } from "@wrnexus/blocks";
const document: BlockDocument = {
schemaVersion: 1,
kind: "email",
meta: { subject: "Welcome", preheader: "Preview", lang: "en" },
theme: { tokens: {} },
root: {
id: "root",
type: "layout.email-document",
version: 1,
props: { width: "600", background: "#f3f4f6" },
styles: {},
slots: {
content: [
{
id: "section",
type: "layout.section",
version: 1,
props: { background: "#ffffff", padding: "24px" },
styles: {},
slots: {
content: [
{
id: "heading",
type: "content.heading",
version: 1,
props: { text: "Hello <world>", level: "1" },
styles: {},
slots: {},
},
{
id: "button",
type: "content.button",
version: 1,
props: { label: "Open", href: "https://example.com/" },
styles: {},
slots: {},
},
],
},
},
],
},
},
};
describe("email renderer", () => {
test("browser HTML parsing preserves every block inside its section", async () => {
const rendered = await renderEmail(document);
expect(rendered.ok).toBe(true);
if (!rendered.ok) return;
const tree = parse(rendered.value.parts.html.content);
const ancestry = new Map<string, string[]>();
const walk = (node: DefaultTreeAdapterMap["node"], parents: string[]) => {
const id = "attrs" in node ? node.attrs.find(attr => attr.name === "data-block-id")?.value : undefined;
if (id) ancestry.set(id, parents);
if ("childNodes" in node) for (const child of node.childNodes) walk(child, id ? [...parents, id] : parents);
};
walk(tree, []);
expect(ancestry.get("heading")).toEqual(["root", "section"]);
expect(ancestry.get("button")).toEqual(["root", "section"]);
});
test("renders deterministic HTML, text, AMP, and MJML", async () => {
const first = await renderEmail(document, { amp: true, mjml: true });
const second = await renderEmail(document, { amp: true, mjml: true });
expect(first).toEqual(second);
expect(first.ok).toBe(true);
if (!first.ok) return;
const value: EmailRenderBundle = first.value;
expect(value.parts.html.content).toContain("Hello &lt;world&gt;");
expect(value.parts.html.content).toContain("color:#111827");
expect(value.parts.text.content).toContain("Hello <world>");
expect(value.parts.amp?.validation.valid).toBe(true);
expect(value.exports?.mjml).toContain("<mjml>");
});
test("validates AMP output", async () => {
expect((await validateAmpEmail("<html></html>")).valid).toBe(false);
});
test("keeps rendered block rows inside valid column tables", async () => {
const columnDocument = structuredClone(document);
const content = columnDocument.root.slots.content[0]!.slots.content;
columnDocument.root.slots.content[0]!.slots.content = [
{
id: "columns",
type: "layout.columns",
version: 1,
props: {},
styles: {},
slots: {
columns: [
{
id: "column",
type: "layout.column",
version: 1,
props: { width: "100%" },
styles: {},
slots: { content },
},
],
},
},
];
const rendered = await renderEmail(columnDocument, { amp: true });
expect(rendered.ok).toBe(true);
if (!rendered.ok) return;
expect(rendered.value.parts.html.content).toContain(
'data-block-id="column" class="wrn-column" width="100%" valign="top"><table role="presentation"',
);
expect(rendered.value.parts.html.content).toContain(
'<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td data-block-id="heading"',
);
});
test("compiles only allowlisted semantic styles", async () => {
const styled = structuredClone(document);
styled.root.slots.content[0]!.slots.content[0]!.styles = {
color: { base: "#123456" },
textAlign: { base: "center" },
padding: { base: "12px 8px" },
backgroundColor: { base: "red;position:fixed" },
};
const rendered = await renderEmail(styled);
expect(rendered.ok).toBe(true);
if (!rendered.ok) return;
expect(rendered.value.parts.html.content).toContain(
"color:#123456;padding:12px 8px;text-align:center",
);
expect(rendered.value.parts.html.content).not.toContain("position:fixed");
});
test("resolves allowlisted personalization values and fallbacks", async () => {
const personalized = structuredClone(document);
personalized.root.slots.content[0]!.slots.content[0]!.props.text =
"Hello {{recipientName|friend}}";
const rendered = await renderEmail(personalized, { variables: { recipientName: "Ada" } });
expect(rendered.ok && rendered.value.parts.html.content).toContain("Hello Ada");
const fallback = await renderEmail(personalized);
expect(fallback.ok && fallback.value.parts.text.content).toContain("Hello friend");
const strict = await renderEmail(personalized, { requireVariables: true });
expect(strict.ok).toBe(true);
personalized.root.slots.content[0]!.slots.content[0]!.props.text = "Hello {{recipientName}}";
expect((await renderEmail(personalized, { requireVariables: true })).ok).toBe(false);
});
test("rejects unsafe MJML and imports supported content", () => {
expect(
importMjml("<mjml><mj-body><mj-raw>x</mj-raw></mj-body></mjml>", {
id: () => crypto.randomUUID(),
subject: "x",
}).ok,
).toBe(false);
let n = 0;
const imported = importMjml(
"<mjml><mj-body><mj-section><mj-column><mj-text>Hello</mj-text></mj-column></mj-section></mj-body></mjml>",
{ id: () => `n${++n}`, subject: "Imported" },
);
expect(imported.ok).toBe(true);
});
test("strict MJML validation catches invalid nesting", async () => {
expect(
(await validateMjml("<mjml><mj-body><mj-text>x</mj-text></mj-body></mjml>")).length,
).toBeGreaterThan(0);
});
test("ships the complete marketing block pack", () => {
expect(emailCoreBlocks.map((block) => block.manifest.type)).toEqual(
expect.arrayContaining([
"content.video",
"content.social",
"content.countdown",
"content.survey",
"content.feed-item",
"commerce.product",
]),
);
for (const block of emailCoreBlocks) {
expect(block.textRenderer).toBeFunction();
expect(block.renderers?.emailHtml).toBeFunction();
expect(block.renderers?.ampEmail).toBeFunction();
expect(block.renderers?.mjml).toBeFunction();
}
});
});