Skip to content

Commit 0f66288

Browse files
authored
refactor(testutil): adopt OpenTestDB/OpenTestDBFile across tests (#152)
## Summary Closes #149 ## Self-review ### Touched **Pipeline** - [ ] `pkg/parser/` — fuzz target covers any new shape - [ ] `pkg/transformer/` or `pkg/diff/` — ID stability / hash inputs unchanged - [ ] `pkg/emitter/` — exactly one snapshot per write - [x] `pkg/store/` + `migrations/` — FTS5 triggers in sync; `add-store-query` followed - [ ] `pkg/query/` or `pkg/compiler/` — token budget honored **MCP surface** - [x] `pkg/mcp/` — `skills/remind` (read) or `skills/memoize` (write) updated - [ ] `pkg/temperature/` — both public skills reflect new values **Edges** - [ ] `cmd/remindb/` — README CLI section updated - [ ] `internal/ignore/`, `internal/tempfile/`, `internal/bench/` — relevant fuzz / fixture / scenario added - [ ] `plugins/<agent>/` — manifest version bumped if shipping ### Process - [ ] Tested manually via CLI or local MCP plugin install - [x] `make test-all` + `make fuzz` green
1 parent 345e07f commit 0f66288

8 files changed

Lines changed: 63 additions & 140 deletions

File tree

internal/testutil/testutil.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testutil
33
import (
44
"context"
55
"fmt"
6+
"path/filepath"
67
"strings"
78
"testing"
89

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

30+
// OpenTestDBFile is OpenTestDB on a file-backed store, returning the db path for reopen/stat tests.
31+
func OpenTestDBFile(t *testing.T) (*store.Store, string) {
32+
t.Helper()
33+
path := filepath.Join(t.TempDir(), "test.db")
34+
35+
st, err := store.Open(path)
36+
if err != nil {
37+
t.Fatalf("Open: %v", err)
38+
}
39+
40+
if err := st.Migrate(context.Background()); err != nil {
41+
t.Fatalf("Migrate: %v", err)
42+
}
43+
44+
t.Cleanup(func() { _ = st.Close() })
45+
return st, path
46+
}
47+
2948
func LogTree(t *testing.T, st *store.Store) {
3049
t.Helper()
3150
ctx := context.Background()

pkg/doctor/doctor_test.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,14 @@ import (
55
"context"
66
"database/sql"
77
"encoding/json"
8-
"path/filepath"
98
"strings"
109
"testing"
1110

12-
"github.com/radimsem/remindb/pkg/store"
11+
"github.com/radimsem/remindb/internal/testutil"
1312
)
1413

15-
func newCleanStore(t *testing.T) (*store.Store, string) {
16-
t.Helper()
17-
18-
dir := t.TempDir()
19-
path := filepath.Join(dir, "doctor.db")
20-
21-
st, err := store.Open(path)
22-
if err != nil {
23-
t.Fatalf("Open: %v", err)
24-
}
25-
t.Cleanup(func() { _ = st.Close() })
26-
27-
if err := st.Migrate(context.Background()); err != nil {
28-
t.Fatalf("Migrate: %v", err)
29-
}
30-
return st, path
31-
}
32-
3314
func TestRunCleanDB(t *testing.T) {
34-
st, _ := newCleanStore(t)
15+
st, _ := testutil.OpenTestDBFile(t)
3516

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

@@ -66,7 +47,7 @@ func TestRunCleanDB(t *testing.T) {
6647
}
6748

6849
func TestHealFixesBrokenFTS(t *testing.T) {
69-
st, path := newCleanStore(t)
50+
st, path := testutil.OpenTestDBFile(t)
7051

7152
insertSampleNodes(t, path)
7253

@@ -99,7 +80,7 @@ func TestHealFixesBrokenFTS(t *testing.T) {
9980
}
10081

10182
func TestHealIsIdempotent(t *testing.T) {
102-
st, _ := newCleanStore(t)
83+
st, _ := testutil.OpenTestDBFile(t)
10384

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

11899
func TestReportJSONShape(t *testing.T) {
119-
st, _ := newCleanStore(t)
100+
st, _ := testutil.OpenTestDBFile(t)
120101

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

@@ -151,7 +132,7 @@ func TestReportJSONShape(t *testing.T) {
151132
}
152133

153134
func TestReportTextShape(t *testing.T) {
154-
st, _ := newCleanStore(t)
135+
st, _ := testutil.OpenTestDBFile(t)
155136

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

@@ -254,7 +235,7 @@ func TestWriteTextHeader(t *testing.T) {
254235
}
255236

256237
func TestWriteTextHeaderHealsToHealthy(t *testing.T) {
257-
st, path := newCleanStore(t)
238+
st, path := testutil.OpenTestDBFile(t)
258239

259240
insertSampleNodes(t, path)
260241
if err := breakFTSSync(path); err != nil {

pkg/inspect/inspect_test.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,11 @@ import (
55
"strings"
66
"testing"
77

8+
"github.com/radimsem/remindb/internal/testutil"
89
"github.com/radimsem/remindb/pkg/inspect"
910
"github.com/radimsem/remindb/pkg/store"
1011
)
1112

12-
func openTestStore(t *testing.T) *store.Store {
13-
t.Helper()
14-
15-
st, err := store.Open(":memory:")
16-
if err != nil {
17-
t.Fatalf("Open: %v", err)
18-
}
19-
20-
if err := st.Migrate(context.Background()); err != nil {
21-
t.Fatalf("Migrate: %v", err)
22-
}
23-
24-
t.Cleanup(func() { _ = st.Close() })
25-
return st
26-
}
27-
2813
func testNode(id, parent, nodeType string) *store.Node {
2914
return &store.Node{
3015
ID: id, ParentID: parent,
@@ -35,7 +20,7 @@ func testNode(id, parent, nodeType string) *store.Node {
3520
}
3621

3722
func TestCollect_Empty(t *testing.T) {
38-
st := openTestStore(t)
23+
st := testutil.OpenTestDB(t)
3924
ctx := context.Background()
4025

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

5439
func TestCollect_PopulatesAllFields(t *testing.T) {
55-
st := openTestStore(t)
40+
st := testutil.OpenTestDB(t)
5641
ctx := context.Background()
5742

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

115100
func TestCollect_RelationCountIncludesPending(t *testing.T) {
116-
st := openTestStore(t)
101+
st := testutil.OpenTestDB(t)
117102
ctx := context.Background()
118103

119104
for _, n := range []*store.Node{

pkg/mcp/resources/doctor_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"reflect"
1010
"testing"
1111

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

6263
func TestHandleDoctor_PassParity(t *testing.T) {
63-
st := openGraphStore(t)
64+
st := testutil.OpenTestDB(t)
6465
d := &Deps{Store: st}
6566

6667
got := readDoctor(t, d)
6768
assertCLIParity(t, st, got, "pass")
6869
}
6970

7071
func TestHandleDoctor_WarnParity(t *testing.T) {
71-
dir := t.TempDir()
72-
path := filepath.Join(dir, "doctor.db")
73-
74-
st, err := store.Open(path)
75-
if err != nil {
76-
t.Fatalf("Open: %v", err)
77-
}
78-
79-
t.Cleanup(func() { _ = st.Close() })
80-
if err := st.Migrate(context.Background()); err != nil {
81-
t.Fatalf("Migrate: %v", err)
82-
}
72+
st, path := testutil.OpenTestDBFile(t)
8373

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

9080
defer func() { _ = db.Close() }()
91-
gone := filepath.Join(dir, "vanished")
81+
gone := filepath.Join(filepath.Dir(path), "vanished")
9282
if _, err := db.Exec(
9383
`INSERT INTO snapshots (cursor_hash, parent_id, message, compile_root) VALUES ('h', NULL, 'm', ?)`,
9484
gone,

pkg/mcp/resources/graph_test.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"testing"
77

8+
"github.com/radimsem/remindb/internal/testutil"
89
"github.com/radimsem/remindb/pkg/store"
910
)
1011

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

19-
func openGraphStore(t *testing.T) *store.Store {
20-
t.Helper()
21-
22-
st, err := store.Open(":memory:")
23-
if err != nil {
24-
t.Fatalf("Open: %v", err)
25-
}
26-
27-
if err := st.Migrate(context.Background()); err != nil {
28-
t.Fatalf("Migrate: %v", err)
29-
}
30-
t.Cleanup(func() { _ = st.Close() })
31-
32-
return st
33-
}
34-
3520
// Fixture: parsed n1→n2, manual n2→n3, pending n1→"Unresolved"; n4 is orphan.
3621
func TestHandleGraph_ParsedManualPending(t *testing.T) {
37-
st := openGraphStore(t)
22+
st := testutil.OpenTestDB(t)
3823
ctx := context.Background()
3924

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

10994
func TestHandleGraph_EmptyDBStableShape(t *testing.T) {
110-
st := openGraphStore(t)
95+
st := testutil.OpenTestDB(t)
11196
d := &Deps{Store: st}
11297

11398
body, err := d.graphBody(context.Background())

pkg/mcp/tools/tools_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

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

26-
st, err := store.Open(":memory:")
27-
if err != nil {
28-
t.Fatalf("Open: %v", err)
29-
}
30-
31-
if err := st.Migrate(context.Background()); err != nil {
32-
t.Fatalf("Migrate: %v", err)
33-
}
34-
t.Cleanup(func() { _ = st.Close() })
27+
st := testutil.OpenTestDB(t)
3528

3629
tracker, err := temperature.NewTracker(st, "", temperature.DefaultConfig(), nil)
3730
if err != nil {

0 commit comments

Comments
 (0)