| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestNormalizeLoadedPermissionPresetsMigratesLegacyValues(t *testing.T) { |
| 9 | cfg := Default() |
| 10 | cfg.Desktop.DefaultToolApprovalMode = "ask" |
| 11 | cfg.Bot.ToolApprovalMode = "auto" |
| 12 | cfg.Bot.Connections = []BotConnectionConfig{{ToolApprovalMode: "yolo"}} |
| 13 | cfg.Bot.Routes = []BotRouteConfig{{ToolApprovalMode: "full-access"}} |
| 14 | |
| 15 | if err := normalizeLoadedConfig(cfg); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | if got := cfg.Desktop.DefaultToolApprovalMode; got != "read-only" { |
| 19 | t.Fatalf("desktop preset = %q, want read-only", got) |
| 20 | } |
| 21 | if got := cfg.Bot.ToolApprovalMode; got != "workspace-write" { |
| 22 | t.Fatalf("bot preset = %q, want workspace-write", got) |
| 23 | } |
| 24 | if got := cfg.Bot.Connections[0].ToolApprovalMode; got != "workspace-write" { |
| 25 | t.Fatalf("connection preset = %q, want workspace-write", got) |
| 26 | } |
| 27 | if got := cfg.Bot.Routes[0].ToolApprovalMode; got != "danger-full-access" { |
| 28 | t.Fatalf("route preset = %q, want danger-full-access", got) |
| 29 | } |
| 30 | if cfg.HasLoadWarnings() { |
| 31 | t.Fatalf("known migration produced warnings: %v", cfg.LoadWarnings()) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestNormalizeLoadedPermissionPresetUnknownFailsClosedWithWarning(t *testing.T) { |
| 36 | cfg := Default() |
| 37 | cfg.Desktop.DefaultToolApprovalMode = "mystery-mode" |
| 38 | cfg.Bot.Connections = []BotConnectionConfig{{ToolApprovalMode: "future-mode"}} |
| 39 | |
| 40 | if err := normalizeLoadedConfig(cfg); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if got := cfg.Desktop.DefaultToolApprovalMode; got != "read-only" { |
| 44 | t.Fatalf("desktop preset = %q, want read-only", got) |
| 45 | } |
| 46 | if got := cfg.Bot.Connections[0].ToolApprovalMode; got != "read-only" { |
| 47 | t.Fatalf("connection preset = %q, want read-only", got) |
| 48 | } |
| 49 | warnings := strings.Join(cfg.LoadWarnings(), "\n") |
| 50 | if !strings.Contains(warnings, "desktop.default_tool_approval_mode") || !strings.Contains(warnings, "bot.connections[0].tool_approval_mode") { |
| 51 | t.Fatalf("warnings = %q, want both incompatible fields", warnings) |
| 52 | } |
| 53 | } |
| 54 |