package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
)
// A plan's step that mints a token carries the lines a setup's page
// draws for its credential, on that step; a step that mints none carries
// none.
func TestAPlanPageShowsTheLimitsOfAStepThatMints(t *testing.T) {
page := renderSetup(t, meSignOff{ID: "p", Agent: "a", Summary: "t", State: "pending", Requested: "2026-11-08T10:00:00Z",
Steps: []meSignOffStep{
{Sentence: "make"},
{Sentence: "mint", Mint: &api.MintView{Agent: "fbot", Scopes: []string{"sync", "api"}, Repos: []string{"suit", "ctx"}, Ceiling: "uv3bBC78", Days: 30}},
}})
if strings.Count(page, `data-hook="mint"`) != 1 {
t.Fatalf("FAIL: the page has %d mint blocks, want 1", strings.Count(page, `data-hook="mint"`))
}
if got, _ := hookAttr(t, page, "mint", "data-step"); got != "2" {
t.Errorf("FAIL: the mint block is on step %q", got)
}
for _, c := range []struct{ hook, attr, want string }{
{"setup-scope", "data-scopes", "sync,api"},
{"setup-reach", "data-repos", "suit,ctx"},
{"setup-ceiling", "data-ceiling", "uv3bBC78"},
{"setup-expires", "data-days", "30"},
} {
if got, ok := hookAttr(t, page, c.hook, c.attr); !ok || got != c.want {
t.Errorf("FAIL: %s %s is %q (present %v), want %q", c.hook, c.attr, got, ok, c.want)
}
}
for _, hook := range []string{"setup-collector", "setup-lapse"} {
if strings.Count(page, `data-hook="`+hook+`"`) != 1 {
t.Errorf("FAIL: %s drawn %d times", hook, strings.Count(page, `data-hook="`+hook+`"`))
}
}
// The mint step's lines sit inside its own step, before the buttons.
if strings.Index(page, `data-hook="setup-lapse"`) > strings.Index(page, `data-hook="signoff-approve"`) {
t.Errorf("FAIL: the limits come after the buttons")
}
plain := renderSetup(t, meSignOff{ID: "p", Agent: "a", Summary: "t", State: "pending", Requested: "2026-11-08T10:00:00Z",
Steps: []meSignOffStep{{Sentence: "one"}, {Sentence: "two"}}})
if strings.Contains(plain, `data-hook="mint"`) || strings.Contains(plain, `data-hook="setup-scope"`) {
t.Errorf("FAIL: a plan with nothing to mint shows limits")
}
if !t.Failed() {
t.Log("ok")
}
}
// A replacement request shows the limits of the token that takes the
// place of the old ones, and which tokens end.
func TestAReplacementRequestShowsItsLimitsAndWhatEnds(t *testing.T) {
page := renderSetup(t, meSignOff{ID: "r", Agent: "fbot", Summary: "s", State: "pending", Requested: "2026-11-08T10:00:00Z",
Mint: &api.MintView{Agent: "jva-bot", Scopes: []string{"api"}, Repos: []string{"test"}, Ceiling: "gjorzh28", Expires: "2027-01-05T00:00:00Z",
Replaces: []api.MintRevoked{{ID: "t1", Name: "work"}, {ID: "t2", Name: "ci"}}}})
for _, c := range []struct{ hook, attr, want string }{
{"mint", "data-agent", "jva-bot"},
{"mint-replaces", "data-tokens", "t1,t2"},
{"setup-scope", "data-scopes", "api"},
{"setup-reach", "data-repos", "test"},
{"setup-ceiling", "data-ceiling", "gjorzh28"},
{"setup-expires", "data-days", "0"},
{"setup-expires", "data-expires", "2027-01-05"},
} {
if got, ok := hookAttr(t, page, c.hook, c.attr); !ok || got != c.want {
t.Errorf("FAIL: %s %s is %q (present %v), want %q", c.hook, c.attr, got, ok, c.want)
}
}
for _, hook := range []string{"setup-collector", "setup-lapse", "signoff-approve"} {
if _, ok := hookAttr(t, page, hook, "data-hook"); !ok {
t.Errorf("FAIL: no %s", hook)
}
}
if !t.Failed() {
t.Log("ok")
}
}