package render
import ( "html/template" "strings" "testing" )
func TestRebase(t *testing.T) { const link = "/u@d.com/x.git/blob/HEAD/" const raw = "/u@d.com/x.git/raw/HEAD/"
cases := []struct {
name string
in string
want string // substring expected in the output
bad string // substring that must NOT appear (optional)
}{
{"relative link", `<a href="DEPLOY.md">d</a>`, `href="` + link + `DEPLOY.md"`, ""},
{"relative subdir link", `<a href="docs/x.md">d</a>`, `href="` + link + `docs/x.md"`, ""},
{"parent link", `<a href="../y.md">y</a>`, `href="` + link + `../y.md"`, ""},
{"relative image uses raw", `<img src="img.png">`, `src="` + raw + `img.png"`, ""},
{"absolute link untouched", `<a href="https://x.io/a">a</a>`, `href="https://x.io/a"`, link},
{"root-relative untouched", `<a href="/abs">a</a>`, `href="/abs"`, link + "abs"},
{"anchor untouched", `<a href="#sec">s</a>`, `href="#sec"`, link},
{"mailto untouched", `<a href="mailto:x@y.io">m</a>`, `href="mailto:x@y.io"`, link},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := string(Rebase(template.HTML(c.in), link, raw))
if !strings.Contains(got, c.want) {
t.Fatalf("Rebase(%q) = %q, want substring %q", c.in, got, c.want)
}
if c.bad != "" && strings.Contains(got, c.bad) {
t.Fatalf("Rebase(%q) = %q, must not contain %q", c.in, got, c.bad)
}
})
}
}