summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/theme/loader_test.go
blob: 1039ab3f1ca53f8ab570e169409d9fbc90e4888b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package theme

import (
	"os"
	"path/filepath"
	"slices"
	"testing"
)

func TestLoadThemesFromJSON(t *testing.T) {
	// Test loading themes
	err := LoadThemesFromJSON()
	if err != nil {
		t.Fatalf("Failed to load themes: %v", err)
	}

	// Check that themes were loaded
	themes := AvailableThemes()
	if len(themes) == 0 {
		t.Fatal("No themes were loaded")
	}

	// Check for expected themes
	expectedThemes := []string{"tokyonight", "opencode", "everforest", "ayu", "example"}
	for _, expected := range expectedThemes {
		found := slices.Contains(themes, expected)
		if !found {
			t.Errorf("Expected theme %s not found", expected)
		}
	}

	// Test getting a specific theme
	tokyonight := GetTheme("tokyonight")
	if tokyonight == nil {
		t.Fatal("Failed to get tokyonight theme")
	}

	// Test theme colors
	primary := tokyonight.Primary()
	if primary.Dark == nil || primary.Light == nil {
		t.Error("Primary color not properly set")
	}
}

func TestColorReferenceResolution(t *testing.T) {
	// Test the example theme which uses references
	example := GetTheme("example")
	if example == nil {
		t.Fatal("Failed to get example theme")
	}

	// Check that brandBlue reference was resolved
	primary := example.Primary()
	if primary.Dark == nil || primary.Light == nil {
		t.Error("Primary color (brandBlue reference) not resolved")
	}

	// Check that nested reference (borderActive -> primary -> brandBlue) works
	borderActive := example.BorderActive()
	if borderActive.Dark == nil || borderActive.Light == nil {
		t.Error("BorderActive color (nested reference) not resolved")
	}
}

func TestLoadThemesFromDirectories(t *testing.T) {
	// Create temporary directories for testing
	tempDir := t.TempDir()

	userConfig := filepath.Join(tempDir, "config")
	projectRoot := filepath.Join(tempDir, "project")
	cwd := filepath.Join(tempDir, "cwd")

	// Create theme directories
	os.MkdirAll(filepath.Join(userConfig, "opencode", "themes"), 0755)
	os.MkdirAll(filepath.Join(projectRoot, ".opencode", "themes"), 0755)
	os.MkdirAll(filepath.Join(cwd, ".opencode", "themes"), 0755)

	// Create test themes with same name to test override behavior
	testTheme1 := `{
		"theme": {
			"primary": "#111111",
			"secondary": "#222222",
			"accent": "#333333",
			"text": "#ffffff",
			"textMuted": "#cccccc",
			"background": "#000000"
		}
	}`

	testTheme2 := `{
		"theme": {
			"primary": "#444444",
			"secondary": "#555555",
			"accent": "#666666",
			"text": "#ffffff",
			"textMuted": "#cccccc",
			"background": "#000000"
		}
	}`

	testTheme3 := `{
		"theme": {
			"primary": "#777777",
			"secondary": "#888888",
			"accent": "#999999",
			"text": "#ffffff",
			"textMuted": "#cccccc",
			"background": "#000000"
		}
	}`

	// Write themes to different directories
	os.WriteFile(filepath.Join(userConfig, "opencode", "themes", "override-test.json"), []byte(testTheme1), 0644)
	os.WriteFile(filepath.Join(projectRoot, ".opencode", "themes", "override-test.json"), []byte(testTheme2), 0644)
	os.WriteFile(filepath.Join(cwd, ".opencode", "themes", "override-test.json"), []byte(testTheme3), 0644)

	// Load themes
	err := LoadThemesFromDirectories(userConfig, projectRoot, cwd)
	if err != nil {
		t.Fatalf("Failed to load themes from directories: %v", err)
	}

	// Check that the theme from CWD (highest priority) won
	overrideTheme := GetTheme("override-test")
	if overrideTheme == nil {
		t.Fatal("Failed to get override-test theme")
	}

	// The primary color should be from testTheme3 (#777777)
	primary := overrideTheme.Primary()
	// We can't directly check the color value, but we can verify it was loaded
	if primary.Dark == nil || primary.Light == nil {
		t.Error("Override theme not properly loaded")
	}
}