package config import ( "os" "path/filepath" "testing" ) func resetRuntime(t *testing.T) { t.Helper() runtimeMu.Lock() runtimeStore = map[string]string{} runtimeMu.Unlock() os.Unsetenv("CONFIG_PATH") os.Unsetenv("MATOMO_URL") os.Unsetenv("MATOMO_SITE_ID") os.Unsetenv("HLS_SEGMENT_DURATION") os.Unsetenv("HLS_WINDOW_SIZE") } func TestConfigPath(t *testing.T) { resetRuntime(t) if got := ConfigPath(); got != "/app/playlist/config.json" { t.Errorf("ConfigPath() = %q, want default /app/playlist/config.json", got) } os.Setenv("CONFIG_PATH", "/tmp/custom.json") defer os.Unsetenv("CONFIG_PATH") if got := ConfigPath(); got != "/tmp/custom.json" { t.Errorf("ConfigPath() = %q, want /tmp/custom.json", got) } } func TestGetSetRuntime(t *testing.T) { resetRuntime(t) if _, ok := GetRuntime("foo"); ok { t.Errorf("expected missing key") } SetRuntime("foo", "bar") v, ok := GetRuntime("foo") if !ok || v != "bar" { t.Errorf("GetRuntime() = %q, %v; want bar, true", v, ok) } } func TestGettersDefaults(t *testing.T) { resetRuntime(t) if got := MatomoURL(); got != "" { t.Errorf("MatomoURL() = %q, want empty", got) } if got := MatomoSiteID(); got != "" { t.Errorf("MatomoSiteID() = %q, want empty", got) } if got := HLSSegmentDuration(); got != 2 { t.Errorf("HLSSegmentDuration() = %d, want 2", got) } if got := HLSWindowSize(); got != 12 { t.Errorf("HLSWindowSize() = %d, want 12", got) } } func TestGettersEnvFallback(t *testing.T) { resetRuntime(t) os.Setenv("HLS_SEGMENT_DURATION", "4") os.Setenv("HLS_WINDOW_SIZE", "30") os.Setenv("MATOMO_URL", "https://analytics.example.com") os.Setenv("MATOMO_SITE_ID", "7") if got := HLSSegmentDuration(); got != 4 { t.Errorf("HLSSegmentDuration() = %d, want 4", got) } if got := HLSWindowSize(); got != 30 { t.Errorf("HLSWindowSize() = %d, want 30", got) } if got := MatomoURL(); got != "https://analytics.example.com" { t.Errorf("MatomoURL() = %q", got) } if got := MatomoSiteID(); got != "7" { t.Errorf("MatomoSiteID() = %q", got) } } func TestLoadRuntimeConfigSeedsFromEnv(t *testing.T) { resetRuntime(t) dir := t.TempDir() os.Setenv("CONFIG_PATH", filepath.Join(dir, "config.json")) os.Setenv("HLS_SEGMENT_DURATION", "8") if err := LoadRuntimeConfig(); err != nil { t.Fatalf("LoadRuntimeConfig error: %v", err) } if got := HLSSegmentDuration(); got != 8 { t.Errorf("HLSSegmentDuration() = %d, want 8 (seeded from env)", got) } } func TestSaveAndLoadRuntimeConfig(t *testing.T) { resetRuntime(t) dir := t.TempDir() os.Setenv("CONFIG_PATH", filepath.Join(dir, "config.json")) if err := SetHLSSegmentDuration(5); err != nil { t.Fatal(err) } if err := SetHLSWindowSize(30); err != nil { t.Fatal(err) } if err := SetMatomoURL("https://stats.example.com"); err != nil { t.Fatal(err) } if err := SetMatomoSiteID("42"); err != nil { t.Fatal(err) } if err := SaveRuntimeConfig(); err != nil { t.Fatalf("SaveRuntimeConfig error: %v", err) } // Reset the store and reload from disk. runtimeMu.Lock() runtimeStore = map[string]string{} runtimeMu.Unlock() if err := LoadRuntimeConfig(); err != nil { t.Fatalf("LoadRuntimeConfig error: %v", err) } if got := HLSSegmentDuration(); got != 5 { t.Errorf("HLSSegmentDuration() = %d, want 5", got) } if got := HLSWindowSize(); got != 30 { t.Errorf("HLSWindowSize() = %d, want 30", got) } if got := MatomoURL(); got != "https://stats.example.com" { t.Errorf("MatomoURL() = %q", got) } if got := MatomoSiteID(); got != "42" { t.Errorf("MatomoSiteID() = %q", got) } } func TestSettersValidation(t *testing.T) { resetRuntime(t) if err := SetHLSSegmentDuration(1); err == nil { t.Error("expected error for duration 1") } if err := SetHLSSegmentDuration(20); err == nil { t.Error("expected error for duration 20") } if err := SetHLSSegmentDuration(6); err != nil { t.Errorf("unexpected error for duration 6: %v", err) } if err := SetHLSWindowSize(2); err == nil { t.Error("expected error for window 2") } if err := SetHLSWindowSize(101); err == nil { t.Error("expected error for window 101") } if err := SetHLSWindowSize(24); err != nil { t.Errorf("unexpected error for window 24: %v", err) } if err := SetMatomoURL("not-a-url"); err == nil { t.Error("expected error for invalid URL") } if err := SetMatomoURL("ftp://example.com"); err == nil { t.Error("expected error for non-http URL") } if err := SetMatomoURL("https://stats.example.com"); err != nil { t.Errorf("unexpected error for valid URL: %v", err) } if err := SetMatomoSiteID("abc"); err == nil { t.Error("expected error for non-numeric site ID") } if err := SetMatomoSiteID("99"); err != nil { t.Errorf("unexpected error for numeric site ID: %v", err) } }