// 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 ", "A summary & more.", "FLUG", "Linux Day"), HTML: `

See people.

`}, {Post: testPost("old", "2026-11-12", "Old", ""), HTML: "

Old text.

"}, {Post: testPost("older", "2026-11-10", "Older", ""), HTML: "

Older text.

"}, } got := feed("https://kmalloc.org", feedLang(), "/news/", items, 2) for _, want := range []string{ "kmalloc.org: News", "https://kmalloc.org/news/", "The news & more.", "en", ``, "New <post>", "https://kmalloc.org/news/2026/11/new.html", `https://kmalloc.org/news/2026/11/new.html`, "Sat, 14 Nov 2026 12:00:00 +0100", "Sat, 14 Nov 2026 12:00:00 +0100", `xmlns:content="http://purl.org/rss/1.0/modules/content/"`, "A summary & more.", `See people.

]]>
`, "FLUG", "Linux Day", } { 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) } } // TestFeedCarriesTheFullTextAsContent checks that the text is in content:encoded and the // description stays a plain summary, and that the end marker of a CDATA section in a text // does not close it. func TestFeedCarriesTheFullTextAsContent(t *testing.T) { post := testPost("a", "2026-11-14", "A", "Short.") post.Author = "Matteo Bini" items := []renderedPost{{Post: post, HTML: "
a ]]> b
\n

Text.

"}} got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5) mustContain(t, "feed", got, "Short.", "Matteo Bini", `xmlns:dc="http://purl.org/dc/elements/1.1/"`, "
a ]]]]> b
") if strings.Contains(got, "<") || strings.Contains(got, "<pre>") { t.Errorf("the description holds markup:\n%s", got) } var parsed struct { Channel struct { Items []struct { Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` } `xml:"item"` } `xml:"channel"` } if err := xml.Unmarshal([]byte(got), &parsed); err != nil { t.Fatalf("feed is not well-formed XML: %v", err) } if len(parsed.Channel.Items) != 1 || parsed.Channel.Items[0].Content != "
a ]]> b
\n

Text.

" { t.Errorf("content:encoded = %+v, want the text back as written", parsed.Channel.Items) } } // 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: ` p`}} 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, "") { 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) } }