// 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",
`
`, 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(`
`, 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)
}
}
// TestConvertWideTable checks that a table that does not fit in 80 columns becomes a list of records, a
// heading for the first cell of each row and a bullet for each other cell, named by its column.
func TestConvertWideTable(t *testing.T) {
long := strings.Repeat("word ", 20) + "end"
html := `
`
if got, want := convert(t, noHeader), "* a | "+long+"\n* b | c\n"; got != want {
t.Errorf("Convert() without a header =\n%q\nwant\n%q", got, want)
}
}
// TestConvertListItems checks the items that have no text, more than one paragraph or a preformatted block.
func TestConvertListItems(t *testing.T) {
tests := []struct {
name, html, want string
}{
{"an item that is only an image has no bullet", `
Text
`, "* Text\n=> /a.png pic\n"},
{"an item that is a link without text has no bullet", `
Text
`, "* Text\n=> /x\n"},
{"an item of only an image in a list of only images", `
`, "=> /a.png A\n=> /b.png B\n"},
{"paragraphs of an item do not stick", `
First
Second
`, "* First Second\n"},
{"cells of a table in an item do not stick", `
a
b
`, "* a b\n"},
{"a preformatted block in an item stays a block", "
Run:
x\n y
Next
", "* Run:\n```\nx\n y\n```\n* Next\n"},
{"a preformatted block that is all of an item", "
code
", "```\ncode\n```\n"},
}
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)
}
})
}
}
// TestConvertAddressWithSpaces checks that a space in an address does not end the address of a link line.
func TestConvertAddressWithSpaces(t *testing.T) {
if got, want := convert(t, `
`), "=> /a%20b/c%20d.html t\n"; got != want {
t.Errorf("Convert() = %q, want %q", got, want)
}
if got, want := Link("/a b", "x"), "=> /a%20b x"; got != want {
t.Errorf("Link() = %q, want %q", got, want)
}
}
// TestConvertWideTableEdges checks the wide tables with no title for a row, with a header only and with one column.
func TestConvertWideTableEdges(t *testing.T) {
long := strings.Repeat("word ", 20) + "end"
tests := []struct {
name, html, want string
}{
{"a row with an empty first cell has no heading",
`
Service
Kept
` + long + `
Chat
1 year
`,
"* Kept: " + long + "\n\n### Chat\n* Kept: 1 year\n"},
{"a header and nothing else keeps the header",
`
` + long + `
Seen
`,
"* " + long + " | Seen\n"},
{"one wide column with a header",
`
Name
` + long + `
short
`,
"Name\n* " + long + "\n* short\n"},
{"one wide column without a header",
`
`, "```\nName\n----\na\n```\n"},
}
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)
}
})
}
}
// TestConvertDecorativeImages checks that the images of the layout leave no link line and those of the content do.
func TestConvertDecorativeImages(t *testing.T) {
tests := []struct {
name, html, want string
}{
{"a logo", `
Text
`, "Text\n"},
{"a logo among classes", `
Text
`, "Text\n"},
{"a decorative image", `
Text
`, "Text\n"},
{"role presentation", `
Text
`, "Text\n"},
{"role none", `
Text
`, "Text\n"},
{"an image of the content", `
`, "=> /chart.png Sales by month\n"},
{"an image that has no class", `
`, "=> /chart.svg A diagram\n"},
{"a class that only contains the word", `