Files
community-rule/tests/unit/ruleFollowUp.test.ts
T
adilallo d9c475a967 Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
2026-11-10 18:32:33 -07:00

100 lines
3.1 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const getSessionPepperMock = vi.fn();
const sendFollowUpMock = vi.fn();
const findManyMock = vi.fn();
const updateMock = vi.fn();
const originFromHostMock = vi.fn();
vi.mock("../../lib/server/env", () => ({
getSessionPepper: () => getSessionPepperMock(),
}));
vi.mock("../../lib/server/mail", () => ({
sendRuleFollowUpEmail: (...args: unknown[]) => sendFollowUpMock(...args),
}));
vi.mock("../../lib/siteMetadata", () => ({
originFromHost: () => originFromHostMock(),
}));
vi.mock("../../lib/logger", () => ({
logger: { info: vi.fn(), error: vi.fn() },
}));
import { sendDueRuleFollowUpEmails } from "../../lib/server/ruleFollowUp";
const db = {
publishedRule: {
findMany: (...args: unknown[]) => findManyMock(...args),
update: (...args: unknown[]) => updateMock(...args),
},
};
beforeEach(() => {
getSessionPepperMock.mockReset();
sendFollowUpMock.mockReset();
findManyMock.mockReset();
updateMock.mockReset();
originFromHostMock.mockReset();
getSessionPepperMock.mockReturnValue("test-pepper-16chars");
originFromHostMock.mockReturnValue("https://staging.communityrule.info");
sendFollowUpMock.mockResolvedValue(undefined);
updateMock.mockResolvedValue({});
});
describe("sendDueRuleFollowUpEmails", () => {
it("sends once to the pen-holder and records sentAt", async () => {
findManyMock.mockResolvedValueOnce([
{
id: "rule-1",
title: "Garden Norms",
user: { email: "pen@example.com" },
},
]);
const now = new Date("2026-11-10T12:00:00.000Z");
const result = await sendDueRuleFollowUpEmails(db as never, now);
expect(result).toEqual({ sent: 1, failed: 0, skipped: 0 });
expect(sendFollowUpMock).toHaveBeenCalledTimes(1);
expect(sendFollowUpMock).toHaveBeenCalledWith(
expect.objectContaining({
to: "pen@example.com",
communityName: "Garden Norms",
origin: "https://staging.communityrule.info",
pdfUrl: expect.stringContaining("/export/pdf?t="),
markdownUrl: expect.stringContaining("/export/md?t="),
}),
);
expect(updateMock).toHaveBeenCalledWith({
where: { id: "rule-1" },
data: { followUpEmailSentAt: now },
});
});
it("skips rows with no mailbox and does not mark them sent", async () => {
findManyMock.mockResolvedValueOnce([
{ id: "rule-2", title: "Guest", user: { email: "" } },
]);
const result = await sendDueRuleFollowUpEmails(db as never);
expect(result.skipped).toBe(1);
expect(sendFollowUpMock).not.toHaveBeenCalled();
expect(updateMock).not.toHaveBeenCalled();
});
it("leaves sentAt unset when mail fails so the hourly job can retry", async () => {
findManyMock.mockResolvedValueOnce([
{
id: "rule-3",
title: "Retry",
user: { email: "pen@example.com" },
},
]);
sendFollowUpMock.mockRejectedValueOnce(new Error("smtp down"));
const result = await sendDueRuleFollowUpEmails(db as never);
expect(result.failed).toBe(1);
expect(updateMock).not.toHaveBeenCalled();
});
});