import { beforeEach, describe, expect, it, vi } from "vitest"; const getSessionPepperMock = vi.fn(); const sendFollowUpMock = vi.fn(); const findManyMock = vi.fn(); const updateManyMock = 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), updateMany: (...args: unknown[]) => updateManyMock(...args), }, }; beforeEach(() => { getSessionPepperMock.mockReset(); sendFollowUpMock.mockReset(); findManyMock.mockReset(); updateManyMock.mockReset(); originFromHostMock.mockReset(); getSessionPepperMock.mockReturnValue("test-pepper-16chars"); originFromHostMock.mockReturnValue("https://staging.communityrule.info"); sendFollowUpMock.mockResolvedValue(undefined); updateManyMock.mockResolvedValue({ count: 1 }); }); 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(updateManyMock).toHaveBeenCalledTimes(1); expect(updateManyMock).toHaveBeenCalledWith({ where: { id: "rule-1", followUpEmailSentAt: null }, data: { followUpEmailSentAt: now }, }); expect(findManyMock).toHaveBeenCalledWith( expect.objectContaining({ where: { followUpEmailSentAt: null, userId: { not: null }, createdAt: { lte: new Date("2026-11-08T12:00:00.000Z") }, }, }), ); }); 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(updateManyMock).not.toHaveBeenCalled(); }); it("does not send when another run already claimed the row", async () => { findManyMock.mockResolvedValueOnce([ { id: "rule-4", title: "Race", user: { email: "pen@example.com" }, }, ]); updateManyMock.mockResolvedValueOnce({ count: 0 }); const result = await sendDueRuleFollowUpEmails(db as never); expect(result).toEqual({ sent: 0, failed: 0, skipped: 1 }); expect(sendFollowUpMock).not.toHaveBeenCalled(); }); it("clears the claim 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 now = new Date("2026-11-10T12:00:00.000Z"); const result = await sendDueRuleFollowUpEmails(db as never, now); expect(result.failed).toBe(1); expect(sendFollowUpMock).toHaveBeenCalledTimes(1); expect(updateManyMock).toHaveBeenNthCalledWith(1, { where: { id: "rule-3", followUpEmailSentAt: null }, data: { followUpEmailSentAt: now }, }); expect(updateManyMock).toHaveBeenNthCalledWith(2, { where: { id: "rule-3", followUpEmailSentAt: now }, data: { followUpEmailSentAt: null }, }); }); });