package api
import (
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/artifact"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/markup"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// In a ticket, a forum post or a landing, "#1" links ticket 1 by its hex
// id; a number the repository never gave stays text; chat keeps #1 as a
// message (54c5175f).
func TestANumberInProseLinksTheTicket(t *testing.T) {
ix, err := index.Open(filepath.Join(t.TempDir(), "i.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ix.Close() })
id := "cafe000000000000000000000000000000000000000000000000000000000001"
if err := ix.FoldNow([]index.Parsed{{Hash: id, Art: &artifact.Artifact{Type: artifact.Ticket, TicketID: id,
Date: "2026-09-01T10:00:00", User: "dan", Fields: []artifact.Field{{Name: "title", Value: "the one"}, {Name: "status", Value: "Open"}}}}}); err != nil {
t.Fatal(err)
}
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
d := Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"q": {Index: ix}}), Numbers: db}
if err := RegisterAll(ops.NewRegistry(), d); err != nil {
t.Fatal(err)
}
d.ticketNumbers("q", &Repo{Index: ix})
out, err := markup.Render([]byte("fixed by #1, not #99"), resolver{ix: ix, repo: "q"})
if err != nil {
t.Fatal(err)
}
got := string(out)
if !strings.Contains(got, `href="/r/q/ticket/`+id+`"`) {
t.Fatalf("FAIL: #1 does not link the ticket:\n%s", got)
}
if strings.Contains(got, "/ticket/99") {
t.Fatalf("FAIL: #99, a number never given, became a link:\n%s", got)
}
chat, err := markup.Render([]byte("see #1"), resolver{ix: ix, repo: "q", chat: true})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(chat), `href="/r/q/ticket/`+id+`"`) {
t.Fatalf("FAIL: in chat #1 is not the ticket:\n%s", chat)
}
old, err := markup.Render([]byte("see #1"), resolver{ix: ix, repo: "q", chat: true, oldSeq: true})
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(old), "/ticket/") {
t.Fatalf("FAIL: in a chat line from before ^N #1 became a ticket link:\n%s", old)
}
t.Log("ok: #1 links the ticket's hex id, #99 stays text, an old chat line keeps #1 a message")
}