package store import "testing" func TestNormalizeGatewaySettingsCacheAndRebuildOverrides(t *testing.T) { hour := 25 settings := normalizeGatewaySettings(GatewaySettings{ HomeTTLSeconds: 2, RecommendTTLHours: 500, ForYouRebuildHour: &hour, }) // TTLs cannot be switched off, so a sub-floor value clamps up and an over-ceiling one // clamps down rather than either reading as "deployed". if settings.HomeTTLSeconds != 5 { t.Fatalf("home TTL should clamp to the floor, got %d", settings.HomeTTLSeconds) } if settings.RecommendTTLHours != 168 { t.Fatalf("recommend TTL should clamp to the ceiling, got %d", settings.RecommendTTLHours) } // An hour outside the clock is a typo, and is dropped rather than clamped. if settings.ForYouRebuildHour != nil { t.Fatalf("an out-of-range rebuild hour should be dropped, got %d", *settings.ForYouRebuildHour) } valid := 0 kept := normalizeGatewaySettings(GatewaySettings{ForYouRebuildHour: &valid}) if kept.ForYouRebuildHour == nil || *kept.ForYouRebuildHour != 0 { t.Fatalf("midnight is a legitimate rebuild hour and must survive, got %v", kept.ForYouRebuildHour) } } // Normalisation is what stands between a hand-edited row (or a console built against an // older vocabulary) and a gateway that cannot decide what day it is. func TestNormalizeGatewaySettingsRefusesWhatItCannotUse(t *testing.T) { settings := normalizeGatewaySettings(GatewaySettings{ Timezone: " Not/AZone ", LogLevel: "LOUD", SessionIdleDays: -4, }) if settings.Timezone != "" { t.Fatalf("an unloadable timezone should be dropped, got %q", settings.Timezone) } if settings.LogLevel != "" { t.Fatalf("an unknown level should be dropped, got %q", settings.LogLevel) } // Session expiry cannot be switched off — a household with no expiry at all is a // database full of live Emby tokens — so a negative reads as "leave it alone". if settings.SessionIdleDays != 0 { t.Fatalf("session expiry should not be switchable off, got %d", settings.SessionIdleDays) } } func TestNormalizeGatewaySettingsDefaultsNotificationDisplayToHomeOnly(t *testing.T) { for _, value := range []string{"", "unknown"} { settings := normalizeGatewaySettings(GatewaySettings{NotificationDisplay: value}) if settings.NotificationDisplay != NotificationDisplayHomeOnly { t.Fatalf("notification display %q became %q, want %q", value, settings.NotificationDisplay, NotificationDisplayHomeOnly) } } } func TestNormalizeGatewaySettingsKeepsNotificationDisplayVocabulary(t *testing.T) { for _, value := range GatewayNotificationDisplays { settings := normalizeGatewaySettings(GatewaySettings{NotificationDisplay: " " + value + " "}) if settings.NotificationDisplay != value { t.Fatalf("notification display %q became %q", value, settings.NotificationDisplay) } } } func TestNormalizeGatewaySettingsKeepsTheThreeMeanings(t *testing.T) { settings := normalizeGatewaySettings(GatewaySettings{ Timezone: "Pacific/Auckland", LogLevel: " Debug ", SonarrAlertMinutes: -9, RadarrAlertMinutes: 0, EmbyHealthSeconds: 5, }) if settings.Timezone != "Pacific/Auckland" { t.Fatalf("a real zone should survive, got %q", settings.Timezone) } if settings.LogLevel != "debug" { t.Fatalf("a level should be trimmed and folded, got %q", settings.LogLevel) } if settings.SonarrAlertMinutes != GatewaySettingsOff { t.Fatalf("every negative should collapse to the one off value, got %d", settings.SonarrAlertMinutes) } if settings.RadarrAlertMinutes != 0 { t.Fatalf("zero must keep meaning deployed, got %d", settings.RadarrAlertMinutes) } // Below the floor rather than refused: an operator asking for a five-second probe has // said "as often as possible", and the answer to that is the fastest allowed. if settings.EmbyHealthSeconds != 10 { t.Fatalf("a value under the floor should clamp to it, got %d", settings.EmbyHealthSeconds) } }