import "../setup.ts";
import { assert, assertFalse, assertStringIncludes } from "@std/assert";
import { type DigestContent, renderDigestEmail } from "../../mail/digest.ts";
const links = {
notifications: "https://g.test/notifications",
followed: "https://g.test/~/followed",
chat: "https://g.test/?chat=open",
settings: "https://g.test/users/ann/~/settings",
unsubscribe: "https://g.test/unsubscribe?token=t",
};
const dump = (title: string, over = {}) => ({
title,
url: "https://g.test/dumps/x",
poster: "bob",
domain: "example.com",
thumbnail: "https://g.test/api/thumbnails/x?v=1",
votes: 1,
comments: 0,
mine: false,
...over,
});
Deno.test("renderDigestEmail: the weekly digest escapes what users wrote and links to unsubscribe", () => {
const content: DigestContent = {
cadence: "weekly",
from: new Date("2026-09-27T08:00:00Z"),
to: new Date("2026-10-04T08:00:00Z"),
reader: "ann",
activity: {
counts: { upvotes: 12, followers: 1 },
highlights: [{
kind: "comment",
who: "bob",
target: { title: "", url: "https://g.test/dumps/y" },
quote: "a bold claim",
at: new Date("2026-09-30T10:00:00Z"),
}],
total: 13,
},
network: { dumps: [dump("Net & work")], more: 2 },
top: [dump("Mine", { mine: true, votes: 4 })],
community: null,
links,
};
const email = renderDigestEmail(content, { style: "smooth", scheme: "auto" });
assertStringIncludes(email.subject, "Your week on gerbeur, Sep 27");
assertFalse(email.html.includes("](https://g.test/dumps/y)",
);
assertStringIncludes(email.text, " > a \\bold\\ claim");
assertFalse(email.html.includes("Community"));
});
Deno.test("renderDigestEmail: the monthly recap leads with a wide image and the month's totals", () => {
const content: DigestContent = {
cadence: "monthly",
from: new Date("2026-09-04T08:00:00Z"),
to: new Date("2026-10-04T08:00:00Z"),
reader: "ann",
activity: null,
network: null,
top: [dump("First"), dump("Second"), dump("Third"), dump("Fourth")],
community: {
members: [{ username: "cy", invitedBy: "ann" }],
membersMore: 0,
playlists: [],
playlistsMore: 0,
chatMessages: 1284,
totals: { dumps: 203, upvotes: 911, comments: 402 },
},
links,
};
const email = renderDigestEmail(content, {
style: "melange",
scheme: "light",
});
assertStringIncludes(email.subject, "The month on gerbeur");
assertStringIncludes(email.html, `width="528"`);
assertStringIncludes(email.html, "1,284");
assertStringIncludes(email.html, "invited by you");
assertStringIncludes(email.html, "Turn off monthly recaps");
// Only #2 and #3 get a thumbnail beside the hero.
assert(email.html.match(/
]*width="64"/g)?.length === 2);
});