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 TestLoadThemeAllowsSpinnerOnlyTopLevelOverrides(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", "spinner.json")
43 if err := os.WriteFile(path, []byte(`{"spinner_frames":[".","o"],"spinner_messages":["working"],"spinner_interval_ms":200}`), 0o644); err != nil {
44 t.Fatal(err)
45 }
46
47 th, _, err := LoadThemeFromHome(home, "spinner", Dark)
48 if err != nil {
49 t.Fatal(err)
50 }
51 if got := len(th.SpinnerFrames); got != 2 {
52 t.Fatalf("spinner frame count = %d, want 2", got)
53 }
54 if th.SpinnerFrames[1] != "o" || th.SpinnerMessages[0] != "working" || th.SpinnerIntervalMS != 200 {
55 t.Fatalf("spinner overrides not applied: %#v %#v %d", th.SpinnerFrames, th.SpinnerMessages, th.SpinnerIntervalMS)
56 }
57 if th.Accent != Dark.Accent {
58 t.Fatalf("accent = %d, want inherited %d", th.Accent, Dark.Accent)
59 }
60}
61
62func TestLoadThemeFallsBackToDarkWhenLightModeMissing(t *testing.T) {
63 home := t.TempDir()
64 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
65 t.Fatal(err)
66 }
67 path := filepath.Join(home, "themes", "darkonly.json")
68 if err := os.WriteFile(path, []byte(`{"colors":{"dark":{"spinner_frames":["◢","◣","◤","◥"],"spinner_messages":["working"],"spinner_interval_ms":120}}}`), 0o644); err != nil {
69 t.Fatal(err)
70 }
71
72 th, _, err := LoadThemeFromHome(home, "darkonly", Light)
73 if err != nil {
74 t.Fatal(err)
75 }
76 if len(th.SpinnerFrames) != 4 || th.SpinnerFrames[0] != "◢" {
77 t.Fatalf("spinner frames = %#v, want dark fallback frames", th.SpinnerFrames)
78 }
79 if len(th.SpinnerMessages) != 1 || th.SpinnerMessages[0] != "working" {
80 t.Fatalf("spinner messages = %#v, want dark fallback message", th.SpinnerMessages)
81 }
82 if th.SpinnerIntervalMS != 120 {
83 t.Fatalf("spinner interval = %d, want 120", th.SpinnerIntervalMS)
84 }
85 if th.FG != Light.FG {
86 t.Fatalf("fg = %d, want inherited light fg %d", th.FG, Light.FG)
87 }
88}
89
90func TestLoadThemeAllowsSharedColorsOverrides(t *testing.T) {
91 home := t.TempDir()
92 if err := os.MkdirAll(filepath.Join(home, "themes"), 0o755); err != nil {
93 t.Fatal(err)
94 }
95 path := filepath.Join(home, "themes", "shared.json")
96 if err := os.WriteFile(path, []byte(`{"colors":{"accent":204,"spinner_messages":["ship"]}}`), 0o644); err != nil {
97 t.Fatal(err)
98 }
99
100 th, _, err := LoadThemeFromHome(home, "shared", Light)
101 if err != nil {
102 t.Fatal(err)
103 }
104 if th.Accent != 204 {
105 t.Fatalf("accent = %d, want 204", th.Accent)
106 }
107 if len(th.SpinnerMessages) != 1 || th.SpinnerMessages[0] != "ship" {
108 t.Fatalf("spinner messages = %#v, want ship", th.SpinnerMessages)
109 }
110 if th.FG != Light.FG {
111 t.Fatalf("fg = %d, want inherited %d", th.FG, Light.FG)
112 }
113}