import { type TestStatus, capitalize, sanitizeExternalUrl } from "@allurereport/core-api"; import mjml2html from "mjml"; import type { MailData } from "./model.js"; /** * Values of the Allure report light theme (web-components semantic tokens), flattened to hex for mail clients. * Status fill and on-fill colors are the ones of the status label and the report statistics. */ const theme = { canvas: "#f1f5f7", surface: "#ffffff", border: "#edf1f5", textPrimary: "#2c333f", textSecondary: "#535e6d", textMuted: "#677180", link: "#2f5fc5", primary: "#7855d0", onPrimary: "#fbfdfe", radiusCard: "8px", radiusControl: "6px", // PT Root UI, the font of the report, is a web font which most mail clients ignore fontFamily: "ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Helvetica, Arial, sans-serif", dark: { canvas: "#2c343f", surface: "#1b2129", border: "#272e39", textPrimary: "#edf1f5", textSecondary: "#c5ccd5", textMuted: "#97a2af", link: "#b2ccff", }, fontFamilyMono: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace", }; const statusFills: Record = { passed: "#3bc95d", failed: "#f43f3b", broken: "#ffd833", skipped: "#a5b7d1", unknown: "#a181ff", }; const statusOnFills: Record = { passed: "#0d121c", failed: "#ffffff", broken: "#0d121c", skipped: "#0d121c", unknown: "#ffffff", }; const statusOrder: TestStatus[] = ["failed", "broken", "passed", "skipped", "unknown"]; export const escapeHtml = (value: string): string => value .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); const safeUrl = (url?: string): string | undefined => { const sanitized = sanitizeExternalUrl(url); return sanitized ? escapeHtml(sanitized) : undefined; }; const link = (label: string, url?: string): string => { const href = safeUrl(url); return href ? `${escapeHtml(label)}` : escapeHtml(label); }; const dot = (status: TestStatus): string => `●`; /** The status pill of the report summary card */ const renderStatusLabel = (status: TestStatus): string => `${capitalize(status) ?? status}`; /** Like the report statistics: the total first, then only the statuses that have tests */ const renderStatistics = ({ stats }: MailData): string => { const items = [ `Total ${stats.total}`, ...statusOrder .filter((status) => (stats[status] ?? 0) > 0) .map( (status) => `${dot(status)} ${capitalize(status) ?? status} ${stats[status]}`, ), ]; return `${items.join("")}
`; }; const renderCi = ({ ci }: MailData): string => { if (!ci) { return ""; } const rows = [ ci.name ? `Build: ${link(ci.name, ci.url)}` : undefined, ci.branch ? `Branch: ${escapeHtml(ci.branch)}` : undefined, ci.pullRequestName ? `Pull request: ${link(ci.pullRequestName, ci.pullRequestUrl)}` : undefined, ].filter(Boolean); return rows.length ? `${rows.join("
")}
` : ""; }; const renderFailed = ({ failed, hiddenFailed }: MailData): string => { if (failed.length === 0) { return ""; } const rows = failed .map( ({ name, status, message }) => `${dot(status)}${escapeHtml(name)}${ message ? `
${escapeHtml(message)}
` : "" }`, ) .join(""); const more = hiddenFailed > 0 ? `
… and ${hiddenFailed} more
` : ""; return `Failed tests ${rows}
${more}
`; }; /** The preheader: the first text a mail client shows in the inbox list, next to the subject */ const renderPreview = ({ stats }: MailData): string => { const parts = statusOrder.filter((status) => (stats[status] ?? 0) > 0).map((status) => `${stats[status]} ${status}`); return stats.total > 0 ? `${parts.join(", ")} of ${stats.total} tests` : "No test results"; }; const darkModeStyles = `@media (prefers-color-scheme: dark) { body, .m-canvas { background-color: ${theme.dark.canvas} !important; } .m-card > table > tbody > tr > td { background-color: ${theme.dark.surface} !important; border-color: ${theme.dark.border} !important; } .m-t1, .m-t1 div { color: ${theme.dark.textPrimary} !important; } .m-t2, .m-t2 div { color: ${theme.dark.textSecondary} !important; } .m-t3, .m-t3 div { color: ${theme.dark.textMuted} !important; } .m-link { color: ${theme.dark.link} !important; } }`; export const renderMjml = (data: MailData): string => { const reportHref = safeUrl(data.reportUrl); return ` ${escapeHtml(data.title)} ${escapeHtml(renderPreview(data))} ${darkModeStyles} ${escapeHtml(data.title)} ${renderStatusLabel(data.status)}  ${data.passRate}% passed · ${escapeHtml(data.duration)} ${renderCi(data)} ${renderStatistics(data)} ${ reportHref ? `Open report` : "" } ${renderFailed(data)} `; }; export const renderMail = async (data: MailData): Promise => { // mjml 4 is synchronous, mjml 5 returns a promise; awaiting handles both const { html } = await mjml2html(renderMjml(data), { validationLevel: "soft" }); return html; };