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 || len(th.SpinnerMessages) == 0 {
40 t.Fatal("spinner defaults 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 TestLoadThemeAllowsSpinnerOnlyTopLevelOverrides(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.SpinnerMessages[0] != "working" || th.SpinnerIntervalMS != 200 {
67 t.Fatalf("spinner overrides not applied: %#v %#v %d", th.SpinnerFrames, th.SpinnerMessages, 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 len(th.SpinnerMessages) != 1 || th.SpinnerMessages[0] != "working" {
85 t.Fatalf("spinner messages = %#v, want dark fallback message", th.SpinnerMessages)
86 }
87 if th.SpinnerIntervalMS != 120 {
88 t.Fatalf("spinner interval = %d, want 120", th.SpinnerIntervalMS)
89 }
90 if th.FG != Light.FG {
91 t.Fatalf("fg = %#v, want inherited light fg %#v", th.FG, Light.FG)
92 }
93}
94
95func TestLoadThemeAllowsSharedColorsOverrides(t *testing.T) {
96 home := writeThemeFile(t, "shared", `{"colors":{"accent":204,"spinner_messages":["ship"]}}`)
97
98 th, _, err := LoadThemeFromHome(home, "shared", Light)
99 if err != nil {
100 t.Fatal(err)
101 }
102 if th.Accent != Color256(204) {
103 t.Fatalf("accent = %#v, want 204", th.Accent)
104 }
105 if len(th.SpinnerMessages) != 1 || th.SpinnerMessages[0] != "ship" {
106 t.Fatalf("spinner messages = %#v, want ship", th.SpinnerMessages)
107 }
108 if th.FG != Light.FG {
109 t.Fatalf("fg = %#v, want inherited %#v", th.FG, Light.FG)
110 }
111}
112
113func TestLoadThemeAcceptsRGBSemanticColors(t *testing.T) {
114 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}}}`)
115
116 detected := Dark
117 detected.Terminal.TrueColor = true
118 th, _, err := LoadThemeFromHome(home, "rgb", detected)
119 if err != nil {
120 t.Fatal(err)
121 }
122 if !th.Terminal.TrueColor {
123 t.Fatal("terminal color mode = 256, want truecolor")
124 }
125 if th.FG != ColorRGB(0x12, 0x34, 0x56) {
126 t.Fatalf("foreground = %#v, want RGB", th.FG)
127 }
128 if th.Accent != ColorRGB(200, 100, 50) {
129 t.Fatalf("accent = %#v, want RGB", th.Accent)
130 }
131 if th.SelectionBG != Color256(237) || th.SelectionFG != Color256(250) {
132 t.Fatalf("indexed semantic colors were not preserved: bg=%#v fg=%#v", th.SelectionBG, th.SelectionFG)
133 }
134 if got := th.FGColor(th.Accent, "x"); !strings.Contains(got, "\x1b[38;2;200;100;50m") {
135 t.Fatalf("RGB accent was not rendered as truecolor: %q", got)
136 }
137}
138
139func TestLoadThemeQuantizesRGBSemanticColorsWithoutTrueColor(t *testing.T) {
140 home := writeThemeFile(t, "rgb-fallback", `{"colors":{"dark":{"accent":"#c86432"}}}`)
141
142 th, _, err := LoadThemeFromHome(home, "rgb-fallback", Dark)
143 if err != nil {
144 t.Fatal(err)
145 }
146 want := nearestXtermColor(200, 100, 50)
147 if got := th.FGColor(th.Accent, "x"); got != sgrFG(want)+"x\x1b[0m" {
148 t.Fatalf("RGB accent fallback = %q, want xterm-256 index %d", got, want)
149 }
150}