- config.go: LLMConfig.FallbackMaxTokens(LLM_FALLBACK_MAX_TOKENS, 默认8192) - runtimeconfig/models.go: Group.MaxTokens(models.json每模型独立预算, 负值拒绝) - model/fallback.go: Chain.WithTokenBudget + applyBudget; runtime.go buildSnapshot接入(active.MaxTokens, env.FallbackMaxTokens) - 优先级: 显式Request.MaxTokens > 组maxTokens > 仅多步链末端的LLM_FALLBACK_MAX_TOKENS > 适配器默认8192; 单步链不动env兜底(避免OpenAI主模型行为回归) - gateway admin: AdminLlmProvider.MaxTokens 透传/回显/负值400 - .env.example + .env(LLM_FALLBACK_MAX_TOKENS=8192); Docs/api-client v0.27 + api-reference + design/04 - 新增测试(config/runtimeconfig/budget_test/admin_write), build/vet/test绿+3平台交叉编译
335 lines
9.7 KiB
Go
335 lines
9.7 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func writeEnvFile(t *testing.T, content string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, ".env")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatalf("write env file: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoad_DefaultsWhenUnset(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
|
|
if cfg.Env != "development" {
|
|
t.Errorf("Env = %q, want development", cfg.Env)
|
|
}
|
|
if cfg.Server.Host != "0.0.0.0" || cfg.Server.Port != 8080 {
|
|
t.Errorf("Server = %+v, want 0.0.0.0:8080", cfg.Server)
|
|
}
|
|
if cfg.LogLevel != "debug" {
|
|
t.Errorf("LogLevel = %q, want debug", cfg.LogLevel)
|
|
}
|
|
if cfg.DB.SQLitePath != "./data/cyrene.db" {
|
|
t.Errorf("SQLitePath = %q, want ./data/cyrene.db", cfg.DB.SQLitePath)
|
|
}
|
|
want := []string{"http://localhost:5173", "http://localhost:3000"}
|
|
if !reflect.DeepEqual(cfg.AllowedOrigins, want) {
|
|
t.Errorf("AllowedOrigins = %v, want %v", cfg.AllowedOrigins, want)
|
|
}
|
|
}
|
|
|
|
func TestLoad_EnvOverridesDefault(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("PORT", "9090")
|
|
t.Setenv("LOG_LEVEL", "warn")
|
|
t.Setenv("IOT_PROVIDER", "none")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Server.Port != 9090 {
|
|
t.Errorf("Port = %d, want 9090", cfg.Server.Port)
|
|
}
|
|
if cfg.LogLevel != "warn" {
|
|
t.Errorf("LogLevel = %q, want warn", cfg.LogLevel)
|
|
}
|
|
if cfg.IoT.Provider != "none" {
|
|
t.Errorf("IoT.Provider = %q, want none", cfg.IoT.Provider)
|
|
}
|
|
}
|
|
|
|
func TestLoad_DotEnvThenProcessEnv(t *testing.T) {
|
|
path := writeEnvFile(t, `PORT=7000
|
|
LOG_LEVEL=error
|
|
# comment line
|
|
IOT_PROVIDER=none
|
|
`)
|
|
// Process env overrides .env for PORT but leaves LOG_LEVEL from .env.
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("PORT", "8000")
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load(%q) error: %v", path, err)
|
|
}
|
|
if cfg.Server.Port != 8000 {
|
|
t.Errorf("Port = %d, want 8000 (process env wins)", cfg.Server.Port)
|
|
}
|
|
if cfg.LogLevel != "error" {
|
|
t.Errorf("LogLevel = %q, want error from .env", cfg.LogLevel)
|
|
}
|
|
if cfg.IoT.Provider != "none" {
|
|
t.Errorf("IoT.Provider = %q, want none from .env", cfg.IoT.Provider)
|
|
}
|
|
}
|
|
|
|
// unsetEnvForTest clears key for the test and restores the original value (or
|
|
// absence) on cleanup, so a .env file is not shadowed by an ambient process env
|
|
// variable. config tests are not parallel, so this is safe.
|
|
func unsetEnvForTest(t *testing.T, key string) {
|
|
t.Helper()
|
|
if v, ok := os.LookupEnv(key); ok {
|
|
t.Cleanup(func() { _ = os.Setenv(key, v) })
|
|
} else {
|
|
t.Cleanup(func() { _ = os.Unsetenv(key) })
|
|
}
|
|
if err := os.Unsetenv(key); err != nil {
|
|
t.Fatalf("unset %s: %v", key, err)
|
|
}
|
|
}
|
|
|
|
func TestLoad_LlmFallbackMaxTokens(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
unsetEnvForTest(t, "LLM_FALLBACK_MAX_TOKENS")
|
|
|
|
// Default: the last-resort fallback budget is a meaningful 8192.
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.LLM.FallbackMaxTokens != 8192 {
|
|
t.Errorf("LLM.FallbackMaxTokens = %d, want default 8192", cfg.LLM.FallbackMaxTokens)
|
|
}
|
|
|
|
// A .env file is parsed into the field.
|
|
path := writeEnvFile(t, "LLM_FALLBACK_MAX_TOKENS=4096\n")
|
|
cfg, err = Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load(%q) error: %v", path, err)
|
|
}
|
|
if cfg.LLM.FallbackMaxTokens != 4096 {
|
|
t.Errorf("LLM.FallbackMaxTokens = %d, want 4096 from .env", cfg.LLM.FallbackMaxTokens)
|
|
}
|
|
|
|
// Process env wins over .env.
|
|
t.Setenv("LLM_FALLBACK_MAX_TOKENS", "2048")
|
|
cfg, err = Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load(%q) error: %v", path, err)
|
|
}
|
|
if cfg.LLM.FallbackMaxTokens != 2048 {
|
|
t.Errorf("LLM.FallbackMaxTokens = %d, want 2048 (process env wins)", cfg.LLM.FallbackMaxTokens)
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingRequiredFails(t *testing.T) {
|
|
// Do not set JWT_SECRET.
|
|
if _, err := Load(""); err == nil {
|
|
t.Fatal("expected error when JWT_SECRET missing, got nil")
|
|
} else if !strings.Contains(err.Error(), "JWT_SECRET") {
|
|
t.Errorf("error should mention JWT_SECRET, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoad_ValidationMinLength(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", "short")
|
|
if _, err := Load(""); err == nil {
|
|
t.Fatal("expected error for JWT_SECRET shorter than 32, got nil")
|
|
} else if !strings.Contains(err.Error(), "at least 32") {
|
|
t.Errorf("error should mention min length, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoad_InvalidPortFails(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("PORT", "not-a-number")
|
|
if _, err := Load(""); err == nil {
|
|
t.Fatal("expected error for invalid PORT, got nil")
|
|
} else if !strings.Contains(err.Error(), "PORT") {
|
|
t.Errorf("error should mention PORT, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingDotEnvIsTolerated(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
// A non-existent .env path must not error.
|
|
cfg, err := Load(filepath.Join(t.TempDir(), "does-not-exist.env"))
|
|
if err != nil {
|
|
t.Fatalf("Load(nonexistent) error: %v", err)
|
|
}
|
|
if cfg.Server.Port != 8080 {
|
|
t.Errorf("Port = %d, want default 8080", cfg.Server.Port)
|
|
}
|
|
}
|
|
|
|
func TestLoad_AuthRefreshDefaults(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Auth.RefreshTTL != 720*time.Hour {
|
|
t.Errorf("Auth.RefreshTTL = %v, want 720h", cfg.Auth.RefreshTTL)
|
|
}
|
|
if cfg.Auth.RefreshRotateDisabled {
|
|
t.Errorf("Auth.RefreshRotateDisabled = true, want false (rotation on by default)")
|
|
}
|
|
}
|
|
|
|
func TestLoad_AuthRefreshEnvOverrides(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("AUTH_REFRESH_TTL", "30m")
|
|
t.Setenv("AUTH_REFRESH_ROTATE_DISABLED", "true")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Auth.RefreshTTL != 30*time.Minute {
|
|
t.Errorf("Auth.RefreshTTL = %v, want 30m", cfg.Auth.RefreshTTL)
|
|
}
|
|
if !cfg.Auth.RefreshRotateDisabled {
|
|
t.Errorf("Auth.RefreshRotateDisabled = false, want true")
|
|
}
|
|
}
|
|
|
|
func TestLoad_CapacityDefaults(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Capacity.Enabled {
|
|
t.Errorf("Capacity.Enabled = true, want false (default, opt-in)")
|
|
}
|
|
if cfg.Capacity.MinConfidence != 0.6 {
|
|
t.Errorf("Capacity.MinConfidence = %v, want 0.6", cfg.Capacity.MinConfidence)
|
|
}
|
|
}
|
|
func TestLoad_CapacityEnvOverrides(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("CAPACITY_ENABLED", "true")
|
|
t.Setenv("CAPACITY_MIN_CONFIDENCE", "0.75")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if !cfg.Capacity.Enabled {
|
|
t.Errorf("Capacity.Enabled = false, want true")
|
|
}
|
|
if cfg.Capacity.MinConfidence != 0.75 {
|
|
t.Errorf("Capacity.MinConfidence = %v, want 0.75", cfg.Capacity.MinConfidence)
|
|
}
|
|
}
|
|
|
|
func TestLoad_VoiceDefaults(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Voice.DashscopeBaseURL != "https://dashscope.aliyuncs.com/api/v1" {
|
|
t.Errorf("DashscopeBaseURL = %q, want the default Bailian URL", cfg.Voice.DashscopeBaseURL)
|
|
}
|
|
if cfg.Voice.STTModel != "qwen-audio-3.0-asr-flash" {
|
|
t.Errorf("STTModel = %q, want qwen-audio-3.0-asr-flash", cfg.Voice.STTModel)
|
|
}
|
|
if cfg.Voice.STTLanguage != "zh" {
|
|
t.Errorf("STTLanguage = %q, want zh", cfg.Voice.STTLanguage)
|
|
}
|
|
if cfg.Voice.TTSModel != "cosyvoice-v3-flash" {
|
|
t.Errorf("TTSModel = %q, want cosyvoice-v3-flash", cfg.Voice.TTSModel)
|
|
}
|
|
if cfg.Voice.TTSVoiceID != "longxiaochun_v3" {
|
|
t.Errorf("TTSVoiceID = %q, want longxiaochun_v3", cfg.Voice.TTSVoiceID)
|
|
}
|
|
if cfg.Voice.TTSFormat != "wav" {
|
|
t.Errorf("TTSFormat = %q, want wav", cfg.Voice.TTSFormat)
|
|
}
|
|
if cfg.Voice.TTSSampleRate != 24000 {
|
|
t.Errorf("TTSSampleRate = %d, want 24000", cfg.Voice.TTSSampleRate)
|
|
}
|
|
if cfg.Voice.VoiceTTSInstruction != "" {
|
|
t.Errorf("VoiceTTSInstruction = %q, want empty (default)", cfg.Voice.VoiceTTSInstruction)
|
|
}
|
|
if cfg.Voice.VoiceClonePrefix != "cyrene" {
|
|
t.Errorf("VoiceClonePrefix = %q, want cyrene (default)", cfg.Voice.VoiceClonePrefix)
|
|
}
|
|
}
|
|
|
|
func TestLoad_VoiceEnvOverrides(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("VOICE_TTS_VOICE_ID", "custom-voice-1")
|
|
t.Setenv("VOICE_STT_MODEL", "qwen-audio-3.0-asr-flash")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Voice.TTSVoiceID != "custom-voice-1" {
|
|
t.Errorf("TTSVoiceID = %q, want custom-voice-1", cfg.Voice.TTSVoiceID)
|
|
}
|
|
if cfg.Voice.STTModel != "qwen-audio-3.0-asr-flash" {
|
|
t.Errorf("STTModel = %q, want qwen-audio-3.0-asr-flash", cfg.Voice.STTModel)
|
|
}
|
|
}
|
|
|
|
func TestLoad_VoiceCloneInstructionEnvOverrides(t *testing.T) {
|
|
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
|
t.Setenv("VOICE_TTS_INSTRUCTION", "语气开朗俏皮,像女朋友撒娇")
|
|
t.Setenv("VOICE_CLONE_PREFIX", "xilian")
|
|
|
|
cfg, err := Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load() error: %v", err)
|
|
}
|
|
if cfg.Voice.VoiceTTSInstruction != "语气开朗俏皮,像女朋友撒娇" {
|
|
t.Errorf("VoiceTTSInstruction = %q, want the env instruction", cfg.Voice.VoiceTTSInstruction)
|
|
}
|
|
if cfg.Voice.VoiceClonePrefix != "xilian" {
|
|
t.Errorf("VoiceClonePrefix = %q, want xilian", cfg.Voice.VoiceClonePrefix)
|
|
}
|
|
}
|
|
|
|
func TestParseDotEnv_QuotedAndExport(t *testing.T) {
|
|
path := writeEnvFile(t, `export FOO=bar
|
|
BAZ='quoted value'
|
|
EMPTY=
|
|
`)
|
|
m, err := parseDotEnv(path)
|
|
if err != nil {
|
|
t.Fatalf("parseDotEnv error: %v", err)
|
|
}
|
|
if m["FOO"] != "bar" {
|
|
t.Errorf("FOO = %q, want bar", m["FOO"])
|
|
}
|
|
if m["BAZ"] != "quoted value" {
|
|
t.Errorf("BAZ = %q, want 'quoted value'", m["BAZ"])
|
|
}
|
|
if v, ok := m["EMPTY"]; !ok || v != "" {
|
|
t.Errorf("EMPTY = %q (present=%v), want empty", v, ok)
|
|
}
|
|
}
|