Răsfoiți Sursa

chore: Fix broken unit tests, add themeLoading test

jamesread 5 luni în urmă
părinte
comite
93d56cc42e

+ 11 - 0
integration-tests/tests/themeLoading/config.yaml

@@ -0,0 +1,11 @@
+---
+listenAddressSingleHTTPFrontend: 0.0.0.0:1337
+
+logLevel: "DEBUG"
+checkForUpdates: false
+
+actions:
+  - title: Test action
+    shell: "echo 'Hello from theme loading test'"
+    icon: ping
+

+ 5 - 0
integration-tests/tests/themeLoading/custom-webui/themes/theme-one/theme.css

@@ -0,0 +1,5 @@
+/* Theme One CSS */
+body {
+  background-color: #theme-one;
+}
+

+ 5 - 0
integration-tests/tests/themeLoading/custom-webui/themes/theme-two/theme.css

@@ -0,0 +1,5 @@
+/* Theme Two CSS */
+body {
+  background-color: #theme-two;
+}
+

+ 73 - 0
integration-tests/tests/themeLoading/themeLoading.mjs

@@ -0,0 +1,73 @@
+import { describe, it, before, after } from 'mocha'
+import { expect } from 'chai'
+import {
+  getRootAndWait,
+  takeScreenshotOnFailure,
+} from '../../lib/elements.js'
+
+describe('config: themeLoading', function () {
+  before(async function () {
+    await runner.start('themeLoading')
+  })
+
+  after(async () => {
+    await runner.stop()
+  })
+
+  afterEach(function () {
+    takeScreenshotOnFailure(this.currentTest, webdriver)
+  })
+
+  it('Available themes are discovered and returned in Init response', async function () {
+    await getRootAndWait()
+
+    // Wait for initResponse to be available
+    await webdriver.wait(async () => {
+      const hasInitResponse = await webdriver.executeScript(
+        'return typeof window.initResponse !== "undefined" && window.initResponse !== null'
+      )
+      return hasInitResponse
+    }, 5000, 'Init response should be available')
+
+    // Get available themes from the Init response
+    const availableThemes = await webdriver.executeScript(
+      'return window.initResponse ? (window.initResponse.availableThemes || []) : []'
+    )
+
+    // Verify themes array exists and is an array
+    expect(availableThemes).to.be.an('array')
+
+    // Verify that themes with theme.css are discovered
+    // theme-one and theme-two have theme.css, invalid-theme does not
+    expect(availableThemes).to.include('theme-one')
+    expect(availableThemes).to.include('theme-two')
+    
+    // Verify that themes without theme.css are not included
+    expect(availableThemes).to.not.include('invalid-theme')
+
+    // Verify themes are sorted alphabetically
+    const sortedThemes = [...availableThemes].sort()
+    expect(availableThemes).to.deep.equal(sortedThemes)
+  })
+
+  it('Available themes list is accessible via JavaScript', async function () {
+    await getRootAndWait()
+
+    // Wait for initResponse to be available
+    await webdriver.wait(async () => {
+      const hasInitResponse = await webdriver.executeScript(
+        'return typeof window.initResponse !== "undefined" && window.initResponse !== null'
+      )
+      return hasInitResponse
+    }, 5000, 'Init response should be available')
+
+    // Verify availableThemes is accessible
+    const availableThemes = await webdriver.executeScript(
+      'return window.initResponse ? (window.initResponse.availableThemes || []) : []'
+    )
+
+    expect(availableThemes).to.be.an('array')
+    expect(availableThemes.length).to.be.at.least(2)
+  })
+})
+

+ 8 - 3
service/internal/api/api.go

@@ -913,8 +913,13 @@ func (api *oliveTinAPI) Init(ctx ctx.Context, req *connect.Request[apiv1.InitReq
 // discoverAvailableThemes finds all available themes in the custom-webui/themes directory.
 // discoverAvailableThemes finds all available themes in the custom-webui/themes directory.
 // A theme is considered available if it has a theme.css file.
 // A theme is considered available if it has a theme.css file.
 func discoverAvailableThemes(cfg *config.Config) []string {
 func discoverAvailableThemes(cfg *config.Config) []string {
-	themesDir := path.Join(cfg.GetDir(), "custom-webui", "themes")
-	
+	configDir := cfg.GetDir()
+	if configDir == "" {
+		return []string{}
+	}
+
+	themesDir := path.Join(configDir, "custom-webui", "themes")
+
 	entries, err := os.ReadDir(themesDir)
 	entries, err := os.ReadDir(themesDir)
 	if err != nil {
 	if err != nil {
 		log.WithFields(log.Fields{
 		log.WithFields(log.Fields{
@@ -932,7 +937,7 @@ func discoverAvailableThemes(cfg *config.Config) []string {
 
 
 		themeName := entry.Name()
 		themeName := entry.Name()
 		themeCssPath := path.Join(themesDir, themeName, "theme.css")
 		themeCssPath := path.Join(themesDir, themeName, "theme.css")
-		
+
 		if _, err := os.Stat(themeCssPath); err == nil {
 		if _, err := os.Stat(themeCssPath); err == nil {
 			themes = append(themes, themeName)
 			themes = append(themes, themeName)
 		}
 		}

+ 3 - 0
service/internal/config/config_helpers.go

@@ -58,5 +58,8 @@ func (cfg *Config) SetDir(dir string) {
 }
 }
 
 
 func (cfg *Config) GetDir() string {
 func (cfg *Config) GetDir() string {
+	if len(cfg.sourceFiles) == 0 {
+		return ""
+	}
 	return cfg.sourceFiles[len(cfg.sourceFiles)-1]
 	return cfg.sourceFiles[len(cfg.sourceFiles)-1]
 }
 }