motorino/internal/build/feed_test.go
tgiachi 3f62c79603 feat(feed): carry the full text in content:encoded
The description of an item is now the plain summary of the post, the full text with
absolute links goes in content:encoded (the end marker of a CDATA section inside
the text is split), and an item has its categories and its author (dc:creator).

Refs #9
2026-11-16 12:09:03 +01:00

103 lines
4.3 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>", "A summary & more.", "FLUG", "Linux Day"), 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>",
`xmlns:content="http://purl.org/rss/1.0/modules/content/"`,
"<description>A summary &amp; more.</description>",
`<content:encoded><![CDATA[<p>See <a href="https://kmalloc.org/people.html">people</a>.</p>]]></content:encoded>`,
"<category>FLUG</category>",
"<category>Linux Day</category>",
} {
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: "<pre>a ]]> b</pre>\n<p>Text.</p>"}}
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
mustContain(t, "feed", got, "<description>Short.</description>", "<dc:creator>Matteo Bini</dc:creator>",
`xmlns:dc="http://purl.org/dc/elements/1.1/"`, "<pre>a ]]]]><![CDATA[> b</pre>")
if strings.Contains(got, "<description><") || strings.Contains(got, "&lt;pre&gt;") {
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 != "<pre>a ]]> b</pre>\n<p>Text.</p>" {
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: `<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)
}
}