loader_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package theme
  2. import (
  3. "os"
  4. "path/filepath"
  5. "slices"
  6. "testing"
  7. )
  8. func TestLoadThemesFromJSON(t *testing.T) {
  9. // Test loading themes
  10. err := LoadThemesFromJSON()
  11. if err != nil {
  12. t.Fatalf("Failed to load themes: %v", err)
  13. }
  14. // Check that themes were loaded
  15. themes := AvailableThemes()
  16. if len(themes) == 0 {
  17. t.Fatal("No themes were loaded")
  18. }
  19. // Check for expected themes
  20. expectedThemes := []string{"tokyonight", "opencode", "everforest", "ayu"}
  21. for _, expected := range expectedThemes {
  22. found := slices.Contains(themes, expected)
  23. if !found {
  24. t.Errorf("Expected theme %s not found", expected)
  25. }
  26. }
  27. // Test getting a specific theme
  28. tokyonight := GetTheme("tokyonight")
  29. if tokyonight == nil {
  30. t.Fatal("Failed to get tokyonight theme")
  31. }
  32. // Test theme colors
  33. primary := tokyonight.Primary()
  34. if primary.Dark == nil || primary.Light == nil {
  35. t.Error("Primary color not properly set")
  36. }
  37. }
  38. func TestColorReferenceResolution(t *testing.T) {
  39. // Load themes first
  40. err := LoadThemesFromJSON()
  41. if err != nil {
  42. t.Fatalf("Failed to load themes: %v", err)
  43. }
  44. // Test a theme that uses references (e.g., solarized uses color definitions)
  45. solarized := GetTheme("solarized")
  46. if solarized == nil {
  47. t.Fatal("Failed to get solarized theme")
  48. }
  49. // Check that color references were resolved
  50. primary := solarized.Primary()
  51. if primary.Dark == nil || primary.Light == nil {
  52. t.Error("Primary color reference not resolved")
  53. }
  54. // Check that all colors are properly resolved
  55. text := solarized.Text()
  56. if text.Dark == nil || text.Light == nil {
  57. t.Error("Text color reference not resolved")
  58. }
  59. }
  60. func TestLoadThemesFromDirectories(t *testing.T) {
  61. // Create temporary directories for testing
  62. tempDir := t.TempDir()
  63. userConfig := filepath.Join(tempDir, "config")
  64. projectRoot := filepath.Join(tempDir, "project")
  65. cwd := filepath.Join(tempDir, "cwd")
  66. // Create theme directories
  67. os.MkdirAll(filepath.Join(userConfig, "opencode", "themes"), 0755)
  68. os.MkdirAll(filepath.Join(projectRoot, ".opencode", "themes"), 0755)
  69. os.MkdirAll(filepath.Join(cwd, ".opencode", "themes"), 0755)
  70. // Create test themes with same name to test override behavior
  71. testTheme1 := `{
  72. "theme": {
  73. "primary": "#111111",
  74. "secondary": "#222222",
  75. "accent": "#333333",
  76. "text": "#ffffff",
  77. "textMuted": "#cccccc",
  78. "background": "#000000"
  79. }
  80. }`
  81. testTheme2 := `{
  82. "theme": {
  83. "primary": "#444444",
  84. "secondary": "#555555",
  85. "accent": "#666666",
  86. "text": "#ffffff",
  87. "textMuted": "#cccccc",
  88. "background": "#000000"
  89. }
  90. }`
  91. testTheme3 := `{
  92. "theme": {
  93. "primary": "#777777",
  94. "secondary": "#888888",
  95. "accent": "#999999",
  96. "text": "#ffffff",
  97. "textMuted": "#cccccc",
  98. "background": "#000000"
  99. }
  100. }`
  101. // Write themes to different directories
  102. os.WriteFile(filepath.Join(userConfig, "opencode", "themes", "override-test.json"), []byte(testTheme1), 0644)
  103. os.WriteFile(filepath.Join(projectRoot, ".opencode", "themes", "override-test.json"), []byte(testTheme2), 0644)
  104. os.WriteFile(filepath.Join(cwd, ".opencode", "themes", "override-test.json"), []byte(testTheme3), 0644)
  105. // Load themes
  106. err := LoadThemesFromDirectories(userConfig, projectRoot, cwd)
  107. if err != nil {
  108. t.Fatalf("Failed to load themes from directories: %v", err)
  109. }
  110. // Check that the theme from CWD (highest priority) won
  111. overrideTheme := GetTheme("override-test")
  112. if overrideTheme == nil {
  113. t.Fatal("Failed to get override-test theme")
  114. }
  115. // The primary color should be from testTheme3 (#777777)
  116. primary := overrideTheme.Primary()
  117. // We can't directly check the color value, but we can verify it was loaded
  118. if primary.Dark == nil || primary.Light == nil {
  119. t.Error("Override theme not properly loaded")
  120. }
  121. }