- Draft: true and a date in the future keep a post out of the pages, the lists, the categories and the feed. motorino build -drafts (next commit) shows them. - news_language in site.json, and an optional Language key in a post, choose the language of the news pages and of the interface strings they use. The news still exist once, not once per language. - New header keys Mastodon (an https address), Draft and Language; a post knows its reading time and whether its description was written. Refs #9
297 lines
12 KiB
Go
297 lines
12 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)
|
|
}
|
|
}
|
|
|
|
// postFile returns a news file with the given extra header lines.
|
|
func postFile(extra string) string {
|
|
return "Title: A post\nDate: 2026-11-14T12:00:00+0100\n" + extra + "-\nOne two three four five.\n"
|
|
}
|
|
|
|
// loadOne loads a single news file and returns its post.
|
|
func loadOne(t *testing.T, file string) (Post, error) {
|
|
t.Helper()
|
|
posts, err := LoadPosts(writeFiles(t, map[string]string{"news/a-post.md": file}))
|
|
if err != nil {
|
|
return Post{}, err
|
|
}
|
|
return posts[0], nil
|
|
}
|
|
|
|
// TestPostMastodon checks the optional reply address and its validation.
|
|
func TestPostMastodon(t *testing.T) {
|
|
p, err := loadOne(t, postFile("Mastodon: https://social.kmalloc.org/@squid/115000000000000000\n"))
|
|
if err != nil || p.Mastodon != "https://social.kmalloc.org/@squid/115000000000000000" {
|
|
t.Fatalf("Mastodon = %q, err = %v", p.Mastodon, err)
|
|
}
|
|
if p, _ = loadOne(t, postFile("")); p.Mastodon != "" {
|
|
t.Errorf("a post without the key has Mastodon %q", p.Mastodon)
|
|
}
|
|
for _, bad := range []string{"http://social.kmalloc.org/@squid/1", "not an address", "https://", "https://a b/c", "javascript:alert(1)"} {
|
|
if _, err := loadOne(t, postFile("Mastodon: "+bad+"\n")); err == nil || !strings.Contains(err.Error(), "Mastodon") {
|
|
t.Errorf("Mastodon %q: error = %v, want one about Mastodon", bad, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestPostDraft checks the Draft key.
|
|
func TestPostDraft(t *testing.T) {
|
|
for value, want := range map[string]bool{"true": true, "True": true, "yes": true, "false": false, "no": false} {
|
|
p, err := loadOne(t, postFile("Draft: "+value+"\n"))
|
|
if err != nil || p.Draft != want {
|
|
t.Errorf("Draft %q: got %v, err %v, want %v", value, p.Draft, err, want)
|
|
}
|
|
}
|
|
if p, _ := loadOne(t, postFile("")); p.Draft {
|
|
t.Error("a post without Draft must not be a draft")
|
|
}
|
|
if _, err := loadOne(t, postFile("Draft: maybe\n")); err == nil || !strings.Contains(err.Error(), "Draft") {
|
|
t.Errorf("Draft maybe: error = %v, want one about Draft", err)
|
|
}
|
|
}
|
|
|
|
// TestPostVisible checks that drafts and future dates stay hidden.
|
|
func TestPostVisible(t *testing.T) {
|
|
now, _ := time.Parse("2006-01-02T15:04:05-0700", "2026-11-15T12:00:00+0100")
|
|
past, _ := loadOne(t, postFile(""))
|
|
draft, _ := loadOne(t, postFile("Draft: true\n"))
|
|
future, _ := loadOne(t, "Title: Later\nDate: 2026-12-01T09:00:00+0100\n-\nSoon.\n")
|
|
if !past.Visible(now) || draft.Visible(now) || future.Visible(now) {
|
|
t.Errorf("Visible: past %v draft %v future %v, want true false false", past.Visible(now), draft.Visible(now), future.Visible(now))
|
|
}
|
|
if !future.Visible(future.Date) {
|
|
t.Error("a post is visible from the moment of its date")
|
|
}
|
|
}
|
|
|
|
// TestPostHasDescription tells a written description from the fallback.
|
|
func TestPostHasDescription(t *testing.T) {
|
|
written, _ := loadOne(t, postFile("Description: Written by hand.\n"))
|
|
fallback, _ := loadOne(t, postFile(""))
|
|
if !written.HasDescription || written.Description != "Written by hand." {
|
|
t.Errorf("written = %+v", written)
|
|
}
|
|
if fallback.HasDescription || fallback.Description != "One two three four five." {
|
|
t.Errorf("fallback: HasDescription %v, Description %q", fallback.HasDescription, fallback.Description)
|
|
}
|
|
}
|
|
|
|
// TestPostReadingMinutes checks the word count and the reading time.
|
|
func TestPostReadingMinutes(t *testing.T) {
|
|
p, _ := loadOne(t, postFile(""))
|
|
if p.Words != 5 || p.ReadingMinutes() != 1 {
|
|
t.Errorf("Words %d, ReadingMinutes %d, want 5 and 1", p.Words, p.ReadingMinutes())
|
|
}
|
|
long := strings.Repeat("word ", 401) + "\n"
|
|
p, _ = loadOne(t, "Title: Long\nDate: 2026-11-14T12:00:00+0100\n-\n"+long)
|
|
if p.Words != 401 || p.ReadingMinutes() != 3 {
|
|
t.Errorf("Words %d, ReadingMinutes %d, want 401 and 3", p.Words, p.ReadingMinutes())
|
|
}
|
|
if (Post{}).ReadingMinutes() != 1 {
|
|
t.Error("an empty post still reads in 1 minute")
|
|
}
|
|
}
|
|
|
|
// TestPostLanguage checks the optional Language key and its validation.
|
|
func TestPostLanguage(t *testing.T) {
|
|
p, err := loadOne(t, postFile("Language: it\n"))
|
|
if err != nil || p.Language != "it" {
|
|
t.Fatalf("Language = %q, err = %v", p.Language, err)
|
|
}
|
|
if p, _ = loadOne(t, postFile("")); p.Language != "" {
|
|
t.Errorf("a post without the key has Language %q, want empty (the builder gives it the language of the news)", p.Language)
|
|
}
|
|
for _, bad := range []string{"Italian", "IT", "i", "it_IT", "it fr"} {
|
|
if _, err := loadOne(t, postFile("Language: "+bad+"\n")); err == nil || !strings.Contains(err.Error(), "Language") {
|
|
t.Errorf("Language %q: error = %v, want one about Language", bad, err)
|
|
}
|
|
}
|
|
}
|