From 9768316e076bcc4c11b25c4477e6d3cc4311398a Mon Sep 17 00:00:00 2001 From: rcourtman Date: Tue, 2 Dec 2025 02:14:13 +0000 Subject: [PATCH] test: Add sendGroupedApprise error path tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add 3 tests for Apprise notification error handling: - No alerts returns error - Not enabled returns error - Empty payload (all nil alerts) returns error Coverage: 56.2% → 75.0% --- internal/notifications/notifications_test.go | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/internal/notifications/notifications_test.go b/internal/notifications/notifications_test.go index 1125bbd..057b037 100644 --- a/internal/notifications/notifications_test.go +++ b/internal/notifications/notifications_test.go @@ -2538,3 +2538,57 @@ func TestGeneratePayloadFromTemplateWithService(t *testing.T) { } }) } + +func TestSendGroupedApprise_NoAlerts(t *testing.T) { + nm := &NotificationManager{} + config := AppriseConfig{Enabled: true} + + err := nm.sendGroupedApprise(config, nil) + if err == nil { + t.Error("expected error for nil alerts") + } + if err != nil && !strings.Contains(err.Error(), "no alerts") { + t.Errorf("expected 'no alerts' error, got: %v", err) + } + + err = nm.sendGroupedApprise(config, []*alerts.Alert{}) + if err == nil { + t.Error("expected error for empty alerts") + } +} + +func TestSendGroupedApprise_NotEnabled(t *testing.T) { + nm := &NotificationManager{} + config := AppriseConfig{Enabled: false} + alertList := []*alerts.Alert{ + {ID: "test-1", ResourceName: "VM1", Level: "warning", Message: "test"}, + } + + err := nm.sendGroupedApprise(config, alertList) + if err == nil { + t.Error("expected error when apprise not enabled") + } + if err != nil && !strings.Contains(err.Error(), "not enabled") { + t.Errorf("expected 'not enabled' error, got: %v", err) + } +} + +func TestSendGroupedApprise_EmptyPayload(t *testing.T) { + nm := &NotificationManager{} + // Provide targets so config stays enabled after normalization + config := AppriseConfig{ + Enabled: true, + Mode: AppriseModeCLI, + Targets: []string{"discord://token"}, + } + // All nil alerts - will produce empty payload + alertList := []*alerts.Alert{nil, nil} + + err := nm.sendGroupedApprise(config, alertList) + if err == nil { + t.Error("expected error for empty payload") + } + if err != nil && !strings.Contains(err.Error(), "failed to build apprise payload") { + t.Errorf("expected 'failed to build apprise payload' error, got: %v", err) + } +}