package index
import (
"path/filepath"
"testing"
)
// The branch summary is rebuilt at open only when the query that
// derives it is not the one that built it (#408): a settled table is
// left alone, and a table from another query is replaced.
func TestBranchSummaryRebuildsOnlyWhenItsQueryChanged(t *testing.T) {
ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
defer ix.Close()
if _, err := ix.w.Exec(`INSERT INTO checkin (hash, date, user, comment) VALUES ('c1', '2026-01-01', 'u', 'm')`); err != nil {
t.Fatal(err)
}
marker := func() int {
t.Helper()
var n int
if err := ix.db.QueryRow(`SELECT count(*) FROM branch WHERE name = 'marker'`).Scan(&n); err != nil {
t.Fatal(err)
}
return n
}
if err := ix.refreshBranchSummary(); err != nil {
t.Fatal(err)
}
if _, err := ix.w.Exec(`INSERT INTO branch (name, tip, date) VALUES ('marker', 'c1', '2026-01-01')`); err != nil {
t.Fatal(err)
}
if err := ix.refreshBranchSummary(); err != nil {
t.Fatal(err)
}
if marker() != 1 {
t.Errorf("FAIL: a settled summary was rebuilt at open")
}
if _, err := ix.w.Exec(`UPDATE meta SET value = 'another query' WHERE key = 'branch_fold'`); err != nil {
t.Fatal(err)
}
if err := ix.refreshBranchSummary(); err != nil {
t.Fatal(err)
}
if marker() != 0 {
t.Errorf("FAIL: a summary built by another query survived")
}
}