package api
import (
"slices"
"strings"
"testing"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
func row(pipeline, status string) CIRun {
return CIRun{RunID: "r-" + pipeline, Workflow: "w." + pipeline, Pipeline: "ci/" + pipeline + ".json", Status: status}
}
// The rule is landingci.go's doc comment; each row of the table is one
// sentence of it.
func TestLandingVerdictRule(t *testing.T) {
both := []string{"quarry", "race"}
for _, c := range []struct {
name string
in judgeIn
want string
}{
// A failed run on the current merge is a failure, whatever the tip says.
{"merge failed, tip passed", judgeIn{Tip: "ci-pass", Merge: true, Rows: []CIRun{row("quarry", "failed")}}, "ci-fail"},
{"merge failed, tip passed, required", judgeIn{Tip: "ci-pass", Merge: true, Required: both, Rows: []CIRun{row("quarry", "failed"), row("race", "completed")}}, "ci-fail"},
{"merge errored at checkout", judgeIn{Tip: "ci-pass", Merge: true, Required: both, Rows: []CIRun{row("quarry", "error")}}, "ci-fail"},
// The merge is what lands: it outranks a tip that says otherwise.
{"tip failed, merge passed", judgeIn{Tip: "ci-fail", Merge: true, Rows: []CIRun{row("quarry", "completed")}}, "ci-pass"},
{"tip failed, merge passed, required", judgeIn{Tip: "ci-fail", Merge: true, Required: both, Rows: []CIRun{row("quarry", "completed"), row("race", "completed")}}, "ci-pass"},
// A run still running is never a failure.
{"merge running, tip failed", judgeIn{Tip: "ci-fail", Merge: true, Rows: []CIRun{row("quarry", "running")}}, "ci-waiting"},
{"merge running, required", judgeIn{Tip: "ci-pass", Merge: true, Required: both, Rows: []CIRun{row("quarry", "completed"), row("race", "running")}}, "ci-waiting"},
{"merge waiting for a runner", judgeIn{Tip: "ci-pass", Merge: true, Rows: []CIRun{row("quarry", RunUnclaimed)}}, "ci-waiting"},
{"merge deferred", judgeIn{Tip: "ci-pass", Merge: true, Rows: []CIRun{row("quarry", "deferred")}}, "ci-waiting"},
// A stale merge counts for nothing, for or against.
{"stale failure, tip passed", judgeIn{Tip: "ci-pass", Merge: true, Stale: true, Rows: []CIRun{row("quarry", "failed")}}, "ci-pass"},
{"stale pass, tip failed", judgeIn{Tip: "ci-fail", Merge: true, Stale: true, Rows: []CIRun{row("quarry", "completed")}}, "ci-fail"},
{"stale pass, required", judgeIn{Tip: "ci-pass", Merge: true, Stale: true, Required: both, Rows: []CIRun{row("quarry", "completed"), row("race", "completed")}}, "ci-waiting"},
{"stale failure, required", judgeIn{Tip: "ci-pass", Merge: true, Stale: true, Required: both, Rows: []CIRun{row("quarry", "failed")}}, "ci-waiting"},
{"a bare tip's rows are not a merge to go stale", judgeIn{Merge: false, Stale: true, Rows: []CIRun{row("quarry", "completed")}}, "ci-pass"},
// A required check with no run is waiting, never pass.
{"required, no runs, tip passed", judgeIn{Tip: "ci-pass", Merge: true, Required: both}, "ci-waiting"},
{"required, one of two has run", judgeIn{Tip: "ci-pass", Merge: true, Required: both, Rows: []CIRun{row("quarry", "completed")}}, "ci-waiting"},
{"required, no runs, tip failed", judgeIn{Tip: "ci-fail", Merge: true, Required: both}, "ci-fail"},
// No runs at all, nothing required: the tip's own word, or nothing.
{"no runs, tip passed", judgeIn{Tip: "ci-pass", Merge: true}, "ci-pass"},
{"no runs, tip failed", judgeIn{Tip: "ci-fail", Merge: true}, "ci-fail"},
{"no runs, no tip verdict", judgeIn{Merge: true}, ""},
// Several pipelines: a failure in any is a failure.
{"one pipeline passes, one fails", judgeIn{Merge: true, Rows: []CIRun{row("quarry", "completed"), row("race", "failed")}}, "ci-fail"},
{"one passes, one running", judgeIn{Merge: true, Rows: []CIRun{row("quarry", "completed"), row("race", "running")}}, "ci-waiting"},
{"required: only the named pipelines count", judgeIn{Merge: true, Required: []string{"quarry"}, Rows: []CIRun{row("quarry", "completed"), row("race", "failed")}}, "ci-pass"},
// A cancelled (superseded) run is no answer.
{"only a cancelled run, tip passed", judgeIn{Tip: "ci-pass", Merge: true, Rows: []CIRun{row("quarry", "cancelled")}}, "ci-pass"},
{"only a cancelled run, required", judgeIn{Tip: "ci-pass", Merge: true, Required: both, Rows: []CIRun{row("quarry", "cancelled"), row("race", "completed")}}, "ci-waiting"},
{"cancelled beside a pass", judgeIn{Merge: true, Rows: []CIRun{row("quarry", "completed"), row("race", "canceled")}}, "ci-pass"},
// A re-run is the newest run of its pipeline.
{"failed, then re-run and passed", judgeIn{Merge: true, Required: []string{"quarry"}, Rows: []CIRun{
{RunID: "a", Workflow: "w.q", Pipeline: "ci/quarry.json", Status: "failed", Started: "2026-01-01T10:00:00Z"},
{RunID: "b", Workflow: "w.q", Pipeline: "ci/quarry.json", Status: "completed", Started: "2026-01-01T11:00:00Z"}}}, "ci-pass"},
// External checks are rows like any other.
{"external failure", judgeIn{Merge: true, Rows: []CIRun{{Workflow: "lint", Pipeline: "external", Status: "failure"}}}, "ci-fail"},
{"external pending", judgeIn{Merge: true, Rows: []CIRun{{Workflow: "lint", Pipeline: "external", Status: "pending"}}}, "ci-waiting"},
} {
if got := judgeRows(c.in).Verdict; got != c.want {
t.Errorf("FAIL: %s: verdict %q, want %q", c.name, got, c.want)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// The gate and the arm's notices read the same judgement: which
// required checks are unmet, which of those failed, which never ran.
func TestLandingJudgementNamesTheRequiredChecks(t *testing.T) {
j := judgeRows(judgeIn{Merge: true, Required: []string{"quarry", "race", "lint", "docs"}, Rows: []CIRun{
row("quarry", "completed"), row("race", "failed"), row("lint", "cancelled"), row("docs", "running")}})
if !slices.Equal(j.Unmet, []string{"race", "lint", "docs"}) {
t.Errorf("FAIL: unmet %v", j.Unmet)
}
if !slices.Equal(j.Failed, []string{"race"}) {
t.Errorf("FAIL: failed %v, want only race: a cancelled run is not a failure", j.Failed)
}
if !slices.Equal(j.NoRun, []string{"lint"}) {
t.Errorf("FAIL: no run %v, want only lint: a cancelled run answers nothing, a running one is on its way", j.NoRun)
}
if !t.Failed() {
t.Log("ok")
}
}
// The badge and the page read the same rows: what landing.get lists as
// the landing's checks is what the verdict was computed from. A tip run
// that failed, which the page does not list, does not decide the badge
// when the merge it does list has passed (#425).
func TestTheListedChecksAreTheJudgedChecks(t *testing.T) {
const tip = "f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1"
const trunk = "t0t0t0t0t0t0t0t0t0t0t0t0t0t0t0t0t0t0t0t0"
pair := "pair:" + tip + ":" + trunk
for _, c := range []struct {
name string
required []string
pair []CIRun
want string
}{
{"the merge passed, the tip run failed", nil, []CIRun{{RunID: "m", Checkin: pair, Pair: pair, Workflow: "w.q", Pipeline: "ci/quarry.json", Status: "completed"}}, "ci-pass"},
{"the merge passed, required", []string{"quarry"}, []CIRun{{RunID: "m", Checkin: pair, Pair: pair, Workflow: "w.q", Pipeline: "ci/quarry.json", Status: "completed"}}, "ci-pass"},
{"the merge failed, the tip run passed", nil, []CIRun{{RunID: "m", Checkin: pair, Pair: pair, Workflow: "w.q", Pipeline: "ci/quarry.json", Status: "failed"}}, "ci-fail"},
} {
tipVerdict := "ci-fail"
if c.want == "ci-fail" {
tipVerdict = "ci-pass"
}
ix := openLandingIndex(t, "feat", tip)
d := Deps{Repos: NewRepos(map[string]*Repo{"test": {Chat: "test", Index: ix}}),
CI: pushedStub{pair: map[string][]CIRun{pair: c.pair}, latestPair: map[string]string{tip: pair}},
Checks: checksStub{required: c.required}}
l, open, err := ix.OpenLandingOn("feat")
if err != nil || !open {
t.Fatalf("no open landing: %v", err)
}
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("dan", "i8"), "landing.get", landingGetIn{Repo: "test", ID: l.ID})
if err != nil {
t.Fatal(err)
}
out := got.(landingGetOut)
if len(out.Checks) != len(c.pair) || out.Checks[0].RunID != "m" {
t.Errorf("FAIL: %s: the page lists %+v, want exactly the merge's run", c.name, out.Checks)
}
if v := d.gatedVerdict("test", l, tipVerdict); v != c.want {
t.Errorf("FAIL: %s: verdict %q from the tip's %q, want %q", c.name, v, tipVerdict, c.want)
}
_, _, rows := d.landingRows("test", l, "")
if !slices.EqualFunc(rows, out.Checks, func(a, b CIRun) bool { return a.RunID == b.RunID && a.Status == b.Status }) {
t.Errorf("FAIL: %s: the judged rows %+v differ from the listed rows %+v", c.name, rows, out.Checks)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// The accept's note says what was tested only when a run of exactly
// that merge ended with an answer (#440): a merge nothing ran on, or
// whose only run was stopped, was not tested.
func TestAMergeNoteClaimsOnlyWhatRan(t *testing.T) {
for _, c := range []struct {
name string
runs []CIRun
want bool
}{
{"no run at all", nil, false},
{"only a cancelled run", []CIRun{{Status: "cancelled"}}, false},
{"only a run still going", []CIRun{{Status: "running"}}, false},
{"a run no runner took up", []CIRun{{Status: RunUnclaimed}}, false},
{"a run that errored before testing", []CIRun{{Status: "error"}}, false},
{"a passed run", []CIRun{{Status: "completed"}}, true},
{"a failed run", []CIRun{{Status: "failed"}}, true},
{"cancelled beside a passed run", []CIRun{{Status: "cancelled"}, {Status: "completed"}}, true},
} {
if got := answered(c.runs); got != c.want {
t.Errorf("FAIL: %s: answered is %v, want %v", c.name, got, c.want)
}
}
// The note is written from that fact alone.
if got := withTestedAgainst("land feat", "", "trunk", "feat"); got != "land feat" {
t.Errorf("FAIL: with nothing tested the message became %q", got)
}
if !t.Failed() {
t.Log("ok")
}
}
// newestPair answers the newest pair tried for a tip, run or not, as the
// store's NewestPairFor does.
type newestPair struct {
*mergeRuns
pair string
}
func (n newestPair) NewestPairFor(_, tip string) (string, error) {
if strings.HasPrefix(n.pair, "pair:"+tip+":") {
return n.pair, nil
}
return "", nil
}
// End to end: the target moved and this landing's only merge run was
// cancelled, so the merge check-in's message does not say checks tested
// it. One that was tested still says so (stalemerge_test).
func TestAMergeCheckinSaysNothingOfChecksThatNeverRan(t *testing.T) {
dir, ids, ix, pipeSync := premergeStore(t)
root := ztip(t, dir, "trunk")
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
ci.green(ztip(t, dir, "one"), root) // one is tested; two's only merge run was cancelled
two := "pair:" + ztip(t, dir, "two") + ":" + root
ci.runs[two] = []CIRun{{Checkin: two, Pair: two, Workflow: "ci", Pipeline: "ci", Status: "cancelled"}}
d := Deps{Version: "v", Checks: noChecks{}, CI: newestPair{ci, two}, Repos: NewRepos(map[string]*Repo{
"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
if _, err := d.landingSay(as("dan", "i"), landingSayIn{Repo: "test", ID: ids[0]}, "accept"); err != nil {
t.Fatal(err)
}
if _, err := d.landingSay(as("dan", "i"), landingSayIn{Repo: "test", ID: ids[1]}, "accept"); err != nil {
t.Fatalf("FAIL: an ungated accept was refused: %v", err)
}
if out := zrun(t, dir, "artifact", ztip(t, dir, "trunk")); strings.Contains(out, root[:12]) {
t.Errorf("FAIL: the merge's message names a tested trunk although no check ran:\n%s", out)
}
if !t.Failed() {
t.Log("ok")
}
}