-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmcp_test.go
More file actions
322 lines (281 loc) · 9.45 KB
/
Copy pathmcp_test.go
File metadata and controls
322 lines (281 loc) · 9.45 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
package main
import (
"os"
"path/filepath"
"testing"
)
func mustMkdir(t *testing.T, path string) {
t.Helper()
if err := os.MkdirAll(path, 0755); err != nil {
t.Fatal(err)
}
}
func mustWrite(t *testing.T, path string, data []byte) {
t.Helper()
if err := os.WriteFile(path, data, 0644); err != nil {
t.Fatal(err)
}
}
func TestReadSettings(t *testing.T) {
tests := []struct {
name string
json string
expectServers int
expectPlugins int
}{
{"both fields", `{"mcpServers":{"a":{},"b":{}},"enabledPlugins":{"x@m":true}}`, 2, 1},
{"servers only", `{"mcpServers":{"a":{}}}`, 1, 0},
{"empty", `{}`, 0, 0},
{"invalid json", `not json`, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "settings.json")
mustWrite(t, path, []byte(tt.json))
s := readSettings(path)
if len(s.MCPServers) != tt.expectServers {
t.Errorf("MCPServers: got %d, want %d", len(s.MCPServers), tt.expectServers)
}
if len(s.EnabledPlugins) != tt.expectPlugins {
t.Errorf("EnabledPlugins: got %d, want %d", len(s.EnabledPlugins), tt.expectPlugins)
}
})
}
}
func TestReadSettings_MissingFile(t *testing.T) {
s := readSettings("/nonexistent/settings.json")
if len(s.MCPServers) != 0 || len(s.EnabledPlugins) != 0 {
t.Errorf("expected empty for missing file")
}
}
func TestParseEnabledPluginMCPs(t *testing.T) {
dir := t.TempDir()
pluginsDir := filepath.Join(dir, "plugins")
enabled := map[string]bool{
"github@official": true,
"slack@official": true,
"disabled@official": false,
"skills@mymarket": true,
}
githubDir := filepath.Join(pluginsDir, "cache", "official", "github", "1.0.0")
mustMkdir(t, githubDir)
mustWrite(t, filepath.Join(githubDir, ".mcp.json"), []byte(`{"github":{}}`))
slackDir := filepath.Join(pluginsDir, "cache", "official", "slack", "1.0.0")
mustMkdir(t, slackDir)
mustWrite(t, filepath.Join(slackDir, ".mcp.json"), []byte(`{"slack":{}}`))
names := parseEnabledPluginMCPs(enabled, pluginsDir)
if len(names) != 2 {
t.Errorf("expected 2 MCP plugins, got %d: %v", len(names), names)
}
found := map[string]bool{}
for _, n := range names {
found[n] = true
}
if !found["github"] || !found["slack"] {
t.Errorf("expected github and slack, got %v", names)
}
}
func TestParseEnabledPluginMCPs_AlternateLayout(t *testing.T) {
dir := t.TempDir()
pluginsDir := filepath.Join(dir, "plugins")
enabled := map[string]bool{"github@official": true}
githubDir := filepath.Join(pluginsDir, "marketplaces", "official", "external_plugins", "github")
mustMkdir(t, githubDir)
mustWrite(t, filepath.Join(githubDir, ".mcp.json"), []byte(`{}`))
names := parseEnabledPluginMCPs(enabled, pluginsDir)
if len(names) != 1 || names[0] != "github" {
t.Errorf("expected [github] from old layout, got %v", names)
}
}
func TestParseEnabledPluginMCPs_DisabledSkipped(t *testing.T) {
dir := t.TempDir()
pluginsDir := filepath.Join(dir, "plugins")
enabled := map[string]bool{"github@official": false}
githubDir := filepath.Join(pluginsDir, "cache", "official", "github", "1.0.0")
mustMkdir(t, githubDir)
mustWrite(t, filepath.Join(githubDir, ".mcp.json"), []byte(`{}`))
names := parseEnabledPluginMCPs(enabled, pluginsDir)
if len(names) != 0 {
t.Errorf("disabled plugins should be skipped, got %v", names)
}
}
func TestParseProjectMCPsFromDir(t *testing.T) {
dir := t.TempDir()
mcpJSON := `{"mcpServers":{"my-server":{"command":"node","args":["server.js"]}}}`
mustWrite(t, filepath.Join(dir, ".mcp.json"), []byte(mcpJSON))
names := parseProjectMCPsFromDir(dir)
if len(names) != 1 || names[0] != "my-server" {
t.Errorf("expected [my-server], got %v", names)
}
}
func TestParseProjectMCPsFromDir_FlatFormat(t *testing.T) {
dir := t.TempDir()
mcpJSON := `{"my-server":{"command":"node","args":["server.js"]}}`
mustWrite(t, filepath.Join(dir, ".mcp.json"), []byte(mcpJSON))
names := parseProjectMCPsFromDir(dir)
if len(names) != 1 || names[0] != "my-server" {
t.Errorf("expected [my-server], got %v", names)
}
}
func TestParseProjectMCPsFromDir_NoMcpFile(t *testing.T) {
names := parseProjectMCPsFromDir(t.TempDir())
if len(names) != 0 {
t.Errorf("expected empty, got %v", names)
}
}
func TestParseProjectMCPsFromDir_EmptyPath(t *testing.T) {
names := parseProjectMCPsFromDir("")
if len(names) != 0 {
t.Errorf("expected empty for empty path, got %v", names)
}
}
func TestProjectPathFromSlug(t *testing.T) {
projects := map[string]claudeProjectConfig{
"/Users/foo/myproject": {},
"/Users/bar/other": {},
}
result := projectPathFromSlug(projects, "/tmp/.claude/projects/-Users-foo-myproject/session.jsonl")
if result != "/Users/foo/myproject" {
t.Errorf("expected /Users/foo/myproject, got %q", result)
}
}
func TestProjectPathFromSlug_Windows(t *testing.T) {
projects := map[string]claudeProjectConfig{
"C:/Users/foo/myproject": {DisabledMCPServers: []string{"plugin:ctx:ctx"}},
}
result := projectPathFromSlug(projects, "/tmp/.claude/projects/C--Users-foo-myproject/session.jsonl")
if result != "C:/Users/foo/myproject" {
t.Errorf("expected C:/Users/foo/myproject, got %q", result)
}
}
func TestProjectPathFromSlug_NoMatch(t *testing.T) {
projects := map[string]claudeProjectConfig{"/Users/foo/bar": {}}
result := projectPathFromSlug(projects, "/tmp/projects/-Users-baz-qux/session.jsonl")
if result != "" {
t.Errorf("expected empty, got %q", result)
}
}
func TestProjectPathFromSlug_EmptyInputs(t *testing.T) {
if r := projectPathFromSlug(nil, "/some/path.jsonl"); r != "" {
t.Errorf("expected empty for nil projects, got %q", r)
}
if r := projectPathFromSlug(map[string]claudeProjectConfig{"a": {}}, ""); r != "" {
t.Errorf("expected empty for empty transcript, got %q", r)
}
}
func TestFindProject(t *testing.T) {
projects := map[string]claudeProjectConfig{
"C:/Users/foo/myproject": {DisabledMCPServers: []string{"plugin:ctx:ctx"}},
}
tests := []struct {
name, lookup string
wantDisabled int
}{
{"exact match", "C:/Users/foo/myproject", 1},
{"backslash lookup", `C:\Users\foo\myproject`, 1},
{"no match", "C:/Users/other/project", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := findProject(projects, tt.lookup)
if len(cfg.DisabledMCPServers) != tt.wantDisabled {
t.Errorf("findProject(%q) disabled=%d, want %d", tt.lookup, len(cfg.DisabledMCPServers), tt.wantDisabled)
}
})
}
}
func TestProjectSlug(t *testing.T) {
tests := []struct {
name, path, expected string
}{
{"unix", "/Users/foo/bar", "-Users-foo-bar"},
{"windows forward slash", "C:/Users/foo/bar", "C--Users-foo-bar"},
{"windows backslash", `C:\Users\foo\bar`, "C--Users-foo-bar"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := projectSlug(tt.path)
if got != tt.expected {
t.Errorf("projectSlug(%q) = %q, want %q", tt.path, got, tt.expected)
}
})
}
}
func TestExtractCwdFromTranscript(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "session.jsonl")
content := `{"type":"summary","summary":"test"}
{"type":"user","cwd":"/home/user/project","message":{"role":"user"}}
{"type":"assistant","cwd":"/home/user/project","message":{"model":"claude-opus-4-6"}}
`
mustWrite(t, path, []byte(content))
cwd := extractCwdFromTranscript(path)
if cwd != "/home/user/project" {
t.Errorf("expected /home/user/project, got %q", cwd)
}
}
func TestExtractCwdFromTranscript_NoCwd(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "session.jsonl")
mustWrite(t, path, []byte("{\"type\":\"summary\"}\n"))
cwd := extractCwdFromTranscript(path)
if cwd != "" {
t.Errorf("expected empty, got %q", cwd)
}
}
func TestCollectDisabledMCPs(t *testing.T) {
proj := claudeProjectConfig{
DisabledMCPServers: []string{"plugin:confluence:confluence", "my-server"},
DisabledMcpjsonServers: []string{"project-db"},
}
disabled := collectDisabledMCPs(proj)
if len(disabled) != 3 {
t.Errorf("expected 3 disabled, got %d: %v", len(disabled), disabled)
}
if !disabled["confluence"] || !disabled["my-server"] || !disabled["project-db"] {
t.Errorf("expected confluence, my-server, project-db disabled, got %v", disabled)
}
}
func TestCollectDisabledMCPs_Empty(t *testing.T) {
disabled := collectDisabledMCPs(claudeProjectConfig{})
if len(disabled) != 0 {
t.Errorf("expected empty, got %v", disabled)
}
}
func TestFilterDisabled(t *testing.T) {
names := []string{"confluence", "jira", "my-server"}
disabled := map[string]bool{"confluence": true, "my-server": true}
result := filterDisabled(names, disabled)
if len(result) != 1 || result[0] != "jira" {
t.Errorf("expected [jira], got %v", result)
}
}
func TestFilterDisabled_NilMap(t *testing.T) {
names := []string{"a", "b"}
result := filterDisabled(names, nil)
if len(result) != 2 {
t.Errorf("expected pass-through with nil disabled, got %v", result)
}
}
func TestDeduplicateAndSort(t *testing.T) {
names := deduplicateAndSort([]string{"github", "slack", "GitHub", "jira", "Slack"})
if len(names) != 3 {
t.Errorf("expected 3 unique names, got %d: %v", len(names), names)
}
if names[0] != "github" || names[1] != "jira" || names[2] != "slack" {
t.Errorf("expected [github jira slack], got %v", names)
}
}
func TestDeduplicateAndSort_Empty(t *testing.T) {
names := deduplicateAndSort(nil)
if len(names) != 0 {
t.Errorf("expected empty, got %v", names)
}
}
func TestDetectMCPs_EmptyGraceful(t *testing.T) {
names := detectMCPs("/nonexistent/path", "")
if len(names) != 0 {
t.Errorf("expected empty for missing paths, got %v", names)
}
}