-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_test.go
More file actions
82 lines (70 loc) · 2.25 KB
/
Copy pathmenu_test.go
File metadata and controls
82 lines (70 loc) · 2.25 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
package main
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"mindstack/internal/config"
)
// writeTestConfig writes a global config.json with the given knowledge base
// registry at the custom config path.
func writeTestConfig(t *testing.T, cfgPath string, kbs map[string]string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(cfgPath), 0755); err != nil {
t.Fatal(err)
}
data, err := json.MarshalIndent(map[string]any{"knowledgeBases": kbs}, "", " ")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cfgPath, data, 0644); err != nil {
t.Fatal(err)
}
}
func TestListLocalKnowledgeBases(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.json")
config.SetCustomConfigPath(cfgPath)
t.Cleanup(func() { config.SetCustomConfigPath("") })
kbA := filepath.Join(dir, "kb-a")
kbB := filepath.Join(dir, "kb-b")
for _, p := range []string{kbA, kbB} {
if err := os.MkdirAll(p, 0755); err != nil {
t.Fatal(err)
}
}
// Register out of order and include a registry entry whose path no longer exists.
writeTestConfig(t, cfgPath, map[string]string{
"kb-b": kbB,
"kb-a": kbA,
"gone": filepath.Join(dir, "gone"),
})
entries := listLocalKnowledgeBases()
if len(entries) != 2 {
t.Fatalf("expected 2 entries, got %d: %+v", len(entries), entries)
}
if entries[0].Name != "kb-a" || entries[0].Path != kbA {
t.Fatalf("expected first entry kb-a/%s, got %+v", kbA, entries[0])
}
if entries[1].Name != "kb-b" || entries[1].Path != kbB {
t.Fatalf("expected second entry kb-b/%s, got %+v", kbB, entries[1])
}
}
func TestListLocalKnowledgeBases_Empty(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.json")
config.SetCustomConfigPath(cfgPath)
t.Cleanup(func() { config.SetCustomConfigPath("") })
writeTestConfig(t, cfgPath, map[string]string{})
if entries := listLocalKnowledgeBases(); len(entries) != 0 {
t.Fatalf("expected no entries, got %+v", entries)
}
}
func TestListLocalKnowledgeBases_MissingConfig(t *testing.T) {
dir := t.TempDir()
config.SetCustomConfigPath(filepath.Join(dir, "config.json"))
t.Cleanup(func() { config.SetCustomConfigPath("") })
if entries := listLocalKnowledgeBases(); len(entries) != 0 {
t.Fatalf("expected no entries without config, got %+v", entries)
}
}