package main
import (
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/artifact"
"craftdesign.group/quarry/internal/index"
)
type sweepChecks struct{}
func (sweepChecks) RequiredChecks(string, string) []string { return []string{"quarry"} }
func (sweepChecks) PremergeMode(string, string) bool { return false }
type sweepRuns struct{}
func (sweepRuns) CIRuns(string, int) ([]api.CIRun, error) { return nil, nil }
func (sweepRuns) CIRunsPage(string, int, int) ([]api.CIRun, error) { return nil, nil }
func (sweepRuns) CIRunsFor(string, string) ([]api.CIRun, error) { return nil, nil }
func (sweepRuns) CIRunsForPair(string, string) ([]api.CIRun, error) { return nil, nil }
func foldLanding(t *testing.T, ix *index.Index, hash, branch, tip string) {
t.Helper()
if err := ix.FoldNow([]index.Parsed{
{Hash: tip, Art: &artifact.Artifact{Type: artifact.Manifest, Date: "2026-01-01T09:00:00", User: "ada", Comment: "work",
Tags: []artifact.Tag{{Name: "*branch", Target: "*", Value: branch}}}},
{Hash: hash, Art: &artifact.Artifact{Type: artifact.Forum, ThreadTitle: "land " + branch, Date: "2026-01-01T10:00:00", User: "ada",
Body: []byte("zit-landing: 1\nbranch: " + branch + "\ntarget: trunk\n\nready\n")}},
}); err != nil {
t.Fatal(err)
}
}
// THE FOLD LOOP ASKS FOR A RUN FOR EVERY LANDING THAT ARRIVES BY PUSH —
// once, and never for the ones already open when the forge started.
//
// zit opens every landing by push, so landing.open's ask never fires
// for them; the sweep after each fold makes the same ask from the same
// producer (49323c33). The first sweep seeds what is open and asks
// nothing, or a restart would be a thousand landings opening at once.
func TestTheFoldLoopAsksForARunWhenAPushedLandingOpens(t *testing.T) {
ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
defer ix.Close()
const first, firstTip = "aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111", "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1"
const second, secondTip = "bbbb2222bbbb2222bbbb2222bbbb2222bbbb2222", "c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2c2"
foldLanding(t, ix, first, "feat/x", firstTip)
var asked []string
rg := newTestRegistrar(t, 25)
rg.deps = &api.Deps{Repos: api.NewRepos(map[string]*api.Repo{"r": {Index: ix}}),
Checks: sweepChecks{}, CI: sweepRuns{},
CITrigger: func(_, checkin string) error { asked = append(asked, checkin); return nil }}
rg.landingSweep("r", ix)
if len(asked) != 0 {
t.Fatalf("FAIL: the seeding sweep asked for runs: %v", asked)
}
foldLanding(t, ix, second, "feat/y", secondTip)
rg.landingSweep("r", ix)
if len(asked) != 1 || asked[0] != secondTip {
t.Fatalf("FAIL: after a landing arrived by push, asked %v, want one run at %.12s", asked, secondTip)
}
rg.landingSweep("r", ix)
if len(asked) != 1 {
t.Fatalf("FAIL: a landing already seen was asked for again: %v", asked)
}
t.Log("ok: a pushed landing gets its run asked for exactly once")
}