motorino/internal/build/feed_test.go
tgiachi 4ab8f8d122
All checks were successful
check / check (pull_request) Successful in 30s
fix: find code blocks with the Markdown parser and make the escape work everywhere
The hand written fence scanner took a line that starts with a code span for a
fence and missed fences in quotes and lists. The code blocks are now the ones
goldmark finds. A backslash before a placeholder is turned into a marker that
survives the Markdown and the list expansion, so it works in a code span and
next to a block name, and two backslashes are a literal one. The em dash rule
reports the line of the body, refuses padded entities and checks the header keys
by name, the feed leaves protocol-relative links alone, base_url needs a host
and a language file cannot set it.
2026-11-15 10:38:56 +01:00

71 lines
2.8 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"encoding/xml"
"strings"
"testing"
"git.kmalloc.org/squid/motorino/internal/site"
)
// feedLang is the language the feed header is built from.
func feedLang() site.Lang {
return site.Lang{Code: "en", Strings: map[string]string{"news_p": "The news & more."}, Nav: map[string]string{"news": "News"}}
}
// TestFeedItems checks the channel, the item fields, the limit and the dates.
func TestFeedItems(t *testing.T) {
items := []renderedPost{
{Post: testPost("new", "2026-11-14", "New <post>", ""), HTML: `<p>See <a href="/people.html">people</a>.</p>`},
{Post: testPost("old", "2026-11-12", "Old", ""), HTML: "<p>Old text.</p>"},
{Post: testPost("older", "2026-11-10", "Older", ""), HTML: "<p>Older text.</p>"},
}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 2)
for _, want := range []string{
"<title>kmalloc.org: News</title>",
"<link>https://kmalloc.org/news/</link>",
"<description>The news &amp; more.</description>",
"<language>en</language>",
`<atom:link href="https://kmalloc.org/news/feed.xml" rel="self" type="application/rss+xml"/>`,
"<title>New &lt;post&gt;</title>",
"<link>https://kmalloc.org/news/2026/11/new.html</link>",
`<guid isPermaLink="true">https://kmalloc.org/news/2026/11/new.html</guid>`,
"<pubDate>Sat, 14 Nov 2026 12:00:00 +0100</pubDate>",
"<lastBuildDate>Sat, 14 Nov 2026 12:00:00 +0100</lastBuildDate>",
`&lt;a href="https://kmalloc.org/people.html"&gt;`,
} {
if !strings.Contains(got, want) {
t.Errorf("feed does not contain %q\n%s", want, got)
}
}
if strings.Contains(got, "older.html") {
t.Error("feed has a third item but the limit is 2")
}
if err := xml.Unmarshal([]byte(got), new(any)); err != nil {
t.Errorf("feed is not well-formed XML: %v", err)
}
}
// TestFeedLeavesProtocolRelativeLinks checks that "//host/x" is not given the site address.
func TestFeedLeavesProtocolRelativeLinks(t *testing.T) {
items := []renderedPost{{Post: testPost("a", "2026-11-14", "A", ""), HTML: `<img src="//cdn.example/x.png"> <a href="/p.html">p</a>`}}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
mustContain(t, "feed", got, `src="//cdn.example/x.png"`, `href="https://kmalloc.org/p.html"`)
if strings.Contains(got, "https://kmalloc.org//cdn") {
t.Error("a protocol-relative link got the site address")
}
}
// TestFeedWithoutPosts checks that an empty feed is still a valid channel.
func TestFeedWithoutPosts(t *testing.T) {
got := feed("https://kmalloc.org", feedLang(), "/news/", nil, 20)
if strings.Contains(got, "lastBuildDate") || strings.Contains(got, "<item>") {
t.Errorf("empty feed has dates or items:\n%s", got)
}
if err := xml.Unmarshal([]byte(got), new(any)); err != nil {
t.Errorf("empty feed is not well-formed XML: %v", err)
}
}