package config import ( "testing" ) func withEnv(t *testing.T, kv map[string]string) { t.Helper() for k, v := range kv { t.Setenv(k, v) } } func TestLoadDefaults(t *testing.T) { cfg, err := Load() if err != nil { t.Fatalf("Load: %v", err) } if cfg.ListenAddr != ":8080" { t.Errorf("ListenAddr = %q, want :8080", cfg.ListenAddr) } if cfg.ScanInterval.String() != "1h0m0s" { t.Errorf("ScanInterval = %v, want 1h", cfg.ScanInterval) } if cfg.DBPath != "/var/lib/vibesonic/vibesonic.db" { t.Errorf("DBPath = %q, want default under DataDir", cfg.DBPath) } if cfg.ExtAuth.Enabled { t.Errorf("ExtAuth.Enabled = true, want false by default") } if len(cfg.ExtAuth.TrustedSources) != 0 { t.Errorf("ExtAuth.TrustedSources should be empty by default, got %v", cfg.ExtAuth.TrustedSources) } } func TestLoadMusicDirs(t *testing.T) { withEnv(t, map[string]string{"VIBESONIC_MUSIC_DIRS": "/music:/music-lossless: /audiobooks "}) cfg, err := Load() if err != nil { t.Fatalf("Load: %v", err) } want := []string{"/music", "/music-lossless", "/audiobooks"} if len(cfg.MusicDirs) != len(want) { t.Fatalf("MusicDirs = %v, want %v", cfg.MusicDirs, want) } for i, w := range want { if cfg.MusicDirs[i] != w { t.Errorf("MusicDirs[%d] = %q, want %q", i, cfg.MusicDirs[i], w) } } } func TestLoadInvalidScanInterval(t *testing.T) { withEnv(t, map[string]string{"VIBESONIC_SCAN_INTERVAL": "not-a-duration"}) if _, err := Load(); err == nil { t.Fatal("expected error for invalid VIBESONIC_SCAN_INTERVAL") } } func TestLoadExtAuthTrustedSources(t *testing.T) { withEnv(t, map[string]string{ "VIBESONIC_EXTAUTH_ENABLED": "true", "VIBESONIC_EXTAUTH_TRUSTED_SOURCES": "127.0.0.1/32, 10.0.0.0/8, ::1", }) cfg, err := Load() if err != nil { t.Fatalf("Load: %v", err) } if !cfg.ExtAuth.Enabled { t.Fatal("ExtAuth.Enabled should be true") } if len(cfg.ExtAuth.TrustedSources) != 3 { t.Fatalf("TrustedSources = %v, want 3 entries", cfg.ExtAuth.TrustedSources) } } func TestLoadExtAuthInvalidCIDR(t *testing.T) { withEnv(t, map[string]string{"VIBESONIC_EXTAUTH_TRUSTED_SOURCES": "not-a-cidr"}) if _, err := Load(); err == nil { t.Fatal("expected error for invalid VIBESONIC_EXTAUTH_TRUSTED_SOURCES") } } func TestLoadExtAuthNoDefaultTrust(t *testing.T) { // Regression guard: external auth must never trust anything unless a // network is explicitly configured. In particular it must not default // to 0.0.0.0/0 or RFC1918 ranges. cfg, err := Load() if err != nil { t.Fatalf("Load: %v", err) } if len(cfg.ExtAuth.TrustedSources) != 0 { t.Fatalf("expected no default trusted sources, got %v", cfg.ExtAuth.TrustedSources) } }