返回 DeepSeek-Reasonix
mcp_manager_actions_test.go
根目录 / internal / cli / mcp_manager_actions_test.go
1 package cli
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "reflect"
8 "strings"
9 "testing"
10
11 "reasonix/internal/config"
12 "reasonix/internal/event"
13 "reasonix/internal/plugin"
14 )
15
16 func TestSplitEditorCommandUsesStaticShellWords(t *testing.T) {
17 got, err := splitEditorCommand(`code --goto "dir/file name.go:12"`)
18 if err != nil {
19 t.Fatalf("splitEditorCommand: %v", err)
20 }
21 want := []string{"code", "--goto", "dir/file name.go:12"}
22 if !reflect.DeepEqual(got, want) {
23 t.Fatalf("args = %#v, want %#v", got, want)
24 }
25 }
26
27 func TestSplitEditorCommandRejectsShellControl(t *testing.T) {
28 if _, err := splitEditorCommand(`vim file; rm -rf tmp`); err == nil {
29 t.Fatal("splitEditorCommand accepted shell control syntax")
30 }
31 }
32
33 func TestMCPActionsOfferOAuthOnlyForEligibleHTTPServers(t *testing.T) {
34 tests := []struct {
35 name string
36 transport string
37 url string
38 authConfigured bool
39 want mcpAction
40 }{
41 {name: "streamable HTTP", transport: "http", url: "https://mcp.example.test/mcp", want: mcpActionAuth},
42 {name: "stdio", transport: "stdio", want: mcpActionConnect},
43 {name: "legacy SSE", transport: "sse", url: "https://mcp.example.test/sse", want: mcpActionConnect},
44 {name: "static authentication", transport: "http", url: "https://mcp.example.test/mcp", authConfigured: true, want: mcpActionConnect},
45 }
46 for _, tc := range tests {
47 t.Run(tc.name, func(t *testing.T) {
48 actions := mcpActionsFor(mcpServerView{
49 Name: "server", Transport: tc.transport, URL: tc.url, Status: "failed",
50 Error: "authentication required", AuthStatus: "required", authConfigured: tc.authConfigured,
51 }, "")
52 if len(actions) == 0 || actions[0].kind != tc.want {
53 t.Fatalf("actions = %+v, want first action %q", actions, tc.want)
54 }
55 })
56 }
57 }
58
59 func TestClearMCPAuthenticationUsesControllerWorkspace(t *testing.T) {
60 isolateCLIConfigHome(t)
61 controllerRoot := t.TempDir()
62 cwdRoot := t.TempDir()
63 const pluginConfig = `
64 [[plugins]]
65 name = "dida"
66 type = "http"
67 url = "https://example.test/mcp?access_token=TOKEN&workspace=main"
68 auto_start = false
69 `
70 writeConfig := func(root, token string) {
71 t.Helper()
72 raw := minimalTestModelTOML + strings.ReplaceAll(pluginConfig, "TOKEN", token)
73 if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte(raw), 0o644); err != nil {
74 t.Fatal(err)
75 }
76 }
77 writeConfig(controllerRoot, "controller-token")
78 writeConfig(cwdRoot, "cwd-token")
79 t.Chdir(cwdRoot)
80
81 ctrl, err := setupProfile(context.Background(), "", 0, false, event.Discard, controllerRoot)
82 if err != nil {
83 t.Fatalf("setupProfile: %v", err)
84 }
85 defer ctrl.Close()
86 oauthState := filepath.Join(plugin.MCPStateDir(config.ReasonixHomeDir(), controllerRoot, "dida"), "oauth.json")
87 if err := os.MkdirAll(filepath.Dir(oauthState), 0o700); err != nil {
88 t.Fatal(err)
89 }
90 if err := os.WriteFile(oauthState, []byte(`{"version":1,"access_token":"private"}`), 0o600); err != nil {
91 t.Fatal(err)
92 }
93
94 pending := []string{}
95 model := chatTUI{ctrl: ctrl, pendingCommit: &pending}
96 model.clearMCPAuthentication(mcpServerView{Name: "dida"})
97
98 controllerRaw, err := os.ReadFile(filepath.Join(controllerRoot, "reasonix.toml"))
99 if err != nil {
100 t.Fatal(err)
101 }
102 if strings.Contains(string(controllerRaw), "controller-token") ||
103 !strings.Contains(string(controllerRaw), "workspace=main") {
104 t.Fatalf("controller config authentication was not cleared:\n%s", controllerRaw)
105 }
106 cwdRaw, err := os.ReadFile(filepath.Join(cwdRoot, "reasonix.toml"))
107 if err != nil {
108 t.Fatal(err)
109 }
110 if !strings.Contains(string(cwdRaw), "cwd-token") {
111 t.Fatalf("cwd config was unexpectedly modified:\n%s", cwdRaw)
112 }
113 if _, err := os.Stat(oauthState); !os.IsNotExist(err) {
114 t.Fatalf("controller OAuth state was not cleared: %v", err)
115 }
116 }
117
117 lines GO