Files
amcs/internal/config/loader_test.go
Hein f41c512f36 test(tools): add unit tests for error handling functions
* Implement tests for error functions like errRequiredField, errInvalidField, and errEntityNotFound.
* Ensure proper metadata is returned for various error scenarios.
* Validate error handling in CRM, Files, and other tools.
* Introduce tests for parsing stored file IDs and UUIDs.
* Enhance coverage for helper functions related to project resolution and session management.
2026-03-31 15:10:07 +02:00

137 lines
3.2 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestResolvePathPrecedence(t *testing.T) {
t.Setenv("AMCS_CONFIG", "/tmp/from-env.yaml")
if got := ResolvePath("/tmp/explicit.yaml"); got != "/tmp/explicit.yaml" {
t.Fatalf("ResolvePath explicit = %q, want %q", got, "/tmp/explicit.yaml")
}
if got := ResolvePath(""); got != "/tmp/from-env.yaml" {
t.Fatalf("ResolvePath env = %q, want %q", got, "/tmp/from-env.yaml")
}
}
func TestResolvePathIgnoresBareYAMLExtension(t *testing.T) {
t.Setenv("AMCS_CONFIG", "/tmp/from-env.yaml")
if got := ResolvePath(".yaml"); got != "/tmp/from-env.yaml" {
t.Fatalf("ResolvePath(.yaml) = %q, want %q", got, "/tmp/from-env.yaml")
}
if got := ResolvePath(".yml"); got != "/tmp/from-env.yaml" {
t.Fatalf("ResolvePath(.yml) = %q, want %q", got, "/tmp/from-env.yaml")
}
}
func TestLoadAppliesEnvOverrides(t *testing.T) {
configPath := filepath.Join(t.TempDir(), "test.yaml")
if err := os.WriteFile(configPath, []byte(`
server:
port: 8080
mcp:
path: "/mcp"
session_timeout: "30m"
auth:
keys:
- id: "test"
value: "secret"
database:
url: "postgres://from-file"
ai:
provider: "litellm"
embeddings:
dimensions: 1536
litellm:
base_url: "http://localhost:4000/v1"
api_key: "file-key"
search:
default_limit: 10
max_limit: 50
logging:
level: "info"
`), 0o600); err != nil {
t.Fatalf("write config: %v", err)
}
t.Setenv("AMCS_DATABASE_URL", "postgres://from-env")
t.Setenv("AMCS_LITELLM_API_KEY", "env-key")
t.Setenv("AMCS_SERVER_PORT", "9090")
cfg, loadedFrom, err := Load(configPath)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if loadedFrom != configPath {
t.Fatalf("loadedFrom = %q, want %q", loadedFrom, configPath)
}
if cfg.Database.URL != "postgres://from-env" {
t.Fatalf("database url = %q, want env override", cfg.Database.URL)
}
if cfg.AI.LiteLLM.APIKey != "env-key" {
t.Fatalf("litellm api key = %q, want env override", cfg.AI.LiteLLM.APIKey)
}
if cfg.Server.Port != 9090 {
t.Fatalf("server port = %d, want 9090", cfg.Server.Port)
}
if cfg.MCP.SessionTimeout != 30*time.Minute {
t.Fatalf("mcp session timeout = %v, want 30m", cfg.MCP.SessionTimeout)
}
}
func TestLoadAppliesOllamaEnvOverrides(t *testing.T) {
configPath := filepath.Join(t.TempDir(), "test.yaml")
if err := os.WriteFile(configPath, []byte(`
server:
port: 8080
mcp:
path: "/mcp"
auth:
keys:
- id: "test"
value: "secret"
database:
url: "postgres://from-file"
ai:
provider: "ollama"
embeddings:
model: "nomic-embed-text"
dimensions: 768
metadata:
model: "llama3.2"
ollama:
base_url: "http://localhost:11434/v1"
api_key: "ollama"
search:
default_limit: 10
max_limit: 50
logging:
level: "info"
`), 0o600); err != nil {
t.Fatalf("write config: %v", err)
}
t.Setenv("AMCS_OLLAMA_BASE_URL", "https://ollama.example.com/v1")
t.Setenv("AMCS_OLLAMA_API_KEY", "remote-key")
cfg, _, err := Load(configPath)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.AI.Ollama.BaseURL != "https://ollama.example.com/v1" {
t.Fatalf("ollama base url = %q, want env override", cfg.AI.Ollama.BaseURL)
}
if cfg.AI.Ollama.APIKey != "remote-key" {
t.Fatalf("ollama api key = %q, want env override", cfg.AI.Ollama.APIKey)
}
}