package index
import (
"testing"
"craftdesign.group/quarry/internal/artifact"
)
// A landing's message is split the way a commit message is: first line
// is the title. So a record whose whole statement is "Closes
// 2058808c3504." carries its only reference in the TITLE, and reading
// the body alone left every such landing invisible on the ticket it
// settled — which is exactly what production showed: 618 landing posts,
// 50 anchors, and tickets with no sign the work existed.
func TestAReferenceInTheTitleAnchorsToo(t *testing.T) {
ix := openIndex(t)
const full = "2058808c35049e2b7f1a4c6d8e0b3a5c7d9f1e2b4a6c8e0d2f4a6b8c0d2e4f60"
if err := ix.FoldNow([]Parsed{{Hash: "aaaa1111", Art: &artifact.Artifact{
Type: artifact.Forum, ThreadTitle: "Closes 2058808c3504.", Date: "2026-01-01T10:00:00",
User: "dan", Body: []byte("zit-landing: 1\nbranch: x\ntarget: trunk\n\nNo hashes here.\n"),
}}}); err != nil {
t.Fatal(err)
}
got, err := ix.MentionsOf(full)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].Root != "aaaa1111" {
t.Fatalf("FAIL: the ticket sees %d mentions, want the landing that closes it: %+v", len(got), got)
}
}
// The body still anchors, and a reference in both places anchors once.
func TestTitleAndBodyAnchorsDoNotDuplicate(t *testing.T) {
ix := openIndex(t)
const full = "fc84aef2899b1d3e5f7a9c1b3d5e7f9a1c3b5d7e9f1a3c5b7d9e1f3a5c7b9d1e"
if err := ix.FoldNow([]Parsed{{Hash: "bbbb2222", Art: &artifact.Artifact{
Type: artifact.Forum, ThreadTitle: "Closes fc84aef2899b.", Date: "2026-01-01T10:00:00",
User: "dan", Body: []byte("the fix for fc84aef2899b is here"),
}}}); err != nil {
t.Fatal(err)
}
got, err := ix.MentionsOf(full)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("FAIL: %d mentions for one reference named twice: %+v", len(got), got)
}
}
// The BACKLINK is keyword-independent, and must stay so. A landing that
// merely names a ticket is still a landing the ticket's reader wants to
// see; only the forward-facing chip needs a declared verb, because only
// a declaration can be acted on.
func TestTheBacklinkNeedsNoVerbAtAll(t *testing.T) {
ix := openIndex(t)
const full = "a488d7fa4200112233445566778899aabbccddeeff00112233445566778899aa"
for i, body := range []string{
"no verb here, just a488d7fa42 in passing",
"toward a488d7fa42",
"closes a488d7fa42",
"[a488d7fa42] in brackets",
} {
hash := string([]byte{'p', byte('1' + i)})
if err := ix.FoldNow([]Parsed{{Hash: hash + "000000", Art: &artifact.Artifact{
Type: artifact.Forum, ThreadTitle: "thread " + hash, Date: "2026-01-0" + hash[1:] + "T10:00:00",
User: "dan", Body: []byte(body),
}}}); err != nil {
t.Fatal(err)
}
}
got, err := ix.MentionsOf(full)
if err != nil {
t.Fatal(err)
}
if len(got) != 4 {
t.Errorf("FAIL: %d of 4 threads backlink to the ticket: %+v", len(got), got)
}
}