motorino/internal/content/posts_test.go
tgiachi 917683afa4 fix: close the minor findings of the first review
Require refuses an empty value. Heading ids: an explicit {#id} must be sound
and unique, generated ids avoid the explicit ones and each other, and a heading
with no ASCII letters gets a fallback. Fenced code blocks keep their
placeholders and a backslash keeps one anywhere. The em dash rule covers the
header of the manifesto pages and the HTML entities. The feed makes src links
absolute, base_url is required, and the news page must be a folder index. A
category written twice in a post counts once, a block name in a news title is no
longer taken for a placeholder, and build refuses extra arguments.
2026-11-15 10:32:19 +01:00

192 lines
7.2 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package content
import (
"strings"
"testing"
"time"
)
// TestPostPath checks that the month comes from the date as written.
func TestPostPath(t *testing.T) {
tests := []struct {
date, slug, want string
}{
{"2026-11-14T12:00:00+0100", "shop", "/news/2026/11/shop.html"},
{"2026-10-31T23:30:00-0500", "x", "/news/2026/10/x.html"},
{"2026-09-18T17:00:45+0200", "gnu-linux-day-2026", "/news/2026/09/gnu-linux-day-2026.html"},
}
for _, tt := range tests {
t.Run(tt.date, func(t *testing.T) {
date, err := time.Parse("2006-01-02T15:04:05-0700", tt.date)
if err != nil {
t.Fatal(err)
}
if got := PostPath(date, tt.slug); got != tt.want {
t.Errorf("PostPath() = %q, want %q", got, tt.want)
}
})
}
}
// ownerSample is the example header the owner gave for the news.
const ownerSample = "Title: GNU/Linux Day 2026\nAuthor: Matteo Bini\nDate: 2026-09-18T17:00:45+0200\n" +
"Categories: Linux Day, FLUG\n-\nThe programme is out. See you there.\n"
// TestLoadPostsOwnerSample checks every field of the owner's example.
func TestLoadPostsOwnerSample(t *testing.T) {
dir := writeFiles(t, map[string]string{"news/gnu-linux-day-2026.md": ownerSample})
posts, err := LoadPosts(dir)
if err != nil {
t.Fatalf("LoadPosts() error = %v", err)
}
if len(posts) != 1 {
t.Fatalf("got %d posts, want 1", len(posts))
}
p := posts[0]
if p.Title != "GNU/Linux Day 2026" || p.Author != "Matteo Bini" || p.Slug != "gnu-linux-day-2026" ||
p.Path != "/news/2026/09/gnu-linux-day-2026.html" || p.Source != "news/gnu-linux-day-2026.md" {
t.Errorf("post = %+v", p)
}
if got := p.Date.Format("2006-01-02"); got != "2026-09-18" {
t.Errorf("Date = %s, want 2026-09-18", got)
}
if len(p.Categories) != 2 || p.Categories[0] != (Category{"Linux Day", "linux-day"}) || p.Categories[1] != (Category{"FLUG", "flug"}) {
t.Errorf("Categories = %+v", p.Categories)
}
if p.Description != "The programme is out." {
t.Errorf("Description = %q, want the first sentence of the body", p.Description)
}
}
// TestLoadPostsDescription checks that an explicit Description wins and that
// the fallback drops the Markdown markup.
func TestLoadPostsDescription(t *testing.T) {
dir := writeFiles(t, map[string]string{
"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nDescription: Chosen.\n-\nIgnored. Text.\n",
"news/b.md": "Title: B\nDate: 2026-01-02T10:00:00+0100\n-\nThis is **bold** and [a link](https://x.org/). More.\n",
})
posts, err := LoadPosts(dir)
if err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, p := range posts {
got[p.Slug] = p.Description
}
if got["a"] != "Chosen." {
t.Errorf("a Description = %q", got["a"])
}
if got["b"] != "This is bold and a link." {
t.Errorf("b Description = %q", got["b"])
}
}
// TestLoadPostsSortedNewestFirst checks the order and the tie break.
func TestLoadPostsSortedNewestFirst(t *testing.T) {
dir := writeFiles(t, map[string]string{
"news/old.md": "Title: Old\nDate: 2026-01-01T10:00:00+0100\n-\nx\n",
"news/new.md": "Title: New\nDate: 2026-03-01T10:00:00+0100\n-\nx\n",
"news/mid1.md": "Title: M1\nDate: 2026-02-01T10:00:00+0100\n-\nx\n",
"news/mid2.md": "Title: M2\nDate: 2026-02-01T10:00:00+0100\n-\nx\n",
})
posts, err := LoadPosts(dir)
if err != nil {
t.Fatal(err)
}
var order []string
for _, p := range posts {
order = append(order, p.Slug)
}
if got, want := strings.Join(order, ","), "new,mid1,mid2,old"; got != want {
t.Errorf("order = %s, want %s", got, want)
}
}
// TestLoadPostsDateLayouts checks both accepted spellings of the offset.
func TestLoadPostsDateLayouts(t *testing.T) {
dir := writeFiles(t, map[string]string{
"news/a.md": "Title: A\nDate: 2026-05-01T10:00:00+02:00\n-\nx\n",
"news/b.md": "Title: B\nDate: 2026-05-02T10:00:00Z\n-\nx\n",
})
if _, err := LoadPosts(dir); err != nil {
t.Errorf("LoadPosts() error = %v", err)
}
}
// TestLoadPostsErrors checks the failures that must name the file.
func TestLoadPostsErrors(t *testing.T) {
tests := []struct {
name string
files map[string]string
want []string
}{
{"em dash", map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\n-\nWords — more.\n"}, []string{"news/a.md"}},
{"bad date", map[string]string{"news/a.md": "Title: A\nDate: yesterday\n-\nx\n"}, []string{"news/a.md", "Date"}},
{"missing date", map[string]string{"news/a.md": "Title: A\n-\nx\n"}, []string{"news/a.md", "Date"}},
{"unknown key", map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nTitolo: A\n-\nx\n"}, []string{"news/a.md", "Titolo"}},
{"bad file name", map[string]string{"news/Not A Slug.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\n-\nx\n"}, []string{"Not A Slug.md"}},
{"empty category slug", map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nCategories: Ok, +++\n-\nx\n"}, []string{"news/a.md", "+++"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := LoadPosts(writeFiles(t, tt.files))
if err == nil {
t.Fatal("LoadPosts() error = nil")
}
for _, want := range tt.want {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not contain %q", err, want)
}
}
})
}
}
// TestLoadPostsDuplicateSlugSameMonth checks that two posts cannot share an address.
func TestLoadPostsDuplicateSlugSameMonth(t *testing.T) {
// Same slug cannot come from two files, so the clash is built with a slug
// that two different months would not share but a repeated date would:
// the check is on the final path, so an equal path is the failure.
posts := []Post{
{Slug: "same", Path: "/news/2026/11/same.html", Source: "news/a.md"},
{Slug: "same", Path: "/news/2026/11/same.html", Source: "news/b.md"},
}
err := checkPaths(posts)
if err == nil || !strings.Contains(err.Error(), "news/a.md") || !strings.Contains(err.Error(), "news/b.md") {
t.Errorf("checkPaths() = %v, want an error naming both files", err)
}
}
// TestLoadPostsCategoryCollision checks that two names with the same slug fail.
func TestLoadPostsCategoryCollision(t *testing.T) {
dir := writeFiles(t, map[string]string{
"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nCategories: C\n-\nx\n",
"news/b.md": "Title: B\nDate: 2026-01-02T10:00:00+0100\nCategories: C++\n-\nx\n",
})
_, err := LoadPosts(dir)
if err == nil || !strings.Contains(err.Error(), "C++") || !strings.Contains(err.Error(), "\"C\"") {
t.Errorf("LoadPosts() error = %v, want it to name both C and C++", err)
}
}
// TestLoadPostsRepeatedCategory checks that a category written twice counts once.
func TestLoadPostsRepeatedCategory(t *testing.T) {
dir := writeFiles(t, map[string]string{"news/a.md": "Title: A\nDate: 2026-01-01T10:00:00+0100\nCategories: News, Linux, news, News\n-\nx\n"})
posts, err := LoadPosts(dir)
if err != nil {
t.Fatal(err)
}
if len(posts[0].Categories) != 2 || posts[0].Categories[0].Name != "News" || posts[0].Categories[1].Name != "Linux" {
t.Errorf("Categories = %+v, want News and Linux once each", posts[0].Categories)
}
}
// TestLoadPostsNoNewsFolder checks that a site without news is fine.
func TestLoadPostsNoNewsFolder(t *testing.T) {
posts, err := LoadPosts(t.TempDir())
if err != nil || len(posts) != 0 {
t.Errorf("LoadPosts() = %v, %v, want no posts and no error", posts, err)
}
}