package index
import (
"context"
"errors"
"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 checkin_fts_ad`,
`DROP TRIGGER IF EXISTS checkin_fts_ai`,
`DROP TRIGGER IF EXISTS checkin_fts_au`,
`UPDATE checkin SET comment = 'quokkaword comment'`,
} {
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")
}
// The same, for the documents search_doc holds: a wiki page's text goes
// with no delete reaching the fts table.
func TestVerifySearchRebuildsADriftedSearchDocIndex(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 search_doc (hash, kind, ref, label, text)
VALUES ('wiki:Home', 'wiki', 'Home', 'Home', 'zebraword page')`); err != nil {
t.Fatal(err)
}
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()
}
// A check cut short by the maintenance lane's shutdown is not a
// disagreement: it reports the cancellation and rebuilds nothing.
func TestVerifySearchContextCancelledLeavesADriftedIndexAlone(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)
}
for _, q := range []string{
`DROP TRIGGER IF EXISTS checkin_fts_ad`,
`DROP TRIGGER IF EXISTS checkin_fts_ai`,
`DROP TRIGGER IF EXISTS checkin_fts_au`,
`UPDATE checkin SET comment = 'quokkaword comment'`,
} {
if _, err := ix.w.Exec(q); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := ix.VerifySearchContext(ctx); !errors.Is(err, context.Canceled) {
t.Errorf("FAIL: a cancelled check returned %v", err)
}
if _, err := ix.w.Exec(`INSERT INTO search_fts(search_fts, rank) VALUES('integrity-check', 1)`); err == nil {
t.Errorf("FAIL: a cancelled check rebuilt the index")
}
if err := ix.VerifySearchContext(context.Background()); err != nil {
t.Errorf("FAIL: a live check returned %v", err)
}
searchConsistent(t, ix, "after a live check")
if !t.Failed() {
t.Log("ok")
}
}
// A rebuild the context cuts off is reported as the cancellation, not as
// a clean run: the maintenance page shows the last result from this.
// The cancel lands between detecting the drift and the rebuild
// statement; cutting inside a statement is a matter of timing no test
// can pin.
func TestVerifySearchContextReportsAnInterruptedRebuild(t *testing.T) {
ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
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)
}
for _, q := range []string{
`DROP TRIGGER IF EXISTS checkin_fts_ad`,
`DROP TRIGGER IF EXISTS checkin_fts_ai`,
`DROP TRIGGER IF EXISTS checkin_fts_au`,
`UPDATE checkin SET comment = 'quokkaword comment'`,
} {
if _, err := ix.w.Exec(q); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
beforeSearchRebuild = cancel
defer func() { beforeSearchRebuild = nil }()
if err := ix.VerifySearchContext(ctx); !errors.Is(err, context.Canceled) {
t.Errorf("FAIL: an interrupted rebuild returned %v", err)
}
if !t.Failed() {
t.Log("ok")
}
}