-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigest-email.test.ts
More file actions
413 lines (397 loc) · 14.5 KB
/
Copy pathdigest-email.test.ts
File metadata and controls
413 lines (397 loc) · 14.5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { describe, expect, it } from "bun:test";
import { buildDigestEmail, sendDigestEmail } from "../src/lib/digest-email.js";
import type { ReleaseLatestItem } from "@buildinternet/releases-api-types";
function rel(over: Partial<ReleaseLatestItem>): ReleaseLatestItem {
return {
id: "rel_1",
version: null,
type: "feature",
title: "Thing shipped",
summary: "We shipped a thing.",
titleGenerated: null,
titleShort: "Thing",
publishedAt: "2026-06-08T00:00:00.000Z",
url: "https://acme.com/changelog/1",
media: [],
source: { slug: "blog", name: "Acme Blog", type: "feed", orgSlug: "acme", orgName: "Acme" },
product: { slug: "widget", name: "Widget" },
coverageCount: 0,
contentChars: null,
contentTokens: null,
...over,
} as ReleaseLatestItem;
}
describe("buildDigestEmail", () => {
it("renders subject, text, and html with release + unsubscribe link", () => {
const { subject, text, html } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [
rel({ titleShort: "ShortHeadline", title: "RawLongTitle" }),
rel({
id: "rel_2",
title: "Second",
source: {
slug: "blog",
name: "Acme Blog",
type: "feed",
orgSlug: "acme",
orgName: "Acme",
},
}),
],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
expect(subject).toContain("2");
expect(text).toContain("ShortHeadline"); // titleShort preferred over title, matching the feed UI
expect(text).not.toContain("RawLongTitle");
// Release rows link upstream, not to the noindexed /release/ stub — same policy
// as the web feed's releaseLinkTarget().
expect(text).toContain("https://acme.com/changelog/1");
expect(text).not.toContain("https://releases.sh/release/rel_1");
expect(text).toContain("reld_x");
expect(html).toContain("Unsubscribe");
expect(html).toContain("https://releases.sh/following");
// Heading is the org's display name, not the source name.
expect(text).toContain("ACME");
expect(text).not.toContain("Acme Blog");
expect(html).toContain(">Acme</a>");
// Wrapped in the shared document shell — without it the body has no
// max-width and lines run the full width of a wide reading pane.
expect(html).toStartWith("<!doctype html>");
expect(html).toContain("max-width:600px");
});
it("falls back to the on-site permalink when a release has no upstream http(s) url", () => {
const { text: slugged } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [rel({ url: null, webUrl: "https://releases.sh/release/rel_1-thing" })],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
expect(slugged).toContain("https://releases.sh/release/rel_1-thing");
// No webUrl either → the bare-ID path, which 308s to canonical.
const { text: bare } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [rel({ url: null })],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
expect(bare).toContain("https://releases.sh/release/rel_1");
});
it("compresses a per-product version burst to the newest release + notes", () => {
const ghSource = {
slug: "sdk",
name: "acme/sdk",
type: "github" as const,
orgSlug: "acme",
orgName: "Acme",
};
const { text, html } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [
rel({
id: "t1",
title: "v4.2.1",
version: "4.2.1",
titleShort: "Fixed a socket leak",
summary: null,
source: ghSource,
}),
rel({
id: "t2",
title: "v4.2.0",
version: "4.2.0",
titleShort: "Added a retry flag",
summary: null,
source: ghSource,
}),
],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
// Product header counts all releases, but only the newest version shows —
// the rest fold into "and N more". No enumerated version list.
expect(text).toContain("Widget · 2 releases");
expect(text).toContain("4.2.1 (and 1 more)");
expect(text).toContain("Latest — Fixed a socket leak");
// "and N more" links to the product page; the pill itself links to the release.
expect(text).toContain("https://releases.sh/acme/widget");
expect(html).toContain('href="https://acme.com/changelog/1"');
expect(html).toContain(">4.2.1</a>");
expect(html).toContain("and 1 more");
expect(html).toContain("· 2 releases");
// The older version is NOT enumerated on its own line.
expect(text).not.toContain("Added a retry flag");
expect(text).not.toContain("4.2.0");
});
it("picks the newest release WITH notes as the rollup representative", () => {
const ghSource = {
slug: "workers-sdk",
name: "cloudflare/workers-sdk",
type: "github" as const,
orgSlug: "cloudflare",
orgName: "Cloudflare",
};
// Newest-first: a dependency-bump (no titleShort, boilerplate body) leads. The
// rollup should skip it as the representative — showing the newest release that
// carries a real note — while still counting it in "· N releases" / "and N more".
const dep = (id: string, v: string) =>
rel({
id,
version: v,
title: v,
titleShort: null,
summary: "### Patch Changes\n\n- Updated dependencies",
source: ghSource,
product: undefined,
});
const real = (id: string, v: string, short: string) =>
rel({
id,
version: v,
title: v,
titleShort: short,
summary: null,
source: ghSource,
product: undefined,
});
const { text, html } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [
dep("d1", "@cloudflare/cli-shared@0.1.8"),
real("r1", "wrangler@4.101.0", "Autoconfig graduates from experimental"),
real("r2", "@cloudflare/workers-auth@0.3.0", "--temporary flag for preview accounts"),
real("r3", "miniflare@4.20260616.0", "cf.image transforms now work locally"),
real("r4", "@cloudflare/vite-plugin@1.41.0", "Experimental cfBuildOutput option"),
],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
expect(text).toContain("· 5 releases");
// The newest substantive release is the representative (version + note agree);
// the leading dependency bump is skipped, and the other four fold away.
expect(text).toContain("wrangler@4.101.0 (and 4 more)");
expect(text).toContain("Latest — Autoconfig graduates from experimental");
expect(text).not.toContain("Updated dependencies");
expect(text).not.toContain("--temporary flag for preview accounts"); // folded
expect(text).not.toContain("cf.image transforms now work locally"); // folded
// "and N more" links to the product/source page.
expect(text).toContain("and 4 more");
expect(html).toContain("and 4 more");
expect(html).toContain("https://releases.sh/cloudflare/workers-sdk");
});
it("dates the daily subject with the start of the covered window (ET)", () => {
const { subject, html } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [rel({})],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
// Ran 13:00 UTC (09:00 ET) on Jun 24, so it covers Jun 23 09:00 ET onward —
// the digest is dated for the day whose news it actually carries.
referenceDate: "2026-06-24T13:00:00.000Z",
});
expect(subject).toBe("Releases digest — Acme · 1 update · Jun 23");
// The HTML masthead mirrors the dated subject.
expect(html).toContain("Jun 23, 2026");
expect(html).not.toContain("Jun 24, 2026");
});
it("labels the weekly subject with the start of the covered window", () => {
const { subject } = buildDigestEmail({
recipientName: "T",
cadence: "weekly",
releases: [rel({}), rel({ id: "rel_2" })],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
referenceDate: "2026-06-24T13:00:00.000Z",
});
expect(subject).toBe("Releases digest — Acme · 2 updates · week of Jun 17");
});
it("omits the date when no referenceDate is supplied (backward compatible)", () => {
const { subject } = buildDigestEmail({
recipientName: "T",
cadence: "daily",
releases: [rel({})],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
expect(subject).toBe("Releases digest — Acme · 1 update");
});
it("falls back to the source name when the source has no org", () => {
const { text } = buildDigestEmail({
recipientName: null,
cadence: "daily",
releases: [
rel({
source: { slug: "indie", name: "Indie Feed", type: "feed", orgSlug: null, orgName: null },
}),
],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
});
expect(text).toContain("INDIE FEED");
});
// Common render config for the importance-ordering cases.
const base = {
recipientName: null,
cadence: "daily" as const,
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
};
const acme = {
slug: "blog",
name: "Acme Blog",
type: "feed" as const,
orgSlug: "acme",
orgName: "Acme",
};
it("leads an org's posts with the high-signal one, even when published earlier", () => {
const { text } = buildDigestEmail({
...base,
// Arrives published-desc from the query: the newer, unremarkable post first.
releases: [
rel({
id: "r_norm",
title: "Routine update",
titleShort: "Routine update",
importance: null,
publishedAt: "2026-06-09T00:00:00.000Z",
source: acme,
}),
rel({
id: "r_hot",
title: "Notable launch",
titleShort: "Notable launch",
importance: 5,
publishedAt: "2026-06-08T00:00:00.000Z",
source: acme,
}),
],
});
expect(text.indexOf("Notable launch")).toBeLessThan(text.indexOf("Routine update"));
});
it("floats an org carrying a high-signal release above a more-recent but unremarkable org", () => {
const alpha = {
slug: "a",
name: "Alpha",
type: "feed" as const,
orgSlug: "alpha",
orgName: "Alpha",
};
const beta = {
slug: "b",
name: "Beta",
type: "feed" as const,
orgSlug: "beta",
orgName: "Beta",
};
const { text } = buildDigestEmail({
...base,
releases: [
rel({
id: "r_a",
title: "Alpha routine",
titleShort: "Alpha routine",
importance: null,
publishedAt: "2026-06-10T00:00:00.000Z",
source: alpha,
}),
rel({
id: "r_b",
title: "Beta breaking change",
titleShort: "Beta breaking change",
importance: 5,
publishedAt: "2026-06-08T00:00:00.000Z",
source: beta,
}),
],
});
expect(text.indexOf("BETA")).toBeLessThan(text.indexOf("ALPHA"));
});
it("keeps NULL-importance posts in chronological order, never below a scored-low post", () => {
const { text } = buildDigestEmail({
...base,
releases: [
rel({
id: "r_null",
title: "Unscored recent",
titleShort: "Unscored recent",
importance: null,
publishedAt: "2026-06-09T00:00:00.000Z",
source: acme,
}),
rel({
id: "r_low",
title: "Low scored older",
titleShort: "Low scored older",
importance: 2,
publishedAt: "2026-06-08T00:00:00.000Z",
source: acme,
}),
],
});
// Neither is high-signal, so published-desc is preserved and the NULL row (more
// recent) stays ahead of the scored-low one — unknown is not treated as unimportant.
expect(text.indexOf("Unscored recent")).toBeLessThan(text.indexOf("Low scored older"));
});
});
describe("sendDigestEmail", () => {
it("returns no_binding when AUTH_EMAIL is absent", async () => {
const res = await sendDigestEmail(
{ DIGEST_EMAIL_FROM: "digests@releases.sh" },
{
to: "t@e.com",
recipientName: "T",
cadence: "daily",
releases: [rel({})],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
},
);
expect(res.sent).toBe(false);
expect(res.reason).toBe("no_binding");
});
it("sends with a List-Unsubscribe header through the binding", async () => {
let captured: any = null;
const res = await sendDigestEmail(
{
AUTH_EMAIL: {
send: async (m: any) => {
captured = m;
return { messageId: "m1" };
},
} as any,
DIGEST_EMAIL_FROM: "digests@releases.sh",
},
{
to: "t@e.com",
recipientName: "T",
cadence: "weekly",
releases: [rel({})],
baseUrl: "https://releases.sh",
manageUrl: "https://releases.sh/following",
unsubscribeUrl: "https://api.releases.sh/v1/digest/unsubscribe/reld_x",
},
);
expect(res.sent).toBe(true);
expect(captured.from).toContain("digests@releases.sh");
expect(captured.headers["List-Unsubscribe"]).toContain("reld_x");
expect(captured.headers["List-Unsubscribe-Post"]).toBe("List-Unsubscribe=One-Click");
});
});