Files
Pulse/internal/ai/patrol_ai_more_test.go
2026-04-03 19:45:38 +01:00

997 lines
30 KiB
Go

package ai
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/baseline"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/chat"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/cost"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/memory"
"github.com/rcourtman/pulse-go-rewrite/internal/ai/tools"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
type noExecutorChatService struct{}
func (n *noExecutorChatService) CreateSession(ctx context.Context) (*ChatSession, error) {
return &ChatSession{ID: "noop"}, nil
}
func (n *noExecutorChatService) ExecuteStream(ctx context.Context, req ChatExecuteRequest, callback ChatStreamCallback) error {
return nil
}
func (n *noExecutorChatService) ExecutePatrolStream(ctx context.Context, req PatrolExecuteRequest, callback ChatStreamCallback) (*PatrolStreamResponse, error) {
return &PatrolStreamResponse{}, nil
}
func (n *noExecutorChatService) GetMessages(ctx context.Context, sessionID string) ([]ChatMessage, error) {
return nil, nil
}
func (n *noExecutorChatService) DeleteSession(ctx context.Context, sessionID string) error {
return nil
}
func (n *noExecutorChatService) ReloadConfig(ctx context.Context, cfg *config.AIConfig) error {
return nil
}
type streamChatService struct {
executor *tools.PulseToolExecutor
}
func (s *streamChatService) CreateSession(ctx context.Context) (*ChatSession, error) {
return &ChatSession{ID: "stream"}, nil
}
func (s *streamChatService) ExecuteStream(ctx context.Context, req ChatExecuteRequest, callback ChatStreamCallback) error {
return nil
}
func (s *streamChatService) ExecutePatrolStream(ctx context.Context, req PatrolExecuteRequest, callback ChatStreamCallback) (*PatrolStreamResponse, error) {
contentData, _ := json.Marshal(struct {
Text string `json:"text"`
}{Text: "hello"})
callback(ChatStreamEvent{Type: "content", Data: contentData})
thinkingData, _ := json.Marshal(struct {
Text string `json:"text"`
}{Text: "thinking"})
callback(ChatStreamEvent{Type: "thinking", Data: thinkingData})
toolStartData, _ := json.Marshal(struct {
ID string `json:"id"`
Name string `json:"name"`
Input string `json:"input"`
RawInput string `json:"raw_input"`
}{
ID: "",
Name: "pulse_query",
Input: `{"action":"health"}`,
RawInput: `{"action":"health"}`,
})
callback(ChatStreamEvent{Type: "tool_start", Data: toolStartData})
toolEndData, _ := json.Marshal(struct {
ID string `json:"id"`
Name string `json:"name"`
Input string `json:"input"`
RawInput string `json:"raw_input"`
Output string `json:"output"`
Success bool `json:"success"`
}{
ID: "",
Name: "pulse_query",
Input: `{"action":"health"}`,
RawInput: `{"action":"health"}`,
Output: `{"status":"ok"}`,
Success: true,
})
callback(ChatStreamEvent{Type: "tool_end", Data: toolEndData})
toolEndMissingData, _ := json.Marshal(struct {
ID string `json:"id"`
Name string `json:"name"`
Input string `json:"input"`
RawInput string `json:"raw_input"`
Output string `json:"output"`
Success bool `json:"success"`
}{
ID: "missing",
Name: "pulse_metrics",
Output: `{"status":"ok"}`,
Success: true,
})
callback(ChatStreamEvent{Type: "tool_end", Data: toolEndMissingData})
return &PatrolStreamResponse{
Content: "analysis complete",
InputTokens: 5,
OutputTokens: 7,
}, nil
}
func (s *streamChatService) GetMessages(ctx context.Context, sessionID string) ([]ChatMessage, error) {
return nil, nil
}
func (s *streamChatService) DeleteSession(ctx context.Context, sessionID string) error {
return nil
}
func (s *streamChatService) ReloadConfig(ctx context.Context, cfg *config.AIConfig) error {
return nil
}
func (s *streamChatService) GetExecutor() *tools.PulseToolExecutor {
return s.executor
}
func samplePatrolState() models.StateSnapshot {
return models.StateSnapshot{
Nodes: []models.Node{
{
ID: "node-1",
Name: "node-1",
Status: "online",
CPU: 0.92, // 92% — triggers triage flag so state is non-quiet
Memory: models.Memory{Usage: 20.0},
Disk: models.Disk{Usage: 10.0},
},
},
}
}
func TestComputePatrolMaxTurns(t *testing.T) {
if got := computePatrolMaxTurns(0, nil); got != patrolMinTurns {
t.Fatalf("expected min turns %d, got %d", patrolMinTurns, got)
}
if got := computePatrolMaxTurns(1000, nil); got != patrolMaxTurnsLimit {
t.Fatalf("expected max turns %d, got %d", patrolMaxTurnsLimit, got)
}
quickScope := &PatrolScope{Depth: PatrolDepthQuick}
if got := computePatrolMaxTurns(0, quickScope); got != patrolQuickMinTurns {
t.Fatalf("expected quick min turns %d, got %d", patrolQuickMinTurns, got)
}
if got := computePatrolMaxTurns(200, quickScope); got != patrolQuickMaxTurns {
t.Fatalf("expected quick max turns %d, got %d", patrolQuickMaxTurns, got)
}
}
func TestEnsureInvestigationToolCall_NoOp(t *testing.T) {
ps := NewPatrolService(nil, nil)
ctx := context.Background()
var mu sync.Mutex
completed := []ToolCallRecord{{ToolName: "pulse_query"}}
raw := []string{"existing"}
ps.ensureInvestigationToolCall(ctx, &tools.PulseToolExecutor{}, &mu, &completed, &raw, true)
if len(completed) != 1 || len(raw) != 1 {
t.Fatalf("expected no changes when investigation tool already present")
}
ps.ensureInvestigationToolCall(ctx, nil, &mu, &completed, &raw, true)
if len(completed) != 1 || len(raw) != 1 {
t.Fatalf("expected no changes when executor is nil")
}
}
func TestGetPatrolSystemPrompt_ModeSwitch(t *testing.T) {
svc := &Service{cfg: &config.AIConfig{PatrolAutoFix: true}}
ps := NewPatrolService(svc, nil)
prompt := ps.getPatrolSystemPrompt()
if !strings.Contains(prompt, "Auto-Fix Mode") || !strings.Contains(prompt, "pulse_control") {
t.Fatalf("expected auto-fix prompt, got: %s", prompt)
}
svc.cfg = &config.AIConfig{PatrolAutoFix: false}
prompt = ps.getPatrolSystemPrompt()
if !strings.Contains(prompt, "Observe Only Mode") {
t.Fatalf("expected observe-only prompt, got: %s", prompt)
}
}
func TestRunAIAnalysis_EarlyErrors(t *testing.T) {
t.Run("nil service", func(t *testing.T) {
ps := NewPatrolService(nil, nil)
_, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, models.StateSnapshot{}), nil, "")
if err == nil {
t.Fatal("expected error when aiService is nil")
}
})
t.Run("budget exceeded", func(t *testing.T) {
store := cost.NewStore(cost.DefaultMaxDays)
store.Record(cost.UsageEvent{
Provider: "openai",
RequestModel: "gpt-4o-mini",
InputTokens: 1_000_000,
OutputTokens: 0,
})
svc := &Service{
cfg: &config.AIConfig{CostBudgetUSD30d: 0.01},
costStore: store,
}
ps := NewPatrolService(svc, nil)
_, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, models.StateSnapshot{}), nil, "")
if err == nil || !strings.Contains(err.Error(), "patrol skipped") {
t.Fatalf("expected budget error, got %v", err)
}
})
t.Run("chat service nil", func(t *testing.T) {
svc := &Service{}
ps := NewPatrolService(svc, nil)
scope := &PatrolScope{NoStream: true}
_, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, samplePatrolState()), scope, "")
if err == nil || !strings.Contains(err.Error(), "chat service not available") {
t.Fatalf("expected chat service error, got %v", err)
}
})
t.Run("executor accessor missing", func(t *testing.T) {
svc := &Service{}
svc.SetChatService(&noExecutorChatService{})
ps := NewPatrolService(svc, nil)
scope := &PatrolScope{NoStream: true}
_, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, samplePatrolState()), scope, "")
if err == nil || !strings.Contains(err.Error(), "executor access") {
t.Fatalf("expected executor access error, got %v", err)
}
})
t.Run("executor nil", func(t *testing.T) {
svc := &Service{}
svc.SetChatService(&mockChatService{executor: nil})
ps := NewPatrolService(svc, nil)
scope := &PatrolScope{NoStream: true}
_, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, samplePatrolState()), scope, "")
if err == nil || !strings.Contains(err.Error(), "tool executor not available") {
t.Fatalf("expected executor nil error, got %v", err)
}
})
}
func TestPatrolSeedRetryBudgets_UsesProviderContextWindow(t *testing.T) {
err := fmt.Errorf("API error (400): {\"error\":{\"message\":\"request exceeds the available context size (8192 tokens)\",\"type\":\"exceed_context_size_error\",\"n_ctx\":8192}}")
got := patrolSeedRetryBudgets(err)
want := []int{4096, 2048, 1024}
if !reflect.DeepEqual(got, want) {
t.Fatalf("patrolSeedRetryBudgets() = %v, want %v", got, want)
}
}
func TestRunAIAnalysis_RetriesWithProviderDerivedSeedBudget(t *testing.T) {
persistence := config.NewConfigPersistence(t.TempDir())
svc := NewService(persistence, nil)
svc.cfg = &config.AIConfig{Enabled: true, PatrolModel: "mock:model"}
svc.provider = &mockProvider{}
executor := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
promptTokens := make([]int, 0, 2)
executionIDs := make([]string, 0, 2)
mockCS := &mockChatService{
executor: executor,
executePatrolStreamFunc: func(ctx context.Context, req PatrolExecuteRequest, callback ChatStreamCallback) (*PatrolStreamResponse, error) {
promptTokens = append(promptTokens, chat.EstimateTokens(req.Prompt))
executionIDs = append(executionIDs, req.ExecutionID)
if len(promptTokens) == 1 {
return nil, fmt.Errorf("API error (400): {\"error\":{\"message\":\"request exceeds the available context size (4096 tokens)\",\"type\":\"exceed_context_size_error\",\"n_ctx\":4096}}")
}
return &PatrolStreamResponse{
Content: "analysis complete",
InputTokens: 8,
OutputTokens: 4,
}, nil
},
}
svc.SetChatService(mockCS)
ps := NewPatrolService(svc, nil)
ps.SetConfig(PatrolConfig{
Enabled: true,
AnalyzeNodes: true,
AnalyzeGuests: true,
})
state := models.StateSnapshot{
Nodes: []models.Node{
{ID: "node-1", Name: "pve-1", Status: "online", CPU: 0.20, Memory: models.Memory{Usage: 30.0}},
},
}
for i := 0; i < 600; i++ {
state.VMs = append(state.VMs, models.VM{
ID: fmt.Sprintf("vm-%d", i),
VMID: 100 + i,
Name: fmt.Sprintf("flagged-guest-%03d-%s", i, strings.Repeat("x", 96)),
Node: "pve-1",
Status: "running",
CPU: 0.95,
Memory: models.Memory{Usage: 91.0},
Disk: models.Disk{Usage: 15.0},
})
}
triage := ps.runDeterministicTriageState(context.Background(), patrolRuntimeStateForTest(ps, state), nil, nil)
seed, _ := ps.buildTriageSeedContextState(triage, patrolRuntimeStateForTest(ps, state), nil, nil)
if got := chat.EstimateTokens(seed); got <= 2048 {
t.Fatalf("expected initial triage seed to exceed retry budget, got %d tokens", got)
}
result, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, state), &PatrolScope{NoStream: true}, "")
if err != nil {
t.Fatalf("runAIAnalysisState returned error: %v", err)
}
if result == nil || result.Response != "analysis complete" {
t.Fatalf("unexpected analysis result: %#v", result)
}
if len(promptTokens) != 2 {
t.Fatalf("expected 2 patrol attempts, got %d", len(promptTokens))
}
if len(executionIDs) != 2 || executionIDs[0] == "" || executionIDs[1] == "" {
t.Fatalf("expected execution IDs for each retry attempt, got %#v", executionIDs)
}
if executionIDs[0] != executionIDs[1] {
t.Fatalf("expected retry attempts to share execution ID, got %#v", executionIDs)
}
if promptTokens[1] >= promptTokens[0] {
t.Fatalf("expected retry prompt to shrink, got %d then %d tokens", promptTokens[0], promptTokens[1])
}
if promptTokens[1] > 2048 {
t.Fatalf("expected retry prompt to honor provider-derived budget, got %d tokens", promptTokens[1])
}
}
func TestSeedResourceInventory_QuietSummary(t *testing.T) {
ps := NewPatrolService(nil, nil)
cfg := DefaultPatrolConfig()
state := models.StateSnapshot{
Nodes: []models.Node{
{
ID: "node-1",
Name: "node-1",
Status: "online",
CPU: 0.10,
Memory: models.Memory{Usage: 10.0},
Disk: models.Disk{Usage: 15.0},
},
{
ID: "node-2",
Name: "node-2",
Status: "online",
CPU: 0.20,
Memory: models.Memory{Usage: 20.0},
Disk: models.Disk{Usage: 25.0},
},
},
}
out := ps.seedResourceInventoryState(patrolRuntimeStateForTest(ps, state), nil, cfg, time.Now(), true, nil)
if !strings.Contains(out, "# Nodes: All 2") {
t.Fatalf("expected quiet node summary, got: %s", out)
}
}
func TestRunAIAnalysis_StreamEvents(t *testing.T) {
executor := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
svc := &Service{
cfg: &config.AIConfig{
Model: "openai:gpt-4o-mini",
PatrolModel: "openai:gpt-4o-mini",
},
costStore: cost.NewStore(cost.DefaultMaxDays),
}
svc.SetChatService(&streamChatService{executor: executor})
ps := NewPatrolService(svc, nil)
scope := &PatrolScope{NoStream: true}
res, err := ps.runAIAnalysisState(context.Background(), patrolRuntimeStateForTest(ps, samplePatrolState()), scope, "")
if err != nil {
t.Fatalf("runAIAnalysis failed: %v", err)
}
if res == nil || res.Response == "" {
t.Fatalf("expected analysis response, got %+v", res)
}
if len(res.ToolCalls) == 0 {
t.Fatalf("expected tool calls to be recorded")
}
summary := svc.costStore.GetSummary(1)
if summary.Totals.InputTokens != 5 || summary.Totals.OutputTokens != 7 {
t.Fatalf("expected patrol usage to be recorded (5/7), got %d/%d", summary.Totals.InputTokens, summary.Totals.OutputTokens)
}
foundPatrol := false
for _, useCase := range summary.UseCases {
if useCase.UseCase == "patrol" {
foundPatrol = true
break
}
}
if !foundPatrol {
t.Fatalf("expected patrol use-case in summary, got %+v", summary.UseCases)
}
}
func TestRunEvaluationPass_RecordsCostUsage(t *testing.T) {
svc := &Service{
cfg: &config.AIConfig{
Model: "openai:gpt-4o-mini",
PatrolModel: "openai:gpt-4o-mini",
},
costStore: cost.NewStore(cost.DefaultMaxDays),
}
var seenExecutionID string
svc.SetChatService(&mockChatService{
executor: tools.NewPulseToolExecutor(tools.ExecutorConfig{}),
executePatrolStreamFunc: func(ctx context.Context, req PatrolExecuteRequest, callback ChatStreamCallback) (*PatrolStreamResponse, error) {
seenExecutionID = req.ExecutionID
return &PatrolStreamResponse{
Content: "evaluation complete",
InputTokens: 5,
OutputTokens: 7,
}, nil
},
})
ps := NewPatrolService(svc, nil)
signals := []DetectedSignal{
{
SignalType: SignalHighCPU,
ResourceID: "node-1",
ResourceName: "node-1",
ResourceType: "node",
SuggestedSeverity: "warning",
Category: "performance",
Summary: "High CPU",
Evidence: "cpu=95",
},
}
resp, err := ps.runEvaluationPass(context.Background(), nil, signals, "patrol-run-eval")
if err != nil {
t.Fatalf("runEvaluationPass failed: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
if seenExecutionID != "patrol-run-eval" {
t.Fatalf("execution_id=%q want patrol-run-eval", seenExecutionID)
}
summary := svc.costStore.GetSummary(1)
if summary.Totals.InputTokens != 5 || summary.Totals.OutputTokens != 7 {
t.Fatalf("expected eval usage to be recorded (5/7), got %d/%d", summary.Totals.InputTokens, summary.Totals.OutputTokens)
}
}
func TestSeedResourceInventory_DetailedSections(t *testing.T) {
ps := NewPatrolService(nil, nil)
cfg := DefaultPatrolConfig()
now := time.Now()
state := models.StateSnapshot{
Nodes: []models.Node{
{
ID: "node-1",
Name: "node-1",
Status: "online",
CPU: 0.55,
Memory: models.Memory{Usage: 65.0},
Disk: models.Disk{Usage: 40.0},
LoadAverage: []float64{1.2, 0.9, 0.7},
Uptime: int64(3600),
PendingUpdates: 5,
},
},
VMs: []models.VM{
{
ID: "vm-1",
VMID: 101,
Name: "vm-1",
Node: "node-1",
Status: "running",
CPU: 0.10,
Memory: models.Memory{Usage: 30.0},
Disk: models.Disk{Usage: 20.0},
LastBackup: now.Add(-2 * time.Hour),
},
},
Containers: []models.Container{
{
ID: "ct-1",
VMID: 200,
Name: "ct-1",
Node: "node-1",
Status: "stopped",
CPU: 0.05,
Memory: models.Memory{Usage: 15.0},
Disk: models.Disk{Usage: 10.0},
},
},
DockerHosts: []models.DockerHost{
{
ID: "docker-1",
Hostname: "docker-1",
Containers: []models.DockerContainer{
{Name: "web", State: "running", Health: "unhealthy"},
{Name: "db", State: "exited"},
},
},
},
PBSInstances: []models.PBSInstance{
{
Name: "pbs-1",
Datastores: []models.PBSDatastore{
{Name: "store", Usage: 55.0, Used: 550 * 1024 * 1024, Total: 1000 * 1024 * 1024},
},
},
},
}
usedBytes := int64(450 * 1024 * 1024)
totalBytes := int64(1000 * 1024 * 1024)
storeUsedBytes := int64(700 * 1024 * 1024)
storeTotalBytes := int64(1000 * 1024 * 1024)
diskSizeBytes := int64(2 * 1024 * 1024 * 1024 * 1024)
ps.SetUnifiedResourceProvider(&mockUnifiedResourceProvider{
getByTypeFunc: func(t unifiedresources.ResourceType) []unifiedresources.Resource {
if t == unifiedresources.ResourceTypeStorage {
return []unifiedresources.Resource{
{
ID: "store-1",
Name: "store-1",
Type: unifiedresources.ResourceTypeStorage,
Status: unifiedresources.StatusWarning,
Storage: &unifiedresources.StorageMeta{
Type: "zfs",
Shared: true,
IsZFS: true,
ZFSPoolState: "DEGRADED",
ZFSReadErrors: 1,
ZFSWriteErrors: 2,
ZFSChecksumErrors: 3,
},
Metrics: &unifiedresources.ResourceMetrics{
Disk: &unifiedresources.MetricValue{
Used: &storeUsedBytes,
Total: &storeTotalBytes,
Percent: 70.0,
},
},
},
}
}
if t == unifiedresources.ResourceTypePhysicalDisk {
return []unifiedresources.Resource{
{
ID: "disk-1",
Name: "disk-1",
Type: unifiedresources.ResourceTypePhysicalDisk,
Status: unifiedresources.StatusOffline,
ParentName: "node-1",
PhysicalDisk: &unifiedresources.PhysicalDiskMeta{
DevPath: "/dev/nvme0n1",
Model: "Samsung PM9A3",
DiskType: "nvme",
SizeBytes: diskSizeBytes,
Health: "FAILED",
Wearout: 12,
Temperature: 58,
},
},
}
}
if t == unifiedresources.ResourceTypeCeph {
return []unifiedresources.Resource{
{
Name: "ceph-1",
Type: unifiedresources.ResourceTypeCeph,
Ceph: &unifiedresources.CephMeta{
HealthStatus: "HEALTH_WARN",
HealthMessage: "OSD down",
NumOSDs: 3,
NumOSDsUp: 2,
NumOSDsIn: 3,
},
Metrics: &unifiedresources.ResourceMetrics{
Disk: &unifiedresources.MetricValue{
Used: &usedBytes,
Total: &totalBytes,
},
},
},
}
}
return nil
},
})
out := ps.seedResourceInventoryState(patrolRuntimeStateForTest(ps, state), nil, cfg, now, false, nil)
for _, part := range []string{
"# Node Metrics",
"# Guest Metrics",
"# Docker",
"health=unhealthy",
"# Storage",
"## Pools",
"## Physical Disks",
"/dev/nvme0n1 (Samsung PM9A3)",
"ZFS errors",
"# Ceph",
"Message: OSD down",
"# PBS Datastores",
} {
if !strings.Contains(out, part) {
t.Fatalf("expected output to contain %q, got: %s", part, out)
}
}
}
func TestSeedResourceInventoryState_UsesRuntimeReadStateForNodesAndGuests(t *testing.T) {
ps := NewPatrolService(nil, nil)
cfg := DefaultPatrolConfig()
now := time.Now()
runtimeState := newPatrolRuntimeState(models.StateSnapshot{})
nodeView := unifiedresources.NewNodeView(&unifiedresources.Resource{
ID: "node-1",
Name: "node-1",
Type: unifiedresources.ResourceTypeAgent,
Status: unifiedresources.StatusOnline,
Metrics: &unifiedresources.ResourceMetrics{
CPU: &unifiedresources.MetricValue{Percent: 55},
Memory: &unifiedresources.MetricValue{Percent: 65},
Disk: &unifiedresources.MetricValue{Percent: 40},
},
})
vmView := unifiedresources.NewVMView(&unifiedresources.Resource{
ID: "qemu/101",
Name: "vm-1",
Type: unifiedresources.ResourceTypeVM,
Status: "running",
Proxmox: &unifiedresources.ProxmoxData{
SourceID: "qemu/101",
VMID: 101,
NodeName: "node-1",
LastBackup: now.Add(-2 * time.Hour),
},
Metrics: &unifiedresources.ResourceMetrics{
CPU: &unifiedresources.MetricValue{Percent: 10},
Memory: &unifiedresources.MetricValue{Percent: 30},
Disk: &unifiedresources.MetricValue{Percent: 20},
},
})
dockerHostView := unifiedresources.NewDockerHostView(&unifiedresources.Resource{
ID: "docker-1",
Name: "docker-1",
Type: unifiedresources.ResourceTypeAgent,
Status: unifiedresources.StatusOnline,
Docker: &unifiedresources.DockerData{
Hostname: "docker-1",
},
ChildCount: 2,
})
parentID := "docker-1"
appRunning := unifiedresources.NewDockerContainerView(&unifiedresources.Resource{
ID: "dc-1",
Name: "web",
Type: unifiedresources.ResourceTypeAppContainer,
Status: unifiedresources.StatusOnline,
ParentID: &parentID,
Docker: &unifiedresources.DockerData{
ContainerState: "running",
Health: "unhealthy",
},
})
appStopped := unifiedresources.NewDockerContainerView(&unifiedresources.Resource{
ID: "dc-2",
Name: "db",
Type: unifiedresources.ResourceTypeAppContainer,
Status: unifiedresources.StatusOffline,
ParentID: &parentID,
Docker: &unifiedresources.DockerData{
ContainerState: "exited",
},
})
runtimeState.readState = &mockReadState{
nodes: []*unifiedresources.NodeView{&nodeView},
vms: []*unifiedresources.VMView{&vmView},
dockerHosts: []*unifiedresources.DockerHostView{&dockerHostView},
dockerCtrs: []*unifiedresources.DockerContainerView{&appRunning, &appStopped},
pbs: []*unifiedresources.PBSInstanceView{
func() *unifiedresources.PBSInstanceView {
pbs := unifiedresources.NewPBSInstanceView(&unifiedresources.Resource{
ID: "pbs-1",
Name: "pbs-1",
Type: unifiedresources.ResourceTypePBS,
Status: unifiedresources.StatusOnline,
PBS: &unifiedresources.PBSData{
Datastores: []unifiedresources.PBSDatastoreMeta{
{
Name: "store",
Used: 550 * 1024 * 1024,
Total: 1000 * 1024 * 1024,
UsagePercent: 55,
},
},
},
})
return &pbs
}(),
},
}
ps.SetReadState(nil)
out := ps.seedResourceInventoryState(runtimeState, nil, cfg, now, false, nil)
for _, part := range []string{
"# Node Metrics",
"| node-1 | online | 55% | 65% | 40%",
"# Guest Metrics",
"| vm-1 | VM | node-1 | - | 10% | 30% | 20% | running | - | 2h ago |",
"# Docker",
"| docker-1 | 2 | 1 | 1 |",
"docker-1/web: health=unhealthy",
"# PBS Datastores",
"pbs-1/store: 55% used",
} {
if !strings.Contains(out, part) {
t.Fatalf("expected runtime-state-backed output to contain %q, got: %s", part, out)
}
}
}
func TestSeedHealthAndAlerts_NoIssues(t *testing.T) {
ps := NewPatrolService(nil, nil)
cfg := DefaultPatrolConfig()
now := time.Now()
ps.SetUnifiedResourceProvider(&mockUnifiedResourceProvider{
getByTypeFunc: func(t unifiedresources.ResourceType) []unifiedresources.Resource {
if t == unifiedresources.ResourceTypePhysicalDisk {
return []unifiedresources.Resource{
{
Name: "disk",
Type: unifiedresources.ResourceTypePhysicalDisk,
ParentName: "node-1",
PhysicalDisk: &unifiedresources.PhysicalDiskMeta{
DevPath: "/dev/sda",
Model: "disk",
Health: "PASSED",
Wearout: 100,
Temperature: 40,
},
},
}
}
return nil
},
})
state := models.StateSnapshot{
ConnectionHealth: map[string]bool{
"node-1": true,
"node-2": true,
},
}
out := ps.seedHealthAndAlertsState(patrolRuntimeStateForTest(ps, state), nil, cfg, now)
if !strings.Contains(out, "All 1 disks healthy") {
t.Fatalf("expected healthy disk summary, got: %s", out)
}
if !strings.Contains(out, "All 2 instances connected") {
t.Fatalf("expected all connections summary, got: %s", out)
}
}
func TestSeedHealthAndAlerts_WithIssues(t *testing.T) {
ps := NewPatrolService(nil, nil)
cfg := DefaultPatrolConfig()
now := time.Now()
ps.SetUnifiedResourceProvider(&mockUnifiedResourceProvider{
getByTypeFunc: func(t unifiedresources.ResourceType) []unifiedresources.Resource {
if t == unifiedresources.ResourceTypePhysicalDisk {
return []unifiedresources.Resource{
{
Name: "disk",
Type: unifiedresources.ResourceTypePhysicalDisk,
ParentName: "node-1",
PhysicalDisk: &unifiedresources.PhysicalDiskMeta{
DevPath: "/dev/sda",
Model: "disk",
Health: "FAILED",
Wearout: 10,
Temperature: 60,
},
},
}
}
return nil
},
})
// Wire ReadState for K8s clusters and Hosts (legacy fallbacks removed).
k8sView := unifiedresources.NewK8sClusterView(&unifiedresources.Resource{
ID: "k8s-1", Name: "k1", Type: unifiedresources.ResourceTypeK8sCluster,
ChildCount: 1,
})
hostView := unifiedresources.NewHostView(&unifiedresources.Resource{
ID: "host-1", Name: "host-1", Type: unifiedresources.ResourceTypeAgent,
Agent: &unifiedresources.AgentData{Hostname: "host-1"},
})
ps.SetReadState(&mockReadState{
k8sClusters: []*unifiedresources.K8sClusterView{&k8sView},
hosts: []*unifiedresources.HostView{&hostView},
})
state := models.StateSnapshot{
ActiveAlerts: []models.Alert{
{Level: "warning", Message: "CPU high", StartTime: now.Add(-time.Hour)},
},
RecentlyResolved: []models.ResolvedAlert{
{Alert: models.Alert{Message: "Disk alert"}, ResolvedTime: now.Add(-2 * time.Hour)},
},
ConnectionHealth: map[string]bool{
"node-1": true,
"node-2": false,
},
KubernetesClusters: []models.KubernetesCluster{
{ID: "k8s-1", Name: "k1", Nodes: []models.KubernetesNode{{Name: "n1"}}},
},
}
out := ps.seedHealthAndAlertsState(patrolRuntimeStateForTest(ps, state), nil, cfg, now)
for _, part := range []string{
"# Disk Health",
"/dev/sda",
"# Active Alerts",
"# Recently Resolved Alerts",
"# Connections",
"Disconnected: node-2",
"# Kubernetes Clusters",
"# Hosts",
} {
if !strings.Contains(out, part) {
t.Fatalf("expected output to contain %q, got: %s", part, out)
}
}
}
func TestSeedHealthAndAlertsState_UsesRuntimeStateProviders(t *testing.T) {
ps := NewPatrolService(nil, nil)
cfg := DefaultPatrolConfig()
now := time.Now()
runtimeState := newPatrolRuntimeState(models.StateSnapshot{
ConnectionHealth: map[string]bool{
"node-1": true,
"node-2": false,
},
KubernetesClusters: []models.KubernetesCluster{
{ID: "k8s-1", Name: "k1", Nodes: []models.KubernetesNode{{Name: "n1"}}},
},
})
k8sView := unifiedresources.NewK8sClusterView(&unifiedresources.Resource{
ID: "k8s-1", Name: "k1", Type: unifiedresources.ResourceTypeK8sCluster,
ChildCount: 1,
})
hostView := unifiedresources.NewHostView(&unifiedresources.Resource{
ID: "host-1", Name: "host-1", Type: unifiedresources.ResourceTypeAgent,
Agent: &unifiedresources.AgentData{Hostname: "host-1"},
})
runtimeState.readState = &mockReadState{
k8sClusters: []*unifiedresources.K8sClusterView{&k8sView},
hosts: []*unifiedresources.HostView{&hostView},
}
runtimeState.unifiedResourceProvider = &mockUnifiedResourceProvider{
getByTypeFunc: func(t unifiedresources.ResourceType) []unifiedresources.Resource {
if t != unifiedresources.ResourceTypePhysicalDisk {
return nil
}
return []unifiedresources.Resource{
{
Name: "disk",
Type: unifiedresources.ResourceTypePhysicalDisk,
ParentName: "node-1",
PhysicalDisk: &unifiedresources.PhysicalDiskMeta{
DevPath: "/dev/sda",
Model: "disk",
Health: "FAILED",
Wearout: 10,
Temperature: 60,
},
},
}
},
}
// Clear service-level providers to prove this path uses the captured runtime state.
ps.SetReadState(nil)
ps.SetUnifiedResourceProvider(nil)
out := ps.seedHealthAndAlertsState(runtimeState, nil, cfg, now)
for _, part := range []string{
"# Disk Health",
"/dev/sda",
"# Connections",
"Disconnected: node-2",
"# Kubernetes Clusters",
"# Hosts",
} {
if !strings.Contains(out, part) {
t.Fatalf("expected runtime-state-backed output to contain %q, got: %s", part, out)
}
}
}
func TestSeedIntelligenceContext_EmptyAnomalies(t *testing.T) {
ps := NewPatrolService(nil, nil)
out := ps.seedIntelligenceContext(seedIntelligence{hasBaselineStore: true}, time.Now())
if !strings.Contains(out, "No anomalies detected") {
t.Fatalf("expected no anomalies message, got: %s", out)
}
}
func TestSeedIntelligenceContext_WithData(t *testing.T) {
ps := NewPatrolService(nil, nil)
now := time.Now()
intel := seedIntelligence{
hasBaselineStore: true,
anomalies: []baseline.AnomalyReport{
{
ResourceID: "node-1",
ResourceName: "node-1",
Metric: "cpu",
CurrentValue: 0.9,
BaselineMean: 0.4,
ZScore: 2.5,
Severity: baseline.AnomalyHigh,
},
},
forecasts: []seedForecast{
{name: "node-1", metric: "memory", severity: "warning", daysToFull: 10, dailyChange: 2.5, current: 75.0},
},
predictions: []FailurePrediction{
{ResourceID: "node-1", EventType: "restart", DaysUntil: 3, Confidence: 0.9, Basis: "pattern"},
},
recentChanges: []memory.Change{
{ResourceID: "vm-1", ResourceType: "vm", Description: "config changed", DetectedAt: now.Add(-time.Hour)},
},
correlations: []*Correlation{
{SourceID: "node-1", TargetID: "vm-1", EventPattern: "cpu_high -> restart", AvgDelay: 10 * time.Minute, Confidence: 0.8, Occurrences: 4},
},
}
out := ps.seedIntelligenceContext(intel, now)
for _, part := range []string{
"# Anomalies",
"# Capacity Forecasts",
"# Failure Predictions",
"# Recent Infrastructure Changes",
"# Known Resource Correlations",
} {
if !strings.Contains(out, part) {
t.Fatalf("expected output to contain %q, got: %s", part, out)
}
}
}