| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | ) |
| 12 | |
| 13 | func TestRunMigratesLegacyConfigBeforeConfigOnlyCommands(t *testing.T) { |
| 14 | isolateCLIConfigHome(t) |
| 15 | legacyPath := filepath.Join(filepath.Dir(config.UserConfigPath()), "reasonix.toml") |
| 16 | if err := os.MkdirAll(filepath.Dir(legacyPath), 0o755); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if err := os.WriteFile(legacyPath, []byte(` |
| 20 | default_model = "deepseek-flash" |
| 21 | |
| 22 | [[plugins]] |
| 23 | name = "legacy-cli" |
| 24 | command = "legacy-bin" |
| 25 | `), 0o644); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | |
| 29 | out := captureStdout(t, func() { |
| 30 | if rc := Run([]string{"mcp", "list"}, "test-version"); rc != 0 { |
| 31 | t.Fatalf("mcp list rc = %d, want 0", rc) |
| 32 | } |
| 33 | }) |
| 34 | if !strings.Contains(out, "legacy-cli") { |
| 35 | t.Fatalf("mcp list should include migrated legacy config:\n%s", out) |
| 36 | } |
| 37 | |
| 38 | body, err := os.ReadFile(config.UserConfigPath()) |
| 39 | if err != nil { |
| 40 | t.Fatalf("read migrated user config: %v", err) |
| 41 | } |
| 42 | for _, want := range []string{fmt.Sprintf("config_version = %d", config.Default().ConfigVersion), `[desktop]`, `name = "legacy-cli"`} { |
| 43 | if !strings.Contains(string(body), want) { |
| 44 | t.Fatalf("migrated config missing %q:\n%s", want, body) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestRunAppliesUserConfigUpgradesOnStartup(t *testing.T) { |
| 50 | isolateCLIConfigHome(t) |
| 51 | path := config.UserConfigPath() |
| 52 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if err := os.WriteFile(path, []byte("config_version = 2\ndefault_model = \"deepseek-flash\"\n"), 0o644); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | |
| 59 | captureStdout(t, func() { |
| 60 | if rc := Run([]string{"mcp", "list"}, "test-version"); rc != 0 { |
| 61 | t.Fatalf("mcp list rc = %d, want 0", rc) |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | body, err := os.ReadFile(path) |
| 66 | if err != nil { |
| 67 | t.Fatalf("read upgraded user config: %v", err) |
| 68 | } |
| 69 | if !strings.Contains(string(body), fmt.Sprintf("config_version = %d", config.Default().ConfigVersion)) { |
| 70 | t.Fatalf("CLI startup should apply user config upgrades:\n%s", body) |
| 71 | } |
| 72 | } |
| 73 |