package index
import (
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/artifact"
)
// #444: a check-in folded before its parent reads as trunk until the
// parent arrives; BranchOf must say nothing for it rather than that.
func TestBranchOfWaitsForAnOrphanedCheckin(t *testing.T) {
ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ix.Close() })
tag := func(name, value string) artifact.Tag { return artifact.Tag{Name: name, Target: "*", Value: value} }
if err := ix.FoldNow([]Parsed{
edgeCi(1, 0, "2026-01-01T00:00:00", tag("*branch", "trunk")),
}); err != nil {
t.Fatal(err)
}
// 3 is a child of 2, which has not arrived; 2 opens branch alpha.
child := edgeCi(3, 2, "2026-01-03T00:00:00")
if err := ix.FoldNow([]Parsed{child}); err != nil {
t.Fatal(err)
}
got, err := ix.BranchOf([]string{child.Hash})
if err != nil {
t.Fatal(err)
}
if b, ok := got[child.Hash]; ok {
t.Errorf("FAIL: BranchOf answered %q for a check-in whose parent has not arrived", b)
}
first := edgeCi(2, 1, "2026-01-02T00:00:00", tag("*branch", "alpha"))
if err := ix.FoldNow([]Parsed{first}); err != nil {
t.Fatal(err)
}
got, err = ix.BranchOf([]string{first.Hash, child.Hash})
if err != nil {
t.Fatal(err)
}
for _, h := range []string{first.Hash, child.Hash} {
if got[h] != "alpha" {
t.Errorf("FAIL: %s reads %q once its parent arrived, want alpha", h[36:], got[h])
}
}
if !t.Failed() {
t.Log("ok")
}
}