From 96bf0b0ca6bed8677bcd48f55682b2ee0d7f9c11 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Tue, 2 Dec 2025 00:37:28 +0000 Subject: [PATCH] test: Add edge cases for buildApprisePayload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Test nil alerts in list are filtered out - Test all-nil alert list returns empty - Test multiple alerts changes title format Coverage: 88.9% → 100% --- internal/notifications/notifications_test.go | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/internal/notifications/notifications_test.go b/internal/notifications/notifications_test.go index aab8e85..1125bbd 100644 --- a/internal/notifications/notifications_test.go +++ b/internal/notifications/notifications_test.go @@ -1387,6 +1387,45 @@ func TestGetEmailConfig(t *testing.T) { } } +func TestBuildApprisePayload(t *testing.T) { + t.Run("nil alerts filtered out", func(t *testing.T) { + alertList := []*alerts.Alert{ + nil, + {ID: "test-1", ResourceName: "VM1", Level: "warning", Message: "test", Value: 80, Threshold: 75}, + nil, + } + title, body, notifyType := buildApprisePayload(alertList, "") + if title == "" || body == "" { + t.Fatalf("expected non-empty title and body, got title=%q, body=%q", title, body) + } + if notifyType != "warning" { + t.Fatalf("expected warning notify type, got %q", notifyType) + } + }) + + t.Run("all nil alerts returns empty", func(t *testing.T) { + alertList := []*alerts.Alert{nil, nil} + title, body, notifyType := buildApprisePayload(alertList, "") + if title != "" || body != "" { + t.Fatalf("expected empty title and body for all-nil list") + } + if notifyType != "info" { + t.Fatalf("expected info notify type for empty, got %q", notifyType) + } + }) + + t.Run("multiple alerts changes title", func(t *testing.T) { + alertList := []*alerts.Alert{ + {ID: "test-1", ResourceName: "VM1", Level: "warning", Message: "test1", Value: 80, Threshold: 75}, + {ID: "test-2", ResourceName: "VM2", Level: "critical", Message: "test2", Value: 95, Threshold: 90}, + } + title, _, _ := buildApprisePayload(alertList, "") + if !strings.Contains(title, "(2)") { + t.Fatalf("expected title to contain count for multiple alerts, got %q", title) + } + }) +} + func TestBuildResolvedNotificationContent(t *testing.T) { t.Run("nil alert list returns empty strings", func(t *testing.T) { title, htmlBody, textBody := buildResolvedNotificationContent(nil, time.Now(), "")