package main
import (
"path/filepath"
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
// Push, error, close: a tip whose only merge errored is not answered,
// and the pair a landing shows is the same one tipAnswered counted. An
// older passing merge answers the tip and is the one shown; with none,
// the tip is re-tested and nothing passes as its verdict.
func TestTipAnsweredAgreesWithLatestPairOnAnErroredMerge(t *testing.T) {
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
defer db.Close()
rg := ®istrar{odb: db}
tip := strings.Repeat("a", 64)
t1, t2 := strings.Repeat("1", 64), strings.Repeat("2", 64)
add := func(id, target, status, started string) {
t.Helper()
pair := "pair:" + tip + ":" + target
if err := db.AddCIRun(oltp.CIRun{RunID: id, Repo: "r", Checkin: pair, Pair: pair, Workflow: "w", Pipeline: id, Status: status}); err != nil {
t.Fatal(err)
}
if err := db.CompleteCIRun(id, status, started); err != nil {
t.Fatal(err)
}
}
add("boom", t2, "error", "2026-01-02T00:00:00Z")
if rg.tipAnswered("r", tip, nil) {
t.Fatal("FAIL: an errored merge answered the tip")
}
if p, _ := db.LatestPairFor("r", tip); p != "" {
t.Fatalf("FAIL: the errored merge %q is still the latest pair", p)
}
add("pass", t1, "completed", "2026-01-01T00:00:00Z")
if !rg.tipAnswered("r", tip, nil) {
t.Fatal("FAIL: an older passing merge did not answer the tip")
}
if p, _ := db.LatestPairFor("r", tip); p != "pair:"+tip+":"+t1 {
t.Fatalf("FAIL: latest pair = %q, want the passing merge", p)
}
}