// SPDX-License-Identifier: GPL-3.0-or-later package gemtext import ( "errors" "strings" "testing" ) // identity leaves every address as it is. func identity(href string) (string, error) { return href, nil } // convert converts html with addresses left as they are and no blocks. func convert(t *testing.T, html string) string { t.Helper() got, err := Convert(html, Options{Link: identity}) if err != nil { t.Fatalf("Convert(%q) error = %v", html, err) } return got } // TestConvert checks the gemtext of every kind of element the site uses. func TestConvert(t *testing.T) { tests := []struct { name, html, want string }{ {"headings", `

Title

Sub

Deep

`, "# Title\n\n## Sub\n\n### Deep\n"}, {"hero with line breaks", `

Technology.
Freedom.
Dissent.

`, "# Technology. Freedom. Dissent.\n"}, {"links go after the paragraph", `

Read the first and the second page.

`, "Read the first and the second page.\n=> /a.html first\n=> https://x.org/b second\n"}, {"the same link once", `

A and A again.

`, "A and A again.\n=> /a A\n"}, {"a paragraph that is only a link", `

All news

`, "=> /x.html All news\n"}, {"line breaks", `

One
Two
Three

`, "One\nTwo\nThree\n"}, {"whitespace is collapsed", "

a\n b\t c

", "a b c\n"}, {"entities", `

Tom & Jerry © <ok>

`, "Tom & Jerry © \n"}, {"unordered list", ``, "* One\n* Two link\n=> /c link\n"}, {"a list of only links has no bullets", ``, "=> /a A\n=> /b B\n"}, {"nested list is flattened", ``, "* A\n* A1\n* B\n"}, {"ordered list", `
  1. First
  2. Second
`, "* 1. First\n* 2. Second\n"}, {"quote", `

One

Two x

`, "> One\n>\n> Two x\n=> /x x\n"}, {"note is a quote", `

Careful

`, "> Careful\n"}, {"preformatted", "
line1\n  line2\n```\n
", "```\nline1\n line2\n ```\n```\n"}, {"table with a header", `
Data
ServiceKept
Mail30 days
Chat1 year
`, "Data\n```\nService Kept\n------- -------\nMail 30 days\nChat 1 year\n```\n"}, {"table without a header", `
abb
cccd
`, "```\na bb\nccc d\n```\n"}, {"table links", `
Mailx
`, "```\nMail x\n```\n=> /m Mail\n"}, {"image with a description", `

kmalloc

`, "=> /logo.svg kmalloc\n"}, {"image without a description is skipped", `

`, ""}, {"lines that look like gemtext are protected", `

# no

=> no

* no

> no

`, " # no\n\n => no\n\n * no\n\n > no\n"}, {"hidden and scripted things are skipped", `

kept

`, "kept\n"}, {"containers are transparent", `

A

Loose text
`, "A\n\nLoose text\n"}, {"time and span are inline", `

by squid

`, "14 November 2026 by squid\n"}, {"empty input", ``, ""}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := convert(t, tt.html); got != tt.want { t.Errorf("Convert() =\n%q\nwant\n%q", got, tt.want) } }) } } // TestConvertLinkMapping checks that Link rewrites and drops addresses, and that its error stops the conversion. func TestConvertLinkMapping(t *testing.T) { link := func(href string) (string, error) { switch { case strings.HasPrefix(href, "#"): return "", nil case href == "/broken.html": return "", errors.New("no such page") } target, _, _ := strings.Cut(href, "#") return strings.Replace(target, ".html", ".gmi", 1), nil } got, err := Convert(`

See a and top.

`, Options{Link: link}) if err != nil || got != "See a and top.\n=> /a.gmi a\n" { t.Errorf("Convert() = %q, %v", got, err) } _, err = Convert(`

x

`, Options{Link: link}) if err == nil || !strings.Contains(err.Error(), "/broken.html") || !strings.Contains(err.Error(), "no such page") { t.Errorf("error = %v, want one that names the address and the cause", err) } _, err = Convert(`

pic

`, Options{Link: link}) if err == nil { t.Error("a broken image address is not an error") } } // TestConvertBlocks checks the placeholders of generated blocks. func TestConvertBlocks(t *testing.T) { block := func(name string) (string, error) { if name != "news_list" { return "", errors.New("unknown block " + name) } return "=> /a 2026-01-01 A\n=> /b 2025-12-31 B", nil } got, err := Convert(`

intro

end

`, Options{Link: identity, Block: block}) if err != nil || got != "intro\n\n=> /a 2026-01-01 A\n=> /b 2025-12-31 B\n\nend\n" { t.Errorf("Convert() = %q, %v", got, err) } if _, err := Convert(``, Options{Link: identity, Block: block}); err == nil { t.Error("an unknown block is not an error") } if _, err := Convert(``, Options{Link: identity}); err == nil { t.Error("a block without a Block function is not an error") } } // TestConvertRejectsBrokenHTML checks that markup that cannot be read is an error. func TestConvertRejectsBrokenHTML(t *testing.T) { if _, err := Convert(`

unclosed`, Options{Link: identity}); err == nil || !strings.Contains(err.Error(), "HTML") { t.Errorf("error = %v, want one about the HTML", err) } } // TestTextAndLink checks the helpers that write single lines. func TestTextAndLink(t *testing.T) { if got := Text(" two \n words "); got != "two words" { t.Errorf("Text() = %q", got) } if got := Text("# not a heading"); got != " # not a heading" { t.Errorf("Text() = %q, want a leading space", got) } if got := Link("/a.gmi", " A page "); got != "=> /a.gmi A page" { t.Errorf("Link() = %q", got) } if got := Link("/a.gmi", ""); got != "=> /a.gmi" { t.Errorf("Link() without a label = %q", got) } }