| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestCLIUpdateChannelDefaultsAndValidation(t *testing.T) { |
| 11 | cfg := Default() |
| 12 | if got := cfg.CLIUpdateChannel(); got != "stable" { |
| 13 | t.Fatalf("default CLI channel = %q, want stable", got) |
| 14 | } |
| 15 | if err := cfg.SetCLIUpdateChannel("preview"); err != nil || cfg.CLIUpdateChannel() != "stable" { |
| 16 | t.Fatalf("SetCLIUpdateChannel(preview) = %q, %v", cfg.CLIUpdateChannel(), err) |
| 17 | } |
| 18 | if err := cfg.SetCLIUpdateChannel("stable"); err != nil || cfg.CLIUpdateChannel() != "stable" { |
| 19 | t.Fatalf("SetCLIUpdateChannel(stable) = %q, %v", cfg.CLIUpdateChannel(), err) |
| 20 | } |
| 21 | if err := cfg.SetCLIUpdateChannel("canary"); err != nil || cfg.CLIUpdateChannel() != "stable" { |
| 22 | t.Fatalf("legacy canary did not migrate to stable: %q, %v", cfg.CLIUpdateChannel(), err) |
| 23 | } |
| 24 | cfg.CLI.UpdateChannel = "unknown-from-newer-version" |
| 25 | if got := cfg.CLIUpdateChannel(); got != "stable" { |
| 26 | t.Fatalf("unknown stored CLI channel = %q, want stable fallback", got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestCLIUpdateChannelIsUserGlobal(t *testing.T) { |
| 31 | home := t.TempDir() |
| 32 | root := t.TempDir() |
| 33 | t.Setenv("REASONIX_HOME", home) |
| 34 | if err := os.WriteFile(filepath.Join(home, "config.toml"), []byte("[cli]\nupdate_channel = \"preview\"\n"), 0o600); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte("[cli]\nupdate_channel = \"stable\"\n"), 0o600); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | cfg, err := LoadForRootReadOnly(root) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if got := cfg.CLIUpdateChannel(); got != "stable" { |
| 45 | t.Fatalf("legacy user channel did not migrate to stable: %q", got) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestProjectCannotOptOldConfigIntoCLIPreview(t *testing.T) { |
| 50 | home := t.TempDir() |
| 51 | root := t.TempDir() |
| 52 | t.Setenv("REASONIX_HOME", home) |
| 53 | if err := os.WriteFile(filepath.Join(home, "config.toml"), []byte("[ui]\ntheme = \"auto\"\n"), 0o600); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte("[cli]\nupdate_channel = \"preview\"\n"), 0o600); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | cfg, err := LoadForRootReadOnly(root) |
| 60 | if err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if got := cfg.CLIUpdateChannel(); got != "stable" { |
| 64 | t.Fatalf("project opted missing/old user config into %q, want stable", got) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestCLIUpdateChannelIsOmittedFromCanonicalRendering(t *testing.T) { |
| 69 | cfg := Default() |
| 70 | if err := cfg.SetCLIUpdateChannel("preview"); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | user := RenderTOMLForScope(cfg, RenderScopeUser) |
| 74 | project := RenderTOMLForScope(cfg, RenderScopeProject) |
| 75 | if strings.Contains(user, "[cli]") || strings.Contains(user, "update_channel") { |
| 76 | t.Fatalf("user render retained retired CLI update channel:\n%s", user) |
| 77 | } |
| 78 | if strings.Contains(project, "[cli]") { |
| 79 | t.Fatalf("project render contains user-global CLI config:\n%s", project) |
| 80 | } |
| 81 | } |
| 82 |