120 lines
2.7 KiB
Go
120 lines
2.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
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 TestLoadAppliesEnvOverrides(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: "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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|