Skip to content
quarry Artifact [803a229f8a]
Login

Artifact [803a229f8a]

Artifact 803a229f8a4241bee8065fc998e0b53b3246cb2624f866064aedc8d0b9168d14:


package index

import "testing"

// The question branch protection has to ask before it trusts a rule.
//
// A pushed check-in resolves to exactly ONE branch — its branch field —
// and that is what the sync child matches protection globs against. So
// a rule naming any OTHER live name of the same branch can never fire,
// while the settings page reports it as protected (d76e42b832f7).
func TestAliasOfNamesWhatCheckinsActuallyResolveTo(t *testing.T) {
	ix := openIndex(t)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	ci(t, ix, "c2", "main", "2026-01-02T10:00:00", "renamed to main", "c1")
	tag(t, ix, "c2", "sym-trunk", "*", "2026-01-02T10:00:00")
	foldSymbols(t, ix)

	alias, is, err := ix.AliasOf("trunk")
	if err != nil {
		t.Fatal(err)
	}
	if !is || alias != "main" {
		t.Fatalf("FAIL: trunk reported alias=%q is=%v, want main/true: a rule protecting trunk "+
			"would refuse nothing and the page would call it protected", alias, is)
	}
	t.Logf("ok: trunk is an alias of %s", alias)
}

// The branch's own name is not an alias of itself, or protecting
// anything at all would be refused.
func TestAliasOfLeavesTheRealNameAlone(t *testing.T) {
	ix := openIndex(t)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	ci(t, ix, "c2", "main", "2026-01-02T10:00:00", "renamed", "c1")
	tag(t, ix, "c2", "sym-trunk", "*", "2026-01-02T10:00:00")
	foldSymbols(t, ix)

	if alias, is, err := ix.AliasOf("main"); err != nil || is {
		t.Fatalf("FAIL: main reported as an alias of %q (err %v)", alias, err)
	}
	t.Log("ok: main is its own name")
}

// A name nothing carries is NOT an alias. Protecting a release branch
// before the release exists is a reasonable thing to do, and refusing
// it would make the guard worse than the bug.
func TestAliasOfAllowsABranchThatDoesNotExistYet(t *testing.T) {
	ix := openIndex(t)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	foldSymbols(t, ix)

	if alias, is, err := ix.AliasOf("release-2027"); err != nil || is {
		t.Fatalf("FAIL: an unused name was called an alias of %q (err %v): "+
			"protecting a branch before it exists must stay possible", alias, err)
	}
	t.Log("ok: a name nothing carries is not an alias")
}

// Once the old name is cancelled it carries nothing, so it is not an
// alias either — it is simply gone, and a rule naming it is a different
// problem from a rule naming a live second name.
func TestAliasOfIgnoresACancelledName(t *testing.T) {
	ix := openIndex(t)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	ci(t, ix, "c2", "main", "2026-01-02T10:00:00", "renamed", "c1")
	tag(t, ix, "c2", "sym-trunk", "*", "2026-01-02T10:00:00")
	tag(t, ix, "c2", "sym-trunk", "-", "2026-01-03T10:00:00")
	foldSymbols(t, ix)

	alias, is, err := ix.AliasOf("trunk")
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("ok: a cancelled name reports alias=%q is=%v", alias, is)
}