1package tui
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func writeThemeFile(t *testing.T, name, body string) string {
11 t.Helper()
12 home := t.TempDir()
13 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
14 t.Fatal(err)
15 }
16 path := filepath.Join(home, "themes", name+".json")
17 if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
18 t.Fatal(err)
19 }
20 return home
21}
22
23func TestLoadThemeAllowsPartialColorOverrides(t *testing.T) {
24 home := writeThemeFile(t, "partial", `{"colors":{"dark":{"accent":204}}}`)
25
26 th, name, err := LoadThemeFromHome(home, "partial", Dark)
27 if err != nil {
28 t.Fatal(err)
29 }
30 if name != "partial" {
31 t.Fatalf("name = %q, want partial", name)
32 }
33 if th.Accent != Color256(204) {
34 t.Fatalf("accent = %#v, want 204", th.Accent)
35 }
36 if th.FG != Dark.FG {
37 t.Fatalf("fg = %#v, want inherited %#v", th.FG, Dark.FG)
38 }
39 if len(th.SpinnerFrames) == 0 {
40 t.Fatal("spinner frames should be inherited")
41 }
42}
43
44func TestLoadThemeAllowsThinkingMaxOverride(t *testing.T) {
45 home := writeThemeFile(t, "thinking", `{"colors":{"dark":{"thinkingMax":201}}}`)
46
47 th, _, err := LoadThemeFromHome(home, "thinking", Dark)
48 if err != nil {
49 t.Fatal(err)
50 }
51 if th.ThinkingMax != Color256(201) {
52 t.Fatalf("thinking max = %#v, want 201", th.ThinkingMax)
53 }
54}
55
56func TestLoadThemeAllowsSpinnerAppearanceOverrides(t *testing.T) {
57 home := writeThemeFile(t, "spinner", `{"spinner_frames":[".","o"],"spinner_messages":["working"],"spinner_interval_ms":200}`)
58
59 th, _, err := LoadThemeFromHome(home, "spinner", Dark)
60 if err != nil {
61 t.Fatal(err)
62 }
63 if got := len(th.SpinnerFrames); got != 2 {
64 t.Fatalf("spinner frame count = %d, want 2", got)
65 }
66 if th.SpinnerFrames[1] != "o" || th.SpinnerIntervalMS != 200 {
67 t.Fatalf("spinner appearance overrides not applied: %#v %d", th.SpinnerFrames, th.SpinnerIntervalMS)
68 }
69 if th.Accent != Dark.Accent {
70 t.Fatalf("accent = %#v, want inherited %#v", th.Accent, Dark.Accent)
71 }
72}
73
74func TestLoadThemeFallsBackToDarkWhenLightModeMissing(t *testing.T) {
75 home := writeThemeFile(t, "darkonly", `{"colors":{"dark":{"spinner_frames":["◢","◣","◤","◥"],"spinner_messages":["working"],"spinner_interval_ms":120}}}`)
76
77 th, _, err := LoadThemeFromHome(home, "darkonly", Light)
78 if err != nil {
79 t.Fatal(err)
80 }
81 if len(th.SpinnerFrames) != 4 || th.SpinnerFrames[0] != "◢" {
82 t.Fatalf("spinner frames = %#v, want dark fallback frames", th.SpinnerFrames)
83 }
84 if th.SpinnerIntervalMS != 120 {
85 t.Fatalf("spinner interval = %d, want 120", th.SpinnerIntervalMS)
86 }
87 if th.FG != Light.FG {
88 t.Fatalf("fg = %#v, want inherited light fg %#v", th.FG, Light.FG)
89 }
90}
91
92func TestLoadThemeIgnoresLegacySpinnerMessages(t *testing.T) {
93 home := writeThemeFile(t, "shared", `{"colors":{"accent":204,"spinner_messages":["ship"]}}`)
94
95 th, _, err := LoadThemeFromHome(home, "shared", Light)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if th.Accent != Color256(204) {
100 t.Fatalf("accent = %#v, want 204", th.Accent)
101 }
102 if len(th.SpinnerFrames) != len(Light.SpinnerFrames) || th.SpinnerIntervalMS != Light.SpinnerIntervalMS {
103 t.Fatalf("legacy spinner_messages changed spinner appearance: %#v %d", th.SpinnerFrames, th.SpinnerIntervalMS)
104 }
105 if th.FG != Light.FG {
106 t.Fatalf("fg = %#v, want inherited %#v", th.FG, Light.FG)
107 }
108}
109
110func TestLoadThemeAcceptsRGBSemanticColors(t *testing.T) {
111 home := writeThemeFile(t, "rgb", `{"colors":{"dark":{"fg":"#123456","muted":{"mode":"rgb","r":18,"g":52,"b":86},"accent":{"mode":"rgb","r":200,"g":100,"b":50},"user": {"mode":"ansi","index":31},"selection_bg":{"mode":"256","index":237},"selection_fg":250}}}`)
112
113 detected := Dark
114 detected.Terminal.TrueColor = true
115 th, _, err := LoadThemeFromHome(home, "rgb", detected)
116 if err != nil {
117 t.Fatal(err)
118 }
119 if !th.Terminal.TrueColor {
120 t.Fatal("terminal color mode = 256, want truecolor")
121 }
122 if th.FG != ColorRGB(0x12, 0x34, 0x56) {
123 t.Fatalf("foreground = %#v, want RGB", th.FG)
124 }
125 if th.Accent != ColorRGB(200, 100, 50) {
126 t.Fatalf("accent = %#v, want RGB", th.Accent)
127 }
128 if th.SelectionBG != Color256(237) || th.SelectionFG != Color256(250) {
129 t.Fatalf("indexed semantic colors were not preserved: bg=%#v fg=%#v", th.SelectionBG, th.SelectionFG)
130 }
131 if got := th.FGColor(th.Accent, "x"); !strings.Contains(got, "\x1b[38;2;200;100;50m") {
132 t.Fatalf("RGB accent was not rendered as truecolor: %q", got)
133 }
134}
135
136func TestLoadThemeQuantizesRGBSemanticColorsWithoutTrueColor(t *testing.T) {
137 home := writeThemeFile(t, "rgb-fallback", `{"colors":{"dark":{"accent":"#c86432"}}}`)
138
139 th, _, err := LoadThemeFromHome(home, "rgb-fallback", Dark)
140 if err != nil {
141 t.Fatal(err)
142 }
143 want := nearestXtermColor(200, 100, 50)
144 if got := th.FGColor(th.Accent, "x"); got != sgrFG(want)+"x\x1b[0m" {
145 t.Fatalf("RGB accent fallback = %q, want xterm-256 index %d", got, want)
146 }
147}