New placeholders for templates/post.html, written in the language of the post: date_long, post_summary, post_meta, post_reading, post_mastodon and post_nav (older, newer and all news), plus og_type and article_meta for Open Graph, which every other page sets to website. A post with a Mastodon address needs the reply_mastodon string; the other strings are optional and a feature without its string is left out. Refs #9
301 lines
12 KiB
Go
301 lines
12 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package build
|
|
|
|
import (
|
|
"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)
|
|
}
|
|
}
|
|
}
|