motorino/internal/build/feed_test.go

61 lines
2.2 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)
}
}
// 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)
}
}