package web
import (
"html/template"
"strings"
"testing"
)
// The badge rides `who`, which is the ONE place a name is drawn with
// its mark — so it reaches the timeline, tickets, the forum, chat,
// landings and attachments from a single rule, rather than from a
// convention every surface has to remember and one of them forgets.
func TestAnAgentIsMarkedWhereverItsNameIsDrawn(t *testing.T) {
old := agentOperator
t.Cleanup(func() { agentOperator = old })
agentOperator = func(name string) string {
if name == "zeitgeist" {
return "dmestas"
}
return ""
}
// Parsed ONCE: a template set refuses Parse after Execute.
tpl, err := template.Must(base.Clone()).New("agent-probe").Parse(`{{who "alpha" .}}`)
if err != nil {
t.Fatal(err)
}
render := func(user string) string {
var b strings.Builder
if err := tpl.Execute(&b, user); err != nil {
t.Fatal(err)
}
return b.String()
}
got := render("zeitgeist")
if !strings.Contains(got, `class="agent-tag"`) {
t.Errorf("FAIL: a machine-operated account is drawn unmarked: %s", got)
} else {
t.Log("ok: an agent's name carries the badge")
}
// The operator is in the title, not the text: the badge repeats on
// every row a machine wrote, and "who runs it" is the second
// question, asked once, not the first, asked constantly.
if !strings.Contains(got, `title="agent, operated by dmestas"`) {
t.Errorf("FAIL: the badge does not say who answers for it: %s", got)
} else {
t.Log("ok: the operator rides the title")
}
if strings.Contains(got, ">agent, operated by") {
t.Error("FAIL: the operator is in the badge's text, which repeats on every row")
}
if strings.Contains(render("jva"), "agent-tag") {
t.Error("FAIL: a person's account is marked as an agent")
} else {
t.Log("ok: a person's name is drawn plain")
}
}
// A forge with no accounts plane has no agents, because it has no
// accounts. The hook is nil there and nothing must panic on it.
func TestWithNoAccountsPlaneNothingIsAnAgent(t *testing.T) {
old := agentOperator
t.Cleanup(func() { agentOperator = old })
agentOperator = nil
if got := agentBadge("zeitgeist"); got != "" {
t.Errorf("FAIL: %q with no accounts plane", got)
} else {
t.Log("ok: no accounts plane, no agents, no panic")
}
}