loader_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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", "example"}
  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. // Test the example theme which uses references
  40. example := GetTheme("example")
  41. if example == nil {
  42. t.Fatal("Failed to get example theme")
  43. }
  44. // Check that brandBlue reference was resolved
  45. primary := example.Primary()
  46. if primary.Dark == nil || primary.Light == nil {
  47. t.Error("Primary color (brandBlue reference) not resolved")
  48. }
  49. // Check that nested reference (borderActive -> primary -> brandBlue) works
  50. borderActive := example.BorderActive()
  51. if borderActive.Dark == nil || borderActive.Light == nil {
  52. t.Error("BorderActive color (nested reference) not resolved")
  53. }
  54. }
  55. func TestLoadThemesFromDirectories(t *testing.T) {
  56. // Create temporary directories for testing
  57. tempDir := t.TempDir()
  58. userConfig := filepath.Join(tempDir, "config")
  59. projectRoot := filepath.Join(tempDir, "project")
  60. cwd := filepath.Join(tempDir, "cwd")
  61. // Create theme directories
  62. os.MkdirAll(filepath.Join(userConfig, "opencode", "themes"), 0755)
  63. os.MkdirAll(filepath.Join(projectRoot, ".opencode", "themes"), 0755)
  64. os.MkdirAll(filepath.Join(cwd, ".opencode", "themes"), 0755)
  65. // Create test themes with same name to test override behavior
  66. testTheme1 := `{
  67. "theme": {
  68. "primary": "#111111",
  69. "secondary": "#222222",
  70. "accent": "#333333",
  71. "text": "#ffffff",
  72. "textMuted": "#cccccc",
  73. "background": "#000000"
  74. }
  75. }`
  76. testTheme2 := `{
  77. "theme": {
  78. "primary": "#444444",
  79. "secondary": "#555555",
  80. "accent": "#666666",
  81. "text": "#ffffff",
  82. "textMuted": "#cccccc",
  83. "background": "#000000"
  84. }
  85. }`
  86. testTheme3 := `{
  87. "theme": {
  88. "primary": "#777777",
  89. "secondary": "#888888",
  90. "accent": "#999999",
  91. "text": "#ffffff",
  92. "textMuted": "#cccccc",
  93. "background": "#000000"
  94. }
  95. }`
  96. // Write themes to different directories
  97. os.WriteFile(filepath.Join(userConfig, "opencode", "themes", "override-test.json"), []byte(testTheme1), 0644)
  98. os.WriteFile(filepath.Join(projectRoot, ".opencode", "themes", "override-test.json"), []byte(testTheme2), 0644)
  99. os.WriteFile(filepath.Join(cwd, ".opencode", "themes", "override-test.json"), []byte(testTheme3), 0644)
  100. // Load themes
  101. err := LoadThemesFromDirectories(userConfig, projectRoot, cwd)
  102. if err != nil {
  103. t.Fatalf("Failed to load themes from directories: %v", err)
  104. }
  105. // Check that the theme from CWD (highest priority) won
  106. overrideTheme := GetTheme("override-test")
  107. if overrideTheme == nil {
  108. t.Fatal("Failed to get override-test theme")
  109. }
  110. // The primary color should be from testTheme3 (#777777)
  111. primary := overrideTheme.Primary()
  112. // We can't directly check the color value, but we can verify it was loaded
  113. if primary.Dark == nil || primary.Light == nil {
  114. t.Error("Override theme not properly loaded")
  115. }
  116. }