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
package ai
import (
"fmt"
"strings"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/alerts"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/operationaltrust"
recoverymodel "github.com/rcourtman/pulse-go-rewrite/internal/recovery/model"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
func TestProjectAttentionItemsUsesCanonicalLifecycleAndStableOrdering(t *testing.T) {
now := time.Date(2026, 7, 19, 1, 0, 0, 0, time.UTC)
active := []alerts.Alert{
attentionTestAlert("warning-old", operationaltrust.OperationalOpen, operationaltrust.SeverityWarning, now.Add(-2*time.Hour), now),
attentionTestAlert("critical", operationaltrust.OperationalOpen, operationaltrust.SeverityCritical, now.Add(-time.Hour), now),
attentionTestAlert("warning-protected", operationaltrust.OperationalOpen, operationaltrust.SeverityWarning, now.Add(-3*time.Hour), now),
}
postures := map[string]recoverymodel.ProtectionPosture{
"resource-warning-old": attentionTestPosture("resource-warning-old", recoverymodel.ProtectionStateUnprotected, now),
"resource-warning-protected": attentionTestPosture(
"resource-warning-protected",
recoverymodel.ProtectionStateProtected,
now,
),
}
projected := ProjectAttentionItems(active, nil, postures, nil, now)
if projected.Summary.ActiveCount != 3 {
t.Fatalf("ActiveCount = %d, want 3", projected.Summary.ActiveCount)
}
if projected.Summary.Calm {
t.Fatal("active projection reported a calm state")
}
if len(projected.Details) != 3 {
t.Fatalf("details = %d, want 3", len(projected.Details))
}
got := []string{
projected.Details[0].Item.ID,
projected.Details[1].Item.ID,
projected.Details[2].Item.ID,
}
want := []string{"critical", "warning-old", "warning-protected"}
for i := range want {
if got[i] != want[i] {
t.Fatalf("order = %v, want %v", got, want)
}
}
if projected.Details[1].Item.OperationalRecordID != "warning-old" {
t.Fatal("attention item did not retain canonical operational record identity")
}
if projected.Details[1].Item.ProtectionPosture == nil ||
projected.Details[1].Item.ProtectionPosture.State != recoverymodel.ProtectionStateUnprotected {
t.Fatal("attention item did not receive canonical protection posture")
}
}
func TestProjectAttentionItemsDeduplicatesHistoryAndActiveByOperationalRecord(t *testing.T) {
now := time.Date(2026, 7, 19, 2, 0, 0, 0, time.UTC)
historical := attentionTestAlert(
"shared-record",
operationaltrust.OperationalResolved,
operationaltrust.SeverityWarning,
now.Add(-3*time.Hour),
now.Add(-time.Hour),
)
resolvedAt := now.Add(-time.Hour)
historical.OperationalRecord.ResolvedAt = &resolvedAt
active := attentionTestAlert(
"shared-record",
operationaltrust.OperationalOpen,
operationaltrust.SeverityWarning,
now.Add(-3*time.Hour),
now,
)
projected := ProjectAttentionItems([]alerts.Alert{active}, []alerts.Alert{historical}, nil, nil, now)
if len(projected.Details) != 1 {
t.Fatalf("details = %d, want one canonical item", len(projected.Details))
}
if projected.Details[0].Item.State != operationaltrust.OperationalOpen {
t.Fatalf("state = %q, want active lifecycle state", projected.Details[0].Item.State)
}
}
func TestProjectAttentionItemsDoesNotReviveNonTerminalHistorySnapshots(t *testing.T) {
now := time.Date(2026, 7, 19, 2, 30, 0, 0, time.UTC)
orphanedOpen := attentionTestAlert(
"historical-open",
operationaltrust.OperationalOpen,
operationaltrust.SeverityCritical,
now.Add(-3*time.Hour),
now.Add(-2*time.Hour),
)
resolved := attentionTestAlert(
"historical-resolved",
operationaltrust.OperationalResolved,
operationaltrust.SeverityWarning,
now.Add(-4*time.Hour),
now.Add(-time.Hour),
)
resolvedAt := now.Add(-time.Hour)
resolved.OperationalRecord.ResolvedAt = &resolvedAt
projected := ProjectAttentionItems(
nil,
[]alerts.Alert{orphanedOpen, resolved},
nil,
nil,
now,
)
if len(projected.Details) != 1 {
t.Fatalf("details = %d, want only the resolved historical record", len(projected.Details))
}
if got := projected.Details[0].Item.ID; got != "historical-resolved" {
t.Fatalf("historical item = %q, want resolved record only", got)
}
if projected.Summary.ActiveCount != 0 || !projected.Summary.Calm {
t.Fatalf("summary = %+v, historical open snapshot revived active work", projected.Summary)
}
}
func TestProjectAttentionItemsRepresentsUncertainEvidenceHonestly(t *testing.T) {
now := time.Date(2026, 7, 19, 3, 0, 0, 0, time.UTC)
stale := attentionTestAlert(
"stale",
operationaltrust.OperationalStale,
operationaltrust.SeverityCritical,
now.Add(-time.Hour),
now,
)
stale.Evidence = nil
unknown := attentionTestAlert(
"unknown",
operationaltrust.OperationalUnknown,
operationaltrust.SeverityWarning,
now.Add(-time.Hour),
now,
)
unknown.Evidence = nil
projected := ProjectAttentionItems([]alerts.Alert{stale, unknown}, nil, nil, nil, now)
if projected.Summary.UncertainCount != 2 || projected.Summary.ActiveCount != 2 {
t.Fatalf("summary = %+v, want two uncertain active items", projected.Summary)
}
if projected.Details[0].Item.EvidenceFreshness != operationaltrust.EvidenceStale {
t.Fatalf(
"stale freshness = %q",
projected.Details[0].Item.EvidenceFreshness,
)
}
if projected.Details[1].Item.EvidenceFreshness != operationaltrust.EvidenceFreshnessUnknown {
t.Fatalf(
"unknown freshness = %q",
projected.Details[1].Item.EvidenceFreshness,
)
}
for _, detail := range projected.Details {
if detail.Item.EvidenceCompleteness != operationaltrust.EvidenceUnavailable {
t.Fatalf("completeness = %q, want unavailable", detail.Item.EvidenceCompleteness)
}
}
}
func TestAttentionFiltersAndPaginationCoverLifecycleStates(t *testing.T) {
now := time.Date(2026, 7, 19, 4, 0, 0, 0, time.UTC)
states := []operationaltrust.OperationalState{
operationaltrust.OperationalOpen,
operationaltrust.OperationalAcknowledged,
operationaltrust.OperationalSuppressed,
operationaltrust.OperationalStale,
operationaltrust.OperationalUnknown,
operationaltrust.OperationalResolved,
}
active := make([]alerts.Alert, 0, len(states)-1)
history := make([]alerts.Alert, 0, 1)
for index, state := range states {
alert := attentionTestAlert(
string(state),
state,
operationaltrust.SeverityWarning,
now.Add(-time.Duration(index+1)*time.Minute),
now,
)
switch state {
case operationaltrust.OperationalAcknowledged:
alert.OperationalRecord.Acknowledgement = &operationaltrust.Acknowledgement{
At: now,
By: "operator",
}
case operationaltrust.OperationalSuppressed:
alert.OperationalRecord.Suppression = &operationaltrust.Suppression{
At: now,
By: "operator",
Reason: "maintenance",
}
case operationaltrust.OperationalResolved:
resolvedAt := now
alert.OperationalRecord.ResolvedAt = &resolvedAt
history = append(history, alert)
continue
}
active = append(active, alert)
}
projected := ProjectAttentionItems(active, history, nil, nil, now)
checks := map[AttentionFilter]int{
AttentionFilterActive: 3,
AttentionFilterOpen: 1,
AttentionFilterAcknowledged: 1,
AttentionFilterSuppressed: 1,
AttentionFilterUncertain: 2,
AttentionFilterResolved: 1,
AttentionFilterAll: 6,
}
for filter, want := range checks {
got, err := FilterAttentionDetails(projected.Details, filter)
if err != nil {
t.Fatalf("FilterAttentionDetails(%q): %v", filter, err)
}
if len(got) != want {
t.Fatalf("FilterAttentionDetails(%q) = %d, want %d", filter, len(got), want)
}
}
page, err := PaginateAttentionDetails(projected.Details, 2, 2)
if err != nil {
t.Fatalf("PaginateAttentionDetails(): %v", err)
}
if len(page) != 2 {
t.Fatalf("page length = %d, want 2", len(page))
}
}
func TestProjectAttentionItemsProducesCurrentCalmEvaluation(t *testing.T) {
now := time.Date(2026, 7, 19, 5, 0, 0, 0, time.UTC)
projected := ProjectAttentionItems(nil, nil, nil, nil, now)
if !projected.Summary.Calm || projected.Summary.ActiveCount != 0 {
t.Fatalf("summary = %+v, want current calm evaluation", projected.Summary)
}
if projected.Summary.CoverageState != "current" ||
!projected.Summary.EvaluatedAt.Equal(now) {
t.Fatalf("summary = %+v, want bounded current evaluation", projected.Summary)
}
}
func TestProjectAttentionItemsLeadsHeldMetricAlertsWithTheLiveReading(t *testing.T) {
// minipc on 2026-10-06: the alert opened at 80°C and stayed open while the
// node read 71-76°C, and Patrol kept saying "Node temperature at 80.0°C".
now := time.Date(2026, 10, 6, 10, 52, 0, 0, time.UTC)
raw := 61.0
tests := []struct {
name string
alertType string
message string
metadata map[string]interface{}
status *models.MetricAlertStatus
want string
}{
{
name: "held under the trigger",
alertType: "temperature",
message: "Node temperature at 80.0°C",
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseLatched, Value: 76, Unit: "°C",
ObservedAt: now.Add(-10 * time.Second), Trigger: 80, Recovery: 75, RecoveryDelaySeconds: 300,
},
want: "Temperature 76°C now, back under the 80°C alert level. Stays open until it reaches 75°C or lower and stays there for 5 minutes.",
},
{
name: "recovering at the clear level",
alertType: "temperature",
message: "Node temperature at 80.0°C",
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseRecovering, Value: 72, Unit: "°C",
ObservedAt: now.Add(-10 * time.Second), Trigger: 80, Recovery: 75,
RecoveryDelaySeconds: 300, RecoveryElapsedSeconds: 120,
},
want: "Temperature 72°C now, recovering. Clears after 5 minutes at 75°C or lower, 2 minutes so far.",
},
{
name: "recovery progress rounds down",
alertType: "temperature",
message: "Node temperature at 80.0°C",
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseRecovering, Value: 72, Unit: "°C",
ObservedAt: now, Trigger: 80, Recovery: 75,
RecoveryDelaySeconds: 300, RecoveryElapsedSeconds: 290,
},
want: "Temperature 72°C now, recovering. Clears after 5 minutes at 75°C or lower, 4 minutes so far.",
},
{
name: "one of a guest's disks",
alertType: "disk",
message: "VM disk (/var) at 92.0%",
metadata: map[string]interface{}{"label": "/var"},
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseRecovering, Value: 84.6, Unit: "%",
ObservedAt: now, Trigger: 90, Recovery: 85,
},
want: "Disk (/var) 85% now, recovering. Clears at 85% or lower.",
},
{
name: "rolling-average rule",
alertType: "cpu",
message: "VM cpu at 93.0%",
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseLatched, Value: 84.2, RawValue: &raw, EvaluationWindowSeconds: 600,
Unit: "%", ObservedAt: now, Trigger: 90, Recovery: 80,
},
want: "CPU averaged 84% over 10 minutes, latest 61% now, back under the 90% alert level. Stays open until it reaches 80% or lower.",
},
{
name: "breaching keeps the message that tracks the breach",
alertType: "temperature",
message: "Node temperature at 88.0°C",
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseBreaching, Value: 88, Unit: "°C",
ObservedAt: now, Trigger: 80, Recovery: 75,
},
want: "Node temperature at 88.0°C",
},
{
name: "an old evaluation is not described as now",
alertType: "temperature",
message: "Node temperature at 80.0°C",
status: &models.MetricAlertStatus{
Phase: models.MetricAlertPhaseLatched, Value: 76, Unit: "°C",
ObservedAt: now.Add(-attentionMetricStatusStaleAfter - time.Minute), Trigger: 80, Recovery: 75,
},
want: "Node temperature at 80.0°C",
},
{
name: "no live status",
alertType: "temperature",
message: "Node temperature at 80.0°C",
want: "Node temperature at 80.0°C",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
alert := attentionTestAlert("held", operationaltrust.OperationalOpen, operationaltrust.SeverityWarning, now.Add(-3*time.Hour), now)
alert.Type = tt.alertType
alert.Message = tt.message
alert.Value = 80
alert.Metadata = tt.metadata
alert.MetricStatus = tt.status
projected := ProjectAttentionItems([]alerts.Alert{alert}, nil, nil, nil, now)
if len(projected.Details) != 1 {
t.Fatalf("details = %d, want 1", len(projected.Details))
}
if got := projected.Details[0].Item.PlainLanguageSummary; got != tt.want {
t.Fatalf("summary = %q, want %q", got, tt.want)
}
})
}
}
func TestAttentionMetricFormattingRoundsLikeTheFrontend(t *testing.T) {
// The frontend rounds with Number(value.toFixed(1)), which works from the
// float's exact value: 2.55 is stored just under 2.55 and reads 2.5.
cases := []struct{ got, want string }{
{formatAttentionMetricValue(2.55, "MB/s"), "2.5 MB/s"},
{formatAttentionMetricValue(4.25, "%"), "4.3%"},
{formatAttentionMetricValue(0.75, "MB/s"), "0.8 MB/s"},
{formatAttentionMetricValue(1.05, "MB/s"), "1.1 MB/s"},
{formatAttentionMetricValue(12.5, "MB/s"), "12.5 MB/s"},
{formatAttentionMetricValue(3.0, "MB/s"), "3 MB/s"},
{formatAttentionMetricValue(84.5, "%"), "85%"},
{formatAttentionMetricValue(71.6, "°C"), "72°C"},
{formatAttentionMetricValue(-10.5, "%"), "-10%"},
{formatAttentionMetricValue(-0.01, "MB/s"), "0 MB/s"},
{formatAttentionMetricValue(0.49999999999999994, "°C"), "0°C"},
{formatAttentionSeconds(4140), "1.1 hours"},
{formatAttentionSeconds(3600), "1 hour"},
{formatAttentionSeconds(90), "2 minutes"},
{formatAttentionElapsed(290), "4 minutes"},
}
for _, tc := range cases {
if tc.got != tc.want {
t.Errorf("formatted %q, want %q", tc.got, tc.want)
}
}
}
func TestAttentionTitleProducesHumanReadableLabels(t *testing.T) {
tests := []struct {
name string
alertType string
incidentCode string
message string
resourceName string
want string
}{
{
name: "VMware alarm uses the actual alarm",
alertType: "storage-incident",
incidentCode: "vmware_alarm_state",
message: "Datastore latency above threshold. Affects 2 dependent resources",
resourceName: "archive-tier",
want: "Datastore latency above threshold on archive-tier",
},
{
name: "VMware alarm recorded before the alarm-name summary",
alertType: "resource-incident",
incidentCode: "vmware_alarm_state",
message: "Network network-302 has VMware alarm Network packet loss above threshold (yellow)",
resourceName: "Edge Stateful",
want: "Network packet loss above threshold on Edge Stateful",
},
{
name: "VMware alarm keeps punctuation that belongs to its name",
alertType: "storage-incident",
incidentCode: "vmware_alarm_state",
message: "Storage warning. Latency (p99) above threshold. Puts backups for 2 protected workloads at risk: db-01, db-02",
resourceName: "archive-tier",
want: "Storage warning. Latency (p99) above threshold on archive-tier",
},
{
name: "VMware alarm strips only the one appended consumer summary",
alertType: "storage-incident",
incidentCode: "vmware_alarm_state",
message: "Latency high. Affects production. Puts backups for 2 protected workloads at risk",
resourceName: "archive-tier",
want: "Latency high. Affects production on archive-tier",
},
{
name: "VMware legacy alarm strips only its final colour",
alertType: "resource-incident",
incidentCode: "vmware_alarm_state",
message: "Network network-302 has VMware alarm Storage (yellow) (red)",
resourceName: "Edge Stateful",
want: "Storage (yellow) on Edge Stateful",
},
{
name: "VMware alarm recorded before the alarm-name summary on storage",
alertType: "storage-incident",
incidentCode: "vmware_alarm_state",
message: "Datastore datastore-202 has VMware alarm Datastore latency above threshold (yellow). Affects 2 dependent resources",
resourceName: "archive-tier",
want: "Datastore latency above threshold on archive-tier",
},
{
name: "VMware host status uses product language",
alertType: "resource-incident",
incidentCode: "vmware_health_state",
message: "vCenter health is yellow",
resourceName: "esxi-07.lab.local",
want: "VMware host health on esxi-07.lab.local",
},
{
name: "VMware datastore status names the datastore",
alertType: "storage-incident",
incidentCode: "vmware_health_state",
message: "vCenter health is yellow",
resourceName: "edge-cold-iscsi",
want: "VMware datastore health on edge-cold-iscsi",
},
{
name: "generic incident avoids incident placeholder",
alertType: "resource_incident",
message: "A monitored resource needs attention",
resourceName: "router-1",
want: "Infrastructure issue on router-1",
},
{
name: "Ceph incident names the degraded placement group",
alertType: "resource-incident",
message: "1 PG degraded",
resourceName: "Mock Cluster Ceph",
want: "Ceph placement groups degraded on Mock Cluster Ceph",
},
{
name: "acronyms stay readable",
alertType: "high_cpu_and_zfs_io",
resourceName: "database-1",
want: "High CPU and ZFS I/O on database-1",
},
{
name: "one-word disk type states what is measured",
alertType: "disk",
resourceName: "pve1",
want: "Disk usage on pve1",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
alert := alerts.Alert{Type: test.alertType, Message: test.message}
if test.incidentCode != "" {
alert.Metadata = map[string]interface{}{"incidentCode": test.incidentCode}
}
got := attentionTitle(alert, test.resourceName)
if got != test.want {
t.Fatalf("attentionTitle() = %q, want %q", got, test.want)
}
})
}
}
func attentionTestAlert(
id string,
state operationaltrust.OperationalState,
severity operationaltrust.OperationalSeverity,
firstObservedAt time.Time,
lastObservedAt time.Time,
) alerts.Alert {
resourceID := "resource-" + id
evidence := attentionTestEvidence(resourceID, lastObservedAt)
record := operationaltrust.OperationalRecord{
ID: id,
CanonicalSpecID: "spec-" + id,
SubjectResourceID: resourceID,
State: state,
Severity: severity,
FirstObservedAt: firstObservedAt,
LastObservedAt: lastObservedAt,
StateChangedAt: lastObservedAt,
EvidenceIDs: []string{evidence.ID},
CauseKey: "cause-" + id,
RelatedResourceIDs: []string{"related-" + id},
ImpactSummary: "Service may be interrupted.",
RecommendedNextStep: "Review the source evidence.",
}
return alerts.Alert{
ID: "alert-" + id,
Type: "service-health",
Level: alerts.AlertLevelWarning,
ResourceID: resourceID,
ResourceName: "Resource " + id,
Message: "The service health check needs attention.",
StartTime: firstObservedAt,
LastSeen: lastObservedAt,
OperationalRecord: &record,
Evidence: []operationaltrust.EvidenceEnvelope{evidence},
}
}
func attentionTestEvidence(
resourceID string,
observedAt time.Time,
) operationaltrust.EvidenceEnvelope {
validUntil := observedAt.Add(time.Hour)
return operationaltrust.EvidenceEnvelope{
ID: "evidence-" + resourceID,
Source: operationaltrust.EvidenceSource{
Provider: "test",
Collector: "test-collector",
},
Subject: operationaltrust.EvidenceSubject{ResourceID: resourceID},
ObservedAt: observedAt,
IngestedAt: observedAt,
ValidUntil: &validUntil,
Completeness: operationaltrust.EvidenceComplete,
Confidence: operationaltrust.EvidenceConfirmed,
Permissions: operationaltrust.EvidencePermissionsSufficient,
}
}
func attentionTestPosture(
resourceID string,
state recoverymodel.ProtectionState,
evaluatedAt time.Time,
) recoverymodel.ProtectionPosture {
return recoverymodel.ProtectionPosture{
SubjectResourceID: resourceID,
State: state,
Freshness: recoverymodel.ProtectionFreshnessCurrent,
Verification: recoverymodel.ProtectionVerificationVerified,
Coverage: recoverymodel.ProtectionCoverageComplete,
ProviderStates: []recoverymodel.ProtectionProviderState{},
EvidenceIDs: []string{},
Explanation: "Test posture.",
EvaluatedAt: evaluatedAt,
}
}
func TestProjectAttentionItemsCollapsesOpenResolvedChurnIntoFlapping(t *testing.T) {
now := time.Date(2026, 8, 10, 15, 0, 0, 0, time.UTC)
alert := attentionTestAlert("flapper", operationaltrust.OperationalOpen, operationaltrust.SeverityWarning, now.Add(-2*24*time.Hour), now)
from := operationaltrust.OperationalOpen
to := operationaltrust.OperationalResolved
// Eleven open/resolved transitions in the last day, plus older churn that
// must not count, plus acknowledgement bookkeeping that never counts.
for i := 0; i < 11; i++ {
alert.Transitions = append(alert.Transitions, operationaltrust.LifecycleTransition{
ID: "t-" + string(rune('a'+i)),
OperationalRecordID: "flapper",
From: from,
To: to,
At: now.Add(-time.Duration(11-i) * time.Hour),
Cause: operationaltrust.TransitionRecoveryEvidence,
})
from, to = to, from
}
alert.Transitions = append(alert.Transitions,
operationaltrust.LifecycleTransition{
ID: "old", OperationalRecordID: "flapper",
From: operationaltrust.OperationalOpen, To: operationaltrust.OperationalResolved,
At: now.Add(-30 * time.Hour), Cause: operationaltrust.TransitionRecoveryEvidence,
},
operationaltrust.LifecycleTransition{
ID: "ack", OperationalRecordID: "flapper",
From: operationaltrust.OperationalOpen, To: operationaltrust.OperationalAcknowledged,
At: now.Add(-20 * time.Minute), Cause: operationaltrust.TransitionAcknowledgement,
},
)
projected := ProjectAttentionItems([]alerts.Alert{alert}, nil, nil, nil, now)
if len(projected.Details) != 1 {
t.Fatalf("details = %d, want 1", len(projected.Details))
}
item := projected.Details[0].Item
if item.Flapping == nil {
t.Fatal("eleven open/resolved transitions in a day were not labelled flapping")
}
if item.Flapping.TransitionCount != 11 {
t.Fatalf("TransitionCount = %d, want 11 (older and acknowledgement transitions excluded)", item.Flapping.TransitionCount)
}
if item.Flapping.WindowHours != 24 {
t.Fatalf("WindowHours = %d, want 24", item.Flapping.WindowHours)
}
if len(projected.Details[0].Timeline) != 13 {
t.Fatalf("timeline = %d, want the full forensic list of 13 transitions retained", len(projected.Details[0].Timeline))
}
}
func TestProjectAttentionItemsLeavesSingleRecurrenceUnlabelled(t *testing.T) {
now := time.Date(2026, 8, 10, 15, 0, 0, 0, time.UTC)
alert := attentionTestAlert("recurred", operationaltrust.OperationalOpen, operationaltrust.SeverityWarning, now.Add(-24*time.Hour), now)
alert.Transitions = append(alert.Transitions,
operationaltrust.LifecycleTransition{
ID: "r1", OperationalRecordID: "recurred",
From: operationaltrust.OperationalOpen, To: operationaltrust.OperationalResolved,
At: now.Add(-3 * time.Hour), Cause: operationaltrust.TransitionRecoveryEvidence,
},
operationaltrust.LifecycleTransition{
ID: "r2", OperationalRecordID: "recurred",
From: operationaltrust.OperationalResolved, To: operationaltrust.OperationalOpen,
At: now.Add(-time.Hour), Cause: operationaltrust.TransitionDetectorDecision,
},
)
projected := ProjectAttentionItems([]alerts.Alert{alert}, nil, nil, nil, now)
if projected.Details[0].Item.Flapping != nil {
t.Fatalf("one recurrence was labelled flapping: %+v", projected.Details[0].Item.Flapping)
}
}
// An alert lifecycle record names its subject by the alert producer's own
// reference. The item names the unified resource that reference binds in
// current inventory, the ID Patrol findings and protection postures carry,
// for every producer shape; the record keeps the alert's reference.
func TestProjectAttentionItemsNamesTheUnifiedSubjectOfEveryAlertKind(t *testing.T) {
now := time.Date(2026, 10, 24, 8, 0, 0, 0, time.UTC)
containerID := strings.Repeat("ab12", 16)
snapshot := models.StateSnapshot{
Nodes: []models.Node{{ID: "pve1-node1", Name: "node1", Instance: "pve1", Status: "online", LastSeen: now}},
VMs: []models.VM{{ID: "pve1:node1:100", VMID: 100, Name: "web-vm", Node: "node1", Instance: "pve1", Status: "running", Type: "qemu", LastSeen: now}},
Containers: []models.Container{{ID: "pve1:node1:101", VMID: 101, Name: "dns-ct", Node: "node1", Instance: "pve1", Status: "running", Type: "lxc", LastSeen: now}},
Storage: []models.Storage{{ID: "pve1-node1-local", Name: "local", Node: "node1", Instance: "pve1", Type: "dir", Status: "available", Enabled: true, Active: true}},
Hosts: []models.Host{{ID: "host-src-1", Hostname: "backup-box", MachineID: "machine-1", Status: "online", LastSeen: now}},
DockerHosts: []models.DockerHost{{ID: "dock-agent-1", AgentID: "dock-agent-1", Hostname: "docker-box", Status: "online", LastSeen: now,
Containers: []models.DockerContainer{{ID: containerID, Name: "web", State: "running", Health: "unhealthy"}}}},
}
registry := unifiedresources.NewRegistry(nil)
registry.IngestSnapshot(snapshot)
resourceIDByName := map[string]string{}
for _, resource := range registry.List() {
resourceIDByName[resource.Name] = resource.ID
}
cases := []struct {
reference string
resource string
}{
{"pve1-node1", "node1"},
{"pve1:node1:100", "web-vm"},
{"pve1:node1:101", "dns-ct"},
{"pve1-node1-local", "local"},
{"pve1-node1-local/zfs-pool:tank", "local"},
{"agent:host-src-1", "backup-box"},
{"agent:host-src-1/disk:docker-data", "backup-box"},
{alerts.DockerHostResourceID("dock-agent-1"), "docker-box"},
{alerts.DockerContainerResourceID("dock-agent-1", containerID, "web"), "web"},
}
active := make([]alerts.Alert, 0, len(cases)+2)
want := make(map[string]string, len(cases)+2)
for i, tc := range cases {
resourceID := resourceIDByName[tc.resource]
if resourceID == "" || resourceID == tc.reference {
t.Fatalf("fixture resource %q missing or named like its alert reference: %q", tc.resource, resourceID)
}
id := fmt.Sprintf("record-%d", i)
active = append(active, attentionSubjectTestAlert(id, tc.reference, now))
want[id] = resourceID
}
// A reference inventory does not bind keeps its own name, as does an alert
// already raised on the unified resource.
active = append(active,
attentionSubjectTestAlert("record-removed", "agent:removed-host/disk:data", now),
attentionSubjectTestAlert("record-unified", resourceIDByName["web-vm"], now),
)
want["record-removed"] = "agent:removed-host/disk:data"
want["record-unified"] = resourceIDByName["web-vm"]
postures := map[string]recoverymodel.ProtectionPosture{
resourceIDByName["web-vm"]: attentionTestPosture(resourceIDByName["web-vm"], recoverymodel.ProtectionStateProtected, now),
}
projected := ProjectAttentionItems(active, nil, postures, registry, now)
if len(projected.Details) != len(want) {
t.Fatalf("details = %d, want %d", len(projected.Details), len(want))
}
references := map[string]string{"record-removed": "agent:removed-host/disk:data", "record-unified": resourceIDByName["web-vm"]}
for i, tc := range cases {
references[fmt.Sprintf("record-%d", i)] = tc.reference
}
for _, detail := range projected.Details {
id := detail.Item.ID
if got := detail.Item.SubjectResourceID; got != want[id] {
t.Errorf("%s (%s): subject = %q, want %q", id, references[id], got, want[id])
}
if got := detail.OperationalRecord.SubjectResourceID; got != references[id] {
t.Errorf("%s: record subject = %q, want the alert's reference %q", id, got, references[id])
}
if got := detail.Item.ProtectionPosture != nil; got != (want[id] == resourceIDByName["web-vm"]) {
t.Errorf("%s: posture attached = %v", id, got)
}
}
// Without inventory every item keeps its record's reference.
for _, detail := range ProjectAttentionItems(active, nil, postures, nil, now).Details {
if detail.Item.SubjectResourceID != references[detail.Item.ID] {
t.Errorf("%s: subject = %q without inventory, want %q", detail.Item.ID, detail.Item.SubjectResourceID, references[detail.Item.ID])
}
}
}
func attentionSubjectTestAlert(id, reference string, now time.Time) alerts.Alert {
alert := attentionTestAlert(id, operationaltrust.OperationalOpen, operationaltrust.SeverityWarning, now.Add(-time.Hour), now)
alert.ResourceID = reference
alert.OperationalRecord.SubjectResourceID = reference
alert.Evidence[0].Subject.ResourceID = reference
return alert
}