Claim each print follow-up before it is sent, and copy launch counts from existing Rules.

This commit is contained in:
adilallo committed 2026-11-10 19:07:36 -07:00
1 parent d9c475a967
commit 214da262df
5 files changed
+108 -21

No files matched your search

+43 -10
View File
@@ -3,7 +3,7 @@ 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 updateManyMock = vi.fn();
const originFromHostMock = vi.fn();
vi.mock("../../lib/server/env", () => ({
@@ -27,7 +27,7 @@ import { sendDueRuleFollowUpEmails } from "../../lib/server/ruleFollowUp";
const db = {
publishedRule: {
findMany: (...args: unknown[]) => findManyMock(...args),
update: (...args: unknown[]) => updateMock(...args),
updateMany: (...args: unknown[]) => updateManyMock(...args),
},
};
@@ -35,12 +35,12 @@ beforeEach(() => {
getSessionPepperMock.mockReset();
sendFollowUpMock.mockReset();
findManyMock.mockReset();
updateMock.mockReset();
updateManyMock.mockReset();
originFromHostMock.mockReset();
getSessionPepperMock.mockReturnValue("test-pepper-16chars");
originFromHostMock.mockReturnValue("https://staging.communityrule.info");
sendFollowUpMock.mockResolvedValue(undefined);
updateMock.mockResolvedValue({});
updateManyMock.mockResolvedValue({ count: 1 });
});
describe("sendDueRuleFollowUpEmails", () => {
@@ -67,10 +67,20 @@ describe("sendDueRuleFollowUpEmails", () => {
markdownUrl: expect.stringContaining("/export/md?t="),
}),
);
expect(updateMock).toHaveBeenCalledWith({
where: { id: "rule-1" },
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 () => {
@@ -80,10 +90,24 @@ describe("sendDueRuleFollowUpEmails", () => {
const result = await sendDueRuleFollowUpEmails(db as never);
expect(result.skipped).toBe(1);
expect(sendFollowUpMock).not.toHaveBeenCalled();
expect(updateMock).not.toHaveBeenCalled();
expect(updateManyMock).not.toHaveBeenCalled();
});
it("leaves sentAt unset when mail fails so the hourly job can retry", async () => {
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",
@@ -92,8 +116,17 @@ describe("sendDueRuleFollowUpEmails", () => {
},
]);
sendFollowUpMock.mockRejectedValueOnce(new Error("smtp down"));
const result = await sendDueRuleFollowUpEmails(db as never);
const now = new Date("2026-11-10T12:00:00.000Z");
const result = await sendDueRuleFollowUpEmails(db as never, now);
expect(result.failed).toBe(1);
expect(updateMock).not.toHaveBeenCalled();
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 },
});
});
});