Skip to content
quarry Artifact [4a31cd21c0]
Login

Artifact [4a31cd21c0]

Artifact 4a31cd21c0f17ab34d27d03c564b700ed87057d3cb01b8677d8659df1f8029e9:


package api

import (
	"errors"
	"strconv"
	"strings"
	"testing"

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

// RE-RUN ASKS FOR WHAT ITS PAGE IS ABOUT.
//
// A short hash is resolved before the run is filed: filed under the
// prefix, a re-run never appeared on the check-in it tested (zeitforge,
// 2026-09-29). A landing's Re-run re-tests its merge with the target as
// it is now; a merge run's Re-run re-runs that exact pair.
func TestReRunAsksForWhatItsPageIsAbout(t *testing.T) {
	tip := strings.Repeat("ab", 20)
	ix := openLandingIndex(t, "feat", tip)
	var tipRuns []string
	var pairRuns [][3]string
	d := Deps{Repos: NewRepos(map[string]*Repo{"alpha": {Chat: "alpha", Index: ix}}),
		CITrigger: func(_, checkin, _ string) error { tipRuns = append(tipRuns, checkin); return nil },
		CIRunPair: func(_, branch, b, tt string) error { pairRuns = append(pairRuns, [3]string{branch, b, tt}); return nil }}
	repo, _ := d.Repos.Get("alpha")
	trigger := func(in ciTriggerIn) error {
		in.Repo = "alpha"
		return d.ciTrigger(repo, in, "")
	}

	if err := trigger(ciTriggerIn{Checkin: tip[:12]}); err != nil || len(tipRuns) != 1 || tipRuns[0] != tip {
		t.Fatalf("FAIL: a short hash ran %v (%v), want the full hash %s", tipRuns, err, tip)
	}
	if err := trigger(ciTriggerIn{Checkin: "abcdef999999"}); !errors.Is(err, ops.ErrNotFound) {
		t.Fatalf("FAIL: a prefix naming nothing answered %v, want not found", err)
	}

	trunk, _ := ix.BranchTip("trunk")
	if err := trigger(ciTriggerIn{Landing: landingHash[:12]}); err != nil || len(pairRuns) != 1 ||
		pairRuns[0] != [3]string{"feat", tip, trunk} {
		t.Fatalf("FAIL: a landing's Re-run ran %v (%v), want feat's merge with trunk now", pairRuns, err)
	}

	b, tt := strings.Repeat("1", 64), strings.Repeat("2", 64)
	if err := trigger(ciTriggerIn{Checkin: "pair:" + b + ":" + tt}); err != nil || len(pairRuns) != 2 ||
		pairRuns[1] != [3]string{"", b, tt} {
		t.Fatalf("FAIL: a merge run's Re-run ran %v (%v), want that exact pair", pairRuns, err)
	}
	t.Log("ok: a prefix resolves, a landing re-tests its merge, a pair re-runs itself")
}

// ci.trigger names a landing the way every landing route does: by number,
// by !N, or by id.
func TestCITriggerTakesALandingByNumberBangOrID(t *testing.T) {
	tip := strings.Repeat("ab", 20)
	ix := openLandingIndex(t, "feat", tip)
	var pairRuns [][3]string
	d := Deps{LandingNumbers: landingNumberDB(t),
		Repos:     NewRepos(map[string]*Repo{"alpha": {Chat: "alpha", Index: ix}}),
		CIRunPair: func(_, branch, b, tt string) error { pairRuns = append(pairRuns, [3]string{branch, b, tt}); return nil }}
	repo, _ := d.Repos.Get("alpha")
	n := d.landingNumberOf("alpha", repo, landingHash)
	if n == 0 {
		t.Fatalf("FAIL: the landing has no number")
	}
	for _, ref := range []string{strconv.Itoa(n), "!" + strconv.Itoa(n), landingHash, landingHash[:12]} {
		if err := d.ciTrigger(repo, ciTriggerIn{Repo: "alpha", Landing: ref}, ""); err != nil {
			t.Errorf("FAIL: ci.trigger with landing %q: %v", ref, err)
		}
	}
	if len(pairRuns) != 4 {
		t.Fatalf("FAIL: %d runs started, want 4", len(pairRuns))
	}
	for _, r := range pairRuns {
		if r != pairRuns[0] {
			t.Errorf("FAIL: runs differ: %v", pairRuns)
		}
	}
	if err := d.ciTrigger(repo, ciTriggerIn{Repo: "alpha", Landing: "!999"}, ""); !errors.Is(err, ops.ErrNotFound) {
		t.Errorf("FAIL: a number naming nothing answered %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A failed armed landing's waiting words name the call that re-runs it:
// the landing has no re-run route of its own.
func TestAFailedArmedLandingNamesTheCallThatReRunsIt(t *testing.T) {
	dir, ids, ix, sync := threeRungs(t)
	ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
	pair := "pair:" + ztip(t, dir, "low") + ":" + ztip(t, dir, "trunk")
	ci.runs[pair] = []CIRun{{Checkin: pair, Workflow: "ci", Pipeline: "ci", Status: "failure"}}
	ci.order = append(ci.order, pair)
	d, _ := prefixDeps(t, dir, ix, sync, ci)
	d.LandingNumbers = landingNumberDB(t)
	store := &waitStub{waiting: map[string]string{}, cleared: map[string]bool{},
		rows: []oltp.AutoAccept{{Repo: "test", Landing: ids["low"], Account: "dan", Tip: ztip(t, dir, "low")}}}
	d.AutoAcceptSweep(store)
	repo, _ := d.Repos.Get("test")
	n := d.landingNumberOf("test", repo, ids["low"])
	want := `POST /api/v1/r/test/ci/trigger {"landing":"` + strconv.Itoa(n) + `"}`
	if w := store.waiting[ids["low"]]; n == 0 || !strings.Contains(w, want) {
		t.Errorf("FAIL: the waiting words %q do not name %s", w, want)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}