// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
)
// buildGem builds the Gemini output of the site made of files, with the options changed by tune.
func buildGem(t *testing.T, files map[string]string, tune func(*GeminiOptions)) (string, error) {
t.Helper()
out := filepath.Join(t.TempDir(), "capsule")
opts := GeminiOptions{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3", Now: fixedNow, Language: "it"}
if tune != nil {
tune(&opts)
}
_, err := BuildGemini(opts)
return out, err
}
// gemSite is the base site with a services page that has a table and an image, a Spanish news page left out and
// the news index of the English pages.
func gemSite(extra map[string]string) map[string]string {
files := withFiles(map[string]string{
"lang/en.json": blogLangEn, "lang/it.json": blogLangIt,
"content/en/services.md": page("services", "Services", "", "Write to **{{contact_shown}}**.\n\n
\n| Service | Kept |
\n| Mail | 30 days |
\n
\n\n\n"),
"content/en/news/index.md": page("news", "News", "Layout: bare\n", "# News\n\n{{news_list}}\n\n## Categories\n\n{{categories_list}}\n\n## Archive\n\n{{news_archive}}\n\n[Feed](/news/feed.xml)\n"),
"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. Mail me at squid@kmalloc.org or visit [the site](https://kmalloc.org/).\n",
})
for k, v := range extra {
files[k] = v
}
return files
}
// gemFiles returns every file of the output, as slash paths relative to it.
func gemFiles(t *testing.T, out string) []string {
t.Helper()
var files []string
err := filepath.WalkDir(out, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
rel, _ := filepath.Rel(out, path)
files = append(files, filepath.ToSlash(rel))
return nil
})
if err != nil {
t.Fatal(err)
}
sort.Strings(files)
return files
}
// TestGeminiTree checks which files the capsule has, with Italian at the root.
func TestGeminiTree(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
have := map[string]bool{}
for _, f := range gemFiles(t, out) {
have[f] = true
}
for _, want := range []string{
"index.gmi", "manifesto.gmi", "persone.gmi", "servizi.gmi", "cronologia-manifesto.gmi", "progetti.gmi",
"en/index.gmi", "en/manifesto.gmi", "en/people.gmi", "en/services.gmi", "es/manifesto.gmi",
"news/index.gmi", "news/feed.gmi", "news/2026/11/shop.gmi", "news/2026/11/status.gmi", "news/2026/09/gnu-linux-day-2026.gmi",
"news/category/flug.gmi", "news/category/linux-day.gmi", "news/2026/index.gmi", "news/2026/11/index.gmi", "news/2026/09/index.gmi",
".meta", "logo.svg",
} {
if !have[want] {
t.Errorf("the capsule has no %s", want)
}
}
for f := range have {
if strings.HasSuffix(f, ".html") || strings.HasPrefix(f, "it/") || strings.HasPrefix(f, "en/news/") || strings.HasPrefix(f, "es/news/") || strings.HasSuffix(f, "robots.txt") {
t.Errorf("the capsule has %s, which it should not", f)
}
}
}
// TestGeminiRootLanguage checks the language at the root: the option, site.json, then the default language.
func TestGeminiRootLanguage(t *testing.T) {
plain, err := buildGem(t, gemSite(nil), func(o *GeminiOptions) { o.Language = "" })
if err != nil {
t.Fatal(err)
}
mustContain(t, "index.gmi", readOut(t, plain, "index.gmi"), "# kmalloc.org")
if !exists(t, plain, "it/index.gmi") || exists(t, plain, "en/index.gmi") {
t.Error("without a choice the default language is at the root and the others under their code")
}
files := gemSite(map[string]string{"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"gemini_language": "it", "languages"`, 1)})
fromSite, err := buildGem(t, files, func(o *GeminiOptions) { o.Language = "" })
if err != nil {
t.Fatal(err)
}
if !exists(t, fromSite, "en/index.gmi") || exists(t, fromSite, "it/index.gmi") {
t.Error("gemini_language in site.json does not put Italian at the root")
}
for name, tune := range map[string]func(*GeminiOptions){"xx": func(o *GeminiOptions) { o.Language = "xx" }, "es has no home": func(o *GeminiOptions) { o.Language = "es" }} {
if _, err := buildGem(t, gemSite(nil), tune); err == nil {
t.Errorf("language %s: no error", name)
}
}
}
// TestGeminiMeta checks the .meta file that sets the language of the documents.
func TestGeminiMeta(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatal(err)
}
want := "en/*:;lang=en\nen/**/*:;lang=en\nes/*:;lang=es\nes/**/*:;lang=es\nnews/*:;lang=en\nnews/**/*:;lang=en\n"
if got := readOut(t, out, ".meta"); got != want {
t.Errorf(".meta =\n%q\nwant\n%q", got, want)
}
plain, err := buildGem(t, gemSite(nil), func(o *GeminiOptions) { o.Language = "en" })
if err != nil {
t.Fatal(err)
}
want = "it/*:;lang=it\nit/**/*:;lang=it\nes/*:;lang=es\nes/**/*:;lang=es\n"
if got := readOut(t, plain, ".meta"); got != want {
t.Errorf(".meta with English at the root =\n%q\nwant\n%q (the news are in the language of the root: no lines for them)", got, want)
}
for _, f := range gemFiles(t, out) {
if strings.HasSuffix(f, ".gmi") && strings.Contains(readOut(t, out, f), ".meta") {
t.Errorf("%s mentions .meta", f)
}
}
}
// TestGeminiPages checks the headings, the layouts and the navigation of a page.
func TestGeminiPages(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatal(err)
}
services := readOut(t, out, "en/services.gmi")
mustContain(t, "en/services.gmi", services, "# Services\n", "Write to squid (at) kmalloc.org.\n",
"```\nService Kept\n------- -------\nMail 30 days\n```\n", "=> /logo.svg The mark of kmalloc\n")
if strings.Contains(services, "<") || strings.Contains(services, "**") {
t.Errorf("the page has markup left:\n%s", services)
}
mustContain(t, "en/services.gmi navigation", services,
"=> /en/ kmalloc.org\n", "=> /en/manifesto.gmi Manifesto\n", "=> /en/people.gmi People\n", "=> /en/services.gmi Services\n", "=> /news/ News\n",
"=> /servizi.gmi Italiano\n")
manifesto := readOut(t, out, "es/manifesto.gmi")
mustContain(t, "es/manifesto.gmi", manifesto, "# Manifiesto\n", "Version 1.0, 2026-11-12\n", "Traduccion de la version 1.0.\n", "=> /es/manifesto.gmi Manifiesto\n")
home := readOut(t, out, "index.gmi")
mustContain(t, "index.gmi", home, "=> /news/ Tutte le news\n", "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n", "=> /en/ English\n")
if strings.HasPrefix(home, "# ") && !strings.Contains(home, "kmalloc.org") {
t.Errorf("home has no title:\n%s", home)
}
}
// TestGeminiHomesLinkToTheNews checks that every home links to the one copy of the news.
func TestGeminiHomesLinkToTheNews(t *testing.T) {
out, err := buildGem(t, gemSite(nil), nil)
if err != nil {
t.Fatal(err)
}
for _, rel := range []string{"index.gmi", "en/index.gmi"} {
mustContain(t, rel, readOut(t, out, rel), "=> /news/ ", "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n")
}
}
// TestGeminiNews checks a post, the index, a category, the archive and the feed.
func TestGeminiNews(t *testing.T) {
out, err := buildGem(t, blogGem(), nil)
if err != nil {
t.Fatal(err)
}
post := readOut(t, out, "news/2026/11/shop.gmi")
mustContain(t, "post", post, "# A shop\n", "14 November 2026 · squid · FLUG · 1 min read\n", "About the shop.\n",
"There is a shop. Mail me at squid@kmalloc.org or visit the site.\n", "=> https://kmalloc.org/ the site\n",
"=> /news/category/flug.gmi FLUG\n", "=> https://social.kmalloc.org/@squid/115 Reply on Mastodon\n",
"=> /news/2026/11/status.gmi Older: Status page\n", "=> /news/ All news\n", "=> /en/manifesto.gmi Manifesto\n")
if strings.Contains(post, "Newer:") {
t.Error("the newest post has a link to a newer one")
}
gnu := readOut(t, out, "news/2026/09/gnu-linux-day-2026.gmi")
mustContain(t, "older post", gnu, "=> /en/people.gmi people\n", "=> /news/2026/11/status.gmi Newer: Status page\n")
index := readOut(t, out, "news/index.gmi")
mustContain(t, "index", index, "# News\n",
"=> /news/2026/11/shop.gmi 2026-11-14 A shop\nAbout the shop.\n", "=> /news/2026/11/status.gmi 2026-11-12 Status page\n",
"=> /news/category/flug.gmi FLUG (2)\n", "=> /news/2026/ 2026 (3)\n", "=> /news/2026/11/ November 2026 (2)\n", "=> /news/feed.gmi Feed\n")
category := readOut(t, out, "news/category/flug.gmi")
mustContain(t, "category", category, "# Category: FLUG\n", "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n", "=> /news/2026/09/gnu-linux-day-2026.gmi 2026-09-18 GNU/Linux Day 2026\n")
month := readOut(t, out, "news/2026/11/index.gmi")
mustContain(t, "month", month, "# Archive: November 2026\n", "=> /news/2026/ Archive of the year 2026\n", "A shop")
if strings.Contains(month, "GNU/Linux") {
t.Error("the archive of November lists a post of September")
}
year := readOut(t, out, "news/2026/index.gmi")
mustContain(t, "year", year, "# Archive: 2026\n", "=> /news/2026/09/ September 2026 (1)\n", "=> /news/2026/11/ November 2026 (2)\n")
}
// blogGem is the site of the tests of the news pages.
func blogGem() map[string]string { return gemSite(nil) }
// TestGeminiFeed checks the feed: newest first, a date before each title.
func TestGeminiFeed(t *testing.T) {
out, err := buildGem(t, blogGem(), nil)
if err != nil {
t.Fatal(err)
}
feed := readOut(t, out, "news/feed.gmi")
want := "=> /news/2026/11/shop.gmi 2026-11-14 A shop\n=> /news/2026/11/status.gmi 2026-11-12 Status page\n=> /news/2026/09/gnu-linux-day-2026.gmi 2026-09-18 GNU/Linux Day 2026\n"
mustContain(t, "feed", feed, "# kmalloc.org: News\n", want)
if first := strings.Index(feed, "A shop"); first > strings.Index(feed, "Status page") {
t.Error("the feed is not newest first")
}
}
// TestGeminiNeverHasBrokenLinksOrEmptyFiles walks the whole capsule: no empty file, and every
// link that stays in the capsule leads to a file.
func TestGeminiNeverHasBrokenLinksOrEmptyFiles(t *testing.T) {
out, err := buildGem(t, blogGem(), nil)
if err != nil {
t.Fatal(err)
}
link := regexp.MustCompile(`(?m)^=> (\S+)`)
for _, f := range gemFiles(t, out) {
data, err := os.ReadFile(filepath.Join(out, filepath.FromSlash(f)))
if err != nil {
t.Fatal(err)
}
if len(strings.TrimSpace(string(data))) == 0 && f != ".meta" {
t.Errorf("%s is empty", f)
}
if !strings.HasSuffix(f, ".gmi") {
continue
}
for _, m := range link.FindAllStringSubmatch(string(data), -1) {
target := m[1]
if strings.Contains(target, ":") || !strings.HasPrefix(target, "/") {
if !strings.Contains(target, ":") {
t.Errorf("%s has the relative link %s", f, target)
}
continue
}
rel := strings.TrimPrefix(target, "/")
if rel == "" || strings.HasSuffix(rel, "/") {
rel += "index.gmi"
}
if !exists(t, out, rel) {
t.Errorf("%s links to %s, which is not in the capsule", f, target)
}
}
}
}
// TestGeminiLinksMustResolve checks that a link to nothing stops the build and names the page and the address.
func TestGeminiLinksMustResolve(t *testing.T) {
for name, extra := range map[string]map[string]string{
"page": {"content/en/people.md": page("people", "People", "", "See [nowhere](/nowhere.html).\n")},
"post": {"news/broken.md": "Title: Broken\nDate: 2026-11-01T12:00:00+0100\n-\nSee [nowhere](/nowhere.html).\n"},
"unsafe scheme": {"content/en/people.md": page("people", "People", "", "[x](javascript:alert(1))\n")},
} {
t.Run(name, func(t *testing.T) {
_, err := buildGem(t, gemSite(extra), nil)
if err == nil || !strings.Contains(err.Error(), "nowhere.html") && !strings.Contains(err.Error(), "javascript") {
t.Errorf("error = %v, want one that names the address", err)
}
})
}
}
// TestGeminiOnlyReferencedAssets checks that an asset is copied when a page uses it and not otherwise.
func TestGeminiOnlyReferencedAssets(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{"content/en/services.md": page("services", "Services", "", "No image.\n")}), nil)
if err != nil {
t.Fatal(err)
}
if exists(t, out, "logo.svg") {
t.Error("an asset no page uses is copied")
}
}
// TestGeminiDraftsAndFuture checks that hidden posts stay out of the capsule, as in the site.
func TestGeminiDraftsAndFuture(t *testing.T) {
out, err := buildGem(t, gemSite(postNews()), nil)
if err != nil {
t.Fatal(err)
}
for _, f := range gemFiles(t, out) {
if strings.Contains(f, "a-draft") || strings.Contains(f, "in-future") || strings.Contains(f, "secret") {
t.Errorf("the capsule has %s, but the post is hidden", f)
}
}
with, err := buildGem(t, gemSite(postNews()), func(o *GeminiOptions) { o.Drafts = true })
if err != nil {
t.Fatal(err)
}
if !exists(t, with, "news/2026/11/a-draft.gmi") {
t.Error("a build with drafts lacks the draft")
}
}
// TestGeminiBlockMustStandAlone checks the rule of the generated blocks.
func TestGeminiBlockMustStandAlone(t *testing.T) {
_, err := buildGem(t, gemSite(map[string]string{"content/en/people.md": page("people", "People", "", "Text {{news_list}} text.\n")}), nil)
if err == nil || !strings.Contains(err.Error(), "news_list") {
t.Errorf("error = %v, want one about {{news_list}}", err)
}
}
// TestGeminiWebPaths checks that a path the website serves by other means can stay a link to the website.
func TestGeminiWebPaths(t *testing.T) {
files := gemSite(map[string]string{
"site.json": strings.Replace(baseFiles()["site.json"], `"languages"`, `"gemini_web_paths": "/~, /pastes/", "languages"`, 1),
"content/en/people.md": page("people", "People", "", "Pages of the [people](/~squid/) and the [pastes](/pastes/x).\n"),
})
out, err := buildGem(t, files, nil)
if err != nil {
t.Fatalf("BuildGemini() error = %v", err)
}
mustContain(t, "en/people.gmi", readOut(t, out, "en/people.gmi"),
"=> https://kmalloc.org/~squid/ people\n", "=> https://kmalloc.org/pastes/x pastes\n")
if _, err := buildGem(t, gemSite(map[string]string{"content/en/people.md": page("people", "People", "", "[x](/~squid/)\n")}), nil); err == nil {
t.Error("a path of the website is a link when the site did not say so")
}
}
// TestGeminiHeadings checks that a page has one first level heading and that code is left alone.
func TestGeminiHeadings(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/people.md": page("people", "People", "", "# People\n\n# Other\n\n```\n# a comment in code\n```\n"),
}), nil)
if err != nil {
t.Fatal(err)
}
got := readOut(t, out, "en/people.gmi")
mustContain(t, "en/people.gmi", got, "# People\n\n## Other\n\n```\n# a comment in code\n```\n")
if strings.Count(got, "# People") != 1 {
t.Errorf("the title is repeated:\n%s", got)
}
home := readOut(t, out, "en/index.gmi")
mustContain(t, "en/index.gmi", home, "# kmalloc.org\n\n## Technology.\n")
}
// TestGeminiKeepsLinksToOtherProtocols checks the protocols that leave the capsule as they are.
func TestGeminiKeepsLinksToOtherProtocols(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{"content/en/people.md": page("people", "People", "",
"[Mail](mailto:squid@kmalloc.org) [Pay](bitcoin:bc1qxyz) [Chat](xmpp:squid@kmalloc.org) [Capsule](gemini://example.org/)\n")}), nil)
if err != nil {
t.Fatal(err)
}
mustContain(t, "en/people.gmi", readOut(t, out, "en/people.gmi"),
"=> mailto:squid@kmalloc.org Mail\n", "=> bitcoin:bc1qxyz Pay\n", "=> xmpp:squid@kmalloc.org Chat\n", "=> gemini://example.org/ Capsule\n")
}
// TestGeminiTitleThatSaysTheHeading checks that a heading the title already says is not repeated, as in
// the real site where the page title is "kmalloc.org: Services" and the text starts with "# Services".
func TestGeminiTitleThatSaysTheHeading(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/people.md": page("people", "kmalloc.org: People", "", "# People\n\nText.\n"),
}), nil)
if err != nil {
t.Fatal(err)
}
got := readOut(t, out, "en/people.gmi")
if !strings.HasPrefix(got, "# kmalloc.org: People\n\nText.\n") {
t.Errorf("en/people.gmi starts with\n%q", got)
}
}
// TestGeminiFeedTitleIsNotPrefixedTwice checks the feed title when the news page title has the prefix already.
func TestGeminiFeedTitleIsNotPrefixedTwice(t *testing.T) {
out, err := buildGem(t, gemSite(map[string]string{
"content/en/news/index.md": page("news", "kmalloc.org: News", "", "{{news_list}}\n"),
}), nil)
if err != nil {
t.Fatal(err)
}
if got := readOut(t, out, "news/feed.gmi"); !strings.HasPrefix(got, "# kmalloc.org: News\n") {
t.Errorf("news/feed.gmi starts with %q", got)
}
}
// TestGeminiTranslationNote checks that the note of a machine translated language reads as one sentence.
func TestGeminiTranslationNote(t *testing.T) {
files := gemSite(map[string]string{
"lang/es.json": strings.Replace(baseFiles()["lang/es.json"], `"translation_note": "Traduccion automatica."`, `"translation_note": "Traduccion automatica, avisa si ves un error:"`, 1),
})
out, err := buildGem(t, files, nil)
if err != nil {
t.Fatal(err)
}
mustContain(t, "es/manifesto.gmi", readOut(t, out, "es/manifesto.gmi"),
"Traduccion automatica, avisa si ves un error: avisar.\n=> https://git.kmalloc.org/squid/kmalloc.org/issues avisar\n")
}