1package tui
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7)
8
9func TestLoadThemeAllowsPartialColorOverrides(t *testing.T) {
10 home := t.TempDir()
11 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
12 t.Fatal(err)
13 }
14 path := filepath.Join(home, "themes", "partial.json")
15 if err := os.WriteFile(path, []byte(`{"colors":{"dark":{"accent":204}}}`), 0o644); err != nil {
16 t.Fatal(err)
17 }
18
19 th, name, err := LoadThemeFromHome(home, "partial", Dark)
20 if err != nil {
21 t.Fatal(err)
22 }
23 if name != "partial" {
24 t.Fatalf("name = %q, want partial", name)
25 }
26 if th.Accent != 204 {
27 t.Fatalf("accent = %d, want 204", th.Accent)
28 }
29 if th.FG != Dark.FG {
30 t.Fatalf("fg = %d, want inherited %d", th.FG, Dark.FG)
31 }
32 if len(th.SpinnerFrames) == 0 || len(th.SpinnerMessages) == 0 {
33 t.Fatal("spinner defaults should be inherited")
34 }
35}
36
37func TestLoadThemeAllowsThinkingMaxOverride(t *testing.T) {
38 home := t.TempDir()
39 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
40 t.Fatal(err)
41 }
42 path := filepath.Join(home, "themes", "thinking.json")
43 if err := os.WriteFile(path, []byte(`{"colors":{"dark":{"thinkingMax":201}}}`), 0o644); err != nil {
44 t.Fatal(err)
45 }
46
47 th, _, err := LoadThemeFromHome(home, "thinking", Dark)
48 if err != nil {
49 t.Fatal(err)
50 }
51 if th.ThinkingMax != 201 {
52 t.Fatalf("thinking max = %d, want 201", th.ThinkingMax)
53 }
54}
55
56func TestLoadThemeAllowsSpinnerOnlyTopLevelOverrides(t *testing.T) {
57 home := t.TempDir()
58 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
59 t.Fatal(err)
60 }
61 path := filepath.Join(home, "themes", "spinner.json")
62 if err := os.WriteFile(path, []byte(`{"spinner_frames":[".","o"],"spinner_messages":["working"],"spinner_interval_ms":200}`), 0o644); err != nil {
63 t.Fatal(err)
64 }
65
66 th, _, err := LoadThemeFromHome(home, "spinner", Dark)
67 if err != nil {
68 t.Fatal(err)
69 }
70 if got := len(th.SpinnerFrames); got != 2 {
71 t.Fatalf("spinner frame count = %d, want 2", got)
72 }
73 if th.SpinnerFrames[1] != "o" || th.SpinnerMessages[0] != "working" || th.SpinnerIntervalMS != 200 {
74 t.Fatalf("spinner overrides not applied: %#v %#v %d", th.SpinnerFrames, th.SpinnerMessages, th.SpinnerIntervalMS)
75 }
76 if th.Accent != Dark.Accent {
77 t.Fatalf("accent = %d, want inherited %d", th.Accent, Dark.Accent)
78 }
79}
80
81func TestLoadThemeFallsBackToDarkWhenLightModeMissing(t *testing.T) {
82 home := t.TempDir()
83 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
84 t.Fatal(err)
85 }
86 path := filepath.Join(home, "themes", "darkonly.json")
87 if err := os.WriteFile(path, []byte(`{"colors":{"dark":{"spinner_frames":["◢","◣","◤","◥"],"spinner_messages":["working"],"spinner_interval_ms":120}}}`), 0o644); err != nil {
88 t.Fatal(err)
89 }
90
91 th, _, err := LoadThemeFromHome(home, "darkonly", Light)
92 if err != nil {
93 t.Fatal(err)
94 }
95 if len(th.SpinnerFrames) != 4 || th.SpinnerFrames[0] != "◢" {
96 t.Fatalf("spinner frames = %#v, want dark fallback frames", th.SpinnerFrames)
97 }
98 if len(th.SpinnerMessages) != 1 || th.SpinnerMessages[0] != "working" {
99 t.Fatalf("spinner messages = %#v, want dark fallback message", th.SpinnerMessages)
100 }
101 if th.SpinnerIntervalMS != 120 {
102 t.Fatalf("spinner interval = %d, want 120", th.SpinnerIntervalMS)
103 }
104 if th.FG != Light.FG {
105 t.Fatalf("fg = %d, want inherited light fg %d", th.FG, Light.FG)
106 }
107}
108
109func TestLoadThemeAllowsSharedColorsOverrides(t *testing.T) {
110 home := t.TempDir()
111 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
112 t.Fatal(err)
113 }
114 path := filepath.Join(home, "themes", "shared.json")
115 if err := os.WriteFile(path, []byte(`{"colors":{"accent":204,"spinner_messages":["ship"]}}`), 0o644); err != nil {
116 t.Fatal(err)
117 }
118
119 th, _, err := LoadThemeFromHome(home, "shared", Light)
120 if err != nil {
121 t.Fatal(err)
122 }
123 if th.Accent != 204 {
124 t.Fatalf("accent = %d, want 204", th.Accent)
125 }
126 if len(th.SpinnerMessages) != 1 || th.SpinnerMessages[0] != "ship" {
127 t.Fatalf("spinner messages = %#v, want ship", th.SpinnerMessages)
128 }
129 if th.FG != Light.FG {
130 t.Fatalf("fg = %d, want inherited %d", th.FG, Light.FG)
131 }
132}