2026-05-27 22:05:02 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLoadReadsCORSAllowedOrigins(t *testing.T) {
|
|
|
|
|
path := filepath.Join(t.TempDir(), "config.yaml")
|
|
|
|
|
content := []byte(`server:
|
|
|
|
|
port: 8083
|
|
|
|
|
cors:
|
|
|
|
|
allowedOrigins:
|
|
|
|
|
- https://console.example.com
|
|
|
|
|
- http://localhost:3000
|
|
|
|
|
`)
|
|
|
|
|
if err := os.WriteFile(path, content, 0600); err != nil {
|
|
|
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg, err := Load(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Load() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
got := cfg.Server.CORS.AllowedOrigins
|
|
|
|
|
want := []string{"https://console.example.com", "http://localhost:3000"}
|
|
|
|
|
if len(got) != len(want) {
|
|
|
|
|
t.Fatalf("len(AllowedOrigins)=%d want %d", len(got), len(want))
|
|
|
|
|
}
|
|
|
|
|
for i := range want {
|
|
|
|
|
if got[i] != want[i] {
|
|
|
|
|
t.Fatalf("AllowedOrigins[%d]=%q want %q", i, got[i], want[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-13 10:40:09 +08:00
|
|
|
|
|
|
|
|
func TestLoadOverridesValuesFromEnvironment(t *testing.T) {
|
|
|
|
|
path := filepath.Join(t.TempDir(), "config.yaml")
|
|
|
|
|
content := []byte(`server:
|
|
|
|
|
port: 8083
|
|
|
|
|
db:
|
2026-08-28 22:23:13 +08:00
|
|
|
type: postgres
|
|
|
|
|
dsn: postgres-dsn
|
2026-06-13 10:40:09 +08:00
|
|
|
storage:
|
|
|
|
|
local:
|
|
|
|
|
baseUrl: /storage
|
|
|
|
|
`)
|
|
|
|
|
if err := os.WriteFile(path, content, 0600); err != nil {
|
|
|
|
|
t.Fatalf("WriteFile() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
t.Setenv("AGENT_DESK_SERVER_PORT", "8090")
|
2026-08-28 22:23:13 +08:00
|
|
|
t.Setenv("AGENT_DESK_DB_DSN", "postgres-override-dsn")
|
2026-06-13 10:40:09 +08:00
|
|
|
t.Setenv("AGENT_DESK_STORAGE_LOCAL_BASEURL", "/files")
|
|
|
|
|
|
|
|
|
|
cfg, err := Load(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Load() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cfg.Server.Port != 8090 {
|
|
|
|
|
t.Fatalf("Server.Port=%d want 8090", cfg.Server.Port)
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
if cfg.DB.Type != "postgres" {
|
|
|
|
|
t.Fatalf("DB.Type=%q want postgres", cfg.DB.Type)
|
2026-06-13 10:40:09 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
if cfg.DB.DSN != "postgres-override-dsn" {
|
|
|
|
|
t.Fatalf("DB.DSN=%q want postgres-override-dsn", cfg.DB.DSN)
|
2026-06-13 10:40:09 +08:00
|
|
|
}
|
|
|
|
|
if cfg.Storage.Local.BaseURL != "/files" {
|
|
|
|
|
t.Fatalf("Storage.Local.BaseURL=%q want /files", cfg.Storage.Local.BaseURL)
|
|
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFromSettingsBuildsRuntimeConfig(t *testing.T) {
|
|
|
|
|
cfg, err := FromSettings(map[string]string{
|
|
|
|
|
"ai_agent_language": "en-US",
|
|
|
|
|
"ai_agent_storage_default": "oss",
|
|
|
|
|
"ai_agent_storage_max_upload_size_mb": "64",
|
|
|
|
|
"ai_agent_storage_oss_private": "true",
|
|
|
|
|
"ai_vector_path": "/var/lib/agent-desk/vectors.db",
|
|
|
|
|
"ai_agent_wxwork_notify_to_users": `[1,2,3]`,
|
|
|
|
|
"ai_agent_wxwork_notify_dedupe_interval": "900",
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("FromSettings() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if cfg.Language != "en-US" || cfg.Storage.Default != "oss" {
|
|
|
|
|
t.Fatalf("unexpected basic config: %+v", cfg)
|
|
|
|
|
}
|
|
|
|
|
if cfg.Storage.MaxUploadSizeMB != 64 || !cfg.Storage.OSS.Private {
|
|
|
|
|
t.Fatalf("unexpected storage config: %+v", cfg.Storage)
|
|
|
|
|
}
|
|
|
|
|
if cfg.VectorDB.Path != "/var/lib/agent-desk/vectors.db" {
|
|
|
|
|
t.Fatalf("VectorDB.Path=%q", cfg.VectorDB.Path)
|
|
|
|
|
}
|
|
|
|
|
if len(cfg.WxWork.Notify.ToUsers) != 3 || cfg.WxWork.Notify.DuplicateCheckInterval != 900 {
|
|
|
|
|
t.Fatalf("unexpected WeCom notify config: %+v", cfg.WxWork.Notify)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFromSettingsRejectsInvalidValues(t *testing.T) {
|
|
|
|
|
if _, err := FromSettings(map[string]string{
|
|
|
|
|
"ai_agent_wxwork_notify_dedupe_interval": "not-a-number",
|
|
|
|
|
}); err == nil {
|
|
|
|
|
t.Fatal("FromSettings() error = nil, want invalid setting error")
|
2026-06-13 10:40:09 +08:00
|
|
|
}
|
|
|
|
|
}
|