Skip to content
quarry Artifact [8dc3d7aced]
Login

Artifact [8dc3d7aced]

Artifact 8dc3d7aceddef3c4dd730cbcf37e046a35ba237a20eacf02f781b8af5e54fca0:


package index

import "testing"

func ls(rows ...LandingSummary) []LandingSummary { return rows }
func open(id, branch, target string) LandingSummary {
	return LandingSummary{ID: id, Branch: branch, Target: target, State: "open", Title: "t-" + id}
}

// RFC 0006: follow target → branch across open landings. The one whose
// target is a plain branch is the base; position counts from it.
func TestStrataIsReadFromTargets(t *testing.T) {
	rows := ls(open("c", "feat-c", "feat-b"), open("a", "feat-a", "trunk"), open("b", "feat-b", "feat-a"),
		open("x", "lone", "trunk"))
	AttachStrata(rows)
	got := map[string]LandingSummary{}
	for _, r := range rows {
		got[r.ID] = r
	}
	for id, want := range map[string][2]int{"a": {1, 3}, "b": {2, 3}, "c": {3, 3}} {
		if got[id].Stack != "a" || got[id].Position != want[0] || got[id].Depth != want[1] {
			t.Errorf("%s: stack=%q pos=%d depth=%d, want a/%d/%d", id, got[id].Stack, got[id].Position, got[id].Depth, want[0], want[1])
		}
	}
	if got["x"].Stack != "" {
		t.Errorf("a standalone landing was put in a stack: %+v", got["x"])
	}
	// Five open landings, two decisions: the stack's base and the loner.
	if n := Decisions(rows); n != 2 {
		t.Errorf("decisions=%d, want 2", n)
	}
	st := StrataOf(rows, "c")
	if st.Base != "a" || len(st.Segments) != 3 || st.Segments[0].ID != "a" || st.Segments[2].ID != "c" {
		t.Fatalf("StrataOf(c) = %+v", st)
	}
	if !st.Segments[0].Landable || st.Segments[1].Landable || st.Segments[2].Landable {
		t.Errorf("landable marks wrong: %+v", st.Segments)
	}
}

// A base that was rejected: everything above it is stacked on work
// nobody will land, and the chain says so rather than reading the next
// rung as a fresh base.
func TestStrataFlagsARejectedBase(t *testing.T) {
	rows := ls(
		LandingSummary{ID: "a", Branch: "feat-a", Target: "trunk", State: "rejected"},
		open("b", "feat-b", "feat-a"), open("c", "feat-c", "feat-b"))
	AttachStrata(rows)
	for _, r := range rows[1:] {
		if r.Broken != "base rejected" {
			t.Errorf("%s: broken=%q, want base rejected", r.ID, r.Broken)
		}
	}
	if st := StrataOf(rows, "c"); st.Broken != "base rejected" || st.Segments[0].Landable {
		t.Errorf("a stack on a rejected base offers a landable rung: %+v", st)
	}
}

// An accepted base not yet retargeted: the rung above is next, and is
// the base of what remains.
func TestStrataMovesUpWhenTheBaseLands(t *testing.T) {
	rows := ls(
		LandingSummary{ID: "a", Branch: "feat-a", Target: "trunk", State: "accepted"},
		open("b", "feat-b", "feat-a"), open("c", "feat-c", "feat-b"))
	AttachStrata(rows)
	var b LandingSummary
	for _, r := range rows {
		if r.ID == "b" {
			b = r
		}
	}
	if b.Stack != "b" || b.Position != 1 || b.Broken != "" {
		t.Fatalf("after the base landed, b = %+v, want the new base", b)
	}
	if n := Decisions(rows); n != 1 {
		t.Errorf("decisions=%d, want 1", n)
	}
}

// A cycle is expressible and must be a rendering fault, not a hang.
func TestStrataReportsACycle(t *testing.T) {
	rows := ls(open("a", "feat-a", "feat-b"), open("b", "feat-b", "feat-a"))
	AttachStrata(rows)
	for _, r := range rows {
		if r.Broken != "cycle" {
			t.Errorf("%s: broken=%q, want cycle", r.ID, r.Broken)
		}
	}
}

// A superseded landing is not a rung: the current landing on the branch is.
func TestStrataSkipsSupersededLandings(t *testing.T) {
	old := open("a0", "feat-a", "trunk")
	old.SupersededBy = "a"
	rows := ls(old, open("a", "feat-a", "trunk"), open("b", "feat-b", "feat-a"))
	AttachStrata(rows)
	for _, r := range rows {
		if r.ID == "b" && r.Stack != "a" {
			t.Errorf("b stacked on %q, want the current landing a", r.Stack)
		}
		if r.ID == "a0" && r.Stack != "" {
			t.Error("the superseded landing was given a stack")
		}
	}
}