Files
memby/server/internal/api/notification_preferences_test.go
T

84 lines
3.0 KiB
Go
Raw Normal View History

2026-08-15 09:23:26 +12:00
package api
import (
"testing"
"github.com/ponzischeme89/memby/server/internal/appupdate"
"github.com/ponzischeme89/memby/server/internal/store"
)
func TestFilterClientAlertsByCategory(t *testing.T) {
prefs := store.DefaultNotificationPreferences()
prefs.SonarrAlerts = false
prefs.LibraryAlerts = false
alerts := []clientAlert{
{ID: "sonarr", Kind: alertKindSonarrAired},
{ID: "radarr", Kind: alertKindRadarrImport},
{ID: "library", Kind: alertKindLibrarySync},
{ID: "system", Kind: alertKindServerDown},
}
filtered := filterClientAlerts(alerts, prefs)
if len(filtered) != 2 || filtered[0].ID != "radarr" || filtered[1].ID != "system" {
t.Fatalf("filtered alerts = %#v", filtered)
}
}
func TestFilterClientAlertsMasterSwitch(t *testing.T) {
prefs := store.DefaultNotificationPreferences()
prefs.Enabled = false
if got := filterClientAlerts([]clientAlert{{ID: "one", Kind: alertKindRadarrImport}}, prefs); len(got) != 0 {
t.Fatalf("master switch returned %#v", got)
}
}
func TestFilterStoredSonarrNotifications(t *testing.T) {
prefs := store.DefaultNotificationPreferences()
prefs.SonarrAlerts = false
notifications := []store.UserNotification{
{ID: 1, Kind: "show-return"},
{ID: 2, Kind: "future-category"},
}
filtered := filterStoredNotifications(notifications, prefs)
if len(filtered) != 1 || filtered[0].ID != 2 {
t.Fatalf("filtered notifications = %#v", filtered)
}
}
func TestUpdatePreferenceNeverSuppressesMandatoryUpdate(t *testing.T) {
mandatory := appupdate.Decision{Status: appupdate.StatusMandatory, Version: "2.0.0"}
if got := updateDecisionForPreferences(mandatory, false); got.Status != appupdate.StatusMandatory {
t.Fatalf("mandatory update was suppressed: %#v", got)
}
optional := appupdate.Decision{Status: appupdate.StatusOptional, Version: "2.0.0"}
if got := updateDecisionForPreferences(optional, false); got.Status != appupdate.StatusNone {
t.Fatalf("optional update was not suppressed: %#v", got)
}
}
2026-08-19 21:30:44 +12:00
// A clear-all press aimed at the alerts on screen must never reach past them. The one case
// that can differ is a kind the viewer's own preferences have withdrawn: it is still a row
// in the table, it is not in their list, and clearing it would be this shortcut deciding
// something the viewer never saw.
func TestClearableNotificationIDsHonourPreferences(t *testing.T) {
notifications := []store.UserNotification{
{ID: 1, Kind: "show-return"},
{ID: 2, Kind: watchTimeWeeklyKind},
{ID: 3, Kind: "library-added"},
}
prefs := store.DefaultNotificationPreferences()
prefs.WatchTimeDigest = false
ids := clearableNotificationIDs(notifications, prefs)
if len(ids) != 2 || ids[0] != 1 || ids[1] != 3 {
t.Fatalf("expected the two visible rows, got %v", ids)
}
prefs.Enabled = false
if ids := clearableNotificationIDs(notifications, prefs); len(ids) != 0 {
t.Fatalf("notifications switched off should clear nothing, got %v", ids)
}
if ids := clearableNotificationIDs(nil, store.DefaultNotificationPreferences()); len(ids) != 0 {
t.Fatalf("an empty list should clear nothing, got %v", ids)
}
}