package web
import (
"strings"
"testing"
)
func renderLandingWithRerun(t *testing.T, d *landingDetail) string {
t.Helper()
var buf strings.Builder
if err := noHooks.template(landingPage).ExecuteTemplate(&buf, "landing-body", pageData{Repo: "quarry", Landing: d, CanTrigger: true}); err != nil {
t.Fatal(err)
}
return buf.String()
}
func greenLanding(stale bool) *landingDetail {
d := &landingDetail{landingRow: landingRow{ID: "cccc2222cccc", Branch: "feat", Target: "trunk", State: "open", CI: "ci-pass", StaleCI: stale},
Required: []string{"test"}, TestedAgainst: strings.Repeat("a", 64), CanAccept: true, MayLand: true}
d.Mergeable = "clean"
d.Checks = []ciRunRow{{RunID: "r1", Workflow: "test", Pipeline: "test", Status: "success"}}
if stale {
d.TargetMovedBy = 3
}
return d
}
// A green verdict about an older trunk is not shown as green: the
// panel's state, the accept control and the re-run say stale.
func TestAStaleGreenReadsStaleOnTheLandingPage(t *testing.T) {
got := renderLandingWithRerun(t, greenLanding(true))
for _, want := range []string{`data-hook="checks-state" data-state="stale"`, `data-hook="stale-verdict"`, `data-hook="rerun-stale"`, `data-accept="stale"`} {
if !strings.Contains(got, want) {
t.Errorf("FAIL: a stale green panel misses %s", want)
}
}
if strings.Contains(got, `data-accept="go"`) {
t.Error("FAIL: a stale verdict on a gated target still offers accept")
}
fresh := renderLandingWithRerun(t, greenLanding(false))
if !strings.Contains(fresh, `data-hook="checks-state" data-state="pass"`) || strings.Contains(fresh, `data-hook="stale-verdict"`) || !strings.Contains(fresh, `data-accept="go"`) {
t.Error("FAIL: a fresh green panel should read pass and offer accept")
}
if !t.Failed() {
t.Log("ok: stale green reads stale, fresh green reads pass")
}
}
// The list's badge for a stale green is its own, never the pass badge.
func TestAStaleGreenIsNotAPassBadgeInTheList(t *testing.T) {
if got := string(landingRow{CI: "ci-pass", StaleCI: true}.Verdict()); !strings.Contains(got, `data-hook="ci-stale"`) || strings.Contains(got, `class="tl-badge ci-pass"`) {
t.Errorf("FAIL: a stale green row renders %q", got)
}
if got := string(landingRow{CI: "ci-pass"}.Verdict()); !strings.Contains(got, "ci-pass") {
t.Errorf("FAIL: a fresh green row renders %q", got)
}
if !t.Failed() {
t.Log("ok: the list marks a stale green as stale")
}
}