package config import ( "testing" "time" ) func TestParseDefaultConfig(t *testing.T) { cfg, err := Parse([]string{}) if err != nil { t.Fatalf("unexpected error: %v", err) } if !cfg.Autodiscover { t.Errorf("expected autodiscover to be enabled by default when no plugs are specified") } if len(cfg.Plugs) != 0 { t.Errorf("expected 0 static plugs, got %v", cfg.Plugs) } if cfg.RiemannAddr != "127.0.0.1:5555" { t.Errorf("expected riemann addr 127.0.0.1:5555, got %s", cfg.RiemannAddr) } if cfg.RiemannWebAddr != "http://127.0.0.1:5556" { t.Errorf("expected riemann web addr http://127.0.0.1:5556, got %s", cfg.RiemannWebAddr) } if cfg.RiemannProto != "tcp" { t.Errorf("expected riemann proto tcp, got %s", cfg.RiemannProto) } if cfg.PollInterval != 15*time.Second { t.Errorf("expected poll interval 15s, got %v", cfg.PollInterval) } } func TestParseStaticPlugs(t *testing.T) { cfg, err := Parse([]string{"-plugs", "192.0.2.1"}) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(cfg.Plugs) != 1 || cfg.Plugs[0] != "192.0.2.1" { t.Errorf("expected plug 192.0.2.1, got %v", cfg.Plugs) } if cfg.Autodiscover { t.Errorf("expected autodiscover to be false when plugs are explicitly specified") } } func TestParseMissingPlugsWithNoAutodiscover(t *testing.T) { _, err := Parse([]string{"-autodiscover=false"}) if err == nil { t.Fatalf("expected error when no plugs specified and autodiscover is false, got nil") } } func TestParseCustomFlags(t *testing.T) { args := []string{ "-plugs", "10.0.0.1, 10.0.0.2", "-kasa-username", "user@example.com", "-kasa-password", "secret", "-poll-interval", "5s", "-verbose", } cfg, err := Parse(args) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(cfg.Plugs) != 2 || cfg.Plugs[0] != "10.0.0.1" || cfg.Plugs[1] != "10.0.0.2" { t.Errorf("expected 2 plugs, got %v", cfg.Plugs) } if cfg.KasaUsername != "user@example.com" { t.Errorf("expected user@example.com, got %s", cfg.KasaUsername) } if cfg.KasaPassword != "secret" { t.Errorf("expected secret, got %s", cfg.KasaPassword) } if cfg.PollInterval != 5*time.Second { t.Errorf("expected 5s, got %v", cfg.PollInterval) } if !cfg.Verbose { t.Errorf("expected verbose=true") } if cfg.RiemannTTL != 20*time.Second { t.Errorf("expected default riemann ttl 20s (4x poll interval 5s), got %v", cfg.RiemannTTL) } } func TestParseRiemannTTLValidation(t *testing.T) { if _, err := Parse([]string{"-poll-interval", "30s", "-riemann-ttl", "30s"}); err == nil { t.Fatalf("expected error when riemann-ttl <= poll-interval") } cfg, err := Parse([]string{"-poll-interval", "30s"}) if err != nil { t.Fatalf("unexpected error: %v", err) } if cfg.RiemannTTL != 2*time.Minute { t.Errorf("expected scaled riemann ttl 2m, got %v", cfg.RiemannTTL) } }