Files
Pulse/internal/ai/attention_test.go
rcourtman 991948f28b Lead alert cards and Patrol with the live reading of held alerts
A threshold alert stays open below its trigger until the reading reaches
the clear level and holds there for the recovery delay, while its message
keeps the last breach. #2567 added the live status and moved node, guest
and Mail Gateway drawers onto it, but the Alerts page card, the open
incident in the History panel and Patrol's attention summary still said
"Node temperature at 95.0°C" while the node read 72°C.

The Alerts card now leads with the reading now and what clears the alert,
keeps the last breach as hover text, and labels the alert and clear
levels in the metric's unit, temperatures in the viewer's °C or °F.
Without a live status it labels a level only for metric types whose unit
is known, so a Mail Gateway queue-age alert no longer reads "limit: 30%"
for a 30 minute threshold. The resource incident panel describes only the
open occurrence whose alert id and start match a live alert, the incident
store's own occurrence key, rechecked on the shared clock; closed
occurrences keep their recorded message.

Patrol's attention summary is built server-side because the Patrol inbox,
the Assistant handoff and the mobile app all read plainLanguageSummary.
A latched or recovering alert with a fresh status gets the live reading
in the frontend helper's words; a breaching alert keeps its message,
which tracks the breach, and a stale status falls back to it.

Every disk alert on a guest carries the guest's name, so the live
reading names the disk from metadata.label as the per-disk message does;
without it two disks on one guest read the same. Recovery progress now
rounds down, so 4m50s into a 5 minute delay no longer reads "5 minutes so
far" while the alert is still open, and the server copy rounds the way the
frontend does (toFixed and Math.round ties), so Patrol and the card agree.

From the sm breakpoint the card's text column keeps a 16rem floor. Before,
it shrank to its longest word beside one row of action buttons, so at a
1024px window every card's text sat in a 110-160px column; the buttons now
wrap instead.

Refs #2068
2026-10-06 20:32:31 +01:00

648 lines
23 KiB
Go

package ai
import (
"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"
)
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, 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, 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,
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, 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, 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, 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, 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, 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, now)
if projected.Details[0].Item.Flapping != nil {
t.Fatalf("one recurrence was labelled flapping: %+v", projected.Details[0].Item.Flapping)
}
}