Skip to content
quarry Artifact [e08b2c5ac3]
Login

Artifact [e08b2c5ac3]

Artifact e08b2c5ac3473b8fe47746ab1794af8d930eeb05b97a346af3e6e888c03fb05b:


package api

import (
	"testing"

	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/zitcli"
)

type pairExternal struct {
	m map[string][]oltp.ExternalCheck
}

func (f pairExternal) CheckReporterToken(string, string) bool               { return true }
func (f pairExternal) UpsertExternalCheck(string, oltp.ExternalCheck) error { return nil }
func (f pairExternal) MintReporterToken(string, string) (string, error)     { return "", nil }
func (f pairExternal) ReporterTokens(string) ([]oltp.ReporterToken, error)  { return nil, nil }
func (f pairExternal) RevokeReporterToken(string, string) error             { return nil }
func (f pairExternal) ExternalChecks(_, s string) ([]oltp.ExternalCheck, error) {
	return f.m[s], nil
}

// An external CI tested the merge with the OLD trunk and reported "ci" green on
// the pair; the only engine run on that pair (an unrequired lint) was cancelled.
// Trunk then moves. Old gate: stale refusal. Does the new one accept?
func TestAStaleMergeTestedByAnExternalCIIsRefusedWhateverTheEngineRan(t *testing.T) {
	for _, st := range []string{"cancelled", "running", "unclaimed"} {
		t.Run(st, func(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)
			p2 := "pair:" + ztip(t, dir, "two") + ":" + root
			ci.mu.Lock()
			ci.order = append(ci.order, p2)
			ci.mu.Unlock()
			ci.pairRuns.runs[p2] = []CIRun{{Checkin: p2, Workflow: "lint", Pipeline: "ci/lint.json", Status: st, Started: "2026-01-01T00:00:00Z"}}
			ext := pairExternal{m: map[string][]oltp.ExternalCheck{p2: {{Subject: p2, Context: "ci", State: "success", Updated: "2026-01-01T00:00:01Z"}}}}
			d := Deps{Version: "v", Checks: mergeChecks{}, CI: ci, External: ext, 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)
			}
			a := ageOf(t, d, ix, ids[1])
			if !a.Stale {
				t.Fatalf("FAIL: the merge should read stale: %+v", a)
			}
			_, err := d.landingSay(as("dan", "i"), landingSayIn{Repo: "test", ID: ids[1]}, "accept")
			if err == nil {
				t.Errorf("FAIL: accepted a merge whose only verdict was tested against a stale trunk (%s)", st)
			} else if !isStaleRefusal(err) {
				t.Errorf("FAIL: refused with %v, want the stale-verdict refusal", err)
			}
		})
	}
}

func TestARequiredNameExternalIsNotMetByAnyExternalReport(t *testing.T) {
	j := judgeRows(judgeIn{Required: []string{"external"}, Merge: true,
		Rows: []CIRun{{Workflow: "lint-bot", Pipeline: "external", Status: "success"}}})
	if len(j.Unmet) != 1 || j.Verdict != "ci-waiting" {
		t.Errorf("FAIL: an unrelated external report met a check named external: %+v", j)
	}
	j = judgeRows(judgeIn{Required: []string{"lint-bot"}, Merge: true,
		Rows: []CIRun{{Workflow: "lint-bot", Pipeline: "external", Status: "success"}}})
	if len(j.Unmet) != 0 {
		t.Errorf("FAIL: an external report by context did not meet its check: %+v", j)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A workflow name can cover two pipelines: a failure in either is a
// failure and both must be green to pass, in either order. (Trunk let
// one green beat one failed; this is a tightening.)
func TestAWorkflowCoveringTwoPipelinesNeedsBothGreen(t *testing.T) {
	a := CIRun{Workflow: "ci", Pipeline: "ci/a.json", Status: "completed"}
	b := CIRun{Workflow: "ci", Pipeline: "ci/b.json", Status: "failed"}
	c := CIRun{Workflow: "ci", Pipeline: "ci/b.json", Status: "running"}
	for _, rows := range [][]CIRun{{a, b}, {b, a}} {
		if j := judgeRows(judgeIn{Required: []string{"ci"}, Merge: true, Rows: rows}); j.Verdict != "ci-fail" || len(j.Unmet) != 1 {
			t.Errorf("FAIL: one green and one failed read %+v", j)
		}
	}
	for _, rows := range [][]CIRun{{a, c}, {c, a}} {
		if j := judgeRows(judgeIn{Required: []string{"ci"}, Merge: true, Rows: rows}); j.Verdict != "ci-waiting" {
			t.Errorf("FAIL: one green and one running read %+v", j)
		}
	}
	a2 := a
	a2.Pipeline = "ci/b.json"
	if j := judgeRows(judgeIn{Required: []string{"ci"}, Merge: true, Rows: []CIRun{a, {Workflow: "ci", Pipeline: "ci/c.json", Status: "completed"}}}); j.Verdict != "ci-pass" {
		t.Errorf("FAIL: both green read %+v", j)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}