package config_test import ( "context" "os" "path/filepath" "strings" "testing" "time" "statuspanel/internal/config" ) func TestLoad(t *testing.T) { t.Run("loads valid yaml with defaults", func(t *testing.T) { path := writeConfig(t, ` interface: eth0 dns: server: 1.1.1.1 domain: example.com targets: - name: router address: 192.0.2.1 `) cfg, err := config.Load(context.Background(), path) if err != nil { t.Fatalf("Load() error = %v", err) } if cfg.Interface != "eth0" { t.Fatalf("Interface = %q, want eth0", cfg.Interface) } if cfg.Refresh.Duration != 2*time.Second { t.Fatalf("Refresh = %v, want 2s", cfg.Refresh.Duration) } if cfg.Ping.Timeout.Duration != time.Second { t.Fatalf("Ping timeout = %v, want 1s", cfg.Ping.Timeout.Duration) } }) t.Run("rejects missing required values", func(t *testing.T) { path := writeConfig(t, `refresh: 1s`) _, err := config.Load(context.Background(), path) if err == nil { t.Fatal("Load() error = nil, want validation error") } for _, want := range []string{"interface is required", "dns.server is required", "dns.domain is required"} { if !strings.Contains(err.Error(), want) { t.Fatalf("Load() error = %q, want containing %q", err, want) } } }) } func TestDurationUnmarshalYAML(t *testing.T) { path := writeConfig(t, ` interface: eth0 refresh: 500ms dns: server: 1.1.1.1 domain: example.com timeout: 250ms ping: timeout: 750ms `) cfg, err := config.Load(context.Background(), path) if err != nil { t.Fatalf("Load() error = %v", err) } if cfg.Refresh.Duration != 500*time.Millisecond { t.Fatalf("Refresh = %v, want 500ms", cfg.Refresh.Duration) } if cfg.DNS.Timeout.Duration != 250*time.Millisecond { t.Fatalf("DNS timeout = %v, want 250ms", cfg.DNS.Timeout.Duration) } if cfg.Ping.Timeout.Duration != 750*time.Millisecond { t.Fatalf("Ping timeout = %v, want 750ms", cfg.Ping.Timeout.Duration) } } func writeConfig(t *testing.T, contents string) string { t.Helper() path := filepath.Join(t.TempDir(), "statuspanel.yaml") if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { t.Fatalf("writing config: %v", err) } return path }