|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { buildRSSFeed } from "./rss.js"; |
| 3 | +import type { ReportEntry } from "../deployer/index-page.js"; |
| 4 | + |
| 5 | +const makeEntry = ( |
| 6 | + path: string, |
| 7 | + title: string, |
| 8 | + subtitle: string, |
| 9 | + dateTo?: string, |
| 10 | +): ReportEntry => ({ |
| 11 | + path, |
| 12 | + week: path.split("/")[1] ?? path, |
| 13 | + year: path.split("/")[0] ?? "", |
| 14 | + title, |
| 15 | + subtitle, |
| 16 | + dateLabel: `${path.split("/")[0]} ${path.split("/")[1]}`, |
| 17 | + dateTo, |
| 18 | +}); |
| 19 | + |
| 20 | +const defaultChannel = (overrides: Record<string, string> = {}) => ({ |
| 21 | + title: "Dev Pulse", |
| 22 | + link: "https://user.github.io/repo", |
| 23 | + description: "Weekly reports by @testuser", |
| 24 | + language: "en", |
| 25 | + timezone: "UTC", |
| 26 | + ...overrides, |
| 27 | +}); |
| 28 | + |
| 29 | +describe("buildRSSFeed", () => { |
| 30 | + it("generates valid RSS 2.0 XML with items sorted newest-first", () => { |
| 31 | + const entries: ReportEntry[] = [ |
| 32 | + makeEntry("2026/W12", "Week 12 Title", "Week 12 subtitle", "2026-03-22"), |
| 33 | + makeEntry("2026/W14", "Week 14 Title", "Week 14 subtitle", "2026-04-05"), |
| 34 | + makeEntry("2026/W13", "Week 13 Title", "Week 13 subtitle", "2026-03-29"), |
| 35 | + ]; |
| 36 | + |
| 37 | + const feed = buildRSSFeed(entries, defaultChannel()); |
| 38 | + |
| 39 | + expect(feed).toContain('<?xml version="1.0" encoding="UTF-8"?>'); |
| 40 | + expect(feed).toContain('<rss version="2.0"'); |
| 41 | + expect(feed).toContain("<title>Dev Pulse</title>"); |
| 42 | + expect(feed).toContain("<link>https://user.github.io/repo/</link>"); |
| 43 | + expect(feed).toContain("<description>Weekly reports by @testuser</description>"); |
| 44 | + expect(feed).toContain("<language>en</language>"); |
| 45 | + |
| 46 | + // Items should be sorted newest first (W14, W13, W12) |
| 47 | + const w14Pos = feed.indexOf("Week 14 Title"); |
| 48 | + const w13Pos = feed.indexOf("Week 13 Title"); |
| 49 | + const w12Pos = feed.indexOf("Week 12 Title"); |
| 50 | + expect(w14Pos).toBeLessThan(w13Pos); |
| 51 | + expect(w13Pos).toBeLessThan(w12Pos); |
| 52 | + }); |
| 53 | + |
| 54 | + it("includes OG image in enclosure and media:content", () => { |
| 55 | + const entries = [makeEntry("2026/W14", "Title", "Subtitle", "2026-04-05")]; |
| 56 | + const feed = buildRSSFeed(entries, defaultChannel({ link: "https://example.com" })); |
| 57 | + |
| 58 | + // enclosure tag |
| 59 | + expect(feed).toContain('url="https://example.com/2026/W14/og.png"'); |
| 60 | + expect(feed).toContain('type="image/png"'); |
| 61 | + |
| 62 | + // media:content with dimensions |
| 63 | + expect(feed).toContain('<media:content url="https://example.com/2026/W14/og.png"'); |
| 64 | + expect(feed).toContain('width="1200"'); |
| 65 | + expect(feed).toContain('height="630"'); |
| 66 | + expect(feed).toContain('medium="image"'); |
| 67 | + }); |
| 68 | + |
| 69 | + it("includes permalink guids", () => { |
| 70 | + const entries = [makeEntry("2026/W14", "Title", "Subtitle", "2026-04-05")]; |
| 71 | + const feed = buildRSSFeed(entries, defaultChannel({ link: "https://example.com" })); |
| 72 | + |
| 73 | + expect(feed).toContain('<guid isPermaLink="true">https://example.com/2026/W14/</guid>'); |
| 74 | + }); |
| 75 | + |
| 76 | + it("escapes XML special characters", () => { |
| 77 | + const entries = [makeEntry("2026/W14", "Title <>&\"'", "Sub <>&", "2026-04-05")]; |
| 78 | + const feed = buildRSSFeed(entries, defaultChannel({ |
| 79 | + title: "Site & Title", |
| 80 | + description: "A <desc>", |
| 81 | + })); |
| 82 | + |
| 83 | + expect(feed).toContain("Site & Title"); |
| 84 | + expect(feed).toContain("Title <>&"'"); |
| 85 | + expect(feed).toContain("Sub <>&"); |
| 86 | + expect(feed).toContain("A <desc>"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("includes atom:link for self-reference", () => { |
| 90 | + const entries = [makeEntry("2026/W14", "Title", "Sub", "2026-04-05")]; |
| 91 | + const feed = buildRSSFeed(entries, defaultChannel({ language: "ja" })); |
| 92 | + |
| 93 | + expect(feed).toContain('href="https://user.github.io/repo/feed.xml"'); |
| 94 | + expect(feed).toContain('rel="self"'); |
| 95 | + expect(feed).toContain("<language>ja</language>"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("computes pubDate as Monday 01:00 local in UTC timezone", () => { |
| 99 | + // dateRange.to = 2026-04-05 (Sunday), so pubDate = Monday 2026-04-06 01:00 UTC |
| 100 | + const entries = [makeEntry("2026/W14", "Title", "Sub", "2026-04-05")]; |
| 101 | + const feed = buildRSSFeed(entries, defaultChannel({ timezone: "UTC" })); |
| 102 | + |
| 103 | + expect(feed).toContain("<pubDate>"); |
| 104 | + expect(feed).toContain("Mon, 06 Apr 2026 01:00:00 GMT"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("computes pubDate with Asia/Tokyo timezone", () => { |
| 108 | + // dateRange.to = 2026-04-05 (Sunday) |
| 109 | + // Monday 01:00 JST = Sunday 16:00 UTC |
| 110 | + const entries = [makeEntry("2026/W14", "Title", "Sub", "2026-04-05")]; |
| 111 | + const feed = buildRSSFeed(entries, defaultChannel({ timezone: "Asia/Tokyo" })); |
| 112 | + |
| 113 | + expect(feed).toContain("Sun, 05 Apr 2026 16:00:00 GMT"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("computes pubDate with US/Eastern timezone", () => { |
| 117 | + // dateRange.to = 2026-04-05 (Sunday), EDT is UTC-4 in April |
| 118 | + // Monday 01:00 EDT = Monday 05:00 UTC |
| 119 | + const entries = [makeEntry("2026/W14", "Title", "Sub", "2026-04-05")]; |
| 120 | + const feed = buildRSSFeed(entries, defaultChannel({ timezone: "US/Eastern" })); |
| 121 | + |
| 122 | + expect(feed).toContain("Mon, 06 Apr 2026 05:00:00 GMT"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("omits pubDate when dateTo is not available", () => { |
| 126 | + const entry: ReportEntry = { |
| 127 | + path: "2026/W14", |
| 128 | + week: "W14", |
| 129 | + year: "2026", |
| 130 | + title: undefined, |
| 131 | + subtitle: undefined, |
| 132 | + dateLabel: "2026 W14", |
| 133 | + }; |
| 134 | + |
| 135 | + const feed = buildRSSFeed([entry], defaultChannel()); |
| 136 | + |
| 137 | + expect(feed).toContain("<title>2026 W14</title>"); |
| 138 | + expect(feed).not.toContain("<pubDate>"); |
| 139 | + }); |
| 140 | + |
| 141 | + it("includes overview and stats in description", () => { |
| 142 | + const entry: ReportEntry = { |
| 143 | + path: "2026/W14", |
| 144 | + week: "W14", |
| 145 | + year: "2026", |
| 146 | + title: "Big Week", |
| 147 | + subtitle: "Shipped auth refactor", |
| 148 | + overview: "This week focused on the auth refactor. The new OAuth2 flow is live.", |
| 149 | + dateLabel: "2026 W14", |
| 150 | + dateTo: "2026-04-05", |
| 151 | + stats: { commits: 42, prs: 5, reviews: 8 }, |
| 152 | + }; |
| 153 | + |
| 154 | + const feed = buildRSSFeed([entry], defaultChannel()); |
| 155 | + |
| 156 | + expect(feed).toContain("Shipped auth refactor"); |
| 157 | + expect(feed).toContain("This week focused on the auth refactor"); |
| 158 | + expect(feed).toContain("Commits: 42, PRs: 5, Reviews: 8"); |
| 159 | + }); |
| 160 | + |
| 161 | + it("includes only subtitle when overview and stats are absent", () => { |
| 162 | + const entries = [makeEntry("2026/W14", "Title", "Just a subtitle", "2026-04-05")]; |
| 163 | + const feed = buildRSSFeed(entries, defaultChannel()); |
| 164 | + |
| 165 | + expect(feed).toContain("Just a subtitle"); |
| 166 | + expect(feed).not.toContain("Commits:"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("includes channel image with site OG image", () => { |
| 170 | + const feed = buildRSSFeed([], defaultChannel({ link: "https://example.com" })); |
| 171 | + |
| 172 | + expect(feed).toContain("<image>"); |
| 173 | + expect(feed).toContain("<url>https://example.com/og.png</url>"); |
| 174 | + expect(feed).toContain("<width>1200</width>"); |
| 175 | + expect(feed).toContain("<height>630</height>"); |
| 176 | + }); |
| 177 | + |
| 178 | + it("includes media RSS namespace", () => { |
| 179 | + const feed = buildRSSFeed([], defaultChannel()); |
| 180 | + |
| 181 | + expect(feed).toContain('xmlns:media="http://search.yahoo.com/mrss/"'); |
| 182 | + }); |
| 183 | + |
| 184 | + it("returns empty items for empty entries", () => { |
| 185 | + const feed = buildRSSFeed([], defaultChannel()); |
| 186 | + |
| 187 | + expect(feed).toContain("<channel>"); |
| 188 | + expect(feed).not.toContain("<item>"); |
| 189 | + }); |
| 190 | +}); |
0 commit comments