Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

44.2 KB
Raw Blame
package ai

import (
	"context"
	"encoding/json"
	"fmt"
	"reflect"
	"strings"
	"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"
	"github.com/rcourtman/pulse-go-rewrite/pkg/diskinventory"
)

type noExecutorChatService struct{}

func TestServicePatrolAutopilotPolicyProviderControlsRuntimeVersion(t *testing.T) {
	now := time.Date(2026, 7, 11, 20, 0, 0, 0, time.UTC)
	actor := unifiedresources.ActionActor{SubjectID: "admin-one", Kind: unifiedresources.ActionActorUser, CredentialID: "session:one", OrgID: "org-a"}
	v1Policy := unifiedresources.PatrolAutopilotServerPolicy{CurrentVersion: unifiedresources.PatrolAutopilotAcknowledgementVersionV1, Now: now}
	v1, _, err := unifiedresources.IssuePatrolAutopilotAcknowledgement(nil, "ack-runtime-v1", actor, v1Policy)
	if err != nil {
		t.Fatal(err)
	}
	v2Policy := unifiedresources.PatrolAutopilotServerPolicy{CurrentVersion: unifiedresources.PatrolAutopilotAcknowledgementVersionV2, Now: now.Add(time.Hour)}
	v2, _, err := unifiedresources.IssuePatrolAutopilotAcknowledgement([]unifiedresources.PatrolAutopilotAcknowledgement{v1}, "ack-runtime-v2", actor, v2Policy)
	if err != nil {
		t.Fatal(err)
	}
	activation, _, err := unifiedresources.BindPatrolAutopilotActivation([]unifiedresources.PatrolAutopilotAcknowledgement{v1, v2}, nil, nil, v2.ID, actor, v2Policy)
	if err != nil {
		t.Fatal(err)
	}

	svc := NewService(nil, nil)
	svc.SetOrgID("org-a")
	svc.cfg = config.NewDefaultAIConfig()
	svc.cfg.PatrolAutonomyLevel = config.PatrolAutonomyFull
	svc.cfg.PatrolAutopilotAcknowledgements = []unifiedresources.PatrolAutopilotAcknowledgement{v1, v2}
	svc.cfg.PatrolAutopilotActivation = &activation
	svc.SetPatrolAutopilotServerPolicyProvider(func() unifiedresources.PatrolAutopilotServerPolicy { return v2Policy })
	if effective := svc.GetEffectivePatrolAutonomyLevel(); effective != config.PatrolAutonomyFull {
		t.Fatalf("V2 runtime activation effective=%q", effective)
	}
	svc.SetPatrolAutopilotServerPolicyProvider(func() unifiedresources.PatrolAutopilotServerPolicy { return v1Policy })
	if effective := svc.GetEffectivePatrolAutonomyLevel(); effective != config.PatrolAutonomyApproval {
		t.Fatalf("runtime did not fail closed after current-version change: effective=%q", effective)
	}
}

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 TestGetPatrolSystemPrompt_ModeSwitch(t *testing.T) {
	fullModeConfig := func() *config.AIConfig {
		now := time.Now().UTC()
		policy := unifiedresources.CurrentPatrolAutopilotServerPolicy(now)
		actor := unifiedresources.ActionActor{SubjectID: "admin", Kind: unifiedresources.ActionActorUser, CredentialID: "session:test", OrgID: "default"}
		acknowledgement, _, err := unifiedresources.IssuePatrolAutopilotAcknowledgement(nil, "ack-prompt-full", actor, policy)
		if err != nil {
			t.Fatal(err)
		}
		activation, _, err := unifiedresources.BindPatrolAutopilotActivation([]unifiedresources.PatrolAutopilotAcknowledgement{acknowledgement}, nil, nil, acknowledgement.ID, actor, policy)
		if err != nil {
			t.Fatal(err)
		}
		return &config.AIConfig{
			PatrolAutonomyLevel:             config.PatrolAutonomyFull,
			PatrolFullModeUnlocked:          true,
			PatrolAutopilotAcknowledgements: []unifiedresources.PatrolAutopilotAcknowledgement{acknowledgement},
			PatrolAutopilotActivation:       &activation,
		}
	}
	tests := []struct {
		name       string
		cfg        *config.AIConfig
		contains   []string
		notContain []string
	}{
		{
			name: "monitor",
			cfg:  &config.AIConfig{PatrolAutonomyLevel: config.PatrolAutonomyMonitor},
			contains: []string{
				"Patrol Control Mode: Watch Only",
				"Do not investigate through remediation flows, queue approvals, or execute any infrastructure-changing action.",
			},
		},
		{
			name: "approval",
			cfg:  &config.AIConfig{PatrolAutonomyLevel: config.PatrolAutonomyApproval},
			contains: []string{
				"Patrol Control Mode: Ask First",
				"Do not execute remediation commands in this mode.",
				"queue it for operator approval",
			},
		},
		{
			name: "assisted",
			cfg:  &config.AIConfig{PatrolAutonomyLevel: config.PatrolAutonomyAssisted},
			contains: []string{
				"Patrol Control Mode: Safe Auto-Fix",
				"execute only low-risk, policy-approved fixes automatically",
				"Queue approval for destructive, high-risk, critical, ambiguous, restricted, or never-auto-remediate actions.",
			},
		},
		{
			name: "full",
			cfg:  fullModeConfig(),
			contains: []string{
				"Patrol Control Mode: Autopilot",
				"execute policy-approved fixes automatically, including critical fixes",
				"Approval boundaries still win",
			},
		},
		{
			name: "locked full downgrades to ask first",
			cfg: &config.AIConfig{
				PatrolAutonomyLevel:    config.PatrolAutonomyFull,
				PatrolFullModeUnlocked: false,
			},
			contains: []string{
				"Patrol Control Mode: Ask First",
			},
			notContain: []string{
				"Patrol Control Mode: Autopilot",
			},
		},
		{
			name: "legacy autofix flag does not overstate autonomy",
			cfg: &config.AIConfig{
				PatrolAutoFix: true,
			},
			contains: []string{
				"Patrol Control Mode: Watch Only",
			},
			notContain: []string{
				"Patrol Control Mode: Autopilot",
				"Auto-Fix Mode",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			svc := &Service{cfg: tt.cfg, orgID: "default"}
			ps := NewPatrolService(svc, nil)
			prompt := ps.getPatrolSystemPrompt()

			for _, want := range tt.contains {
				if !strings.Contains(prompt, want) {
					t.Fatalf("prompt missing %q:\n%s", want, prompt)
				}
			}
			for _, forbidden := range tt.notContain {
				if strings.Contains(prompt, forbidden) {
					t.Fatalf("prompt contained forbidden %q:\n%s", forbidden, prompt)
				}
			}
			if strings.Contains(prompt, "Observe Only Mode") {
				t.Fatalf("prompt should use Patrol control-mode vocabulary, got legacy observe-only wording:\n%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 TestRunAIAnalysis_PreservesPartialEvidenceOnProviderError(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{})
	mockCS := &mockChatService{
		executor: executor,
		executePatrolStreamFunc: func(ctx context.Context, req PatrolExecuteRequest, callback ChatStreamCallback) (*PatrolStreamResponse, error) {
			creator := executor.GetPatrolFindingCreator()
			if creator == nil {
				return nil, fmt.Errorf("patrol finding creator not set")
			}
			if _, _, err := creator.CreateFinding(tools.PatrolFindingInput{
				Key:          "container-unhealthy",
				Severity:     "warning",
				Category:     "reliability",
				ResourceID:   "container-1",
				ResourceName: "faulty",
				ResourceType: "app-container",
				Title:        "Container health check failed",
				Description:  "The container is unhealthy.",
				Evidence:     "Docker reports health=unhealthy.",
			}); err != nil {
				return nil, err
			}

			start, _ := json.Marshal(map[string]string{
				"id": "call-1", "name": "pulse_query", "input": `{"action":"get"}`,
			})
			callback(ChatStreamEvent{Type: "tool_start", Data: start})
			end, _ := json.Marshal(map[string]any{
				"id": "call-1", "name": "pulse_query", "input": `{"action":"get"}`,
				"output": `{"health":"unhealthy"}`, "success": true,
			})
			callback(ChatStreamEvent{Type: "tool_end", Data: end})

			return &PatrolStreamResponse{
				Content:      "Partial investigation completed before the provider failed.",
				InputTokens:  30_049,
				OutputTokens: 1_686,
			}, fmt.Errorf("provider interrupted after partial output")
		},
	}
	svc.SetChatService(mockCS)

	ps := NewPatrolService(svc, nil)
	ps.SetConfig(PatrolConfig{Enabled: true, AnalyzeDocker: true})
	state := models.StateSnapshot{DockerHosts: []models.DockerHost{{
		ID: "docker-1", Hostname: "colima", Containers: []models.DockerContainer{{
			ID: "container-1", Name: "faulty", State: "running", Status: "unhealthy",
		}},
	}}}

	result, err := ps.runAIAnalysisState(
		context.Background(),
		patrolRuntimeStateForTest(ps, state),
		&PatrolScope{NoStream: true, Depth: PatrolDepthQuick},
		"execution-1",
	)
	if err == nil || !strings.Contains(err.Error(), "provider interrupted") {
		t.Fatalf("expected provider error, got %v", err)
	}
	if result == nil {
		t.Fatal("expected partial analysis result alongside provider error")
	}
	if result.InputTokens != 30_049 || result.OutputTokens != 1_686 {
		t.Fatalf("partial tokens = %d/%d", result.InputTokens, result.OutputTokens)
	}
	if len(result.ToolCalls) != 1 || result.ToolCalls[0].ToolName != "pulse_query" || !result.ToolCalls[0].Success {
		t.Fatalf("partial tool calls = %+v", result.ToolCalls)
	}
	if len(result.Findings) != 1 || result.Findings[0].ResourceID != "container-1" {
		t.Fatalf("partial findings = %+v", result.Findings)
	}
	if !strings.Contains(result.Response, "Partial investigation") {
		t.Fatalf("partial response = %q", result.Response)
	}
}

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 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, Observation: models.MemoryObservation{State: "current", Source: "available-field", ObservedAt: now}},
			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",
		fmt.Sprintf("| vm-1 | VM | node-1 | 10%% | 30%% (current; source available-field; observed %s; cache-aware guest usage) | 20%% | running | - | 2h ago |", now.UTC().Format(time.RFC3339)),
		"# 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 (SMART PASSED/OK).") {
		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)
	}
}

// Patrol's seed must not tell the model a node recovered when its alert only
// moved to the node's Pulse agent.
func TestSeedHealthAndAlerts_HandoverIsNotARecovery(t *testing.T) {
	ps := NewPatrolService(nil, nil)
	cfg := DefaultPatrolConfig()
	now := time.Now()
	summary := "Alert moved to pve1 (Host Agent). This is not a recovery: check the agent for the current reading."
	state := models.StateSnapshot{
		RecentlyResolved: []models.ResolvedAlert{
			{
				Alert: models.Alert{
					ResourceName: "pve1",
					Message:      "Memory usage at 95%",
					Resolution:   &models.AlertResolution{Reason: "moved_to_agent", Summary: summary},
				},
				ResolvedTime: now.Add(-3 * time.Minute),
			},
			{
				Alert:        models.Alert{ResourceName: "pve2", Message: "CPU usage at 90%"},
				ResolvedTime: now.Add(-5 * time.Minute),
			},
		},
	}

	out := ps.seedHealthAndAlertsState(patrolRuntimeStateForTest(ps, state), nil, cfg, now)
	if !strings.Contains(out, "- pve1: Memory usage at 95% — closed 3m ago. "+summary+"\n") {
		t.Fatalf("expected the handover line, got: %s", out)
	}
	if strings.Contains(out, "pve1: Memory usage at 95% — resolved") {
		t.Fatalf("handover reported as a recovery: %s", out)
	}
	if !strings.Contains(out, "- pve2: CPU usage at 90% — resolved 5m ago\n") {
		t.Fatalf("expected the ordinary recovery line, got: %s", out)
	}
}

func TestSeedHealthAndAlerts_UnknownDiskHealthDoesNotClaimSMARTPassed(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 nil
			}
			return []unifiedresources.Resource{
				{
					Name:       "disk",
					Type:       unifiedresources.ResourceTypePhysicalDisk,
					ParentName: "node-1",
					Status:     unifiedresources.StatusOnline,
					PhysicalDisk: &unifiedresources.PhysicalDiskMeta{
						DevPath:     "/dev/sda",
						Model:       "disk",
						Health:      "UNKNOWN",
						Wearout:     100,
						Temperature: 40,
					},
				},
			}
		},
	})

	out := ps.seedHealthAndAlertsState(patrolRuntimeStateForTest(ps, models.StateSnapshot{}), nil, cfg, now)
	if !strings.Contains(out, "No disk issues detected across 1 disks; SMART health is unknown for 1 disk(s).") {
		t.Fatalf("expected unknown SMART health summary, got: %s", out)
	}
	if strings.Contains(out, "SMART PASSED") {
		t.Fatalf("unknown SMART health must not be reported as passed, got: %s", out)
	}
}

