blob: 5482d0c22887f64d45c39ef05cbaec7ed76dd62e [file] [log] [blame]
package config
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
assert.NotNil(t, cfg)
// Note: Server name and version are now set by the CLI layer as constants
// so they will be empty in the default config
// Test KorAP defaults
assert.Equal(t, "https://korap.ids-mannheim.de", cfg.KorAP.BaseURL)
assert.Equal(t, "v1.0", cfg.KorAP.APIVersion)
assert.Equal(t, 30, cfg.KorAP.Timeout)
assert.Equal(t, 3, cfg.KorAP.MaxRetries)
// Test OAuth defaults
assert.False(t, cfg.OAuth.Enabled, "Expected OAuth to be disabled by default")
// Test logging defaults
assert.Equal(t, "info", cfg.Logging.Level)
assert.Equal(t, "text", cfg.Logging.Format)
}
func TestConfigValidate(t *testing.T) {
// Test valid configuration
cfg := DefaultConfig()
err := cfg.Validate()
assert.NoError(t, err)
// Test invalid OAuth configuration
cfg.OAuth.Enabled = true
cfg.OAuth.ClientID = ""
err = cfg.Validate()
assert.Error(t, err, "Expected validation error for empty OAuth client ID")
// Test invalid KorAP configuration
cfg = DefaultConfig()
cfg.KorAP.BaseURL = ""
err = cfg.Validate()
assert.Error(t, err, "Expected validation error for empty KorAP base URL")
// Test invalid logging configuration
cfg = DefaultConfig()
cfg.Logging.Level = "invalid"
err = cfg.Validate()
assert.Error(t, err, "Expected validation error for invalid log level")
}
func TestKorAPConfigValidate(t *testing.T) {
tests := []struct {
name string
config KorAPConfig
expectErr bool
}{
{
name: "valid config",
config: KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "v1.0",
Timeout: 30,
MaxRetries: 3,
},
expectErr: false,
},
{
name: "empty base URL",
config: KorAPConfig{
BaseURL: "",
APIVersion: "v1.0",
Timeout: 30,
MaxRetries: 3,
},
expectErr: true,
},
{
name: "empty API version",
config: KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "",
Timeout: 30,
MaxRetries: 3,
},
expectErr: true,
},
{
name: "zero timeout",
config: KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "v1.0",
Timeout: 0,
MaxRetries: 3,
},
expectErr: true,
},
{
name: "negative timeout",
config: KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "v1.0",
Timeout: -1,
MaxRetries: 3,
},
expectErr: true,
},
{
name: "negative max retries",
config: KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "v1.0",
Timeout: 30,
MaxRetries: -1,
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if tt.expectErr {
assert.Error(t, err, "Expected validation error but got none")
} else {
assert.NoError(t, err, "Unexpected validation error")
}
})
}
}
func TestKorAPConfigGetEndpoint(t *testing.T) {
config := KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "v1.0",
}
expected := "https://korap.ids-mannheim.de/api/v1.0"
actual := config.GetKorAPEndpoint()
assert.Equal(t, expected, actual)
}
func TestLoggingConfigValidate(t *testing.T) {
tests := []struct {
name string
config LoggingConfig
expectErr bool
}{
{
name: "valid text format",
config: LoggingConfig{
Level: "info",
Format: "text",
},
expectErr: false,
},
{
name: "valid json format",
config: LoggingConfig{
Level: "debug",
Format: "json",
},
expectErr: false,
},
{
name: "invalid level",
config: LoggingConfig{
Level: "invalid",
Format: "text",
},
expectErr: true,
},
{
name: "invalid format",
config: LoggingConfig{
Level: "info",
Format: "invalid",
},
expectErr: true,
},
{
name: "all log levels",
config: LoggingConfig{
Level: "trace",
Format: "json",
},
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if tt.expectErr {
assert.Error(t, err, "Expected validation error but got none")
} else {
assert.NoError(t, err, "Unexpected validation error")
}
})
}
}
func TestLoggingConfigValidateWithFile(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "korap-mcp-test")
assert.NoError(t, err, "Failed to create temp dir")
defer os.RemoveAll(tempDir)
// Create a writable log file
logFile := filepath.Join(tempDir, "test.log")
file, err := os.Create(logFile)
assert.NoError(t, err)
file.Close()
config := LoggingConfig{
Level: "info",
Format: "json",
File: logFile,
}
err = config.Validate()
assert.NoError(t, err, "Validation failed for writable log file")
// Test with invalid log file path
config.File = "/root/invalid.log"
err = config.Validate()
assert.Error(t, err, "Expected validation error for invalid log file path")
}
func TestServerConfigDefaults(t *testing.T) {
cfg := DefaultConfig()
assert.Empty(t, cfg.Server.ConfigFile, "Expected empty config file by default")
// Name and version are now constants set by CLI layer, so they will be empty here
assert.Empty(t, cfg.Server.Name, "Server name should be empty in default config (set by CLI constants)")
assert.Empty(t, cfg.Server.Version, "Server version should be empty in default config (set by CLI constants)")
}
// Benchmark tests
func BenchmarkDefaultConfig(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = DefaultConfig()
}
}
func BenchmarkConfigValidate(b *testing.B) {
cfg := DefaultConfig()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = cfg.Validate()
}
}
func BenchmarkKorAPGetEndpoint(b *testing.B) {
config := KorAPConfig{
BaseURL: "https://korap.ids-mannheim.de",
APIVersion: "v1.0",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = config.GetKorAPEndpoint()
}
}