// 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) } } // 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 := `` + `
Data
ServiceKeptSeen
Mail` + long + `all
Chatsome
` want := "Data\n\n### Mail\n* Kept: " + long + "\n* Seen: all\n\n### Chat\n* Seen: some\n=> /m Mail\n" if got := convert(t, html); got != want { t.Errorf("Convert() =\n%q\nwant\n%q", got, want) } noHeader := `
a` + long + `
bc
` 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\n=> /a.png pic\n"}, {"an item that is a link without text has no bullet", ``, "* 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\n"}, {"cells of a table in an item do not stick", ``, "* a b\n"}, {"a preformatted block in an item stays a block", "", "* Run:\n```\nx\n y\n```\n* Next\n"}, {"a preformatted block that is all of an item", "", "```\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, `

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", `
ServiceKept
` + long + `
Chat1 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", `
` + long + `
short
`, "* " + long + "\n* short\n"}, {"one narrow column", `
Name
a
`, "```\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", `

mark Text

`, "Text\n"}, {"a decorative image", `

swirl Text

`, "Text\n"}, {"role presentation", `

swirl Text

`, "Text\n"}, {"role none", `

swirl Text

`, "Text\n"}, {"an image of the content", `

Sales by month

`, "=> /chart.png Sales by month\n"}, {"an image that has no class", `

A diagram

`, "=> /chart.svg A diagram\n"}, {"a class that only contains the word", `

Type

`, "=> /l.png Type\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) } }) } }