func TestSeedHealthAndAlerts_SMARTAttributeIssue(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 nil
			}
			return []unifiedresources.Resource{
				{
					Name:       "disk",
					Type:       unifiedresources.ResourceTypePhysicalDisk,
					ParentName: "node-1",
					Status:     unifiedresources.StatusOnline,
					PhysicalDisk: &unifiedresources.PhysicalDiskMeta{
						DevPath:     "/dev/sda",
						Model:       "disk",
						Health:      "PASSED",
						Wearout:     100,
						Temperature: 40,
						SMART: &unifiedresources.SMARTMeta{
							PendingSectors: patrolSMARTInt64Pointer(2),
						},
					},
				},
			}
		},
	})

	out := ps.seedHealthAndAlertsState(patrolRuntimeStateForTest(ps, models.StateSnapshot{}), nil, cfg, now)
	if strings.Contains(out, "All 1 disks healthy") {
		t.Fatalf("SMART counter evidence must not be summarized as all healthy, got: %s", out)
	}
	if !strings.Contains(out, "SMART Evidence") || !strings.Contains(out, "pending sectors=2") {
		t.Fatalf("expected SMART counter evidence in disk health table, got: %s", out)
	}
}

func patrolSMARTInt64Pointer(value int64) *int64 { return &value }

