Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
test(ai): add patrol alert review, resource state, and scoped run tests
rcourtman3a9c321c5090
diff --git a/internal/ai/patrol_alerts_review_test.go b/internal/ai/patrol_alerts_review_test.gonew file mode 100644index 0000000000..4bbfe839b6--- /dev/null+++ b/internal/ai/patrol_alerts_review_test.go@@ -0,0 +1,116 @@+package ai++import (+ "context"+ "strings"+ "testing"+ "time"++ "github.com/rcourtman/pulse-go-rewrite/internal/models"+)++func TestShouldResolveAlert_UsageAndMissing(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ now := time.Now()++ usageAlert := AlertInfo{+ ID: "alert-usage",+ Type: "usage",+ ResourceID: "storage-1",+ Value: 92,+ Threshold: 90,+ StartTime: now.Add(-2 * time.Hour),+ }+ state := models.StateSnapshot{+ Storage: []models.Storage{{ID: "storage-1", Usage: 80}},+ }+ resolved, reason := ps.shouldResolveAlert(context.Background(), usageAlert, state, nil)+ if !resolved || !strings.Contains(reason, "usage dropped") {+ t.Fatalf("expected usage alert to resolve, got resolved=%v reason=%q", resolved, reason)+ }++ missingAlert := AlertInfo{+ ID: "alert-missing",+ Type: "usage",+ ResourceID: "storage-2",+ Value: 95,+ Threshold: 90,+ StartTime: now.Add(-25 * time.Hour),+ }+ resolved, reason = ps.shouldResolveAlert(context.Background(), missingAlert, state, nil)+ if !resolved || !strings.Contains(reason, "resource no longer present") {+ t.Fatalf("expected missing storage alert to resolve, got resolved=%v reason=%q", resolved, reason)+ }+}++func TestShouldResolveAlert_CPUAndOffline(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ now := time.Now()++ cpuAlert := AlertInfo{+ ID: "alert-cpu",+ Type: "cpu",+ ResourceID: "node-1",+ ResourceName: "node-1",+ ResourceType: "node",+ Value: 95,+ Threshold: 90,+ StartTime: now.Add(-30 * time.Minute),+ }+ state := models.StateSnapshot{+ Nodes: []models.Node{{ID: "node-1", Name: "node-1", CPU: 0.10, Status: "online"}},+ }+ resolved, reason := ps.shouldResolveAlert(context.Background(), cpuAlert, state, nil)+ if !resolved || !strings.Contains(reason, "cpu dropped") {+ t.Fatalf("expected cpu alert to resolve, got resolved=%v reason=%q", resolved, reason)+ }++ offlineAlert := AlertInfo{+ ID: "alert-offline",+ Type: "offline",+ ResourceID: "vm-1",+ ResourceName: "vm-1",+ ResourceType: "vm",+ StartTime: now.Add(-30 * time.Minute),+ }+ state.VMs = []models.VM{{ID: "vm-1", Name: "vm-1", Status: "running"}}+ resolved, reason = ps.shouldResolveAlert(context.Background(), offlineAlert, state, nil)+ if !resolved || !strings.Contains(reason, "resource is now online") {+ t.Fatalf("expected offline alert to resolve, got resolved=%v reason=%q", resolved, reason)+ }+}++func TestReviewAndResolveAlerts(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ resolver := &stubAlertResolver{}+ ps.alertResolver = resolver++ now := time.Now()+ resolver.alerts = []AlertInfo{+ {+ ID: "recent",+ Type: "usage",+ ResourceID: "storage-1",+ Value: 95,+ Threshold: 90,+ StartTime: now.Add(-5 * time.Minute),+ },+ {+ ID: "stale",+ Type: "usage",+ ResourceID: "storage-1",+ Value: 95,+ Threshold: 90,+ StartTime: now.Add(-30 * time.Minute),+ },+ }++ state := models.StateSnapshot{Storage: []models.Storage{{ID: "storage-1", Usage: 70}}}+ resolved := ps.reviewAndResolveAlerts(context.Background(), state, false)+ if resolved != 1 {+ t.Fatalf("expected 1 alert resolved, got %d", resolved)+ }+ if len(resolver.clears) != 1 || resolver.clears[0] != "stale" {+ t.Fatalf("expected stale alert to be resolved, got %v", resolver.clears)+ }+}diff --git a/internal/ai/patrol_resource_state_test.go b/internal/ai/patrol_resource_state_test.gonew file mode 100644index 0000000000..63f343dd47--- /dev/null+++ b/internal/ai/patrol_resource_state_test.go@@ -0,0 +1,69 @@+package ai++import (+ "strings"+ "testing"++ "github.com/rcourtman/pulse-go-rewrite/internal/models"+)++func TestGetResourceCurrentState(t *testing.T) {+ ps := NewPatrolService(nil, nil)++ state := models.StateSnapshot{+ Storage: []models.Storage{{ID: "storage-1", Name: "local", Usage: 42, Status: "active"}},+ Nodes: []models.Node{{ID: "node-1", Name: "node-1", CPU: 0.2, Memory: models.Memory{Usage: 33}, Status: "online"}},+ VMs: []models.VM{{ID: "vm-1", Name: "vm-1", CPU: 0.1, Memory: models.Memory{Usage: 44}, Status: "running"}},+ Containers: []models.Container{{ID: "ct-1", Name: "ct-1", CPU: 0.3, Memory: models.Memory{Usage: 55}, Status: "running"}},+ DockerHosts: []models.DockerHost{{+ ID: "docker-1",+ Hostname: "dockhost",+ Status: "online",+ Containers: []models.DockerContainer{{ID: "dc-1", Name: "web", CPUPercent: 12.3, MemoryPercent: 45.6, State: "running"}},+ }},+ }++ cases := []struct {+ alert AlertInfo+ assert string+ }{+ {alert: AlertInfo{ResourceType: "Storage", ResourceID: "storage-1", ResourceName: "local"}, assert: "Storage 'local'"},+ {alert: AlertInfo{ResourceType: "node", ResourceID: "node-1", ResourceName: "node-1"}, assert: "Node 'node-1'"},+ {alert: AlertInfo{ResourceType: "vm", ResourceID: "vm-1", ResourceName: "vm-1"}, assert: "VM 'vm-1'"},+ {alert: AlertInfo{ResourceType: "container", ResourceID: "ct-1", ResourceName: "ct-1"}, assert: "Container 'ct-1'"},+ {alert: AlertInfo{ResourceType: "docker", ResourceID: "dc-1", ResourceName: "web"}, assert: "Docker container 'web'"},+ {alert: AlertInfo{ResourceType: "unknown", ResourceID: "x"}, assert: "Resource state unknown"},+ }++ for _, tc := range cases {+ stateText := ps.getResourceCurrentState(tc.alert, state)+ if !strings.Contains(stateText, tc.assert) {+ t.Fatalf("expected %q in state text, got %q", tc.assert, stateText)+ }+ }+}++func TestGetCurrentMetricValue_DockerAndStorage(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ state := models.StateSnapshot{+ Storage: []models.Storage{{ID: "storage-1", Name: "local", Usage: 77}},+ DockerHosts: []models.DockerHost{{+ Containers: []models.DockerContainer{{ID: "dc-1", Name: "web", CPUPercent: 12.3, MemoryPercent: 45.6}},+ }},+ }++ storageAlert := AlertInfo{ResourceType: "Storage", ResourceID: "storage-1"}+ if got := ps.getCurrentMetricValue(storageAlert, state); got != 77 {+ t.Fatalf("expected storage usage 77, got %.1f", got)+ }++ dockerCPU := AlertInfo{ResourceType: "docker", ResourceID: "dc-1", Type: "cpu"}+ if got := ps.getCurrentMetricValue(dockerCPU, state); got != 12.3 {+ t.Fatalf("expected docker cpu 12.3, got %.1f", got)+ }++ dockerMem := AlertInfo{ResourceType: "docker", ResourceID: "dc-1", Type: "memory"}+ if got := ps.getCurrentMetricValue(dockerMem, state); got != 45.6 {+ t.Fatalf("expected docker memory 45.6, got %.1f", got)+ }+}diff --git a/internal/ai/patrol_scoped_run_test.go b/internal/ai/patrol_scoped_run_test.gonew file mode 100644index 0000000000..a84bb62850--- /dev/null+++ b/internal/ai/patrol_scoped_run_test.go@@ -0,0 +1,40 @@+package ai++import (+ "context"+ "testing"+ "time"+)++func TestRunScopedPatrol_Disabled(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ ps.config = PatrolConfig{Enabled: false}+ ps.runScopedPatrol(context.Background(), PatrolScope{ResourceIDs: []string{"node-1"}})+ // No panic and no state changes expected.+}++func TestRunScopedPatrol_RequeueWhenRunInProgress(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ ps.config = PatrolConfig{Enabled: true}+ ps.runInProgress = true++ scope := PatrolScope{ResourceIDs: []string{"node-1"}, Reason: TriggerReasonManual}+ ps.runScopedPatrol(context.Background(), scope)++ if !ps.runInProgress {+ t.Fatalf("expected runInProgress to remain true when run is already in progress")+ }+}++func TestRunScopedPatrol_StuckRunClearsAndNoStateProvider(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ ps.config = PatrolConfig{Enabled: true}+ ps.runInProgress = true+ ps.runStartedAt = time.Now().Add(-21 * time.Minute)++ ps.runScopedPatrol(context.Background(), PatrolScope{ResourceIDs: []string{"node-1"}, Reason: TriggerReasonManual})++ if ps.runInProgress {+ t.Fatalf("expected stuck run to be cleared")+ }+}