package index
import (
"path/filepath"
"testing"
)
// Opening an index no longer checks its search table, and VerifySearch
// rebuilds one that disagrees with its content.
func TestVerifySearchRebuildsADriftedIndex(t *testing.T) {
path := filepath.Join(t.TempDir(), "index.db")
ix, err := Open(path)
if err != nil {
t.Fatal(err)
}
defer ix.Close()
rec := CheckinRecord{Hash: "aaaa000000000000000000000000000000000000000000000000000000000001",
Date: "2026-09-15T00:00:00", User: "dan", Comment: "zebraword comment"}
if err := ix.FoldCheckins([]CheckinRecord{rec}); err != nil {
t.Fatal(err)
}
// Drift: the content row goes with no delete reaching the fts table.
for _, q := range []string{
`DROP TRIGGER IF EXISTS search_doc_ad`,
`DROP TRIGGER IF EXISTS search_doc_ai`,
`DROP TRIGGER IF EXISTS search_doc_au`,
`DELETE FROM search_doc`,
} {
if _, err := ix.w.Exec(q); err != nil {
t.Fatal(err)
}
}
if _, err := ix.w.Exec(`INSERT INTO search_fts(search_fts, rank) VALUES('integrity-check', 1)`); err == nil {
t.Fatal("the test did not make the search index drift")
}
ix.VerifySearch()
searchConsistent(t, ix, "after VerifySearch")
}
// A store that goes away before its turn is left alone, without a panic
// or a rebuild attempt on a closed handle.
func TestVerifySearchOnAClosedIndexIsQuiet(t *testing.T) {
ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
ix.Close()
ix.VerifySearch()
}