Files
navidrome/plugins/config_validation_test.go
Deluan Quintão f1e75c40dc feat(plugins): add JSONForms-based plugin configuration UI (#4911)
* feat(plugins): add JSONForms schema for plugin configuration

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enhance error handling by formatting validation errors with field names

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enforce required fields in config validation and improve error handling

Signed-off-by: Deluan <deluan@navidrome.org>

* format JS code

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add config schema validation and enhance manifest structure

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: refactor plugin config parsing and add unit tests

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add config validation error message in Portuguese

* feat: enhance AlwaysExpandedArrayLayout with description support and improve array control testing

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: update Discord Rust plugin configuration to use JSONForm for user tokens and enhance schema validation

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: resolve React Hooks linting issues in plugin UI components

* Apply suggestions from code review

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* format code

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: migrate schema validation to use santhosh-tekuri/jsonschema and improve error formatting

Signed-off-by: Deluan <deluan@navidrome.org>

* address PR comments

Signed-off-by: Deluan <deluan@navidrome.org>

* fix flaky test

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enhance array layout and configuration handling with AJV defaults

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement custom tester to exclude enum arrays from AlwaysExpandedArrayLayout

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add error boundary for schema rendering and improve error messages

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: refine non-enum array control logic by utilizing JSONForms schema resolution

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add error styling to ToggleEnabledSwitch for disabled state

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: adjust label positioning and styling in SchemaConfigEditor for improved layout

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement outlined input controls renderers to replace custom fragile CSS

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: remove margin from last form control inside array items for better spacing

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enhance AJV error handling to transform required errors for field-level validation

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: set default value for User Tokens in manifest.json to improve user experience

Signed-off-by: Deluan <deluan@navidrome.org>

* format

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add margin to outlined input controls for improved spacing

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: remove redundant margin rule for last form control in array items

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: adjust font size of label elements in SchemaConfigEditor for improved readability

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2026-01-19 20:51:00 -05:00

187 lines
4.8 KiB
Go

//go:build !windows
package plugins
import (
"errors"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Config Validation", func() {
Describe("ValidateConfig", func() {
Context("when manifest has no config schema", func() {
It("returns an error", func() {
manifest := &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
}
err := ValidateConfig(manifest, `{"key": "value"}`)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("no configurable options"))
})
})
Context("when manifest has config schema", func() {
var manifest *Manifest
BeforeEach(func() {
manifest = &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
Config: &ConfigDefinition{
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"apiKey": map[string]any{
"type": "string",
"description": "API key for the service",
"minLength": float64(1),
},
"timeout": map[string]any{
"type": "integer",
"minimum": float64(1),
"maximum": float64(300),
},
"enabled": map[string]any{
"type": "boolean",
},
},
"required": []any{"apiKey"},
},
},
}
})
It("accepts valid config", func() {
err := ValidateConfig(manifest, `{"apiKey": "secret123", "timeout": 30}`)
Expect(err).ToNot(HaveOccurred())
})
It("rejects empty config when required fields are missing", func() {
err := ValidateConfig(manifest, "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("apiKey"))
err = ValidateConfig(manifest, "{}")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("apiKey"))
})
It("rejects config missing required field", func() {
err := ValidateConfig(manifest, `{"timeout": 30}`)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("apiKey"))
})
It("rejects config with wrong type", func() {
err := ValidateConfig(manifest, `{"apiKey": "secret", "timeout": "not a number"}`)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("timeout"))
})
It("rejects config with value out of range", func() {
err := ValidateConfig(manifest, `{"apiKey": "secret", "timeout": 500}`)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("timeout"))
})
It("rejects config with empty required string", func() {
err := ValidateConfig(manifest, `{"apiKey": ""}`)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("apiKey"))
})
It("rejects invalid JSON", func() {
err := ValidateConfig(manifest, `{invalid json}`)
Expect(err).To(HaveOccurred())
var validationErr *ConfigValidationErrors
Expect(errors.As(err, &validationErr)).To(BeTrue())
Expect(validationErr.Errors[0].Message).To(ContainSubstring("invalid JSON"))
})
})
Context("with enum values", func() {
It("accepts valid enum value", func() {
manifest := &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
Config: &ConfigDefinition{
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"logLevel": map[string]any{
"type": "string",
"enum": []any{"debug", "info", "warn", "error"},
},
},
},
},
}
err := ValidateConfig(manifest, `{"logLevel": "info"}`)
Expect(err).ToNot(HaveOccurred())
})
It("rejects invalid enum value", func() {
manifest := &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
Config: &ConfigDefinition{
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"logLevel": map[string]any{
"type": "string",
"enum": []any{"debug", "info", "warn", "error"},
},
},
},
},
}
err := ValidateConfig(manifest, `{"logLevel": "verbose"}`)
Expect(err).To(HaveOccurred())
})
})
})
Describe("HasConfigSchema", func() {
It("returns false when config is nil", func() {
manifest := &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
}
Expect(manifest.HasConfigSchema()).To(BeFalse())
})
It("returns false when schema is nil", func() {
manifest := &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
Config: &ConfigDefinition{},
}
Expect(manifest.HasConfigSchema()).To(BeFalse())
})
It("returns true when schema is present", func() {
manifest := &Manifest{
Name: "test",
Author: "test",
Version: "1.0.0",
Config: &ConfigDefinition{
Schema: map[string]any{
"type": "object",
},
},
}
Expect(manifest.HasConfigSchema()).To(BeTrue())
})
})
})