func TestPatrolPhysicalDiskHealthIssueDistinguishesKnownZeroWearout(t *testing.T) {
	if !patrolPhysicalDiskHealthIssue(patrolPhysicalDiskRow{
		diskType: "nvme",
		health:   "PASSED",
		wearout:  0,
	}) {
		t.Fatal("explicit zero NVMe life should be a health issue")
	}
	if patrolPhysicalDiskHealthIssue(patrolPhysicalDiskRow{
		diskType: "sata",
		health:   "UNKNOWN",
		wearout:  0,
	}) {
		t.Fatal("unreported SATA wearout should remain neutral")
	}
}

// A retained last-known temperature (a disk in standby, a host agent past its
// reporting lease) is context, not heat. Patrol's disk issue gate, triage flags
// and finding verification judge only a collected reading, and both seed
// tables label the retained value as last known.
func TestPatrolPhysicalDiskRowsJudgeHeatOnCollectedTemperature(t *testing.T) {
	standby := &diskinventory.CollectionStatus{Temperature: diskinventory.Unavailable("smartctl", "disk is in standby")}
	collected := &diskinventory.CollectionStatus{Temperature: diskinventory.Available("smartctl")}
	unified := newPatrolRuntimeState(models.StateSnapshot{})
	unified.unifiedResourceProvider = &mockUnifiedResourceProvider{
		getByTypeFunc: func(t unifiedresources.ResourceType) []unifiedresources.Resource {
			if t != unifiedresources.ResourceTypePhysicalDisk {
				return nil
			}
			return []unifiedresources.Resource{
				{ID: "disk-standby", Name: "standby", Type: unifiedresources.ResourceTypePhysicalDisk, Status: unifiedresources.StatusOnline, ParentName: "tower",
					PhysicalDisk: &unifiedresources.PhysicalDiskMeta{DevPath: "/dev/sda", Model: "WDC WD80EFAX", DiskType: "sata", Health: "PASSED", Wearout: -1, Temperature: 61, Collection: standby}},
				{ID: "disk-hot", Name: "hot", Type: unifiedresources.ResourceTypePhysicalDisk, Status: unifiedresources.StatusOnline, ParentName: "tower",
					PhysicalDisk: &unifiedresources.PhysicalDiskMeta{DevPath: "/dev/sdb", Model: "WDC WD80EFAX", DiskType: "sata", Health: "PASSED", Wearout: -1, Temperature: 58, Collection: collected}},
			}
		},
	}
	legacy := newPatrolRuntimeState(models.StateSnapshot{PhysicalDisks: []models.PhysicalDisk{
		{ID: "disk-standby", Node: "tower", DevPath: "/dev/sda", Model: "WDC WD80EFAX", Type: "sata", Health: "PASSED", Wearout: -1, Temperature: 61, Collection: standby},
		{ID: "disk-hot", Node: "tower", DevPath: "/dev/sdb", Model: "WDC WD80EFAX", Type: "sata", Health: "PASSED", Wearout: -1, Temperature: 58, Collection: collected},
	}})

	for name, snap := range map[string]patrolRuntimeState{"unified": unified, "state": legacy} {
		t.Run(name, func(t *testing.T) {
			byID := map[string]patrolPhysicalDiskRow{}
			for _, row := range patrolPhysicalDiskRows(snap, nil) {
				byID[row.id] = row
			}
			if patrolPhysicalDiskHealthIssue(byID["disk-standby"]) {
				t.Fatalf("a standby disk's retained 61C is not a health issue: %+v", byID["disk-standby"])
			}
			if !patrolPhysicalDiskHealthIssue(byID["disk-hot"]) {
				t.Fatalf("a collected 58C is a health issue: %+v", byID["disk-hot"])
			}
			flags := triageDiskHealthChecksState(snap, nil)
			if len(flags) != 1 || !strings.Contains(flags[0].Reason, "Disk temperature 58") {
				t.Fatalf("triage flags = %+v, want only the collected 58C disk", flags)
			}
			if verification, ok := patrolLookupPhysicalDiskVerificationState(snap, "disk-standby"); !ok || verification.temperature != 0 {
				t.Fatalf("standby disk verification = %+v (found %v), want no temperature", verification, ok)
			}
		})
	}

	ps := NewPatrolService(nil, nil)
	cfg := DefaultPatrolConfig()
	now := time.Now()
	health := ps.seedHealthAndAlertsState(unified, nil, cfg, now)
	for _, part := range []string{"| last known 61°C (disk is in standby) |", "| 58°C |"} {
		if !strings.Contains(health, part) {
			t.Fatalf("disk health table missing %q:\n%s", part, health)
		}
	}
	inventory := ps.seedResourceInventoryState(unified, nil, cfg, now, false, nil)
	for _, part := range []string{"| last known 61C (disk is in standby) |", "| 58C |"} {
		if !strings.Contains(inventory, part) {
			t.Fatalf("physical disk inventory missing %q:\n%s", part, inventory)
		}
	}
}

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)
		}
	}
}

func TestSeedAlertSubjectNamesTheResourceOnce(t *testing.T) {
	tests := []struct {
		alert models.Alert
		want  string
	}{
		{
			alert: models.Alert{ResourceName: "Edge Stateful", Message: "Network packet loss above threshold"},
			want:  "Edge Stateful: Network packet loss above threshold",
		},
		{
			alert: models.Alert{ResourceName: "Field Office Edge 01", Message: "Docker host 'Field Office Edge 01' is offline"},
			want:  "Docker host 'Field Office Edge 01' is offline",
		},
		{alert: models.Alert{Message: "CPU high"}, want: "CPU high"},
	}
	for _, test := range tests {
		if got := seedAlertSubject(test.alert); got != test.want {
			t.Fatalf("seedAlertSubject(%+v) = %q, want %q", test.alert, got, test.want)
		}
	}
}