|
| 1 | +import { JSDOM } from 'jsdom'; |
| 2 | +import { site } from 'virtual:hoverboard/site'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | +import type { Post } from '../../models/post'; |
| 5 | +import { GET } from './rss.xml'; |
| 6 | + |
| 7 | +const posts = [ |
| 8 | + { |
| 9 | + id: 'speakers & more', |
| 10 | + title: 'Speakers <announced>', |
| 11 | + brief: 'Meet them', |
| 12 | + content: 'Meet **them**.<script>alert(1)</script>', |
| 13 | + published: '2026-10-08', |
| 14 | + }, |
| 15 | + { |
| 16 | + id: 'launch', |
| 17 | + title: 'Launch', |
| 18 | + brief: 'It is on', |
| 19 | + content: 'Only the start', |
| 20 | + source: '/data/posts/launch.md', |
| 21 | + published: '2026-09-01', |
| 22 | + }, |
| 23 | +] as Post[]; |
| 24 | + |
| 25 | +vi.mock('../../data/content', () => ({ loadContent: async () => ({ blog: posts }) })); |
| 26 | +vi.mock('virtual:hoverboard/markdown', () => ({ |
| 27 | + posts: { 'launch.md': 'See [the schedule](/schedule) and .' }, |
| 28 | +})); |
| 29 | + |
| 30 | +const feed = async () => (await GET({} as never)).text(); |
| 31 | + |
| 32 | +const contents = async () => { |
| 33 | + const { document } = new JSDOM(await feed(), { contentType: 'text/xml' }).window; |
| 34 | + return [...document.getElementsByTagName('content:encoded')].map((item) => item.textContent); |
| 35 | +}; |
| 36 | + |
| 37 | +describe('blog RSS feed', () => { |
| 38 | + it('lists every post with an absolute link and its date', async () => { |
| 39 | + const xml = await feed(); |
| 40 | + |
| 41 | + expect(xml).toContain(`<link>${new URL('blog', site.url).href}</link>`); |
| 42 | + expect(xml).toContain(`<link>${new URL('blog/launch', site.url).href}</link>`); |
| 43 | + expect(xml).toContain('<pubDate>Tue, 01 Sep 2026 00:00:00 GMT</pubDate>'); |
| 44 | + expect(xml).toContain('<description>It is on</description>'); |
| 45 | + expect(xml.match(/<item>/g)).toHaveLength(2); |
| 46 | + }); |
| 47 | + |
| 48 | + it('escapes post titles and links', async () => { |
| 49 | + const xml = await feed(); |
| 50 | + |
| 51 | + expect(xml).toContain('<title>Speakers <announced></title>'); |
| 52 | + expect(xml).toContain(`${new URL('blog/speakers%20', site.url).href}&%20more</link>`); |
| 53 | + }); |
| 54 | + |
| 55 | + it('sets the language to the site locale', async () => { |
| 56 | + expect(await feed()).toContain(`<language>${site.locales.source}</language>`); |
| 57 | + }); |
| 58 | + |
| 59 | + it('has the whole post as sanitized HTML', async () => { |
| 60 | + expect(await feed()).toContain('xmlns:content="http://purl.org/rss/1.0/modules/content/"'); |
| 61 | + expect((await contents())[0]).toBe('<p>Meet <strong>them</strong>.</p>\n'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('reads the post from its source file, with absolute links and images', async () => { |
| 65 | + const content = (await contents())[1]; |
| 66 | + |
| 67 | + expect(content).toContain(`<a href="${new URL('schedule', site.url).href}"`); |
| 68 | + expect(content).toContain(`<img src="${new URL('images/hall.jpg', site.url).href}"`); |
| 69 | + expect(content).not.toContain('Only the start'); |
| 70 | + }); |
| 71 | +}); |
0 commit comments