- groupArchive compared each post with the last group only, so two posts near midnight
at the end of a month, written with different offsets, listed the same month twice and
the build failed with "both published as". Posts are grouped by their own year and
month, newest first.
- {{news_archive}} is empty when there are no posts, so the index says there is no news
once, and the heading of an empty {{news_archive}} or {{categories_list}} goes with it.
- tests for exactly ten posts, eleven posts, a single post, a title with quotes, an
ampersand, a tag and non Latin letters, and a neighbour written in another language
Refs #9
706 lines
33 KiB
Go
706 lines
33 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fixedNow is the moment the blog tests build at, so that drafts and future
|
|
// dates do not depend on the clock.
|
|
var fixedNow = time.Date(2026, 11, 15, 12, 0, 0, 0, time.FixedZone("CET", 3600))
|
|
|
|
// withFiles returns the base site with files added or replaced; an empty value removes the file.
|
|
func withFiles(extra map[string]string) map[string]string {
|
|
files := baseFiles()
|
|
for name, content := range extra {
|
|
if content == "" {
|
|
delete(files, name)
|
|
continue
|
|
}
|
|
files[name] = content
|
|
}
|
|
return files
|
|
}
|
|
|
|
// buildWith builds the site made of files with the options changed by tune.
|
|
func buildWith(t *testing.T, files map[string]string, tune func(*Options)) (string, error) {
|
|
t.Helper()
|
|
out := filepath.Join(t.TempDir(), "public")
|
|
opts := Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3", Now: fixedNow}
|
|
if tune != nil {
|
|
tune(&opts)
|
|
}
|
|
_, err := Build(opts)
|
|
return out, err
|
|
}
|
|
|
|
// exists reports whether the output has the file rel.
|
|
func exists(t *testing.T, out, rel string) bool {
|
|
t.Helper()
|
|
_, err := readOutMaybe(out, rel)
|
|
return err == nil
|
|
}
|
|
|
|
// postNews returns the news files used by the draft and future tests.
|
|
func postNews() map[string]string {
|
|
return map[string]string{
|
|
"news/a-draft.md": "Title: A draft\nDate: 2026-11-10T12:00:00+0100\nCategories: Secret\nDraft: true\n-\nNot yet.\n",
|
|
"news/in-future.md": "Title: In the future\nDate: 2026-12-24T12:00:00+0100\nCategories: Later\n-\nSoon.\n",
|
|
}
|
|
}
|
|
|
|
// TestDraftsAndFutureStayHidden checks that a draft and a post dated in the
|
|
// future are not published anywhere, and that no category exists only for them.
|
|
func TestDraftsAndFutureStayHidden(t *testing.T) {
|
|
out, err := buildWith(t, withFiles(postNews()), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
for _, rel := range []string{"news/2026/11/a-draft.html", "news/2026/12/in-future.html", "news/category/secret.html", "news/category/later.html"} {
|
|
if exists(t, out, rel) {
|
|
t.Errorf("%s is published, but the post is hidden", rel)
|
|
}
|
|
}
|
|
for _, rel := range []string{"news/index.html", "news/feed.xml", "index.html"} {
|
|
got := readOut(t, out, rel)
|
|
for _, hidden := range []string{"A draft", "In the future", "Secret", "Later"} {
|
|
if strings.Contains(got, hidden) {
|
|
t.Errorf("%s mentions %q, but the post is hidden", rel, hidden)
|
|
}
|
|
}
|
|
}
|
|
mustContain(t, "news index", readOut(t, out, "news/index.html"), "Status page")
|
|
}
|
|
|
|
// TestDraftsFlagShowsHiddenPosts checks the preview build.
|
|
func TestDraftsFlagShowsHiddenPosts(t *testing.T) {
|
|
out, err := buildWith(t, withFiles(postNews()), func(o *Options) { o.Drafts = true })
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
for _, rel := range []string{"news/2026/11/a-draft.html", "news/2026/12/in-future.html", "news/category/secret.html"} {
|
|
if !exists(t, out, rel) {
|
|
t.Errorf("%s is missing from a build with drafts", rel)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestFuturePostAppearsWhenItsDateComes checks the moment of publication.
|
|
func TestFuturePostAppearsWhenItsDateComes(t *testing.T) {
|
|
later := time.Date(2026, 12, 24, 12, 0, 0, 0, time.FixedZone("CET", 3600))
|
|
out, err := buildWith(t, withFiles(postNews()), func(o *Options) { o.Now = later })
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if !exists(t, out, "news/2026/12/in-future.html") {
|
|
t.Error("a post is published from the moment of its date")
|
|
}
|
|
if exists(t, out, "news/2026/11/a-draft.html") {
|
|
t.Error("a draft stays hidden whatever the date")
|
|
}
|
|
}
|
|
|
|
// TestNewsLanguage checks the language of the news and its errors.
|
|
func TestNewsLanguage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
extra map[string]string
|
|
want string
|
|
}{
|
|
{"unknown language in site.json", map[string]string{
|
|
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"news_language": "xx", "languages"`, 1)}, `news_language "xx"`},
|
|
{"no news page in that language", map[string]string{
|
|
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"news_language": "it", "languages"`, 1)}, `content/it`},
|
|
{"post in a language the site does not have", map[string]string{
|
|
"news/tedesco.md": "Title: Deutsch\nDate: 2026-11-01T12:00:00+0100\nLanguage: de\n-\nText.\n"}, `Language "de"`},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := buildWith(t, withFiles(tt.extra), nil)
|
|
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
|
t.Errorf("error = %v, want one that contains %q", err, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// monthsJSON writes the month_01..month_12 strings of a test language.
|
|
func monthsJSON(names ...string) string {
|
|
var b strings.Builder
|
|
for i, n := range names {
|
|
b.WriteString(`"month_` + string(rune('0'+(i+1)/10)) + string(rune('0'+(i+1)%10)) + `": "` + n + `", `)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// blogLangEn and blogLangIt are the English and Italian language files with the strings the blog features need.
|
|
var (
|
|
blogLangEn = `{"lang_name": "English", "sections": "Sections", "home_link": "/", "nojs": "No JavaScript.",
|
|
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Version", "toc": "Contents", "history_a": "Manifesto history",
|
|
"news_p": "What is new.", "news_all": "All news", "news_empty": "No news yet.", "category_h": "Category",
|
|
"date_long": "{d} {month} {y}", "date_month_year": "{month} {y}", "reading_time": "{n} min read",
|
|
` + monthsJSON("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") + `
|
|
"post_older": "Older", "post_newer": "Newer", "post_nav": "Other news", "reply_mastodon": "Reply on Mastodon",
|
|
"read_more": "Read more", "pager_label": "News pages", "pager_newer": "Newer news", "pager_older": "Older news", "pager_page": "Page {n} of {total}",
|
|
"archive_h": "Archive", "archive_year_h": "Year", "archive_up": "Archive of the year",
|
|
"nav": {"manifesto": "Manifesto", "people": "People", "services": "Services", "news": "News"}}`
|
|
blogLangIt = `{"lang_name": "Italiano", "sections": "Sezioni", "home_link": "/it/", "nojs": "Niente JavaScript.",
|
|
"powered_by": "Powered by", "back": "kmalloc.org", "version": "Versione", "toc": "Indice", "history_a": "Cronologia",
|
|
"news_p": "Le novita.", "news_all": "Tutte le news", "news_empty": "Nessuna news.", "category_h": "Categoria",
|
|
"date_long": "{d} {month} {y}", "date_month_year": "{month} {y}", "reading_time": "{n} min di lettura",
|
|
` + monthsJSON("gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre") + `
|
|
"post_older": "Piu vecchia", "post_newer": "Piu recente", "post_nav": "Altre news", "reply_mastodon": "Rispondi su Mastodon",
|
|
"read_more": "Leggi tutto", "pager_label": "Pagine delle news", "pager_newer": "News piu recenti", "pager_older": "News piu vecchie", "pager_page": "Pagina {n} di {total}",
|
|
"archive_h": "Archivio", "archive_year_h": "Anno", "archive_up": "Archivio dell'anno",
|
|
"nav": {"manifesto": "Manifesto", "people": "Persone", "services": "Servizi", "news": "News"}}`
|
|
)
|
|
|
|
// blogPostTemplate is a post page that uses every placeholder of the blog.
|
|
const blogPostTemplate = `<h1>{{post_title}}</h1>
|
|
|
|
{{post_summary}}
|
|
{{post_meta}}
|
|
{{post_body}}
|
|
{{post_mastodon}}
|
|
{{post_nav}}
|
|
`
|
|
|
|
// blogBaseTemplate is the base template with the Open Graph placeholders.
|
|
const blogBaseTemplate = `<!doctype html>
|
|
<html lang="{{lang}}">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>{{title_html}}</title>
|
|
<meta property="og:type" content="{{og_type}}">
|
|
<meta property="og:title" content="{{title_html}}">
|
|
{{article_meta}}
|
|
<meta name="description" content="{{description_attr}}">
|
|
{{alt_links}}
|
|
<style>
|
|
{{css}}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
{{header}}
|
|
|
|
{{content}}
|
|
{{footer}}
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
// blogSite is the base site with the blog language strings and templates.
|
|
func blogSite(extra map[string]string) map[string]string {
|
|
files := map[string]string{
|
|
"lang/en.json": blogLangEn, "lang/it.json": blogLangIt,
|
|
"templates/post.html": blogPostTemplate, "templates/base.html": blogBaseTemplate,
|
|
"news/shop.md": "Title: A shop\nAuthor: squid\nDate: 2026-11-14T12:00:00+0100\nCategories: FLUG\nDescription: About the shop.\nMastodon: https://social.kmalloc.org/@squid/115\n-\nThere is a shop.\n",
|
|
}
|
|
for k, v := range extra {
|
|
files[k] = v
|
|
}
|
|
return withFiles(files)
|
|
}
|
|
|
|
// TestPostPageHasTheBlogFields checks the long date, the byline, the summary,
|
|
// the reading time, the Mastodon link and the links to the other news.
|
|
func TestPostPageHasTheBlogFields(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(nil), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
shop := readOut(t, out, "news/2026/11/shop.html")
|
|
mustContain(t, "shop post", shop,
|
|
`<p class="lede">About the shop.</p>`,
|
|
`<p class="meta"><time datetime="2026-11-14">14 November 2026</time> · squid · <a href="/news/category/flug.html">FLUG</a> · 1 min read</p>`,
|
|
`<p><a href="https://social.kmalloc.org/@squid/115" rel="noopener">Reply on Mastodon</a></p>`,
|
|
`<nav aria-label="Other news">`,
|
|
`<li><a href="/news/2026/11/status.html" rel="prev">Older: Status page</a></li>`,
|
|
`<li><a href="/news/">All news</a></li>`,
|
|
)
|
|
if strings.Contains(shop, "Newer:") {
|
|
t.Error("the newest post links to a newer one")
|
|
}
|
|
status := readOut(t, out, "news/2026/11/status.html")
|
|
mustContain(t, "status post", status,
|
|
`<p class="meta"><time datetime="2026-11-12">12 November 2026</time> · 1 min read</p>`,
|
|
`<li><a href="/news/2026/11/shop.html" rel="next">Newer: A shop</a></li>`,
|
|
`<li><a href="/news/2026/09/gnu-linux-day-2026.html" rel="prev">Older: GNU/Linux Day 2026</a></li>`,
|
|
)
|
|
for _, absent := range []string{"lede", "Reply on Mastodon"} {
|
|
if strings.Contains(status, absent) {
|
|
t.Errorf("a post without a written description or a Mastodon address shows %q", absent)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestPostPageSkipsFeaturesWithoutStrings checks that a language file without
|
|
// the optional strings gives plain pages and not an error.
|
|
func TestPostPageSkipsFeaturesWithoutStrings(t *testing.T) {
|
|
files := blogSite(map[string]string{"lang/en.json": baseFiles()["lang/en.json"], "news/shop.md": baseFiles()["news/shop.md"]})
|
|
out, err := buildWith(t, files, nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
status := readOut(t, out, "news/2026/11/status.html")
|
|
mustContain(t, "status post", status, `<time datetime="2026-11-12">2026-11-12</time>`)
|
|
for _, absent := range []string{"min read", "<nav", "Older"} {
|
|
if strings.Contains(status, absent) {
|
|
t.Errorf("without the strings the page shows %q", absent)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestMastodonNeedsItsString checks that an address with no label is an error, not a silent omission.
|
|
func TestMastodonNeedsItsString(t *testing.T) {
|
|
files := blogSite(map[string]string{"lang/en.json": baseFiles()["lang/en.json"]})
|
|
_, err := buildWith(t, files, nil)
|
|
if err == nil || !strings.Contains(err.Error(), "reply_mastodon") {
|
|
t.Errorf("error = %v, want one that names reply_mastodon", err)
|
|
}
|
|
}
|
|
|
|
// TestPostInAnotherLanguage checks that a post with Language uses the strings and the page language of that language.
|
|
func TestPostInAnotherLanguage(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{
|
|
"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
ciao := readOut(t, out, "news/2026/11/ciao.html")
|
|
mustContain(t, "italian post", ciao, `<html lang="it">`, `5 novembre 2026`, `1 min di lettura`, `<nav aria-label="Altre news">`)
|
|
list := readOut(t, out, "news/index.html")
|
|
mustContain(t, "news index", list, `hreflang="it" lang="it">Ciao</a>`)
|
|
if strings.Contains(list, `hreflang="en"`) {
|
|
t.Error("an English post on the English index is marked as another language")
|
|
}
|
|
}
|
|
|
|
// TestPostOpenGraphArticle checks og:type and the article meta on posts only.
|
|
func TestPostOpenGraphArticle(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(nil), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
mustContain(t, "post", readOut(t, out, "news/2026/09/gnu-linux-day-2026.html"),
|
|
`<meta property="og:type" content="article">`,
|
|
`<meta property="article:published_time" content="2026-09-18T17:00:45+02:00">`,
|
|
`<meta property="article:tag" content="Linux Day">`,
|
|
`<meta property="article:tag" content="FLUG">`)
|
|
for _, rel := range []string{"index.html", "news/index.html", "news/category/flug.html", "it/index.html"} {
|
|
got := readOut(t, out, rel)
|
|
mustContain(t, rel, got, `<meta property="og:type" content="website">`)
|
|
if strings.Contains(got, "article:") {
|
|
t.Errorf("%s has article meta", rel)
|
|
}
|
|
}
|
|
}
|
|
|
|
// blogIndex is a news index that uses the blog list and the archive.
|
|
const blogIndex = "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n{{news_blog}}\n\n## Archive\n\n{{news_archive}}\n"
|
|
|
|
// archiveTemplate is the page of a year or a month of the archive.
|
|
const archiveTemplate = "<h1>{{archive_h}}: {{archive_name}}</h1>\n\n{{archive_nav}}\n\n{{archive_posts}}\n"
|
|
|
|
// manyPosts adds n posts, dated one per day from 2026-10-01, to the files.
|
|
func manyPosts(files map[string]string, n int) map[string]string {
|
|
for i := 1; i <= n; i++ {
|
|
day := "2026-10-" + string(rune('0'+i/10)) + string(rune('0'+i%10))
|
|
name := "p" + string(rune('0'+i/10)) + string(rune('0'+i%10))
|
|
files["news/"+name+".md"] = "Title: Post " + name[1:] + "\nDate: " + day + "T12:00:00+0100\nCategories: Many\n-\nText of " + name + ".\n"
|
|
}
|
|
return files
|
|
}
|
|
|
|
// TestNewsBlogEntries checks one entry of the blog list and the absence of a pager for a short list.
|
|
func TestNewsBlogEntries(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
index := readOut(t, out, "news/index.html")
|
|
mustContain(t, "news index", index,
|
|
"<p class=\"note\" role=\"note\">\n"+
|
|
`<span class="meta"><time datetime="2026-11-14">14 November 2026</time> · <a href="/news/category/flug.html">FLUG</a></span><br>`+"\n"+
|
|
"<strong>A shop.</strong>\n"+
|
|
"About the shop.\n"+
|
|
`<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`+"\n</p>",
|
|
// a post with two categories, and one with a description made of its first sentence
|
|
`<a href="/news/category/linux-day.html">Linux Day</a>, <a href="/news/category/flug.html">FLUG</a></span><br>`,
|
|
`<strong>Status page.</strong>`,
|
|
)
|
|
if got := strings.Count(index, `role="note"`); got != 3 {
|
|
t.Errorf("the index has %d panels, want 3", got)
|
|
}
|
|
if strings.Contains(index, "<article") {
|
|
t.Error("the index has an article element, the panels replaced them")
|
|
}
|
|
if strings.Contains(index, "News pages") {
|
|
t.Error("a list that fits one page has a pager")
|
|
}
|
|
}
|
|
|
|
// TestNewsBlogPagination checks the pages of a long list.
|
|
func TestNewsBlogPagination(t *testing.T) {
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate})
|
|
out, err := buildWith(t, manyPosts(files, 22), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
first := readOut(t, out, "news/index.html")
|
|
mustContain(t, "page 1", first, `<nav aria-label="News pages">`, `Page 1 of 3`, `<a href="/news/page/2/" rel="next">Older news</a>`)
|
|
if strings.Contains(first, `rel="prev"`) || strings.Count(first, `role="note"`) != 10 || !strings.Contains(first, "Post 15") || strings.Contains(first, "Post 14") {
|
|
t.Errorf("page 1 is wrong:\n%s", first)
|
|
}
|
|
second := readOut(t, out, "news/page/2/index.html")
|
|
mustContain(t, "page 2", second, `Page 2 of 3`, `<a href="/news/" rel="prev">Newer news</a>`, `<a href="/news/page/3/" rel="next">Older news</a>`,
|
|
"<title>News, Page 2 of 3</title>", "Post 14", "Post 05")
|
|
if strings.Count(second, `role="note"`) != 10 || strings.Contains(second, "Post 15") || strings.Contains(second, "Post 04") {
|
|
t.Errorf("page 2 is wrong:\n%s", second)
|
|
}
|
|
third := readOut(t, out, "news/page/3/index.html")
|
|
mustContain(t, "page 3", third, `Page 3 of 3`, `<a href="/news/page/2/" rel="prev">Newer news</a>`, "GNU/Linux Day 2026")
|
|
if strings.Contains(third, `rel="next"`) || strings.Count(third, `role="note"`) != 5 {
|
|
t.Errorf("page 3 is wrong:\n%s", third)
|
|
}
|
|
if exists(t, out, "news/page/4/index.html") || exists(t, out, "news/page/1/index.html") {
|
|
t.Error("there is a page 4 or a page 1 under /news/page/")
|
|
}
|
|
robots := readOut(t, out, "robots.txt")
|
|
mustContain(t, "robots.txt", robots, "/news/page/2/")
|
|
}
|
|
|
|
// TestNewsBlogNeedsItsStrings checks that a blog list without read_more is an error.
|
|
func TestNewsBlogNeedsItsStrings(t *testing.T) {
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "lang/en.json": strings.Replace(blogLangEn, `"read_more": "Read more", `, "", 1), "templates/archive.html": archiveTemplate})
|
|
_, err := buildWith(t, files, nil)
|
|
if err == nil || !strings.Contains(err.Error(), "read_more") {
|
|
t.Errorf("error = %v, want one that names read_more", err)
|
|
}
|
|
}
|
|
|
|
// TestNewsBlogOnlyOnAnIndex checks that the blog list needs an index page.
|
|
func TestNewsBlogOnlyOnAnIndex(t *testing.T) {
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "content/en/extra.md": page("extra", "Extra", "", "{{news_blog}}\n"), "templates/archive.html": archiveTemplate})
|
|
_, err := buildWith(t, files, nil)
|
|
if err == nil || !strings.Contains(err.Error(), "index") {
|
|
t.Errorf("error = %v, want one about an index page", err)
|
|
}
|
|
}
|
|
|
|
// TestArchivePages checks the pages of a year and of a month, and the block.
|
|
func TestArchivePages(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
mustContain(t, "news index", readOut(t, out, "news/index.html"),
|
|
`<li><a href="/news/2026/">2026</a> (3)`,
|
|
`<li><a href="/news/2026/11/">November 2026</a> (2)</li>`,
|
|
`<li><a href="/news/2026/09/">September 2026</a> (1)</li>`)
|
|
year := readOut(t, out, "news/2026/index.html")
|
|
mustContain(t, "year", year, "<h1>Archive: 2026</h1>", `<a href="/news/2026/11/">November 2026</a> (2)`, "A shop", "Status page", "GNU/Linux Day 2026")
|
|
month := readOut(t, out, "news/2026/11/index.html")
|
|
mustContain(t, "month", month, "<h1>Archive: November 2026</h1>", `<a href="/news/2026/">Archive of the year 2026</a>`, "A shop", "Status page")
|
|
if strings.Contains(month, "GNU/Linux Day 2026") {
|
|
t.Error("the month of November lists a post of September")
|
|
}
|
|
if exists(t, out, "news/2026/12/index.html") || exists(t, out, "news/2025/index.html") {
|
|
t.Error("an archive page exists for a month or a year without posts")
|
|
}
|
|
}
|
|
|
|
// TestNoArchiveWithoutTemplate checks that the archive is opt in, and that its block then is an error.
|
|
func TestNoArchiveWithoutTemplate(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": strings.Replace(blogIndex, "{{news_archive}}", "", 1)}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if exists(t, out, "news/2026/index.html") {
|
|
t.Error("an archive page is built without templates/archive.html")
|
|
}
|
|
_, err = buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex}), nil)
|
|
if err == nil || !strings.Contains(err.Error(), "archive") {
|
|
t.Errorf("error = %v, want one that names the archive template", err)
|
|
}
|
|
}
|
|
|
|
// TestNewsPanelTitlePunctuation checks the full stop after the title of a panel: added, unless the title ends with one, a question mark or an exclamation mark.
|
|
func TestNewsPanelTitlePunctuation(t *testing.T) {
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
|
"news/q.md": "Title: Why a shop?\nDate: 2026-11-01T12:00:00+0100\n-\nText.\n",
|
|
"news/e.md": "Title: It is open!\nDate: 2026-11-02T12:00:00+0100\n-\nText.\n",
|
|
"news/d.md": "Title: Done.\nDate: 2026-11-03T12:00:00+0100\n-\nText.\n",
|
|
"news/a.md": "Title: Tom & Jerry\nDate: 2026-11-04T12:00:00+0100\n-\nText.\n"})
|
|
out, err := buildWith(t, files, nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
index := readOut(t, out, "news/index.html")
|
|
mustContain(t, "news index", index, "<strong>Why a shop?</strong>", "<strong>It is open!</strong>", "<strong>Done.</strong>", "<strong>Tom & Jerry.</strong>",
|
|
`aria-label="Read more: Tom & Jerry"`)
|
|
for _, wrong := range []string{"shop?.", "open!.", "Done..", "Done.</strong>."} {
|
|
if strings.Contains(index, wrong) {
|
|
t.Errorf("the index has %q", wrong)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestNewsPanelWithoutCategoriesOrDescription checks the two optional lines of a panel.
|
|
func TestNewsPanelWithoutCategoriesOrDescription(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
|
"news/bare.md": "Title: Bare\nDate: 2026-11-13T12:00:00+0100\nDescription:\n-\n"}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
mustContain(t, "news index", readOut(t, out, "news/index.html"),
|
|
"<p class=\"note\" role=\"note\">\n"+
|
|
`<span class="meta"><time datetime="2026-11-13">13 November 2026</time></span><br>`+"\n"+
|
|
"<strong>Bare.</strong>\n"+
|
|
`<a href="/news/2026/11/bare.html" aria-label="Read more: Bare">Read more</a>.`+"\n</p>")
|
|
}
|
|
|
|
// TestPanelsOnCategoryAndArchivePages checks that the lists of the categories and of the archives are panels too,
|
|
// and that the page of a post and the latest news of the home page are not.
|
|
func TestPanelsOnCategoryAndArchivePages(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
shop := "<strong>A shop.</strong>\nAbout the shop.\n" + `<a href="/news/2026/11/shop.html" aria-label="Read more: A shop">Read more</a>.`
|
|
for rel, want := range map[string]int{"news/category/flug.html": 2, "news/2026/index.html": 3, "news/2026/11/index.html": 2} {
|
|
got := readOut(t, out, rel)
|
|
if n := strings.Count(got, `role="note"`); n != want {
|
|
t.Errorf("%s has %d panels, want %d", rel, n, want)
|
|
}
|
|
if !strings.Contains(got, shop) {
|
|
t.Errorf("%s lacks the panel of the shop post", rel)
|
|
}
|
|
if strings.Contains(got, "<li><time") {
|
|
t.Errorf("%s still has the list of dates", rel)
|
|
}
|
|
}
|
|
if post := readOut(t, out, "news/2026/11/shop.html"); strings.Contains(post, `role="note"`) {
|
|
t.Error("the page of a post has a panel")
|
|
}
|
|
if home := readOut(t, out, "index.html"); strings.Contains(home, `role="note"`) || !strings.Contains(home, "<li><time") {
|
|
t.Error("the latest news of the home page changed")
|
|
}
|
|
}
|
|
|
|
// TestPanelsWithoutReadMore checks that a language without read_more gets panels whose title is the link on the
|
|
// category and archive pages, as before the panels, and an error on the blog list, which has no other link.
|
|
func TestPanelsWithoutReadMore(t *testing.T) {
|
|
withoutMore := strings.Replace(blogLangEn, `"read_more": "Read more", `, "", 1)
|
|
out, err := buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "templates/archive.html": archiveTemplate}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
for _, rel := range []string{"news/category/flug.html", "news/2026/11/index.html"} {
|
|
got := readOut(t, out, rel)
|
|
mustContain(t, rel, got, `<strong><a href="/news/2026/11/shop.html">A shop.</a></strong>`+"\nAbout the shop.\n</p>")
|
|
if strings.Contains(got, "aria-label") || strings.Contains(got, "Read more") {
|
|
t.Errorf("%s has a read more link without the string", rel)
|
|
}
|
|
}
|
|
_, err = buildWith(t, blogSite(map[string]string{"lang/en.json": withoutMore, "content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate}), nil)
|
|
if err == nil || !strings.Contains(err.Error(), "read_more") {
|
|
t.Errorf("error = %v, want one that names read_more", err)
|
|
}
|
|
}
|
|
|
|
// TestPanelOfAPostInAnotherLanguage checks that the link of a panel says the language of a post that is not in the language of the page.
|
|
func TestPanelOfAPostInAnotherLanguage(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
|
"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
index := readOut(t, out, "news/index.html")
|
|
mustContain(t, "news index", index, `<a href="/news/2026/11/ciao.html" aria-label="Read more: Ciao" hreflang="it" lang="it">Read more</a>.`)
|
|
if strings.Count(index, `hreflang="it" lang="it">Read more`) != 1 {
|
|
t.Error("an English post is marked as written in Italian")
|
|
}
|
|
}
|
|
|
|
// TestArchiveGroupsByTheDateAsWritten checks that two posts either side of midnight, written with different
|
|
// offsets, land in their own months and that no month is listed twice, whatever the order of the instants.
|
|
func TestArchiveGroupsByTheDateAsWritten(t *testing.T) {
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
|
"news/late.md": "Title: Late\nDate: 2026-11-30T23:45:00+0000\n-\nText.\n",
|
|
"news/early.md": "Title: Early\nDate: 2026-12-01T00:30:00+0100\n-\nText.\n",
|
|
"news/mid.md": "Title: Mid\nDate: 2026-11-29T12:00:00+0100\n-\nText.\n"})
|
|
out, err := buildWith(t, files, func(o *Options) { o.Now = time.Date(2026, 12, 20, 12, 0, 0, 0, time.UTC) })
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
index := readOut(t, out, "news/index.html")
|
|
mustContain(t, "news index", index, `<a href="/news/2026/">2026</a> (6)`)
|
|
dec, nov, sep := strings.Index(index, ">December 2026</a> (1)"), strings.Index(index, ">November 2026</a> (4)"), strings.Index(index, ">September 2026</a> (1)")
|
|
if dec < 0 || nov < dec || sep < nov {
|
|
t.Errorf("the months are not December, November, September, one each:\n%s", index)
|
|
}
|
|
if strings.Count(index, "November 2026</a>") != 1 {
|
|
t.Error("November is listed more than once")
|
|
}
|
|
month := readOut(t, out, "news/2026/11/index.html")
|
|
late, mid, shop := strings.Index(month, "<strong>Late.</strong>"), strings.Index(month, "<strong>Mid.</strong>"), strings.Index(month, "<strong>A shop.</strong>")
|
|
if late < 0 || mid < late || shop < mid || strings.Contains(month, "<strong>Early.</strong>") {
|
|
t.Errorf("the month of November is wrong:\n%s", month)
|
|
}
|
|
mustContain(t, "month of December", readOut(t, out, "news/2026/12/index.html"), "<strong>Early.</strong>")
|
|
}
|
|
|
|
// TestNoNewsAtAll checks that a site without posts says so once, and that the empty lists take their headings with them.
|
|
func TestNoNewsAtAll(t *testing.T) {
|
|
index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n{{news_blog}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
|
|
files := blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate,
|
|
"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""})
|
|
out, err := buildWith(t, files, nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
got := readOut(t, out, "news/index.html")
|
|
if n := strings.Count(got, "No news yet."); n != 1 {
|
|
t.Errorf("the index says there is no news %d times, want once:\n%s", n, got)
|
|
}
|
|
for _, heading := range []string{">Categories</h2>", ">Archive</h2>"} {
|
|
if strings.Contains(got, heading) {
|
|
t.Errorf("the index keeps the heading %q of an empty list", heading)
|
|
}
|
|
}
|
|
mustContain(t, "news index", got, "<h1>News</h1>")
|
|
}
|
|
|
|
// TestHomeKeepsItsHeadingWithoutNews checks that the heading before the latest news is the page's own and stays.
|
|
func TestHomeKeepsItsHeadingWithoutNews(t *testing.T) {
|
|
files := blogSite(map[string]string{"news/shop.md": "", "news/gnu-linux-day-2026.md": "", "news/status.md": ""})
|
|
out, err := buildWith(t, files, nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
mustContain(t, "home", readOut(t, out, "index.html"), `<h1 class="hero">Technology.</h1>`)
|
|
}
|
|
|
|
// TestHeadingStaysWithAListThatHasItems checks that the heading of a generated list is dropped only when the list is empty.
|
|
func TestHeadingStaysWithAListThatHasItems(t *testing.T) {
|
|
index := "Title: News\nID: news\nDescription: About News.\n-\n# News\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n"
|
|
out, err := buildWith(t, blogSite(map[string]string{"content/en/news/index.md": index, "templates/archive.html": archiveTemplate}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
mustContain(t, "news index", readOut(t, out, "news/index.html"), ">Categories</h2>", ">Archive</h2>")
|
|
}
|
|
|
|
// listFiles returns the site of the pagination tests: the three base posts and extra ones, so that n posts in all.
|
|
func listFiles(t *testing.T, n int) map[string]string {
|
|
t.Helper()
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate})
|
|
return manyPosts(files, n-3)
|
|
}
|
|
|
|
// TestExactlyOnePageOfPosts checks that ten posts make one page with no pager and no second page.
|
|
func TestExactlyOnePageOfPosts(t *testing.T) {
|
|
out, err := buildWith(t, listFiles(t, 10), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
index := readOut(t, out, "news/index.html")
|
|
if n := strings.Count(index, `role="note"`); n != 10 {
|
|
t.Errorf("the index has %d panels, want 10", n)
|
|
}
|
|
if strings.Contains(index, "News pages") || strings.Contains(index, "Page 1 of") || exists(t, out, "news/page/2/index.html") {
|
|
t.Error("ten posts have a pager or a second page")
|
|
}
|
|
}
|
|
|
|
// TestEleventhPostIsAloneOnPageTwo checks the page that holds the one post that does not fit.
|
|
func TestEleventhPostIsAloneOnPageTwo(t *testing.T) {
|
|
out, err := buildWith(t, listFiles(t, 11), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
first := readOut(t, out, "news/index.html")
|
|
mustContain(t, "page 1", first, "Page 1 of 2", `<a href="/news/page/2/" rel="next">Older news</a>`)
|
|
if n := strings.Count(first, `role="note"`); n != 10 || strings.Contains(first, "GNU/Linux Day 2026.") {
|
|
t.Errorf("page 1 has %d panels or the oldest post", n)
|
|
}
|
|
second := readOut(t, out, "news/page/2/index.html")
|
|
mustContain(t, "page 2", second, "Page 2 of 2", `<a href="/news/" rel="prev">Newer news</a>`, "<strong>GNU/Linux Day 2026.</strong>")
|
|
if n := strings.Count(second, `role="note"`); n != 1 || strings.Contains(second, `rel="next"`) {
|
|
t.Errorf("page 2 has %d panels or a link to a third page", n)
|
|
}
|
|
if exists(t, out, "news/page/3/index.html") {
|
|
t.Error("eleven posts have a third page")
|
|
}
|
|
}
|
|
|
|
// TestOnlyOnePost checks that the one post has no link to a newer or an older post anywhere, and no pager.
|
|
func TestOnlyOnePost(t *testing.T) {
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
|
"news/gnu-linux-day-2026.md": "", "news/status.md": ""})
|
|
out, err := buildWith(t, files, nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
for _, rel := range []string{"news/2026/11/shop.html", "news/index.html", "news/2026/11/index.html"} {
|
|
got := readOut(t, out, rel)
|
|
for _, absent := range []string{"Older", "Newer", `rel="prev"`, `rel="next"`, "News pages"} {
|
|
if strings.Contains(got, absent) {
|
|
t.Errorf("%s has %q, but there is one post", rel, absent)
|
|
}
|
|
}
|
|
}
|
|
mustContain(t, "the post", readOut(t, out, "news/2026/11/shop.html"), `<li><a href="/news/">All news</a></li>`)
|
|
}
|
|
|
|
// TestUnusualTitle checks that a title with quotes, an ampersand, a tag and non Latin letters is escaped everywhere.
|
|
func TestUnusualTitle(t *testing.T) {
|
|
title := `Say "hi" & <b>go</b> ünï 日本語`
|
|
files := blogSite(map[string]string{"content/en/news/index.md": blogIndex, "templates/archive.html": archiveTemplate,
|
|
"news/odd.md": "Title: " + title + "\nDate: 2026-11-15T09:00:00+0100\nCategories: Odd\n-\nText.\n"})
|
|
out, err := buildWith(t, files, nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
escaped := `Say "hi" & <b>go</b> ünï 日本語`
|
|
attr := `Say "hi" & <b>go</b> ünï 日本語`
|
|
for _, rel := range []string{"news/index.html", "news/category/odd.html", "news/2026/11/index.html"} {
|
|
mustContain(t, rel, readOut(t, out, rel), "<strong>"+escaped+".</strong>", `aria-label="Read more: `+attr+`"`)
|
|
}
|
|
mustContain(t, "the post", readOut(t, out, "news/2026/11/odd.html"), "<h1>"+escaped+"</h1>")
|
|
mustContain(t, "the post below", readOut(t, out, "news/2026/11/shop.html"), `Newer: `+escaped)
|
|
for _, rel := range []string{"news/index.html", "news/category/odd.html", "news/2026/11/odd.html", "news/feed.xml"} {
|
|
if strings.Contains(readOut(t, out, rel), "<b>go</b>") {
|
|
t.Errorf("%s has the tag of the title as markup", rel)
|
|
}
|
|
}
|
|
var feed struct {
|
|
Channel struct {
|
|
Items []struct {
|
|
Title string `xml:"title"`
|
|
} `xml:"item"`
|
|
} `xml:"channel"`
|
|
}
|
|
if err := xml.Unmarshal([]byte(readOut(t, out, "news/feed.xml")), &feed); err != nil {
|
|
t.Fatalf("the feed is not well formed: %v", err)
|
|
}
|
|
if len(feed.Channel.Items) == 0 || feed.Channel.Items[0].Title != title {
|
|
t.Errorf("the first item of the feed is %+v, want the title %q", feed.Channel.Items, title)
|
|
}
|
|
}
|
|
|
|
// TestNeighbourInAnotherLanguageIsMarked checks that the link to an older post in another language names the language.
|
|
func TestNeighbourInAnotherLanguageIsMarked(t *testing.T) {
|
|
out, err := buildWith(t, blogSite(map[string]string{"news/ciao.md": "Title: Ciao\nDate: 2026-11-05T09:00:00+0100\nLanguage: it\n-\nUn testo.\n"}), nil)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
mustContain(t, "status post", readOut(t, out, "news/2026/11/status.html"), `rel="prev">Older: <span lang="it">Ciao</span></a>`)
|
|
mustContain(t, "italian post", readOut(t, out, "news/2026/11/ciao.html"), `rel="next">Piu recente: <span lang="en">Status page</span></a>`)
|
|
}
|