Skip to content
quarry Artifact [3784445c72]
Login

Artifact [3784445c72]

Artifact 3784445c7261dacef7c5f656bdefa4b03b4f590838388b993410ecce58958f93:


package index

import (
	"path/filepath"
	"testing"
)

func countRows(t *testing.T, ix *Index, q string, args ...any) int {
	t.Helper()
	var n int
	if err := ix.db.QueryRow(q, args...).Scan(&n); err != nil {
		t.Fatal(err)
	}
	return n
}

// The event backfill is one-time work: a marker in meta records it is
// done, so a second open of the same unchanged index does not look at
// the event table again, and a rebuilt index (Reset drops meta) runs it
// again.
func TestEventBackfillRunsOnceAndAgainAfterARebuild(t *testing.T) {
	path := filepath.Join(t.TempDir(), "index.db")
	ix := openIndexAt(t, path)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	if _, err := ix.w.Exec(`DELETE FROM event`); err != nil {
		t.Fatal(err)
	}
	if err := ix.backfillEvents(); err != nil {
		t.Fatal(err)
	}
	if n := countRows(t, ix, `SELECT count(*) FROM event`); n != 1 {
		t.Errorf("FAIL: backfill wrote %d events, want 1", n)
	}
	if !ix.metaDone(eventsBackfilledKey) {
		t.Errorf("FAIL: the backfill left no marker")
	}
	// Removing the rows shows whether a second run touches them: a
	// second run would see an empty event table and write one back.
	if _, err := ix.w.Exec(`DELETE FROM event`); err != nil {
		t.Fatal(err)
	}
	if err := ix.backfillEvents(); err != nil {
		t.Fatal(err)
	}
	if n := countRows(t, ix, `SELECT count(*) FROM event`); n != 0 {
		t.Errorf("FAIL: a recorded backfill ran again and wrote %d events", n)
	}
	if err := ix.Reset(); err != nil {
		t.Fatal(err)
	}
	if ix.metaDone(eventsBackfilledKey) {
		t.Errorf("FAIL: a rebuilt index kept the marker")
	}
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	if _, err := ix.w.Exec(`DELETE FROM event`); err != nil {
		t.Fatal(err)
	}
	if err := ix.backfillEvents(); err != nil {
		t.Fatal(err)
	}
	if n := countRows(t, ix, `SELECT count(*) FROM event`); n != 1 {
		t.Errorf("FAIL: a rebuilt index did not backfill: %d events", n)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// An index that already has events is marked on its first open after
// the marker exists, which is what spares the count on every boot after.
func TestEventBackfillMarksAnIndexThatHasEvents(t *testing.T) {
	ix := openIndex(t)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	if n := countRows(t, ix, `SELECT count(*) FROM event`); n == 0 {
		if err := ix.backfillEvents(); err != nil {
			t.Fatal(err)
		}
	}
	if _, err := ix.w.Exec(`DELETE FROM meta WHERE key = ?`, eventsBackfilledKey); err != nil {
		t.Fatal(err)
	}
	if err := ix.backfillEvents(); err != nil {
		t.Fatal(err)
	}
	if !ix.metaDone(eventsBackfilledKey) {
		t.Errorf("FAIL: an index with events was not marked")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A second registration of an unchanged repository leaves the branch
// summary alone; a rebuilt index derives it again; a moved tip shows in
// it (the fold refreshes it).
func TestBranchSummarySkippedWhenSettledAndFollowsTheTip(t *testing.T) {
	path := filepath.Join(t.TempDir(), "index.db")
	ix := openIndexAt(t, path)
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	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.Close(); err != nil {
		t.Fatal(err)
	}
	ix = openIndexAt(t, path)
	if n := countRows(t, ix, `SELECT count(*) FROM branch WHERE name = 'marker'`); n != 1 {
		t.Errorf("FAIL: reopening an unchanged index refreshed its branch summary")
	}
	// A moved tip: the fold's refresh brings the summary to it.
	ci(t, ix, "c2", "trunk", "2026-01-02T10:00:00", "second", "c1")
	foldBranches(t, ix)
	if n := countRows(t, ix, `SELECT count(*) FROM branch WHERE name = 'trunk' AND tip = 'c2'`); n != 1 {
		t.Errorf("FAIL: a moved tip is not in the summary")
	}
	// A rebuilt index has no record of a summary, so it derives one.
	if err := ix.Reset(); 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)
	}
	ci(t, ix, "c1", "trunk", "2026-01-01T10:00:00", "first")
	if err := ix.refreshBranchSummary(); err != nil {
		t.Fatal(err)
	}
	if n := countRows(t, ix, `SELECT count(*) FROM branch WHERE name = 'marker'`); n != 0 {
		t.Errorf("FAIL: a rebuilt index kept a summary nothing recorded")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}