-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdoctor_test.go
More file actions
311 lines (256 loc) · 7.21 KB
/
Copy pathdoctor_test.go
File metadata and controls
311 lines (256 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package doctor
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"strings"
"testing"
"github.com/radimsem/remindb/internal/testutil"
)
func TestRunCleanDB(t *testing.T) {
st, _ := testutil.OpenTestDBFile(t)
report := Run(context.Background(), st)
want := map[string]string{
"fts5_sync": "pass",
"orphan_parent_id": "pass",
"head_cursor": "pass",
"dangling_diffs": "pass",
"schema_version": "pass",
"stale_compile_root": "pass",
}
if len(report.Checks) != len(want) {
t.Fatalf("checks count: got %d, want %d", len(report.Checks), len(want))
}
for _, c := range report.Checks {
w, ok := want[c.Name]
if !ok {
t.Errorf("unexpected check %q", c.Name)
continue
}
if c.Status != w {
t.Errorf("%s: got %s, want %s (%s)", c.Name, c.Status, w, c.Detail)
}
}
if report.HasFailures() {
t.Errorf("clean DB reports failures")
}
}
func TestHealFixesBrokenFTS(t *testing.T) {
st, path := testutil.OpenTestDBFile(t)
insertSampleNodes(t, path)
if err := breakFTSSync(path); err != nil {
t.Fatalf("breakFTSSync: %v", err)
}
pre := Run(context.Background(), st)
if !containsFailure(pre, "fts5_sync") {
t.Fatalf("expected fts5_sync to fail before heal")
}
post := Heal(context.Background(), st)
for _, c := range post.Checks {
if c.Name != "fts5_sync" {
continue
}
if c.Status != "pass" {
t.Errorf("fts5_sync after heal: got %s (%s)", c.Status, c.Detail)
}
if !c.FixApplied {
t.Errorf("fts5_sync: expected FixApplied=true")
}
}
if post.HasFailures() {
t.Errorf("post-heal has failures: %+v", post.Checks)
}
}
func TestHealIsIdempotent(t *testing.T) {
st, _ := testutil.OpenTestDBFile(t)
first := Heal(context.Background(), st)
second := Heal(context.Background(), st)
if first.HasFailures() || second.HasFailures() {
t.Errorf("idempotent heal should not produce failures")
}
for _, c := range second.Checks {
if c.FixApplied {
t.Errorf("second heal applied a fix on a healthy DB: %+v", c)
}
}
}
func TestReportJSONShape(t *testing.T) {
st, _ := testutil.OpenTestDBFile(t)
report := Run(context.Background(), st)
var buf bytes.Buffer
if err := report.WriteJSON(&buf); err != nil {
t.Fatalf("WriteJSON: %v", err)
}
var decoded struct {
Status string `json:"status"`
Checks []map[string]any `json:"checks"`
}
if err := json.Unmarshal(buf.Bytes(), &decoded); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if decoded.Status != report.Status().String() {
t.Errorf("status header: got %q, want %q", decoded.Status, report.Status().String())
}
if len(decoded.Checks) != len(report.Checks) {
t.Fatalf("round-trip count: got %d, want %d", len(decoded.Checks), len(report.Checks))
}
for _, entry := range decoded.Checks {
for _, k := range []string{"name", "status", "detail"} {
if _, ok := entry[k]; !ok {
t.Errorf("missing key %q in %+v", k, entry)
}
}
}
}
func TestReportTextShape(t *testing.T) {
st, _ := testutil.OpenTestDBFile(t)
report := Run(context.Background(), st)
var buf bytes.Buffer
if err := report.WriteText(&buf, false); err != nil {
t.Fatalf("WriteText: %v", err)
}
out := buf.String()
if len(out) == 0 {
t.Fatalf("empty text report")
}
for _, c := range report.Checks {
if !bytes.Contains(buf.Bytes(), []byte(c.Name)) {
t.Errorf("text report missing check name %q", c.Name)
}
}
}
func TestDiffSorted(t *testing.T) {
cases := []struct {
want, have []string
expect []string
}{
{[]string{"a", "b", "c"}, []string{"a", "b", "c"}, nil},
{[]string{"a", "b", "c"}, []string{"a", "c"}, []string{"b"}},
{[]string{"a", "b", "c"}, []string{}, []string{"a", "b", "c"}},
}
for i, tc := range cases {
got := diffSorted(tc.want, tc.have)
if !equalStringSlice(got, tc.expect) {
t.Errorf("case %d: got %v, want %v", i, got, tc.expect)
}
}
}
func TestStatusWorstWins(t *testing.T) {
mk := func(statuses ...Status) Report {
checks := make([]CheckReport, 0, len(statuses))
for _, s := range statuses {
checks = append(checks, CheckReport{Status: s.String()})
}
return Report{Checks: checks}
}
cases := []struct {
name string
in Report
want Status
}{
{"empty", mk(), Pass},
{"all pass", mk(Pass, Pass), Pass},
{"has warn", mk(Pass, Warn, Pass), Warn},
{"has fail", mk(Pass, Fail, Pass), Fail},
{"fail beats warn", mk(Warn, Fail, Warn), Fail},
}
for _, tc := range cases {
if got := tc.in.Status(); got != tc.want {
t.Errorf("%s: got %v, want %v", tc.name, got, tc.want)
}
}
}
func TestWriteTextHeader(t *testing.T) {
cases := []struct {
name string
report Report
header string
}{
{"healthy", Report{Checks: []CheckReport{{Name: "a", Status: "pass"}}}, "✓ Database is healthy"},
{"warnings", Report{Checks: []CheckReport{{Name: "a", Status: "warn"}}}, "⚠ Database has warnings"},
{"unhealthy", Report{Checks: []CheckReport{{Name: "a", Status: "fail"}}}, "✗ Database is unhealthy"},
}
for _, tc := range cases {
var buf bytes.Buffer
if err := tc.report.WriteText(&buf, false); err != nil {
t.Fatalf("%s: WriteText: %v", tc.name, err)
}
lines := strings.SplitN(buf.String(), "\n", 3)
if len(lines) < 3 {
t.Fatalf("%s: want header + blank + checklist, got %q", tc.name, buf.String())
}
if lines[0] != tc.header {
t.Errorf("%s: header = %q, want %q", tc.name, lines[0], tc.header)
}
if lines[1] != "" {
t.Errorf("%s: want blank line after header, got %q", tc.name, lines[1])
}
if !strings.Contains(lines[2], "a") {
t.Errorf("%s: checklist missing after header, got %q", tc.name, lines[2])
}
}
}
func TestWriteTextHeaderHealsToHealthy(t *testing.T) {
st, path := testutil.OpenTestDBFile(t)
insertSampleNodes(t, path)
if err := breakFTSSync(path); err != nil {
t.Fatalf("breakFTSSync: %v", err)
}
post := Heal(context.Background(), st)
if post.HasFailures() {
t.Fatalf("post-heal still has failures: %+v", post.Checks)
}
var buf bytes.Buffer
if err := post.WriteText(&buf, false); err != nil {
t.Fatalf("WriteText: %v", err)
}
header := strings.SplitN(buf.String(), "\n", 2)[0]
if header != "✓ Database is healthy" {
t.Errorf("post-fix header = %q, want %q", header, "✓ Database is healthy")
}
}
func containsFailure(r Report, name string) bool {
for _, c := range r.Checks {
if c.Name == name && c.Status == "fail" {
return true
}
}
return false
}
func insertSampleNodes(t *testing.T, path string) {
t.Helper()
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatalf("sql.Open: %v", err)
}
defer func() { _ = db.Close() }()
_, err = db.Exec(`
INSERT INTO nodes (id, parent_id, source_file, node_type, depth, label, content, format, token_count, content_hash, temperature)
VALUES
('n0000000001', NULL, '/x', 'p', 0, 'l1', '', 'plain', 0, 'h1', 0.5),
('n0000000002', NULL, '/x', 'p', 0, 'l2', '', 'plain', 0, 'h2', 0.5)`)
if err != nil {
t.Fatalf("insert: %v", err)
}
}
func breakFTSSync(path string) error {
db, err := sql.Open("sqlite", path)
if err != nil {
return err
}
defer func() { _ = db.Close() }()
_, err = db.Exec(`INSERT INTO nodes_fts(nodes_fts) VALUES('delete-all')`)
return err
}
func equalStringSlice(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}