package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
)
// A RUNNER REQUEST'S PAGE SHOWS THE CODE TO COMPARE, WHERE IT CAME FROM,
// WHAT IT GIVES, AND A WARNING; THE ASKER'S ADDRESS IS A DATA HOOK, NOT A
// LABEL.
func TestARunnerRequestPageShowsCodeAddressAndWarning(t *testing.T) {
var dup strings.Builder
d := meSignOff{ID: "abc", Agent: "runner at 203.0.113.9", Repo: "suit", Summary: "Set up a CI runner for suit (reports host build-box)", State: "pending", Requested: "2026-11-18T10:00:00Z", Pickup: strings.Repeat("ab", 32), SameRepo: 3}
if err := noHooks.template(signOffPageTmpl).ExecuteTemplate(&dup, "content", pageData{Title: "Approve?", SignOff: &d}); err != nil {
t.Fatal(err)
}
if v, _ := hookAttr(t, dup.String(), "runner-duplicates", "data-count"); v != "3" {
t.Errorf("FAIL: duplicate count %q, want 3", v)
}
pickup := strings.Repeat("ab", 32)
render := func(match bool, state string) string {
var b strings.Builder
s := meSignOff{ID: "abc", Agent: "runner at 203.0.113.9", Repo: "suit", Summary: "Set up a CI runner for suit (reports host build-box)",
State: state, Requested: "2026-11-18T10:00:00Z", Pickup: pickup, Match: match, Reason: "forge words"}
if err := noHooks.template(signOffPageTmpl).ExecuteTemplate(&b, "content", pageData{Title: "Approve?", SignOff: &s}); err != nil {
t.Fatal(err)
}
return b.String()
}
elsewhere := render(false, "pending")
if v, _ := hookAttr(t, elsewhere, "runner-request", "data-code"); v != api.RunnerShortCode(pickup) {
t.Errorf("FAIL: the page's code is %q, want the short code of the pickup code", v)
}
if v, _ := hookAttr(t, elsewhere, "runner-request", "data-address"); v != "203.0.113.9" {
t.Errorf("FAIL: address %q", v)
}
if v, _ := hookAttr(t, elsewhere, "runner-request", "data-match"); v != "false" {
t.Errorf("FAIL: match %q", v)
}
if v, _ := hookAttr(t, elsewhere, "runner-host", "data-host"); v != "build-box" {
t.Errorf("FAIL: reported host %q", v)
}
if v, _ := hookAttr(t, elsewhere, "runner-host", "data-verified"); v != "false" {
t.Errorf("FAIL: the host is not marked unverified")
}
if _, ok := hookAttr(t, elsewhere, "runner-duplicates", "data-hook"); ok {
t.Errorf("FAIL: a lone request warns of duplicates")
}
for _, hook := range []string{"runner-elsewhere", "runner-warning", "signoff-approve", "signoff-deny"} {
if _, ok := hookAttr(t, elsewhere, hook, "data-hook"); !ok {
t.Errorf("FAIL: no %s on the page", hook)
}
}
if v, _ := hookAttr(t, elsewhere, "signoff-agent", "data-runner"); v != "true" {
t.Errorf("FAIL: the asker is not marked as a runner")
}
if strings.Contains(elsewhere, "forge words") || strings.Contains(elsewhere, "runner at") {
t.Errorf("FAIL: the stored reason or the raw asker is shown")
}
here := render(true, "pending")
if _, ok := hookAttr(t, here, "runner-elsewhere", "data-hook"); ok {
t.Errorf("FAIL: the network notice shows for a request from this network")
}
if _, ok := hookAttr(t, render(true, "approved"), "runner-warning", "data-hook"); ok {
t.Errorf("FAIL: an approved request still warns")
}
var b strings.Builder
if err := noHooks.template(requestsPageTmpl).ExecuteTemplate(&b, "content", pageData{Title: "Requests", Requests: &requestsView{Tab: "waiting",
Runners: []meSignOff{{ID: "abc", Agent: "runner at 203.0.113.9", Summary: "x", State: "pending", Requested: "2026-11-18T10:00:00Z", Pickup: pickup}}, RunnerCount: 300}}); err != nil {
t.Fatal(err)
}
if v, _ := hookAttr(t, b.String(), "runner-requests", "data-count"); v != "300" {
t.Errorf("FAIL: the runner section's count is %q, want 300", v)
}
if v, _ := hookAttr(t, b.String(), "requests-none", "data-hook"); v == "" && !strings.Contains(b.String(), `data-hook="requests-none"`) {
t.Errorf("FAIL: runner requests hid the empty state of the others")
}
if v, _ := hookAttr(t, b.String(), "request", "data-runner"); v != "true" {
t.Errorf("FAIL: the list row is not marked as a runner request")
}
if v, _ := hookAttr(t, b.String(), "runner-code", "data-code"); v != api.RunnerShortCode(pickup) {
t.Errorf("FAIL: the row's code is %q", v)
}
if !t.Failed() {
t.Log("ok")
}
}
// The inbox row names no raw asker for a runner request, only that one
// asks; the address is on the request's page.
func TestTheInboxReasonOfARunnerRequestDoesNotPrintTheAsker(t *testing.T) {
got := attentionReason(attentionEvent{Kind: "signoff", From: "runner at 203.0.113.9", Repo: "suit"})
if strings.Contains(got, "runner at") || strings.Contains(got, "203.0.113.9") {
t.Errorf("FAIL: the reason carries the stored asker")
}
if other := attentionReason(attentionEvent{Kind: "signoff", From: "zeitgeist", Repo: "suit"}); !strings.Contains(other, "zeitgeist") {
t.Errorf("FAIL: an agent's request lost its name")
}
if !t.Failed() {
t.Log("ok")
}
}
// The pickup line is for a request whose collector the page cannot
// otherwise name; a runner request shows its code instead.
func TestARunnerRequestPageHasNoSeparatePickupLine(t *testing.T) {
var b strings.Builder
s := meSignOff{ID: "abc", Agent: "runner at 203.0.113.9", Repo: "suit", Summary: "x", State: "pending", Requested: "2026-11-18T10:00:00Z", Pickup: strings.Repeat("ab", 32)}
if err := noHooks.template(signOffPageTmpl).ExecuteTemplate(&b, "content", pageData{Title: "Approve?", SignOff: &s}); err != nil {
t.Fatal(err)
}
if _, ok := hookAttr(t, b.String(), "signoff-pickup", "data-hook"); ok {
t.Errorf("FAIL: the pickup line shows beside the runner code")
}
if !t.Failed() {
t.Log("ok")
}
}