package config import ( "testing" "time" ) func TestLoadDefaults(t *testing.T) { for _, key := range []string{ "PORT", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "CONTROL_TOKEN", "SSE_HEARTBEAT", "ML_TIMEOUT", "BOOTSTRAP_REFRESH", "BOOTSTRAP_TIMEOUT", } { t.Setenv(key, "") } cfg := Load() if cfg.Addr != ":8080" { t.Errorf("Addr=%q", cfg.Addr) } if cfg.FastAPIURL != "http://localhost:8000" { t.Errorf("FastAPIURL=%q", cfg.FastAPIURL) } if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond { t.Errorf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout) } if cfg.RefreshInterval != 60*time.Second || cfg.BootstrapTimeout != 20*time.Second { t.Errorf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout) } if cfg.ControlAuthConfigured() { t.Error("control auth should be unconfigured by default") } if cfg.ControlToken != "" { t.Errorf("ControlToken=%q, want empty by default", cfg.ControlToken) } } func TestLoadFromEnv(t *testing.T) { t.Setenv("PORT", "9999") t.Setenv("FASTAPI_URL", "http://ml:8000") t.Setenv("CONTROL_USER", "user") t.Setenv("CONTROL_PASS", "pass") t.Setenv("CONTROL_TOKEN", "token-123") t.Setenv("SSE_HEARTBEAT", "5s") t.Setenv("ML_TIMEOUT", "2s") t.Setenv("BOOTSTRAP_REFRESH", "30s") t.Setenv("BOOTSTRAP_TIMEOUT", "10s") cfg := Load() if cfg.Addr != ":9999" || cfg.FastAPIURL != "http://ml:8000" { t.Fatalf("cfg=%+v", cfg) } if cfg.ControlToken != "token-123" { t.Fatalf("ControlToken=%q, want token-123", cfg.ControlToken) } if cfg.Heartbeat != 5*time.Second || cfg.MLTimeout != 2*time.Second { t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout) } if cfg.RefreshInterval != 30*time.Second || cfg.BootstrapTimeout != 10*time.Second { t.Fatalf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout) } if !cfg.ControlAuthConfigured() { t.Fatal("control auth should be configured") } } func TestDurationAcceptsPlainSeconds(t *testing.T) { t.Setenv("SSE_HEARTBEAT", "7") t.Setenv("ML_TIMEOUT", "3") t.Setenv("BOOTSTRAP_REFRESH", "45") t.Setenv("BOOTSTRAP_TIMEOUT", "12") cfg := Load() if cfg.Heartbeat != 7*time.Second || cfg.MLTimeout != 3*time.Second { t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout) } if cfg.RefreshInterval != 45*time.Second || cfg.BootstrapTimeout != 12*time.Second { t.Fatalf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout) } } func TestDurationFallsBackOnInvalidValue(t *testing.T) { t.Setenv("SSE_HEARTBEAT", "soon") t.Setenv("ML_TIMEOUT", "-1s") t.Setenv("BOOTSTRAP_REFRESH", "later") t.Setenv("BOOTSTRAP_TIMEOUT", "-3s") cfg := Load() if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond { t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout) } if cfg.RefreshInterval != 60*time.Second || cfg.BootstrapTimeout != 20*time.Second { t.Fatalf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout) } }