package index
import (
"testing"
)
const blHash = "891147b28d96ff9d0123456789abcdef01234567"
func refsOf(t *testing.T, ix *Index, hash string) map[string]bool {
t.Helper()
rows, err := ix.ReferencedBy(hash)
if err != nil {
t.Fatal(err)
}
got := map[string]bool{}
for _, r := range rows {
got[r.Kind+":"+r.Ref] = true
}
return got
}
// Fossil's backlink table: a wiki page's bare [hash] or /info/hash link
// lists the page under the check-in that hash prefixes.
func TestWikiBacklinksFollowFossilsRule(t *testing.T) {
ix := openIndex(t)
foldWiki(t, ix, "w1", "Bare", "2026-01-01T00:00:00.000", "see [891147b28d96ff9d] for it")
foldWiki(t, ix, "w2", "Info", "2026-01-01T00:00:01.000", "see [/info/891147B28D96|the build] here")
foldWiki(t, ix, "w3", "Short", "2026-01-01T00:00:02.000", "see [891] only")
foldWiki(t, ix, "w4", "Other", "2026-01-01T00:00:03.000", "see [ffffffffffff] only")
foldWiki(t, ix, "w5", "Doc", "2026-01-01T00:00:04.000", "see [/doc/891147b28d96ff9d/a.md]")
got := refsOf(t, ix, blHash)
for _, want := range []string{"wiki:Bare", "wiki:Info"} {
if !got[want] {
t.Errorf("FAIL: %s is not listed: %v", want, got)
}
}
for _, not := range []string{"wiki:Short", "wiki:Other", "wiki:Doc"} {
if got[not] {
t.Errorf("FAIL: %s is listed: %v", not, got)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// Editing the page to drop the reference removes the backlink; only the
// newest version counts.
func TestWikiBacklinkGoesWhenTheReferenceIsDropped(t *testing.T) {
ix := openIndex(t)
foldWiki(t, ix, "w1", "Page", "2026-01-01T00:00:00.000", "[891147b28d96ff9d]")
if !refsOf(t, ix, blHash)["wiki:Page"] {
t.Fatal("FAIL: not listed after the first edit")
}
foldWiki(t, ix, "w2", "Page", "2026-01-02T00:00:00.000", "nothing here")
if refsOf(t, ix, blHash)["wiki:Page"] {
t.Errorf("FAIL: still listed after the edit dropped it")
}
foldWiki(t, ix, "w3", "Page", "2026-01-03T00:00:00.000", "back: [891147b28d96ff9d]")
if !refsOf(t, ix, blHash)["wiki:Page"] {
t.Errorf("FAIL: not listed when the reference returns")
}
if !t.Failed() {
t.Log("ok")
}
}
// A Markdown page names a check-in by link or by bracketed hash; text in
// code does not count.
func TestMarkdownBacklinks(t *testing.T) {
ix := openIndex(t)
if err := ix.FoldNow([]Parsed{
wikiArt(0, "Md1", "2026-01-01T00:00:00.000", "pat", "text/x-markdown", "a [link](/info/891147b28d96ff9d \"t\") b"),
wikiArt(1, "Md2", "2026-01-01T00:00:00.000", "pat", "text/x-markdown", "a [891147b28d96ff9d] b"),
wikiArt(2, "Md3", "2026-01-01T00:00:00.000", "pat", "text/x-markdown", "`[891147b28d96ff9d]`\n\n```\n[891147b28d96ff9d]\n```\n"),
wikiArt(3, "Txt", "2026-01-01T00:00:00.000", "pat", "text/plain", "[891147b28d96ff9d]"),
}); err != nil {
t.Fatal(err)
}
got := refsOf(t, ix, blHash)
for _, want := range []string{"wiki:Md1", "wiki:Md2"} {
if !got[want] {
t.Errorf("FAIL: %s is not listed: %v", want, got)
}
}
for _, not := range []string{"wiki:Md3", "wiki:Txt"} {
if got[not] {
t.Errorf("FAIL: %s is listed: %v", not, got)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// A ticket comment names a check-in the same way; a private ticket is
// never listed.
func TestTicketBacklinks(t *testing.T) {
ix := openIndex(t)
foldTicket(t, ix, "c1", "t1", "2026-01-01T00:00:00.000", "title", "Open", "icomment", "fixed in [891147b28d96ff9d]")
foldTicket(t, ix, "c2", "t2", "2026-01-01T00:00:00.000", "title", "Hidden", "is_private", "1", "icomment", "[891147b28d96ff9d]")
got := refsOf(t, ix, blHash)
if !got["ticket:t1"] {
t.Errorf("FAIL: the ticket is not listed: %v", got)
}
if got["ticket:t2"] {
t.Errorf("FAIL: a private ticket is listed: %v", got)
}
if !t.Failed() {
t.Log("ok")
}
}
// A store made before the backlink table existed learns its pages.
func TestBacklinksAreBackfilledOnAnOldStore(t *testing.T) {
ix := openIndex(t)
foldWiki(t, ix, "w1", "Old", "2026-01-01T00:00:00.000", "[891147b28d96ff9d]")
if _, err := ix.w.Exec(`DELETE FROM backlink`); err != nil {
t.Fatal(err)
}
if refsOf(t, ix, blHash)["wiki:Old"] {
t.Fatal("FAIL: precondition: still listed")
}
if err := backfillBacklinks(ix.w); err != nil {
t.Fatal(err)
}
if !refsOf(t, ix, blHash)["wiki:Old"] {
t.Errorf("FAIL: the backfill did not pick the page up")
}
if !t.Failed() {
t.Log("ok")
}
}