| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/event" |
| 12 | ) |
| 13 | |
| 14 | func TestSplitEditorCommandUsesStaticShellWords(t *testing.T) { |
| 15 | got, err := splitEditorCommand(`code --goto "dir/file name.go:12"`) |
| 16 | if err != nil { |
| 17 | t.Fatalf("splitEditorCommand: %v", err) |
| 18 | } |
| 19 | want := []string{"code", "--goto", "dir/file name.go:12"} |
| 20 | if !reflect.DeepEqual(got, want) { |
| 21 | t.Fatalf("args = %#v, want %#v", got, want) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestSplitEditorCommandRejectsShellControl(t *testing.T) { |
| 26 | if _, err := splitEditorCommand(`vim file; rm -rf tmp`); err == nil { |
| 27 | t.Fatal("splitEditorCommand accepted shell control syntax") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestClearMCPAuthenticationUsesControllerWorkspace(t *testing.T) { |
| 32 | isolateCLIConfigHome(t) |
| 33 | controllerRoot := t.TempDir() |
| 34 | cwdRoot := t.TempDir() |
| 35 | const pluginConfig = ` |
| 36 | [[plugins]] |
| 37 | name = "dida" |
| 38 | type = "http" |
| 39 | url = "https://example.test/mcp?access_token=TOKEN&workspace=main" |
| 40 | auto_start = false |
| 41 | ` |
| 42 | writeConfig := func(root, token string) { |
| 43 | t.Helper() |
| 44 | raw := minimalTestModelTOML + strings.ReplaceAll(pluginConfig, "TOKEN", token) |
| 45 | if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte(raw), 0o644); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | writeConfig(controllerRoot, "controller-token") |
| 50 | writeConfig(cwdRoot, "cwd-token") |
| 51 | t.Chdir(cwdRoot) |
| 52 | |
| 53 | ctrl, err := setupProfile(context.Background(), "", 0, false, event.Discard, "", controllerRoot) |
| 54 | if err != nil { |
| 55 | t.Fatalf("setupProfile: %v", err) |
| 56 | } |
| 57 | defer ctrl.Close() |
| 58 | |
| 59 | pending := []string{} |
| 60 | model := chatTUI{ctrl: ctrl, pendingCommit: &pending} |
| 61 | model.clearMCPAuthentication(mcpServerView{Name: "dida"}) |
| 62 | |
| 63 | controllerRaw, err := os.ReadFile(filepath.Join(controllerRoot, "reasonix.toml")) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if strings.Contains(string(controllerRaw), "controller-token") || |
| 68 | !strings.Contains(string(controllerRaw), "workspace=main") { |
| 69 | t.Fatalf("controller config authentication was not cleared:\n%s", controllerRaw) |
| 70 | } |
| 71 | cwdRaw, err := os.ReadFile(filepath.Join(cwdRoot, "reasonix.toml")) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if !strings.Contains(string(cwdRaw), "cwd-token") { |
| 76 | t.Fatalf("cwd config was unexpectedly modified:\n%s", cwdRaw) |
| 77 | } |
| 78 | } |
| 79 |