Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package testutil
import (
"context"
"fmt"
"path/filepath"
"strings"
"testing"

Expand All @@ -26,6 +27,24 @@ func OpenTestDB(t *testing.T) *store.Store {
return st
}

// OpenTestDBFile is OpenTestDB on a file-backed store, returning the db path for reopen/stat tests.
func OpenTestDBFile(t *testing.T) (*store.Store, string) {
t.Helper()
path := filepath.Join(t.TempDir(), "test.db")

st, err := store.Open(path)
if err != nil {
t.Fatalf("Open: %v", err)
}

if err := st.Migrate(context.Background()); err != nil {
t.Fatalf("Migrate: %v", err)
}

t.Cleanup(func() { _ = st.Close() })
return st, path
}

func LogTree(t *testing.T, st *store.Store) {
t.Helper()
ctx := context.Background()
Expand Down
33 changes: 7 additions & 26 deletions pkg/doctor/doctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,14 @@ import (
"context"
"database/sql"
"encoding/json"
"path/filepath"
"strings"
"testing"

"github.com/radimsem/remindb/pkg/store"
"github.com/radimsem/remindb/internal/testutil"
)

func newCleanStore(t *testing.T) (*store.Store, string) {
t.Helper()

dir := t.TempDir()
path := filepath.Join(dir, "doctor.db")

st, err := store.Open(path)
if err != nil {
t.Fatalf("Open: %v", err)
}
t.Cleanup(func() { _ = st.Close() })

if err := st.Migrate(context.Background()); err != nil {
t.Fatalf("Migrate: %v", err)
}
return st, path
}

func TestRunCleanDB(t *testing.T) {
st, _ := newCleanStore(t)
st, _ := testutil.OpenTestDBFile(t)

report := Run(context.Background(), st)

Expand Down Expand Up @@ -66,7 +47,7 @@ func TestRunCleanDB(t *testing.T) {
}

func TestHealFixesBrokenFTS(t *testing.T) {
st, path := newCleanStore(t)
st, path := testutil.OpenTestDBFile(t)

insertSampleNodes(t, path)

Expand Down Expand Up @@ -99,7 +80,7 @@ func TestHealFixesBrokenFTS(t *testing.T) {
}

func TestHealIsIdempotent(t *testing.T) {
st, _ := newCleanStore(t)
st, _ := testutil.OpenTestDBFile(t)

first := Heal(context.Background(), st)
second := Heal(context.Background(), st)
Expand All @@ -116,7 +97,7 @@ func TestHealIsIdempotent(t *testing.T) {
}

func TestReportJSONShape(t *testing.T) {
st, _ := newCleanStore(t)
st, _ := testutil.OpenTestDBFile(t)

report := Run(context.Background(), st)

Expand Down Expand Up @@ -151,7 +132,7 @@ func TestReportJSONShape(t *testing.T) {
}

func TestReportTextShape(t *testing.T) {
st, _ := newCleanStore(t)
st, _ := testutil.OpenTestDBFile(t)

report := Run(context.Background(), st)

Expand Down Expand Up @@ -254,7 +235,7 @@ func TestWriteTextHeader(t *testing.T) {
}

func TestWriteTextHeaderHealsToHealthy(t *testing.T) {
st, path := newCleanStore(t)
st, path := testutil.OpenTestDBFile(t)

insertSampleNodes(t, path)
if err := breakFTSSync(path); err != nil {
Expand Down
23 changes: 4 additions & 19 deletions pkg/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@ import (
"strings"
"testing"

"github.com/radimsem/remindb/internal/testutil"
"github.com/radimsem/remindb/pkg/inspect"
"github.com/radimsem/remindb/pkg/store"
)

func openTestStore(t *testing.T) *store.Store {
t.Helper()

st, err := store.Open(":memory:")
if err != nil {
t.Fatalf("Open: %v", err)
}

if err := st.Migrate(context.Background()); err != nil {
t.Fatalf("Migrate: %v", err)
}

t.Cleanup(func() { _ = st.Close() })
return st
}

func testNode(id, parent, nodeType string) *store.Node {
return &store.Node{
ID: id, ParentID: parent,
Expand All @@ -35,7 +20,7 @@ func testNode(id, parent, nodeType string) *store.Node {
}

func TestCollect_Empty(t *testing.T) {
st := openTestStore(t)
st := testutil.OpenTestDB(t)
ctx := context.Background()

s, err := inspect.Collect(ctx, st)
Expand All @@ -52,7 +37,7 @@ func TestCollect_Empty(t *testing.T) {
}

func TestCollect_PopulatesAllFields(t *testing.T) {
st := openTestStore(t)
st := testutil.OpenTestDB(t)
ctx := context.Background()

n1 := testNode("aaaaaaaa", "", "heading")
Expand Down Expand Up @@ -113,7 +98,7 @@ func TestCollect_PopulatesAllFields(t *testing.T) {
}

func TestCollect_RelationCountIncludesPending(t *testing.T) {
st := openTestStore(t)
st := testutil.OpenTestDB(t)
ctx := context.Background()

for _, n := range []*store.Node{
Expand Down
18 changes: 4 additions & 14 deletions pkg/mcp/resources/doctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"testing"

"github.com/radimsem/remindb/internal/testutil"
"github.com/radimsem/remindb/pkg/doctor"
"github.com/radimsem/remindb/pkg/store"
)
Expand Down Expand Up @@ -60,26 +61,15 @@ func assertCLIParity(t *testing.T, st *store.Store, got map[string]any, wantStat
}

func TestHandleDoctor_PassParity(t *testing.T) {
st := openGraphStore(t)
st := testutil.OpenTestDB(t)
d := &Deps{Store: st}

got := readDoctor(t, d)
assertCLIParity(t, st, got, "pass")
}

func TestHandleDoctor_WarnParity(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "doctor.db")

st, err := store.Open(path)
if err != nil {
t.Fatalf("Open: %v", err)
}

t.Cleanup(func() { _ = st.Close() })
if err := st.Migrate(context.Background()); err != nil {
t.Fatalf("Migrate: %v", err)
}
st, path := testutil.OpenTestDBFile(t)

// A snapshot whose compile_root no longer exists trips stale_compile_root → warn.
db, err := sql.Open("sqlite", path)
Expand All @@ -88,7 +78,7 @@ func TestHandleDoctor_WarnParity(t *testing.T) {
}

defer func() { _ = db.Close() }()
gone := filepath.Join(dir, "vanished")
gone := filepath.Join(filepath.Dir(path), "vanished")
if _, err := db.Exec(
`INSERT INTO snapshots (cursor_hash, parent_id, message, compile_root) VALUES ('h', NULL, 'm', ?)`,
gone,
Expand Down
21 changes: 3 additions & 18 deletions pkg/mcp/resources/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"testing"

"github.com/radimsem/remindb/internal/testutil"
"github.com/radimsem/remindb/pkg/store"
)

Expand All @@ -16,25 +17,9 @@ func graphNode_(id string) *store.Node {
}
}

func openGraphStore(t *testing.T) *store.Store {
t.Helper()

st, err := store.Open(":memory:")
if err != nil {
t.Fatalf("Open: %v", err)
}

if err := st.Migrate(context.Background()); err != nil {
t.Fatalf("Migrate: %v", err)
}
t.Cleanup(func() { _ = st.Close() })

return st
}

// Fixture: parsed n1→n2, manual n2→n3, pending n1→"Unresolved"; n4 is orphan.
func TestHandleGraph_ParsedManualPending(t *testing.T) {
st := openGraphStore(t)
st := testutil.OpenTestDB(t)
ctx := context.Background()

for _, id := range []string{"aaaaaaaaaa1", "bbbbbbbbbb1", "cccccccccc1", "dddddddddd1"} {
Expand Down Expand Up @@ -107,7 +92,7 @@ func TestHandleGraph_ParsedManualPending(t *testing.T) {
}

func TestHandleGraph_EmptyDBStableShape(t *testing.T) {
st := openGraphStore(t)
st := testutil.OpenTestDB(t)
d := &Deps{Store: st}

body, err := d.graphBody(context.Background())
Expand Down
11 changes: 2 additions & 9 deletions pkg/mcp/tools/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

gomcp "github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/radimsem/remindb/internal/redaction"
"github.com/radimsem/remindb/internal/testutil"
"github.com/radimsem/remindb/pkg/compiler"
"github.com/radimsem/remindb/pkg/config"
"github.com/radimsem/remindb/pkg/query"
Expand All @@ -23,15 +24,7 @@ import (
func setup(t *testing.T) (*Deps, *store.Store) {
t.Helper()

st, err := store.Open(":memory:")
if err != nil {
t.Fatalf("Open: %v", err)
}

if err := st.Migrate(context.Background()); err != nil {
t.Fatalf("Migrate: %v", err)
}
t.Cleanup(func() { _ = st.Close() })
st := testutil.OpenTestDB(t)

tracker, err := temperature.NewTracker(st, "", temperature.DefaultConfig(), nil)
if err != nil {
Expand Down
Loading
Loading