package api
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// trunkMoves adds a check-in to trunk on file, so every rung stands
// behind it.
func trunkMoves(t *testing.T, dir, file, body string, sync func() (int, error)) {
t.Helper()
zrun(t, dir, "update", "trunk")
zwrite(t, dir, file, body)
zrun(t, dir, "add", file)
zrun(t, dir, "commit", "-m", "trunk moves", "--user", "u")
if _, err := sync(); err != nil {
t.Fatal(err)
}
}
// landing.update says what it did: the merge it wrote and that the
// branch changed, then that a second call has nothing to do.
func TestUpdateAnswersTheMergeItWroteAndThenThatThereIsNothingToDo(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
before := ztip(t, dir, "low")
trunkMoves(t, dir, "t.txt", "t\n", sync)
got, err := r.Invoke(as("dan", "i"), "landing.update", landingSayIn{Repo: "test", ID: ids["low"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingUpdateOut)
now := ztip(t, dir, "low")
if now == before || !out.Changed || out.Hash != now || out.State != "open" || out.Say == "" {
t.Errorf("FAIL: the update answered %+v with the branch at %s (was %s)", out, now, before)
}
got, err = r.Invoke(as("dan", "i"), "landing.update", landingSayIn{Repo: "test", ID: ids["low"]})
if err != nil {
t.Fatalf("FAIL: a second update: %v", err)
}
again := got.(landingUpdateOut)
if again.Changed || again.Hash != "" || again.Say == "" || ztip(t, dir, "low") != now {
t.Errorf("FAIL: the second update answered %+v", again)
}
if !t.Failed() {
t.Log("ok")
}
}
// A press on a stack whose base stands behind trunk updates the base
// itself, runs the checks again and arms the stack; it does not stop
// and ask a person to press update.
func TestAStackWhoseBaseIsBehindIsUpdatedByThePress(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
d, _ := prefixDeps(t, dir, ix, sync, ci)
var runs []string
d.CIRunPair = func(_, branch, _, _ string) error { runs = append(runs, branch); return nil }
trunkMoves(t, dir, "t.txt", "t\n", sync)
before := ztip(t, dir, "low")
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("dan", "i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingLandOut)
if ztip(t, dir, "low") == before {
t.Errorf("FAIL: the base was not updated")
}
if len(out.Landed) != 0 || len(out.Armed) != 1 || out.Armed[0] != ids["high"] {
t.Errorf("FAIL: the press answered %+v, want nothing landed and the top armed", out)
}
if !strings.Contains(out.Reason, "updated") {
t.Errorf("FAIL: the reason does not say the base was updated: %q", out.Reason)
}
if len(runs) == 0 {
t.Errorf("FAIL: no checks were run again after the update")
}
if !t.Failed() {
t.Log("ok")
}
}
// A conflict stops the walk, writes nothing, and the reason says so (the
// walk's preflight reads it before any update is tried).
func TestAStackWhoseBaseCannotBeUpdatedSaysWhy(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
trunkMoves(t, dir, "low.txt", "trunk wrote this\n", sync)
before := ztip(t, dir, "low")
r := ops.NewRegistry()
if err := RegisterAll(r, d); err != nil {
t.Fatal(err)
}
got, err := r.Invoke(as("dan", "i"), "landing.land", landingLandIn{Repo: "test", ID: ids["high"]})
if err != nil {
t.Fatal(err)
}
out := got.(landingLandOut)
if ztip(t, dir, "low") != before {
t.Errorf("FAIL: a conflicting update wrote to the base")
}
if out.Stopped != ids["low"] || !strings.Contains(out.Reason, "conflict") || len(out.Armed) != 0 {
t.Errorf("FAIL: the press answered %+v, want the base stopped on a conflict, nothing armed", out)
}
if !t.Failed() {
t.Log("ok")
}
}
// The armed sweep does the same for a rung that is not green.
func TestTheSweepUpdatesAnArmedBaseThatIsBehindAndNotGreen(t *testing.T) {
dir, ids, ix, sync := threeRungs(t)
d, _ := prefixDeps(t, dir, ix, sync, &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}})
trunkMoves(t, dir, "t.txt", "t\n", sync)
before := ztip(t, dir, "low")
store := &waitStub{waiting: map[string]string{}, cleared: map[string]bool{}}
for _, b := range []string{"low", "mid", "high"} {
store.rows = append(store.rows, armRow(t, dir, ids, b))
}
if landed := d.AutoAcceptSweep(store); len(landed) != 0 {
t.Errorf("FAIL: the sweep landed %v", landed)
}
if ztip(t, dir, "low") == before {
t.Errorf("FAIL: the sweep did not update the base")
}
if store.lapsed[ids["low"]] || store.lapsed[ids["high"]] {
t.Errorf("FAIL: the update lapsed an arm: %v", store.lapsed)
}
if w := store.waiting[ids["low"]]; !strings.Contains(w, "updated") {
t.Errorf("FAIL: the base's autoWaiting does not say it was updated: %q", w)
}
if !t.Failed() {
t.Log("ok")
}
}
func armRow(t *testing.T, dir string, ids map[string]string, branch string) oltp.AutoAccept {
t.Helper()
return oltp.AutoAccept{Repo: "test", Landing: ids[branch], Account: "dan", Tip: ztip(t, dir, branch)}
}