package markup
import (
"strings"
"testing"
)
// describedRes is tokenRes that also knows one check-in's comment and
// maps fossil's /info pages, as the forge's resolver does.
type describedRes struct{ tokenRes }
const describedHash = "cdc8e88f7b0123456789"
func (r describedRes) ResolveToken(t RefToken) (string, bool) {
if t.Kind == RefCheckin {
return "/r/demo/checkin/" + t.ID, true
}
return r.tokenRes.ResolveToken(t)
}
func (describedRes) DescribeHash(prefix string) string {
if strings.HasPrefix(describedHash, prefix) || strings.HasPrefix(prefix, describedHash) {
return "Fix [abcdef0123] in the parser"
}
return ""
}
func (describedRes) ResolvePath(p string) string {
if strings.HasPrefix(p, "/info/") {
return "/r/demo/checkin/" + strings.TrimPrefix(p, "/info/")
}
return ""
}
// A check-in link the author wrote reads as a typed one does: its
// comment as the title, and the excerpt where the link is the bare
// hash. Words of the author's own are kept without an excerpt, and the
// excerpt is not itself scanned for references (jva, 2026-10-30).
func TestAnAuthoredCheckinLinkIsDescribed(t *testing.T) {
bare := string(resolveRefs([]byte(`<p><a href="/r/demo/checkin/`+describedHash+`">`+describedHash[:10]+`</a></p>`), describedRes{}))
if !strings.Contains(bare, `title="Fix [abcdef0123] in the parser"`) || !strings.Contains(bare, `<span class="cref"> (Fix [abcdef0123] in the parser)</span>`) {
t.Errorf("FAIL: a bare authored link is not described:\n%s", bare)
}
if strings.Count(bare, "<a ") != 1 {
t.Errorf("FAIL: the excerpt's own hash became a link:\n%s", bare)
}
worded := string(resolveRefs([]byte(`<p><a href="/r/demo/checkin/`+describedHash+`">this fix</a></p>`), describedRes{}))
if !strings.Contains(worded, `title="Fix [abcdef0123] in the parser"`) || strings.Contains(worded, "cref") {
t.Errorf("FAIL: a worded link should keep its words and carry only the title:\n%s", worded)
}
}
// In a ticket comment "./" is fossil's repository root, so
// [./info/HASH|this] is a link reading "this", not literal brackets.
func TestADotSlashInfoTargetLinksOutsideADocument(t *testing.T) {
got, err := RenderFossilWiki([]byte("see [./info/"+describedHash+"|this]"), describedRes{})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(got), `href="/r/demo/checkin/`+describedHash+`"`) || !strings.Contains(string(got), ">this</a>") || strings.Contains(string(got), "[./info") {
t.Errorf("FAIL: ./info did not link:\n%s", got)
}
}