-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
327 lines (277 loc) · 9.2 KB
/
Copy pathmain_test.go
File metadata and controls
327 lines (277 loc) · 9.2 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"bytes"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/corbym/memoryweb/db"
)
func newTestStore(t *testing.T) (*db.Store, string) {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "test.db")
s, err := db.New(path)
if err != nil {
t.Fatalf("db.New: %v", err)
}
t.Cleanup(func() { s.Close() })
return s, path
}
func captureStdout(t *testing.T, fn func()) string {
t.Helper()
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("os.Pipe: %v", err)
}
os.Stdout = w
fn()
w.Close()
os.Stdout = old
out, err := io.ReadAll(r)
if err != nil {
t.Fatalf("read captured stdout: %v", err)
}
return string(out)
}
func TestPrintLiveRemaining_SilentWithoutDomain(t *testing.T) {
out := captureStdout(t, func() {
printLiveRemaining(db.PurgeResult{LiveRemaining: 3}, "", false)
})
if out != "" {
t.Errorf("expected no output when domain is unscoped, got %q", out)
}
}
func TestPrintLiveRemaining_SilentWhenIncludeLiveUsed(t *testing.T) {
out := captureStdout(t, func() {
printLiveRemaining(db.PurgeResult{LiveRemaining: 0}, "recordari-commercial", true)
})
if out != "" {
t.Errorf("expected no output when includeLive already swept up live nodes, got %q", out)
}
}
func TestPrintLiveRemaining_SilentWhenNoneRemain(t *testing.T) {
out := captureStdout(t, func() {
printLiveRemaining(db.PurgeResult{LiveRemaining: 0}, "recordari-commercial", false)
})
if out != "" {
t.Errorf("expected no output when LiveRemaining is 0, got %q", out)
}
}
func TestPrintLiveRemaining_WarnsWhenLiveNodesRemain(t *testing.T) {
out := captureStdout(t, func() {
printLiveRemaining(db.PurgeResult{LiveRemaining: 2}, "recordari-commercial", false)
})
if !strings.Contains(out, "2 live node(s)") || !strings.Contains(out, "recordari-commercial") {
t.Errorf("expected a note naming the domain and live count, got %q", out)
}
if !strings.Contains(out, "--include-live") {
t.Errorf("expected the note to mention --include-live as the escape hatch, got %q", out)
}
}
func TestDrawProgressBar_Format(t *testing.T) {
var buf bytes.Buffer
drawProgressBar(&buf, 5, 10)
got := buf.String()
if !strings.HasPrefix(got, "\r") {
t.Errorf("progress bar should start with \\r; got %q", got)
}
if !strings.Contains(got, "5/10") {
t.Errorf("progress bar should contain '5/10'; got %q", got)
}
if !strings.Contains(got, "50%") {
t.Errorf("progress bar should contain '50%%'; got %q", got)
}
if !strings.Contains(got, "[") || !strings.Contains(got, "]") {
t.Errorf("progress bar should contain '[' and ']'; got %q", got)
}
}
func TestDrawProgressBar_Complete(t *testing.T) {
var buf bytes.Buffer
drawProgressBar(&buf, 10, 10)
got := buf.String()
if !strings.Contains(got, "10/10") {
t.Errorf("complete bar should show '10/10'; got %q", got)
}
if !strings.Contains(got, "100%") {
t.Errorf("complete bar should show '100%%'; got %q", got)
}
// At 100% the bar should be all '=' with no '>'
if strings.Contains(got, ">") {
t.Errorf("complete bar should not contain '>'; got %q", got)
}
}
func TestDrawProgressBar_First(t *testing.T) {
var buf bytes.Buffer
drawProgressBar(&buf, 1, 100)
got := buf.String()
if !strings.Contains(got, "1/100") {
t.Errorf("first step should show '1/100'; got %q", got)
}
// Should contain the '>' cursor marker
if !strings.Contains(got, ">") {
t.Errorf("in-progress bar should contain '>'; got %q", got)
}
}
// ── runDoctor tests ───────────────────────────────────────────────────────────
func TestRunDoctor_TextOutput_ContainsSections(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir() // no hooks configured
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
for _, want := range []string{
"Database:", "sqlite-vec:", "Ollama binary:", "Ollama server:", "Ollama model:",
"Claude hooks:", "Graph:", "Drift:", "Last activity:", "Update:",
} {
if !strings.Contains(out, want) {
t.Errorf("expected section %q in output; got:\n%s", want, out)
}
}
}
func TestRunDoctor_DatabaseCheck_PassesOnFreshDB(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
// The fresh DB should report a passing database check.
if !strings.Contains(out, "[✓] Database:") {
t.Errorf("fresh DB should produce [✓] Database; got:\n%s", out)
}
// The DB path should appear in the output.
if !strings.Contains(out, dbPath) {
t.Errorf("DB path should appear in output; got:\n%s", out)
}
}
func TestRunDoctor_HooksCheck_FailsWhenNoSettings(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir() // no .claude/settings.local.json
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
if !strings.Contains(out, "[✗] Claude hooks:") {
t.Errorf("missing hooks should produce [✗] Claude hooks; got:\n%s", out)
}
}
func TestRunDoctor_ReturnsFalse_WhenHooksMissing(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir() // no hooks → "fail"
var buf bytes.Buffer
passed := runDoctor(store, &buf, dbPath, home, false)
// Hooks fail means overall result is false.
if passed {
t.Errorf("runDoctor should return false when hooks are missing; output:\n%s", buf.String())
}
}
func TestRunDoctor_JSONOutput_ValidSchema(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, true)
var report DoctorReport
if err := json.Unmarshal(buf.Bytes(), &report); err != nil {
t.Fatalf("JSON output is not valid: %v\nraw: %s", err, buf.String())
}
if len(report.Checks) == 0 {
t.Error("JSON report should contain at least one check")
}
for _, c := range report.Checks {
if c.Name == "" {
t.Error("every check should have a non-empty Name")
}
switch c.Status {
case "ok", "fail", "warn", "info":
// valid
default:
t.Errorf("unexpected status %q in check %q", c.Status, c.Name)
}
}
}
func TestRunDoctor_GraphStats_ReflectAddedNode(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
// Add a node so the graph stats show something meaningful.
if _, err := store.AddNode("test node", "desc", "why", "test-domain", nil, "", ""); err != nil {
t.Fatalf("AddNode: %v", err)
}
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
if !strings.Contains(out, "1 live") {
t.Errorf("expected '1 live' in graph stats; got:\n%s", out)
}
if !strings.Contains(out, "test-domain") {
t.Errorf("expected domain 'test-domain' in graph stats; got:\n%s", out)
}
}
func TestRunDoctor_DriftSnapshot_ShowsCandidate(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
// Add a node whose label contains "deprecated" so drift detects it.
if _, err := store.AddNode("deprecated old feature", "d", "w", "test-domain", nil, "", ""); err != nil {
t.Fatalf("AddNode: %v", err)
}
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
if !strings.Contains(out, "1 candidate") {
t.Errorf("expected '1 candidate' in drift line; got:\n%s", out)
}
}
func TestRunDoctor_DriftSnapshot_CategorisesResolvedPlaceholder(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
placeholder, err := store.AddNode("Story needed: api/openapi/admin.yaml", "", "", "test-domain", nil, "", "goal")
if err != nil {
t.Fatalf("AddNode placeholder: %v", err)
}
done, err := store.AddNode("STORY-139 complete", "shipped 2026-06-28", "", "test-domain", nil, "", "")
if err != nil {
t.Fatalf("AddNode done: %v", err)
}
if _, err := store.AddEdge(placeholder.ID, done.ID, "connects_to", "closes this placeholder"); err != nil {
t.Fatalf("AddEdge: %v", err)
}
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
if !strings.Contains(out, "resolved placeholders") {
t.Errorf("expected the resolved-placeholder drift candidate to be categorised as 'resolved placeholders', not folded into 'transient'; got:\n%s", out)
}
}
func TestRunDoctor_AuditLog_ShowsLastActivity(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
n, err := store.AddNode("audit target", "d", "w", "proj", nil, "", "")
if err != nil {
t.Fatalf("AddNode: %v", err)
}
if err := store.ArchiveNode(n.ID, "test"); err != nil {
t.Fatalf("ArchiveNode: %v", err)
}
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
if strings.Contains(out, "(no activity recorded)") {
t.Errorf("expected last activity to be populated; got:\n%s", out)
}
if !strings.Contains(out, "archive") {
t.Errorf("expected 'archive' in last activity; got:\n%s", out)
}
}
func TestRunDoctor_UpdateCheck_DevBuild(t *testing.T) {
store, dbPath := newTestStore(t)
home := t.TempDir()
// Version is "dev" in tests (the zero value of the package variable),
// so the update check should report the dev-build info message.
var buf bytes.Buffer
runDoctor(store, &buf, dbPath, home, false)
out := buf.String()
if !strings.Contains(out, "dev build") {
t.Errorf("expected 'dev build' in update check line; got:\n%s", out)
}
}