swadhinbiswas Add digest delivery retries and observability metrics · 2/19/2026
4b685b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
const runDueDigestsMock = vi.fn();
const loggerInfoMock = vi.fn();
const loggerErrorMock = vi.fn();
vi.mock("@/lib/chat-notifications", () => ({
runDueDigests: runDueDigestsMock,
}));
vi.mock("@/lib/logger", () => ({
logger: {
info: loggerInfoMock,
error: loggerErrorMock,
},
}));
describe("notification digest cron API", () => {
beforeEach(() => {
runDueDigestsMock.mockReset();
loggerInfoMock.mockReset();
loggerErrorMock.mockReset();
vi.unstubAllEnvs();
});
afterEach(() => {
vi.unstubAllEnvs();
});
it("returns 401 when CRON_SECRET is missing", async () => {
vi.stubEnv("CRON_SECRET", "");
vi.resetModules();
const mod = await import("@/pages/api/cron/notification-digests");
const request = new Request("http://localhost/api/cron/notification-digests", {
method: "POST",
headers: { Authorization: "Bearer anything" },
});
const response = await mod.POST({ request, url: new URL(request.url) } as any);
expect(response.status).toBe(401);
expect(runDueDigestsMock).not.toHaveBeenCalled();
});
it("returns 401 when token is invalid", async () => {
vi.stubEnv("CRON_SECRET", "super-secret");
vi.resetModules();
const mod = await import("@/pages/api/cron/notification-digests");
const request = new Request("http://localhost/api/cron/notification-digests", {
method: "POST",
headers: { Authorization: "Bearer wrong" },
});
const response = await mod.POST({ request, url: new URL(request.url) } as any);
expect(response.status).toBe(401);
expect(runDueDigestsMock).not.toHaveBeenCalled();
});
it("passes dryRun=true to digest runner", async () => {
vi.stubEnv("CRON_SECRET", "super-secret");
runDueDigestsMock.mockResolvedValue({
checked: 4,
due: 2,
sent: 2,
skippedNoEmail: 0,
skippedEmpty: 0,
failed: 0,
});
vi.resetModules();
const mod = await import("@/pages/api/cron/notification-digests");
const request = new Request("http://localhost/api/cron/notification-digests?dryRun=true", {
method: "POST",
headers: { Authorization: "Bearer super-secret" },
});
const response = await mod.POST({ request, url: new URL(request.url) } as any);
const body = await response.json();
expect(response.status).toBe(200);
expect(body.success).toBe(true);
expect(body.dryRun).toBe(true);
expect(body.maxRetries).toBe(1);
expect(runDueDigestsMock).toHaveBeenCalledWith({ dryRun: true, maxRetries: 1 });
expect(loggerInfoMock).toHaveBeenCalledTimes(1);
});
it("passes maxRetries query param to digest runner", async () => {
vi.stubEnv("CRON_SECRET", "super-secret");
runDueDigestsMock.mockResolvedValue({
checked: 1,
due: 1,
sent: 1,
skippedNoEmail: 0,
skippedEmpty: 0,
failed: 0,
retried: 1,
recovered: 1,
});
vi.resetModules();
const mod = await import("@/pages/api/cron/notification-digests");
const request = new Request("http://localhost/api/cron/notification-digests?maxRetries=3", {
method: "POST",
headers: { Authorization: "Bearer super-secret" },
});
const response = await mod.POST({ request, url: new URL(request.url) } as any);
const body = await response.json();
expect(response.status).toBe(200);
expect(body.maxRetries).toBe(3);
expect(runDueDigestsMock).toHaveBeenCalledWith({ dryRun: false, maxRetries: 3 });
});
it("returns 500 when digest runner throws", async () => {
vi.stubEnv("CRON_SECRET", "super-secret");
runDueDigestsMock.mockRejectedValue(new Error("boom"));
vi.resetModules();
const mod = await import("@/pages/api/cron/notification-digests");
const request = new Request("http://localhost/api/cron/notification-digests", {
method: "POST",
headers: { Authorization: "Bearer super-secret" },
});
const response = await mod.POST({ request, url: new URL(request.url) } as any);
const body = await response.json();
expect(response.status).toBe(500);
expect(body.success).toBe(false);
expect(body.error).toBe("boom");
expect(loggerErrorMock).toHaveBeenCalledTimes(1);
});
});