package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/api"
)
// A run no runner has taken up is its own state on the landing page: not
// running, not failed, and it never reads as the passing it is not.
func TestALandingShowsARunNoRunnerHasTakenUp(t *testing.T) {
ix := landingIndex(t, false)
srv := checksServer(t, ix, map[string][]string{"trunk": {"test"}},
[]api.CIRun{{RunID: "run-1", Workflow: "test", Pipeline: "ci/test.json", Status: api.RunUnclaimed,
Started: "2026-01-01T10:00:00Z", WaitingSince: "2026-01-01T10:00:00Z"}}, "")
body := asDan(t, srv, "/r/alpha/landing/"+strings.Repeat("a1", 6))
if !strings.Contains(body, `data-hook="check-row" data-state="unclaimed"`) {
t.Errorf("FAIL: the check row does not carry the unclaimed state")
}
if strings.Contains(body, `data-hook="check-row" data-state="running"`) || strings.Contains(body, `data-hook="check-row" data-state="fail"`) {
t.Errorf("FAIL: an unclaimed run is drawn as running or failed")
}
if strings.Contains(body, `data-accept="go"`) {
t.Errorf("FAIL: the accept is open on a required check no runner has run")
}
if !t.Failed() {
t.Log("ok")
}
}
func TestUnclaimedIsNotAnEndedRun(t *testing.T) {
if ciStateOf(api.RunUnclaimed) != api.RunUnclaimed {
t.Errorf("FAIL: state %q", ciStateOf(api.RunUnclaimed))
}
if ciRunOver(api.RunUnclaimed) {
t.Errorf("FAIL: an unclaimed run reads as over")
}
l := landingDetail{Checks: []ciRunRow{{Workflow: "test", Status: api.RunUnclaimed}}}
if s := l.ChecksSummary(); s.State != "wait" || !s.Open {
t.Errorf("FAIL: summary %+v, want an open wait", s)
}
if !l.AnyRunning() {
t.Errorf("FAIL: Re-run is offered over a run that is already queued")
}
if !t.Failed() {
t.Log("ok")
}
}
// The panel counts the rows the verdict is computed from: an unrequired
// failing run or a stale merge's failure is not "a failing check" beside
// a pass.
func TestThePanelCountsOnlyTheJudgedRows(t *testing.T) {
pass := ciRunRow{Workflow: "test", Pipeline: "ci/test.json", Status: "completed"}
lint := ciRunRow{Workflow: "lint", Pipeline: "ci/lint.json", Status: "failed"}
l := landingDetail{Required: []string{"test"}, Checks: []ciRunRow{pass, lint}}
if s := l.ChecksSummary(); s.State != "pass" {
t.Errorf("FAIL: an unrequired failure made the panel %q", s.State)
}
l = landingDetail{Checks: []ciRunRow{pass, lint}, StaleCI: true}
if s := l.ChecksSummary(); s.State == "fail" {
t.Errorf("FAIL: a stale merge's failure headlines the panel")
}
l = landingDetail{Checks: []ciRunRow{pass, lint}}
if s := l.ChecksSummary(); s.State != "fail" {
t.Errorf("FAIL: with nothing required a failing run is a failure, got %q", s.State)
}
if !t.Failed() {
t.Log("ok")
}
}