| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | ) |
| 11 | |
| 12 | func TestActivateBaseStyleClearsActivePack(t *testing.T) { |
| 13 | home := t.TempDir() |
| 14 | t.Setenv("REASONIX_HOME", home) |
| 15 | app := NewApp() |
| 16 | |
| 17 | // Seed a user pack and activate it. |
| 18 | m := &ThemePackManifest{ |
| 19 | SchemaVersion: 1, ID: "user-amber-skin", Name: "User Amber", BaseStyle: "amber", |
| 20 | Recipes: defaultThemePackRecipes(), |
| 21 | } |
| 22 | staging, err := writeThemeStaging(m, "", nil) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | defer os.RemoveAll(staging) |
| 27 | if err := publishThemeDir(m.ID, staging, false); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := app.ActivateThemePack(m.ID); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if err := app.ActivateBaseStyle("slate"); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | exp, err := app.GetThemeExperience() |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if exp.ActiveThemeID != "" || exp.ActivePack != nil { |
| 41 | t.Fatalf("expected no active pack after base style, got %+v", exp) |
| 42 | } |
| 43 | if exp.BaseStyle != "slate" || exp.EffectiveStyle != "slate" { |
| 44 | t.Fatalf("base/effective = %q/%q, want slate", exp.BaseStyle, exp.EffectiveStyle) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestActivateThemePackRejectsBaseStyle(t *testing.T) { |
| 49 | home := t.TempDir() |
| 50 | t.Setenv("REASONIX_HOME", home) |
| 51 | app := NewApp() |
| 52 | if err := app.ActivateThemePack("amber"); err == nil { |
| 53 | t.Fatal("expected base style activation to fail") |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestMigrateV1BaseActiveIDToBaseStyle(t *testing.T) { |
| 58 | home := t.TempDir() |
| 59 | t.Setenv("REASONIX_HOME", home) |
| 60 | // Write v1 state with base style as activeThemeId. |
| 61 | statePath := filepath.Join(config.MemoryUserDir(), themeStateFileName) |
| 62 | if err := os.MkdirAll(filepath.Dir(statePath), 0o755); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | raw, _ := json.Marshal(map[string]any{"schemaVersion": 1, "activeThemeId": "amber"}) |
| 66 | if err := os.WriteFile(statePath, raw, 0o644); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | app := NewApp() |
| 70 | exp, err := app.GetThemeExperience() |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if exp.ActiveThemeID != "" { |
| 75 | t.Fatalf("activeThemeId should be cleared, got %q", exp.ActiveThemeID) |
| 76 | } |
| 77 | if exp.BaseStyle != "amber" { |
| 78 | t.Fatalf("baseStyle = %q, want amber after migration", exp.BaseStyle) |
| 79 | } |
| 80 | // File rewritten as v2 without base id. |
| 81 | st := loadThemeDesktopState() |
| 82 | if st.SchemaVersion != themeStateSchemaVer || st.ActiveThemeID != "" { |
| 83 | t.Fatalf("persisted state = %+v", st) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestOfficialThemeOrderFixed(t *testing.T) { |
| 88 | resetOfficialRegistryForTest() |
| 89 | themes := officialThemes() |
| 90 | if len(themes) != len(officialThemeOrderFixed) { |
| 91 | t.Fatalf("count %d want %d", len(themes), len(officialThemeOrderFixed)) |
| 92 | } |
| 93 | for i, ot := range themes { |
| 94 | if ot.manifest.ID != officialThemeOrderFixed[i] { |
| 95 | t.Fatalf("order[%d] = %q, want %q", i, ot.manifest.ID, officialThemeOrderFixed[i]) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestDisableThemePackKeepsBaseStyle(t *testing.T) { |
| 101 | home := t.TempDir() |
| 102 | t.Setenv("REASONIX_HOME", home) |
| 103 | app := NewApp() |
| 104 | if err := app.ActivateBaseStyle("nocturne"); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | // Activate official if present. |
| 108 | if len(officialThemes()) == 0 { |
| 109 | t.Skip("no official themes") |
| 110 | } |
| 111 | id := officialThemes()[0].manifest.ID |
| 112 | if err := app.ActivateThemePack(id); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | if err := app.DisableThemePack(); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | exp, err := app.GetThemeExperience() |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if exp.ActiveThemeID != "" { |
| 123 | t.Fatalf("expected disabled pack, got %q", exp.ActiveThemeID) |
| 124 | } |
| 125 | if exp.BaseStyle != "nocturne" { |
| 126 | t.Fatalf("baseStyle should stay nocturne, got %q", exp.BaseStyle) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestRestoreGraphiteAppearance(t *testing.T) { |
| 131 | home := t.TempDir() |
| 132 | t.Setenv("REASONIX_HOME", home) |
| 133 | app := NewApp() |
| 134 | _ = app.ActivateBaseStyle("carbon") |
| 135 | if len(officialThemes()) > 0 { |
| 136 | _ = app.ActivateThemePack(officialThemes()[0].manifest.ID) |
| 137 | } |
| 138 | if err := app.RestoreGraphiteAppearance(); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | exp, err := app.GetThemeExperience() |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | if exp.BaseStyle != "graphite" || exp.ActiveThemeID != "" { |
| 146 | t.Fatalf("got base=%q active=%q", exp.BaseStyle, exp.ActiveThemeID) |
| 147 | } |
| 148 | } |
| 149 |