motorino/internal/build/news_test.go

107 lines
3.8 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"strings"
"testing"
"time"
"git.kmalloc.org/squid/motorino/internal/content"
)
// testPost returns a post dated on the given day (noon, +0100).
func testPost(slug, day, title, description string, cats ...string) content.Post {
date, err := time.Parse("2006-01-02T15:04:05-0700", day+"T12:00:00+0100")
if err != nil {
panic(err)
}
var categories []content.Category
for _, name := range cats {
slugName, _ := content.CategorySlug(name)
categories = append(categories, content.Category{Name: name, Slug: slugName})
}
return content.Post{
Slug: slug, Path: content.PostPath(date, slug), Title: title, Description: description,
Date: date, Categories: categories, Source: "news/" + slug + ".md",
}
}
// TestNewsListMarksEnglishOnItalian checks the hreflang mark on other languages.
func TestNewsListMarksEnglishOnItalian(t *testing.T) {
posts := []content.Post{testPost("shop", "2026-11-14", "A shop <new>", "About the shop.")}
en := newsList(posts, "en")
it := newsList(posts, "it")
want := `<li><time datetime="2026-11-14">2026-11-14</time> <a href="/news/2026/11/shop.html">A shop &lt;new&gt;</a>: About the shop.</li>`
if !strings.Contains(en, want) {
t.Errorf("en list %q does not contain %q", en, want)
}
if !strings.Contains(it, `<a href="/news/2026/11/shop.html" hreflang="en" lang="en">`) {
t.Errorf("it list %q lacks the hreflang mark", it)
}
if !strings.HasPrefix(en, "<ul>\n") || !strings.HasSuffix(en, "\n</ul>") {
t.Errorf("list is not wrapped in <ul>: %q", en)
}
}
// TestNewsListWithoutDescription checks that an empty description adds no colon.
func TestNewsListWithoutDescription(t *testing.T) {
got := newsList([]content.Post{testPost("a", "2026-01-01", "Title", "")}, "en")
if strings.Contains(got, ": ") {
t.Errorf("list %q has a colon but no description", got)
}
}
// TestNewsListEmpty checks that no posts give an empty string.
func TestNewsListEmpty(t *testing.T) {
if got := newsList(nil, "en"); got != "" {
t.Errorf("newsList(nil) = %q, want empty", got)
}
}
// TestLatestNewsLimit checks that only the first n posts are listed.
func TestLatestNewsLimit(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", ""), testPost("b", "2026-02-01", "B", ""), testPost("c", "2026-01-01", "C", ""),
}
got := latestNews(posts, "en", 2)
if strings.Count(got, "<li>") != 2 || strings.Contains(got, ">C<") || strings.Contains(got, "<ul>") {
t.Errorf("latestNews() = %q, want two items and no <ul>", got)
}
}
// TestCategoriesListCountsAndSorts checks the category index.
func TestCategoriesListCountsAndSorts(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", "", "Linux Day", "FLUG"),
testPost("b", "2026-02-01", "B", "", "FLUG"),
testPost("c", "2026-01-01", "C", "", "News"),
}
html, cats := categoriesList(posts)
if len(cats) != 3 || cats[0].Name != "FLUG" || cats[1].Name != "Linux Day" || cats[2].Name != "News" {
t.Errorf("categories = %+v, want FLUG, Linux Day, News", cats)
}
for _, want := range []string{
`<a href="/news/category/flug.html">FLUG</a> (2)`,
`<a href="/news/category/linux-day.html">Linux Day</a> (1)`,
} {
if !strings.Contains(html, want) {
t.Errorf("html %q does not contain %q", html, want)
}
}
if emptyHTML, emptyCats := categoriesList(nil); emptyHTML != "" || len(emptyCats) != 0 {
t.Errorf("categoriesList(nil) = %q, %v, want empty", emptyHTML, emptyCats)
}
}
// TestPostsIn checks that posts are picked by category address.
func TestPostsIn(t *testing.T) {
posts := []content.Post{
testPost("a", "2026-03-01", "A", "", "FLUG"),
testPost("b", "2026-02-01", "B", "", "News"),
}
got := postsIn(posts, "flug")
if len(got) != 1 || got[0].Slug != "a" {
t.Errorf("postsIn(flug) = %+v, want only post a", got)
}
}