Skip to content
quarry Artifact [2090c8f436]
Login

Artifact [2090c8f436]

Artifact 2090c8f436c805d73e96fa9cef6cba656e463400de1cb17ec0cd1992d1c01a05:


package api

import (
	"strings"
	"testing"

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

// Ticket #390: a settled landing is a record of what it proposed, so the
// check-ins, the runs and the answers about accepting it do not follow
// its branch after it settles.
func TestASettledLandingIsFrozenAtItsTip(t *testing.T) {
	dir, ids, ix, pipeSync := premergeStore(t)
	ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
	trunk0 := ztip(t, dir, "trunk")
	tip1 := ztip(t, dir, "one")
	ci.green(tip1, trunk0)

	zrun(t, dir, "update", "trunk")
	zrun(t, dir, "merge", "one")
	zrun(t, dir, "commit", "-m", "merge one from a clone", "--user", "u")
	zrun(t, dir, "update", "one")
	zwrite(t, dir, "one.txt", "one\nmore\n")
	zrun(t, dir, "commit", "-m", "new work on one", "--user", "u")
	tip2 := ztip(t, dir, "one")
	trunk1 := ztip(t, dir, "trunk")
	ci.green(tip2, trunk1)
	if _, err := pipeSync(); err != nil {
		t.Fatal(err)
	}

	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", LandingNumbers: landingNumberDB(t), CI: ci, Checks: mergeChecks{},
		Repos: NewRepos(map[string]*Repo{"test": {Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}); err != nil {
		t.Fatal(err)
	}
	get := func(id string) landingGetOut {
		t.Helper()
		g, err := r.Invoke(as("dan", "i8"), "landing.get", landingGetIn{Repo: "test", ID: id})
		if err != nil {
			t.Fatal(err)
		}
		return g.(landingGetOut)
	}
	first := get(ids[0])
	if first.Landing.State != "accepted" {
		t.Fatalf("setup: first landing reads %q, want accepted", first.Landing.State)
	}
	if first.CanAccept {
		t.Errorf("FAIL: an accepted landing reports canAccept")
	}
	if first.Behind != 0 {
		t.Errorf("FAIL: an accepted landing reports behind %d", first.Behind)
	}
	if first.MayLand {
		t.Errorf("FAIL: an accepted landing reports mayLand")
	}
	if len(first.Checks) == 0 {
		t.Errorf("FAIL: the accepted landing lists no checks; want the run on its own pair")
	}
	for _, c := range first.Checks {
		if !strings.Contains(c.Checkin, tip1) || strings.Contains(c.Checkin, tip2) {
			t.Errorf("FAIL: the accepted landing lists a run on %q, want only its own tip %s", c.Checkin, tip1)
		}
	}

	// The new landing, then rejected, is frozen the same way.
	out, err := r.Invoke(as("dan", "3"), "landing.open",
		landingOpenIn{Repo: "test", Branch: "one", Target: "trunk", Message: "propose the new work"})
	if err != nil {
		t.Fatal(err)
	}
	second := out.(landingOpenOut).ID
	if _, err := r.Invoke(as("dan", "i"), "landing.reject", landingSayIn{Repo: "test", ID: second, Message: "not this"}); err != nil {
		t.Fatal(err)
	}
	zwrite(t, dir, "one.txt", "one\nmore\nlater\n")
	zrun(t, dir, "commit", "-m", "later work on one", "--user", "u")
	tip3 := ztip(t, dir, "one")
	ci.green(tip3, trunk1)
	if _, err := pipeSync(); err != nil {
		t.Fatal(err)
	}
	rej := get(second)
	if rej.CanAccept || rej.Behind != 0 || rej.MayLand {
		t.Errorf("FAIL: a rejected landing reports canAccept %v, behind %d, mayLand %v", rej.CanAccept, rej.Behind, rej.MayLand)
	}
	for _, c := range rej.Checks {
		if strings.Contains(c.Checkin, tip3) {
			t.Errorf("FAIL: the rejected landing lists a run on %q, made after it was rejected", c.Checkin)
		}
	}
	if len(rej.Checks) == 0 {
		t.Errorf("FAIL: the rejected landing lists no checks; want the run on the tip it had")
	}
	again := get(ids[0])
	if len(again.Commits) != len(first.Commits) || len(again.Checks) != len(first.Checks) {
		t.Errorf("FAIL: the accepted landing changed with later work: %d commits and %d checks, then %d and %d",
			len(first.Commits), len(first.Checks), len(again.Commits), len(again.Checks))
	}
	if !t.Failed() {
		t.Log("ok")
	}
}