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.

104 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen, waitFor, within } from '@solidjs/testing-library';
import { Suspense, type JSX } from 'solid-js';
import { resetCreateNonSuspendingQueryCacheForTest } from '@/hooks/createNonSuspendingQuery';
import {
  PATROL_CONTROL_ANCHOR,
  PATROL_CONTROL_STARTER,
  PATROL_CONTROL_STARTER_QUERY_PARAM,
  PATROL_OPERATIONS_LOOP_ANCHOR,
} from '@/routing/resourceLinks';
import { resetAIRuntimeState } from '@/stores/aiRuntimeState';
import {
  getPublicPricingUrl,
  SELF_HOSTED_PRO_BILLING_PLAN_SELECTION_HREF,
} from '@/utils/pricingHandoff';
import patrolIntelligenceSurfaceSource from '@/features/patrol/PatrolIntelligenceSurface.tsx?raw';
import { AGENT_PATROL_CONTROL_STATUS_PATH } from '@/api/agentCapabilities';
import { aiIntelligenceStore } from '@/stores/aiIntelligence';
import { PATROL_MANUAL_SYNC_TIMEOUT_MS } from '@/features/patrol/usePatrolIntelligenceState';
import { getPatrolSetupAction } from '@/utils/patrolRuntimeActions';

import { AIIntelligence } from '../AIIntelligence';

const { findingsPanelState, runHistoryState, intelligenceState } = vi.hoisted(() => ({
  findingsPanelState: {
    latestProps: null as {
      filterOverride?: string;
      filterFindingIds?: string[];
      scopeResourceIds?: string[];
      scopeResourceTypes?: string[];
      showScopeWarnings?: boolean;
      nextPatrolAt?: string;
      lastPatrolAt?: string;
      lastPatrolLabel?: string;
      patrolIntervalMs?: number;
      runSnapshotId?: string;
    } | null,
  },
  runHistoryState: {
    selection: null as Record<string, unknown> | null,
  },
  intelligenceState: {
    findings: [] as Array<Record<string, unknown>>,
    summary: null as {
      timestamp: string;
      overall_health: {
        score: number;
        grade: 'A' | 'B' | 'C' | 'D' | 'F';
        trend: 'improving' | 'stable' | 'declining';
        factors: Array<Record<string, unknown>>;
        prediction: string;
      };
      findings_count: {
        critical: number;
        warning: number;
        watch: number;
        info: number;
        total: number;
      };
      predictions_count: number;
      recent_changes_count: number;
      recent_changes?: Array<{
        id: string;
        observedAt: string;
        resourceId: string;
        kind: string;
        sourceType: string;
        sourceAdapter?: string;
        confidence: string;
        reason?: string;
        relatedResources?: string[];
      }>;
      policy_posture?: {
        total_resources: number;
        sensitivity_counts: Record<string, number>;
        routing_counts: Record<string, number>;
        redaction_counts?: Record<string, number>;
      };
      learning: {
        resources_with_knowledge: number;
        total_notes: number;
        resources_with_baselines: number;
        patterns_detected: number;
        correlations_learned: number;
        incidents_tracked: number;
      };
    } | null,
  },
}));

const getPatrolStatusMock = vi.fn();
const getPatrolAutonomySettingsMock = vi.fn();
const updatePatrolAutonomySettingsMock = vi.fn();
const createPatrolAutopilotAcknowledgementMock = vi.fn();
const triggerPatrolRunMock = vi.fn();
const getPatrolRunHistoryMock = vi.fn();
const apiFetchJSONMock = vi.fn();
const hasFeatureMock = vi.fn();
const runtimeCapabilitiesMock = vi.fn();
const getRuntimeCapabilityBlockMock = vi.fn();
const licenseStatusMock = vi.fn();
const loadLicenseStatusMock = vi.fn();
const loadCommercialPostureMock = vi.fn();
const getUpgradeActionDestinationMock = vi.fn();
const getUpgradeActionUrlOrFallbackMock = vi.fn();
const presentationPolicyHidesUpgradePromptsMock = vi.fn();
const presentationPolicyHidesCommercialSurfacesMock = vi.fn();

// Workspace views are routes, so the mocked location follows navigate().
const routerLocation = vi.hoisted(() => ({ setPath: (_path: string) => {} }));
vi.mock('@solidjs/router', async () => {
  const { createSignal } = await import('solid-js');
  const [pathname, setPathname] = createSignal('/patrol');
  routerLocation.setPath = setPathname;
  return {
    A: (props: {
      href: string;
      class?: string;
      children?: JSX.Element;
      'aria-label'?: string;
      title?: string;
    }) => (
      <a href={props.href} class={props.class} aria-label={props['aria-label']} title={props.title}>
        {props.children}
      </a>
    ),
    useLocation: () => ({
      hash: '',
      get pathname() {
        return pathname();
      },
      search: '',
    }),
    useNavigate: () => (to: string) => setPathname(to.split('?')[0]),
  };
});
const notificationSuccessMock = vi.fn();
const notificationErrorMock = vi.fn();
const recordWorkflowPromptActivityMock = vi.fn();

vi.mock('@/api/patrol', () => ({
  getPatrolStatus: (...args: unknown[]) => getPatrolStatusMock(...args),
  getPatrolAutonomySettings: (...args: unknown[]) => getPatrolAutonomySettingsMock(...args),
  updatePatrolAutonomySettings: (...args: unknown[]) => updatePatrolAutonomySettingsMock(...args),
  createPatrolAutopilotAcknowledgement: (...args: unknown[]) =>
    createPatrolAutopilotAcknowledgementMock(...args),
  revokePatrolAutopilotAcknowledgement: vi.fn(),
  triggerPatrolRun: (...args: unknown[]) => triggerPatrolRunMock(...args),
  getPatrolRunHistory: (...args: unknown[]) => getPatrolRunHistoryMock(...args),
  getPatrolObjectives: vi.fn().mockResolvedValue([]),
  PATROL_DIGEST_DEFAULT_DAYS: 7,
  getPatrolDigest: vi.fn().mockResolvedValue({
    generated_at: '2026-09-01T12:00:00Z',
    window: {
      start: '2026-08-25T12:00:00Z',
      end: '2026-09-01T12:00:00Z',
      days: 7,
      history_complete: true,
    },
    mode: 'monitor',
    runs: {
      total: 0,
      scheduled: 0,
      event_triggered: 0,
      manual: 0,
      failed: 0,
      checks: 0,
      resources_covered: 0,
    },
    findings: {
      new: 0,
      open_by_severity: { critical: 0, warning: 0, watch: 0, info: 0 },
      resolved: 0,
      auto_resolved: 0,
      dismissed: 0,
      suppressed: 0,
    },
    investigations: { total: 0, by_outcome: {} },
    actions: {
      proposed: 0,
      approved: 0,
      rejected: 0,
      executed: 0,
      verified: 0,
      failed: 0,
      pending: 0,
    },
    alerts: { reviewed: 0 },
    spend: { estimated_usd: 0, pricing_known: true, input_tokens: 0, output_tokens: 0, calls: 0 },
  }),
  createPatrolObjective: vi.fn(),
  updatePatrolObjective: vi.fn(),
  deletePatrolObjective: vi.fn(),
}));

vi.mock('@/api/ai', () => ({
  AIAPI: {
    getModels: (...args: unknown[]) => apiFetchJSONMock('/api/ai/models', ...args),
    getSettings: (...args: unknown[]) => apiFetchJSONMock('/api/settings/ai', ...args),
    updateSettings: (...args: unknown[]) => apiFetchJSONMock('/api/settings/ai/update', ...args),
  },
}));

vi.mock('@/api/workflowPrompts', () => ({
  recordWorkflowPromptActivity: (...args: unknown[]) => recordWorkflowPromptActivityMock(...args),
  PULSE_OPERATIONS_LOOP_WORKFLOW_PROMPT_NAME: 'pulse_operations_loop',
  PULSE_PATROL_CONTROL_WORKFLOW_PROMPT_SURFACE: 'patrol_control',
}));

vi.mock('@/utils/apiClient', () => ({
  apiFetchJSON: (...args: unknown[]) => apiFetchJSONMock(...args),
  getOrgID: () => '',
}));

vi.mock('@/hooks/useResources', () => ({
  useResources: () => ({ resources: () => [] }),
}));

vi.mock('@/stores/license', () => ({
  hasFeature: (...args: unknown[]) => hasFeatureMock(...args),
  getRuntimeCapabilityBlock: (...args: unknown[]) => getRuntimeCapabilityBlockMock(...args),
  runtimeCapabilities: (...args: unknown[]) => runtimeCapabilitiesMock(...args),
  loadRuntimeCapabilities: (...args: unknown[]) => loadLicenseStatusMock(...args),
}));

vi.mock('@/stores/licenseCommercial', () => ({
  getUpgradeActionDestination: (...args: unknown[]) => getUpgradeActionDestinationMock(...args),
  commercialPosture: (...args: unknown[]) => licenseStatusMock(...args),
  licenseStatus: (...args: unknown[]) => licenseStatusMock(...args),
  loadCommercialPosture: (...args: unknown[]) => loadCommercialPostureMock(...args),
  loadRuntimeCapabilities: (...args: unknown[]) => loadLicenseStatusMock(...args),
  getUpgradeActionUrlOrFallback: (...args: unknown[]) => getUpgradeActionUrlOrFallbackMock(...args),
}));

vi.mock('@/stores/sessionPresentationPolicy', () => ({
  presentationPolicyHidesCommercialSurfaces: () => presentationPolicyHidesCommercialSurfacesMock(),
  presentationPolicyHidesUpgradePrompts: () => presentationPolicyHidesUpgradePromptsMock(),
  presentationPolicyIsReadOnly: () => false,
}));

vi.mock('@/stores/notifications', () => ({
  notificationStore: {
    success: (...args: unknown[]) => notificationSuccessMock(...args),
    error: (...args: unknown[]) => notificationErrorMock(...args),
  },
}));

vi.mock('@/stores/aiIntelligence', () => {
  const store = {
    loadFindings: vi.fn().mockResolvedValue(undefined),
    loadPatrolFindings: vi.fn().mockResolvedValue(undefined),
    loadIntelligenceSummary: vi.fn().mockResolvedValue(undefined),
    loadPendingApprovals: vi.fn().mockResolvedValue(undefined),
    loadDashboardData: vi.fn().mockImplementation(async () => {
      await Promise.all([
        store.loadFindings(),
        store.loadIntelligenceSummary(),
        store.loadPendingApprovals(),
      ]);
    }),
    get findings() {
      return intelligenceState.findings;
    },
    get patrolFindings() {
      return intelligenceState.findings;
    },
    get intelligenceSummary() {
      return intelligenceState.summary;
    },
    get patrolPendingApprovals() {
      return [];
    },
  };

  return {
    aiIntelligenceStore: store,
  };
});

vi.mock('@/components/AI/FindingsPanel', () => ({
  FindingsPanel: (props: Record<string, unknown>) => {
    findingsPanelState.latestProps = {
      filterOverride: typeof props.filterOverride === 'string' ? props.filterOverride : undefined,
      filterFindingIds: Array.isArray(props.filterFindingIds)
        ? [...(props.filterFindingIds as string[])]
        : undefined,
      scopeResourceIds: Array.isArray(props.scopeResourceIds)
        ? [...(props.scopeResourceIds as string[])]
        : undefined,
      scopeResourceTypes: Array.isArray(props.scopeResourceTypes)
        ? [...(props.scopeResourceTypes as string[])]
        : undefined,
      showScopeWarnings:
        typeof props.showScopeWarnings === 'boolean' ? props.showScopeWarnings : undefined,
      nextPatrolAt: typeof props.nextPatrolAt === 'string' ? props.nextPatrolAt : undefined,
      lastPatrolAt: typeof props.lastPatrolAt === 'string' ? props.lastPatrolAt : undefined,
      lastPatrolLabel:
        typeof props.lastPatrolLabel === 'string' ? props.lastPatrolLabel : undefined,
      patrolIntervalMs:
        typeof props.patrolIntervalMs === 'number' ? props.patrolIntervalMs : undefined,
      runSnapshotId:
        props.runSnapshot &&
        typeof props.runSnapshot === 'object' &&
        typeof (props.runSnapshot as { id?: unknown }).id === 'string'
          ? ((props.runSnapshot as { id: string }).id ?? undefined)
          : undefined,
    };
    return <div data-testid="findings-panel" />;
  },
}));

vi.mock('@/components/patrol', () => ({
  ApprovalBanner: () => <div data-testid="approval-banner" />,
  RunHistoryPanel: (props: { onSelectRun?: (run: Record<string, unknown> | null) => void }) => (
    <div data-testid="run-history-panel">
      <button type="button" onClick={() => props.onSelectRun?.(runHistoryState.selection)}>
        Select mocked run
      </button>
    </div>
  ),
  CountdownTimer: () => <div data-testid="countdown-timer" />,
}));

vi.mock('@/hooks/usePatrolStream', () => ({
  usePatrolStream: () => ({
    isStreaming: () => false,
    phase: () => '',
    currentTool: () => '',
    tokens: () => 0,
  }),
}));

vi.mock('@/components/shared/PageHeader', () => ({
  PageHeader: (props: { title?: string; description?: string; actions?: unknown }) => (
    <div>
      <h1>{props.title}</h1>
      <p>{props.description}</p>
      <div>{props.actions as any}</div>
    </div>
  ),
}));

vi.mock('@/components/shared/Toggle', () => ({
  TogglePrimitive: (props: {
    checked?: boolean;
    disabled?: boolean;
    onToggle?: (value: boolean) => void;
    ariaLabel?: string;
    ariaLabelledBy?: string;
    ariaDescribedBy?: string;
  }) => (
    <button
      type="button"
      aria-label={props.ariaLabel}
      aria-labelledby={props.ariaLabel ? undefined : props.ariaLabelledBy}
      aria-describedby={props.ariaDescribedBy}
      aria-pressed={props.checked}
      disabled={props.disabled}
      onClick={() => props.onToggle?.(!props.checked)}
    />
  ),
  Toggle: (props: {
    checked?: boolean;
    disabled?: boolean;
    onChange?: (event: Event & { currentTarget: HTMLInputElement }) => void;
    ariaLabel?: string;
    ariaLabelledBy?: string;
    ariaDescribedBy?: string;
  }) => (
    <input
      type="checkbox"
      checked={props.checked}
      disabled={props.disabled}
      aria-label={props.ariaLabel}
      aria-labelledby={props.ariaLabel ? undefined : props.ariaLabelledBy}
      aria-describedby={props.ariaDescribedBy}
      onChange={(event) => props.onChange?.(event as Event & { currentTarget: HTMLInputElement })}
    />
  ),
}));

vi.mock('@/components/Brand/PulsePatrolLogo', () => ({
  PulsePatrolLogo: (props: { decorative?: boolean; title?: string }) => (
    <svg
      data-testid="pulse-patrol-logo"
      aria-hidden={props.decorative ? 'true' : undefined}
      aria-label={props.decorative ? undefined : (props.title ?? 'Pulse Patrol')}
    />
  ),
}));

const defaultPatrolStatus = (overrides: Record<string, unknown> = {}) => ({
  runtime_state: 'active',
  running: false,
  license_required: false,
  blocked_reason: '',
  blocked_at: '',
  ...overrides,
});

const defaultAISettings = {
  patrol_enabled: true,
  patrol_interval_minutes: 360,
  patrol_model: '',
  model: '',
  alert_triggered_analysis: false,
  patrol_event_triggers_enabled: true,
  patrol_auto_fix: false,
  auto_fix_model: '',
};

const defaultAgentCapabilitiesManifest = () => ({
  version: 'v1',
  surfaceContract: {
    core: {
      id: 'pulse_intelligence_core',
      label: 'Pulse Intelligence Core',
      description:
        'Canonical context, governed actions, safety gates, approval state, action audit, and verification.',
    },
    proactiveEngine: {
      id: 'pulse_patrol',
      label: 'Pulse Patrol',
      description: 'The proactive detection and investigation engine.',
    },
    operatorSurfaces: [
      {
        id: 'pulse_mcp',
        label: 'Pulse MCP',
        description: 'The external-agent adapter.',
        native: false,
        externalAdapter: true,
        affordances: {
          tools: true,
          resources: true,
          prompts: true,
          capabilityMetadata: true,
        },
      },
    ],
  },
  surfaceToolContracts: [
    {
      surfaceId: 'pulse_mcp',
      surfaceLabel: 'Pulse MCP',
      toolSource: 'capability_manifest',
      toolNames: [
        'get_patrol_control_status',
        'get_fleet_context',
        'get_resource_context',
        'list_findings',
        'plan_action',
        'decide_action',
        'execute_action',
        'resolve_finding',
      ],
      capabilityNames: [
        'get_patrol_control_status',
        'get_fleet_context',
        'get_resource_context',
        'list_findings',
        'plan_action',
        'decide_action',
        'execute_action',
        'resolve_finding',
      ],
      affordances: {
        tools: true,
        resources: true,
        prompts: true,
        capabilityMetadata: true,
      },
    },
  ],
  mcpAdapter: {
    serverName: 'pulse',
    command: 'pulse-mcp',
    baseUrlFlag: '--base-url',
    defaultBaseUrl: 'http://localhost:7655',
    tokenEnv: 'PULSE_API_TOKEN',
    configFamilies: [{ id: 'opencode', label: 'OpenCode', shape: 'opencode_mcp' }],
  },
  requiredScopes: [],
  categories: [],
  workflowPrompts: [{ name: 'pulse_operations_loop', label: 'Ask Patrol to handle an issue' }],
  capabilities: [],
});

const defaultPatrolAutonomySettings = (overrides: Record<string, unknown> = {}) => ({
  autonomy_level: 'monitor',
  requested_autonomy_level: 'monitor',
  effective_autonomy_level: 'monitor',
  full_mode_unlocked: false,
  autopilot_acknowledgement: {
    code: 'not_requested',
    active: false,
    currentVersion: 1,
    acceptedScope: [],
    acceptedLimits: {
      policyAllowlistRequired: true,
      emergencyStopHonored: true,
      approvalFloorsHonored: true,
      verificationReconciledWhenSupported: true,
      evidenceClassDisclosed: true,
      inconclusiveOutcomeAllowed: true,
      executionSuccessIsNotOutcomeTruth: true,
    },
  },
  investigation_budget: 15,
  investigation_timeout_sec: 300,
  ...overrides,
});

const defaultOperationsLoopStatus = (overrides: Record<string, unknown> = {}) => ({
  nextAction: 'run_patrol',
  progressLabel: 'Run Patrol to produce actionable issue evidence.',
  steps: [
    { id: 'patrol', label: 'Patrol', status: 'current', count: 0 },
    { id: 'assistant', label: 'Assistant', status: 'pending' },
    { id: 'governance', label: 'Governance', status: 'pending', count: 0 },
    { id: 'verification', label: 'Verification', status: 'pending', count: 0 },
  ],
  patrolEvidenceCount: 0,
  patrolIssueEvidenceCount: 0,
  activeFindingCount: 0,
  pendingApprovalCount: 0,
  governedActionCount: 0,
  approvedDecisionCount: 0,
  rejectedDecisionCount: 0,
  verifiedOutcomeCount: 0,
  operationsLoopStarterCount: 0,
  assistantOperationsLoopStarterCount: 0,
  patrolOperationsLoopStarterCount: 0,
  patrolControlOperationsLoopStarterCount: 0,
  patrolControlCompletedOperationsLoopCount: 0,
  patrolControlResolvedOperationsLoopCount: 0,
  patrolControlValueState: 'not_started',
  patrolAutonomyOperationsLoopStarterCount: 0,
  patrolAutonomyCompletedOperationsLoopCount: 0,
  patrolAutonomyResolvedOperationsLoopCount: 0,
  patrolAutonomyValueState: 'not_started',
  proActivationOperationsLoopStarterCount: 0,
  proActivationCompletedOperationsLoopCount: 0,
  proActivationResolvedOperationsLoopCount: 0,
  proActivationValueProofState: 'not_started',
  mcpOperationsLoopStarterCount: 0,
  externalAgentReady: true,
  windowStart: '2026-06-01T00:00:00Z',
  generatedAt: '2026-06-20T00:00:00Z',
  ...overrides,
});

async function openPatrolActivityMode() {
  await waitFor(() => {
    expect(screen.getByRole('tab', { name: 'Activity' })).toBeInTheDocument();
  });
  fireEvent.click(screen.getByRole('tab', { name: 'Activity' }));
}

describe('AIIntelligence entitlement gating', () => {
  it('keeps Patrol page data sync bounded without making it a primary action', () => {
    expect(patrolIntelligenceSurfaceSource).toContain('Review and history');
    expect(patrolIntelligenceSurfaceSource).toContain('Actions and approvals');
  });

  it('keeps active decisions ahead of objective configuration and historical review', () => {
    const attentionIndex = patrolIntelligenceSurfaceSource.indexOf('<PatrolAttentionWorkbench');
    const objectivesIndex = patrolIntelligenceSurfaceSource.indexOf('<PatrolObjectivesPanel');
    const verifiedOutcomesIndex = patrolIntelligenceSurfaceSource.indexOf('<PatrolRecentWorkPanel');
    const weeklyDigestIndex = patrolIntelligenceSurfaceSource.indexOf('<PatrolWeeklyDigestCard');
    const historyIndex = patrolIntelligenceSurfaceSource.indexOf('Review and history');

    expect(attentionIndex).toBeGreaterThan(-1);
    expect(objectivesIndex).toBeGreaterThan(attentionIndex);
    expect(verifiedOutcomesIndex).toBeGreaterThan(attentionIndex);
    expect(historyIndex).toBeGreaterThan(verifiedOutcomesIndex);
    expect(weeklyDigestIndex).toBeGreaterThan(attentionIndex);
    expect(weeklyDigestIndex).toBeLessThan(verifiedOutcomesIndex);
  });

  beforeEach(() => {
    routerLocation.setPath('/patrol');
    resetCreateNonSuspendingQueryCacheForTest();
    resetAIRuntimeState();
    getPatrolStatusMock.mockReset();
    getPatrolAutonomySettingsMock.mockReset();
    updatePatrolAutonomySettingsMock.mockReset();
    createPatrolAutopilotAcknowledgementMock.mockReset();
    triggerPatrolRunMock.mockReset();
    getPatrolRunHistoryMock.mockReset();
    apiFetchJSONMock.mockReset();
    hasFeatureMock.mockReset();
    runtimeCapabilitiesMock.mockReset();
    getRuntimeCapabilityBlockMock.mockReset();
    licenseStatusMock.mockReset();
    loadLicenseStatusMock.mockReset();
    loadCommercialPostureMock.mockReset();
    getUpgradeActionDestinationMock.mockReset();
    getUpgradeActionUrlOrFallbackMock.mockReset();
    presentationPolicyHidesUpgradePromptsMock.mockReset();
    presentationPolicyHidesCommercialSurfacesMock.mockReset();
    notificationSuccessMock.mockReset();
    notificationErrorMock.mockReset();
    recordWorkflowPromptActivityMock.mockReset();
    findingsPanelState.latestProps = null;
    runHistoryState.selection = null;
    intelligenceState.findings = [];
    intelligenceState.summary = null;

    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus());
    getPatrolAutonomySettingsMock.mockResolvedValue(defaultPatrolAutonomySettings());
    updatePatrolAutonomySettingsMock.mockResolvedValue({
      settings: defaultPatrolAutonomySettings(),
    });
    createPatrolAutopilotAcknowledgementMock.mockResolvedValue({
      created: true,
      acknowledgement: {
        ...defaultPatrolAutonomySettings().autopilot_acknowledgement,
        code: 'active',
        active: true,
        acknowledgementId: 'ack-test',
      },
    });
    triggerPatrolRunMock.mockResolvedValue(undefined);
    getPatrolRunHistoryMock.mockResolvedValue([]);
    apiFetchJSONMock.mockImplementation(async (path: string) => {
      if (path === '/api/ai/models') {
        return { models: [] };
      }
      if (path === '/api/settings/ai') {
        return defaultAISettings;
      }
      if (path === '/api/settings/ai/update') {
        return defaultAISettings;
      }
      if (path === '/api/agent/capabilities') {
        return defaultAgentCapabilitiesManifest();
      }
      if (path === AGENT_PATROL_CONTROL_STATUS_PATH) {
        return defaultOperationsLoopStatus();
      }
      return {};
    });
    hasFeatureMock.mockImplementation(
      (feature: string) => !['ai_alerts', 'ai_autofix'].includes(feature),
    );
    runtimeCapabilitiesMock.mockReturnValue({
      runtime: 'community',
      capabilities: ['ai_intelligence'],
      blocked_capabilities: [],
    });
    getRuntimeCapabilityBlockMock.mockReturnValue(undefined);
    licenseStatusMock.mockReturnValue({ subscription_state: 'expired' });
    loadLicenseStatusMock.mockResolvedValue(undefined);
    loadCommercialPostureMock.mockResolvedValue(undefined);
    getUpgradeActionDestinationMock.mockImplementation((feature?: string) =>
      feature === 'ai_autofix'
        ? { href: SELF_HOSTED_PRO_BILLING_PLAN_SELECTION_HREF, external: false }
        : {
            href: getPublicPricingUrl(feature),
            external: true,
          },
    );
    getUpgradeActionUrlOrFallbackMock.mockImplementation((feature?: string) =>
      getPublicPricingUrl(feature),
    );
    presentationPolicyHidesUpgradePromptsMock.mockReturnValue(false);
    presentationPolicyHidesCommercialSurfacesMock.mockReturnValue(false);
    recordWorkflowPromptActivityMock.mockResolvedValue(undefined);
    window.history.replaceState({}, '', '/patrol');
  });

  it('keeps the Patrol page heading accessible name singular when the logo is present', async () => {
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });

    expect(screen.queryByRole('heading', { name: 'Pulse Patrol Patrol' })).not.toBeInTheDocument();
    expect(screen.getByTestId('pulse-patrol-logo')).toHaveAttribute('aria-hidden', 'true');
  });

  it('keeps the Patrol route handoff anchored without rendering idle run prompts as current work', async () => {
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });

    expect(
      screen.getByText(
        'See what needs a decision, choose the next step, and keep a verified record.',
      ),
    ).toBeInTheDocument();
    expect(
      screen.queryByText(
        'Patrol detects operational issues, brings forward the next decision, and records only verified outcomes.',
      ),
    ).not.toBeInTheDocument();
    const patrolControlAnchor = document.getElementById(PATROL_CONTROL_ANCHOR);
    const operationsLoopAnchor = document.getElementById(PATROL_OPERATIONS_LOOP_ANCHOR);
    expect(patrolControlAnchor).not.toBeNull();
    const patrolControl = within(patrolControlAnchor!);
    expect(patrolControl.queryByRole('group', { name: 'Patrol mode' })).not.toBeInTheDocument();
    expect(patrolControl.getByText('Patrol mode')).toBeInTheDocument();
    expect(patrolControl.getAllByText('Watch only').length).toBeGreaterThan(0);
    expect(
      patrolControl.getByText(
        'Patrol checks infrastructure and reports issues only. It does not start fixes.',
      ),
    ).toBeInTheDocument();
    expect(patrolControl.queryByRole('button', { name: 'Limits' })).not.toBeInTheDocument();
    expect(patrolControl.queryByRole('button', { name: 'Ask first' })).toBeNull();
    expect(patrolControl.queryByRole('button', { name: 'Safe auto-fix' })).toBeNull();
    expect(patrolControl.queryByRole('button', { name: 'Autopilot' })).toBeNull();
    expect(patrolControl.queryByText('Patrol handles')).not.toBeInTheDocument();
    expect(patrolControl.queryByText('May do')).not.toBeInTheDocument();
    expect(patrolControl.queryByText('Needs Pro for')).not.toBeInTheDocument();
    expect(patrolControl.queryByText('Will not')).not.toBeInTheDocument();
    expect(patrolControl.queryByText('Pro')).not.toBeInTheDocument();
    expect(patrolControl.queryByRole('button', { name: 'Ask first Pro' })).toBeNull();
    expect(patrolControl.queryByRole('button', { name: 'Safe auto-fix Pro' })).toBeNull();
    expect(patrolControl.queryByRole('button', { name: 'Autopilot Pro' })).toBeNull();
    expect(screen.queryByRole('link', { name: 'Plans & Billing' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'View plans' })).not.toBeInTheDocument();
    expect(patrolControlAnchor).toContainElement(operationsLoopAnchor);
    expect(screen.queryByTestId('patrol-current-work')).not.toBeInTheDocument();
    expect(screen.queryByText('Current Patrol issues appear here.')).not.toBeInTheDocument();
    expect(screen.getByRole('tab', { name: 'Inbox' })).toHaveAttribute('aria-selected', 'true');
    expect(screen.getByRole('region', { name: 'Patrol decision inbox' })).toBeInTheDocument();
    expect(
      screen.queryByText('Issues Patrol found. Infrastructure stays unchanged.'),
    ).not.toBeInTheDocument();
    expect(screen.queryByText('Current Patrol work')).not.toBeInTheDocument();
    expect(screen.queryByText('Ready to run')).not.toBeInTheDocument();
    expect(screen.queryByText('Current issue')).not.toBeInTheDocument();
    expect(screen.queryByText('Recorded outcome')).not.toBeInTheDocument();
    expect(screen.queryByText('Duty trail')).not.toBeInTheDocument();
    expect(screen.queryByText('External agents')).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Pulse MCP' })).not.toBeInTheDocument();
    expect(screen.getAllByRole('button', { name: 'Check now' }).length).toBeGreaterThan(0);
  });

  it('consumes Patrol control route handoffs without loading legacy loop status', async () => {
    window.history.replaceState(
      {},
      '',
      `/patrol?${PATROL_CONTROL_STARTER_QUERY_PARAM}=${PATROL_CONTROL_STARTER}#${PATROL_CONTROL_ANCHOR}`,
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(recordWorkflowPromptActivityMock).toHaveBeenCalledWith({
        name: 'pulse_operations_loop',
        surface: 'patrol_control',
      });
    });

    expect(
      apiFetchJSONMock.mock.calls.some(([path]) => path === AGENT_PATROL_CONTROL_STATUS_PATH),
    ).toBe(false);
    await waitFor(() => {
      expect(window.location.pathname).toBe('/patrol');
      expect(window.location.search).toBe('');
      expect(window.location.hash).toBe(`#${PATROL_CONTROL_ANCHOR}`);
    });
  });

  it('surfaces provider issues before a manual run can start', async () => {
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        trigger_status: {
          running: false,
          pending_triggers: 0,
          current_interval_ms: 300000,
          recent_events: 0,
          is_busy_mode: false,
          alert_triggers_enabled: true,
          anomaly_triggers_enabled: true,
          event_triggers_blocked: true,
          event_triggers_blocked_reason: 'background_automation_disabled',
          event_triggers_blocked_message:
            'Automatic Patrol checks from alerts and anomalies are paused by the local development safety guard. Manual Patrol still works.',
        },
        readiness: {
          status: 'not_ready',
          ready: false,
          summary:
            'The selected Patrol model is a reasoning-only model family that commonly does not emit tool calls.',
          provider: 'ollama',
          model: 'ollama:deepseek-r1:7b-llama-distill-q4_K_M',
          checks: [
            {
              id: 'tools',
              status: 'not_ready',
              label: 'Patrol tools',
              message:
                'The selected Patrol model is a reasoning-only model family that commonly does not emit tool calls.',
              action: 'open_provider_settings',
            },
          ],
        },
      }),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol needs setup' })).toBeInTheDocument();
    });
    expect(
      screen.getByText(
        'Patrol cannot check infrastructure until its selected model passes the Patrol tool check.',
      ),
    ).toBeInTheDocument();
    expect(screen.getByText('Selected model cannot run Patrol tools.')).toBeInTheDocument();
    expect(
      screen.queryByText(
        'The selected Patrol model is a reasoning-only model family that commonly does not emit tool calls.',
      ),
    ).not.toBeInTheDocument();
    expect(screen.queryByRole('heading', { name: 'Open work' })).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol setup issue')).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol readiness issue')).not.toBeInTheDocument();
    expect(screen.queryByText(/Automation:/)).not.toBeInTheDocument();
    expect(
      screen.queryByText(
        'Trigger status: Automatic Patrol checks from alerts and anomalies are paused by the local development safety guard. Manual Patrol still works.',
      ),
    ).not.toBeInTheDocument();
    expect(screen.queryByText(/Trigger status:/)).not.toBeInTheDocument();
    expect(screen.queryByText(/local development safety guard/)).not.toBeInTheDocument();
    const providerSettingsLinks = screen.getAllByRole('link', {
      name: 'Check Patrol model',
    });
    expect(providerSettingsLinks.length).toBeGreaterThan(0);
    expect(providerSettingsLinks[0]).toHaveAttribute('href', '/settings/pulse-intelligence/patrol');
    expect(screen.queryByRole('button', { name: /Check now/i })).not.toBeInTheDocument();
    expect(triggerPatrolRunMock).not.toHaveBeenCalled();
  });

  it('surfaces provider warnings without blocking manual runs', async () => {
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        readiness: {
          status: 'warning',
          ready: true,
          summary:
            "Ollama connectivity alone does not prove tool support. qwen3:8b passes Patrol's tool check; run ollama pull qwen3:8b and select it as the Patrol model.",
          provider: 'ollama',
          model: 'ollama:llama3',
          checks: [
            {
              id: 'tools',
              status: 'warning',
              label: 'Patrol tools',
              message:
                "Ollama connectivity alone does not prove tool support. qwen3:8b passes Patrol's tool check; run ollama pull qwen3:8b and select it as the Patrol model.",
              action: 'open_provider_settings',
            },
          ],
        },
      }),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByText('Patrol model warning')).toBeInTheDocument();
    });
    expect(screen.queryByText('Patrol readiness warning')).not.toBeInTheDocument();
    const runButtons = screen.getAllByRole('button', { name: /Check now/i });
    for (const button of runButtons) {
      expect(button).not.toBeDisabled();
    }

    fireEvent.click(runButtons[0]);
    await waitFor(() => {
      expect(triggerPatrolRunMock).toHaveBeenCalled();
    });
  });

  it('moves manual Patrol runs out of Starting state once the start request is accepted', async () => {
    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getAllByRole('button', { name: /Check now/i }).length).toBeGreaterThan(0);
    });

    // Stall the Patrol refresh that follows an accepted start: the run button
    // must leave Starting without waiting on it.
    const loadIntelligenceSummary = vi.mocked(aiIntelligenceStore.loadIntelligenceSummary);
    loadIntelligenceSummary.mockClear();
    loadIntelligenceSummary.mockImplementation(() => new Promise(() => {}));
    try {
      fireEvent.click(screen.getAllByRole('button', { name: /Check now/i })[0]);

      await waitFor(() => {
        expect(triggerPatrolRunMock).toHaveBeenCalled();
        expect(screen.getAllByRole('button', { name: /Running/i }).length).toBeGreaterThan(0);
        expect(loadIntelligenceSummary).toHaveBeenCalled();
      });
      expect(screen.queryByRole('button', { name: /Starting/i })).not.toBeInTheDocument();
    } finally {
      loadIntelligenceSummary.mockResolvedValue(null);
    }
  });

  it('surfaces backend readiness rejection when a stale manual run request reaches the server', async () => {
    triggerPatrolRunMock.mockRejectedValue(
      Object.assign(
        new Error(
          'The selected Patrol model is a reasoning-only model family that commonly does not emit tool calls.',
        ),
        { code: 'patrol_readiness_not_ready', status: 409 },
      ),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getAllByRole('button', { name: /Check now/i }).length).toBeGreaterThan(0);
    });

    fireEvent.click(screen.getAllByRole('button', { name: /Check now/i })[0]);

    await waitFor(() => {
      expect(notificationErrorMock).toHaveBeenCalledWith(
        'The selected Patrol model is a reasoning-only model family that commonly does not emit tool calls.',
      );
    });
  });

  it('keeps legacy Patrol status payloads without readiness compatible', async () => {
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ readiness: undefined }));

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });
    expect(screen.queryByText('Patrol setup issue')).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol setup warning')).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol readiness issue')).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol readiness warning')).not.toBeInTheDocument();
    for (const button of screen.getAllByRole('button', { name: /Check now/i })) {
      expect(button).not.toBeDisabled();
    }
  });

  it('does not expose raw supporting context as a Patrol page details panel', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    intelligenceState.findings = [
      {
        id: 'finding-storage-context',
        status: 'active',
        severity: 'warning',
        resourceId: 'storage-2',
        resourceName: 'Storage 2',
        title: 'Storage issue needs Patrol context',
      },
    ];
    intelligenceState.summary = {
      timestamp: '2026-03-01T00:00:00Z',
      overall_health: {
        score: 63,
        grade: 'C',
        trend: 'stable',
        factors: [
          {
            name: 'Patrol coverage incomplete',
            impact: -0.35,
            description:
              'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
            category: 'coverage',
          },
        ],
        prediction:
          'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      recent_changes: [],
      policy_posture: {
        total_resources: 3,
        sensitivity_counts: {},
        routing_counts: {},
      },
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 1,
        incidents_tracked: 0,
      },
    };
    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });
    expect(screen.queryByRole('button', { name: 'Details' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Hide details' })).not.toBeInTheDocument();
    expect(
      screen.queryByText('Extra activity and policy context for the selected item.'),
    ).not.toBeInTheDocument();
    expect(
      screen.queryByText('Details available: related patterns, policy limits.'),
    ).not.toBeInTheDocument();
    expect(screen.queryByText('For reference')).not.toBeInTheDocument();
    expect(
      screen.queryByText(
        'This explains what Patrol saw. It does not change the finding or start a new patrol.',
      ),
    ).not.toBeInTheDocument();
    expect(screen.queryByRole('heading', { name: 'Nearby activity' })).not.toBeInTheDocument();
    expect(screen.queryByRole('heading', { name: 'Related patterns' })).not.toBeInTheDocument();
    expect(screen.queryByRole('heading', { name: 'Policy limits' })).not.toBeInTheDocument();
    expect(screen.queryByText('2 related patterns Patrol can use')).not.toBeInTheDocument();
    expect(
      screen.queryByText('What Patrol was allowed to inspect or act on.'),
    ).not.toBeInTheDocument();

    await openPatrolActivityMode();
    fireEvent.click(screen.getByRole('button', { name: 'History' }));
    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol history' })).toBeInTheDocument();
    });
    expect(screen.queryByRole('button', { name: 'Details' })).not.toBeInTheDocument();
  });

  afterEach(() => {
    cleanup();
  });

  it('locks paid patrol controls without promoting checkout from ordinary Patrol workflows', async () => {
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        license_required: true,
      }),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(loadLicenseStatusMock).toHaveBeenCalled();
      expect(getPatrolStatusMock).toHaveBeenCalled();
    });

    const patrolControlAnchor = document.getElementById(PATROL_CONTROL_ANCHOR);
    expect(patrolControlAnchor).not.toBeNull();
    const patrolControl = within(patrolControlAnchor!);
    expect(patrolControl.queryByRole('group', { name: 'Patrol mode' })).not.toBeInTheDocument();
    expect(patrolControl.getByText('Patrol mode')).toBeInTheDocument();
    expect(patrolControl.getAllByText('Watch only').length).toBeGreaterThan(0);
    expect(patrolControl.queryByRole('button', { name: 'Limits' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Ask first, requires Pulse Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Safe auto-fix, requires Pulse Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Autopilot, requires Pulse Pro' })).toBeNull();
    expect(patrolControl.queryByText('Pro')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Ask first Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Safe auto-fix Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Autopilot Pro' })).toBeNull();
    expect(screen.queryByRole('link', { name: 'Plans & Billing' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'View plans' })).not.toBeInTheDocument();
    expect(screen.queryByText('Unlock Patrol mode')).not.toBeInTheDocument();
    expect(screen.queryByText('More Patrol modes')).not.toBeInTheDocument();
    expect(
      screen.queryByText(
        'This plan can only watch. Pulse Pro unlocks investigation, approval-backed fixes, and automatic policy-approved fixes.',
      ),
    ).not.toBeInTheDocument();

    expect(screen.getByRole('link', { name: 'Open Patrol settings' })).toHaveAttribute(
      'href',
      '/settings/pulse-intelligence/patrol',
    );
    expect(screen.queryByRole('dialog', { name: 'Patrol schedule & model' })).toBeNull();
    expect(screen.queryByText('Container update risk')).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Upgrade to Pro' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Upgrade' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /start free trial/i })).not.toBeInTheDocument();
  });

  it('locks paid patrol controls without upgrade prompts in default self-hosted mode', async () => {
    presentationPolicyHidesUpgradePromptsMock.mockReturnValue(true);
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        license_required: true,
      }),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(loadLicenseStatusMock).toHaveBeenCalled();
      expect(getPatrolStatusMock).toHaveBeenCalled();
    });

    const patrolControlAnchor = document.getElementById(PATROL_CONTROL_ANCHOR);
    expect(patrolControlAnchor).not.toBeNull();
    const patrolControl = within(patrolControlAnchor!);
    expect(patrolControl.queryByRole('group', { name: 'Patrol mode' })).not.toBeInTheDocument();
    expect(patrolControl.getByText('Patrol mode')).toBeInTheDocument();
    expect(patrolControl.getAllByText('Watch only').length).toBeGreaterThan(0);
    expect(patrolControl.queryByRole('button', { name: 'Limits' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Ask first, requires Pulse Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Safe auto-fix, requires Pulse Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Autopilot, requires Pulse Pro' })).toBeNull();
    expect(patrolControl.queryByText('Pro')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Ask first Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Safe auto-fix Pro' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'Autopilot Pro' })).toBeNull();
    expect(screen.queryByRole('link', { name: 'Plans & Billing' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'View plans' })).not.toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Open Patrol settings' })).toHaveAttribute(
      'href',
      '/settings/pulse-intelligence/patrol',
    );
    expect(screen.queryByRole('dialog', { name: 'Patrol schedule & model' })).toBeNull();
    expect(screen.queryByText('This install can only watch')).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol is in watch-only mode')).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Upgrade to Pro' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Upgrade' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /start free trial/i })).not.toBeInTheDocument();
  });

  it('hides paid patrol badges when commercial surfaces are hidden', async () => {
    presentationPolicyHidesCommercialSurfacesMock.mockReturnValue(true);
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        license_required: true,
      }),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(loadLicenseStatusMock).toHaveBeenCalled();
      expect(getPatrolStatusMock).toHaveBeenCalled();
    });

    const patrolControlAnchor = document.getElementById(PATROL_CONTROL_ANCHOR);
    expect(patrolControlAnchor).not.toBeNull();
    const patrolControl = within(patrolControlAnchor!);
    expect(patrolControl.getAllByText('Watch only').length).toBeGreaterThan(0);
    expect(patrolControl.queryByText('Pro')).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'View plans' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Upgrade to Pro' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /start free trial/i })).not.toBeInTheDocument();
  });

  describe('Patrol mode runtime lock', () => {
    const useLicensedCommunityRuntime = () => {
      getRuntimeCapabilityBlockMock.mockImplementation((feature: string) =>
        feature === 'ai_autofix'
          ? {
              key: 'ai_autofix',
              reason: 'paid_runtime_required',
              action_url: 'https://pulserelay.pro/download.html',
            }
          : undefined,
      );
      runtimeCapabilitiesMock.mockReturnValue({
        runtime: { build: 'community', label: 'Pulse Community runtime' },
        capabilities: ['ai_intelligence'],
        blocked_capabilities: [
          { key: 'ai_autofix', reason: 'paid_runtime_required' },
          { key: 'ai_alerts', reason: 'paid_runtime_required' },
        ],
      });
      getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: true }));
    };

    it('points a licensed community runtime at the Pro runtime download', async () => {
      useLicensedCommunityRuntime();

      render(() => <AIIntelligence />);

      await waitFor(() => {
        expect(screen.getAllByText(/Pulse Pro runtime required/).length).toBeGreaterThan(0);
      });
      const downloads = screen.getAllByRole('link', { name: 'Open Pro downloads' });
      expect(downloads.length).toBeGreaterThan(0);
      for (const link of downloads) {
        expect(link).toHaveAttribute('href', 'https://pulserelay.pro/download.html');
      }
    });

    it('keeps Pro runtime wording off the page when commercial surfaces are hidden', async () => {
      useLicensedCommunityRuntime();
      presentationPolicyHidesCommercialSurfacesMock.mockReturnValue(true);
      presentationPolicyHidesUpgradePromptsMock.mockReturnValue(true);

      render(() => <AIIntelligence />);

      await waitFor(() => {
        expect(getPatrolStatusMock).toHaveBeenCalled();
      });
      const patrolControlAnchor = document.getElementById(PATROL_CONTROL_ANCHOR);
      expect(patrolControlAnchor).not.toBeNull();
      const patrolControl = within(patrolControlAnchor!);
      expect(patrolControl.getAllByText('Watch only').length).toBeGreaterThan(0);
      expect(patrolControl.queryByRole('group', { name: 'Patrol mode' })).not.toBeInTheDocument();
      expect(screen.queryByText(/Pulse Pro runtime/)).not.toBeInTheDocument();
      expect(screen.queryByText(/Install the Pulse Pro runtime/)).not.toBeInTheDocument();
      expect(screen.queryByRole('link', { name: 'Open Pro downloads' })).not.toBeInTheDocument();
    });

    it('saves only Watch only, without an unlock field, while governed fixes are locked', async () => {
      useLicensedCommunityRuntime();
      // A previous entitlement left a paid mode and the compatibility unlock in memory.
      getPatrolAutonomySettingsMock.mockResolvedValue(
        defaultPatrolAutonomySettings({
          autonomy_level: 'assisted',
          requested_autonomy_level: 'assisted',
          effective_autonomy_level: 'assisted',
          full_mode_unlocked: true,
        }),
      );

      render(() => <AIIntelligence />);

      const modes = within(await screen.findByRole('group', { name: 'Patrol mode' }));
      await waitFor(() => expect(getPatrolAutonomySettingsMock).toHaveBeenCalled());
      await getPatrolAutonomySettingsMock.mock.results[0]?.value;
      await new Promise((resolve) => setTimeout(resolve, 0));
      expect(modes.getByRole('button', { name: 'Watch only' })).toHaveAttribute(
        'aria-pressed',
        'true',
      );
      for (const paidMode of ['Ask first', 'Safe auto-fix', 'Autopilot']) {
        expect(modes.getByRole('button', { name: paidMode })).toBeDisabled();
        fireEvent.click(modes.getByRole('button', { name: paidMode }));
      }
      expect(screen.queryByRole('dialog', { name: 'Activate Autopilot' })).toBeNull();
      expect(updatePatrolAutonomySettingsMock).not.toHaveBeenCalled();

      fireEvent.click(modes.getByRole('button', { name: 'Watch only' }));

      // The browser sends no full_mode_unlocked field; the API clears stale
      // unlock and Autopilot activation state on every non-Autopilot save.
      await waitFor(() => {
        expect(updatePatrolAutonomySettingsMock).toHaveBeenCalledTimes(1);
      });
      expect(updatePatrolAutonomySettingsMock).toHaveBeenCalledWith({
        autonomy_level: 'monitor',
        investigation_budget: 15,
        investigation_timeout_sec: 300,
      });
    });
  });

  it('keeps the Patrol model catalog out of the operator page', async () => {
    apiFetchJSONMock.mockImplementation(async (path: string) => {
      if (path === '/api/ai/models') {
        return {
          models: [
            {
              id: 'deepseek:deepseek-v4-flash',
              name: 'DeepSeek V4 Flash',
              description: 'DeepSeek: current V4 Flash direct API model',
            },
          ],
        };
      }
      if (path === '/api/settings/ai') {
        return {
          ...defaultAISettings,
          model: 'openrouter:minimax/minimax-m2.5',
          patrol_model: 'deepseek:deepseek-v4-flash',
        };
      }
      if (path === '/api/settings/ai/update') {
        return defaultAISettings;
      }
      return {};
    });

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });
    await waitFor(() => {
      expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/ai/models');
    });

    expect(screen.getByRole('link', { name: 'Open Patrol settings' })).toHaveAttribute(
      'href',
      '/settings/pulse-intelligence/patrol',
    );
    expect(screen.queryByRole('dialog', { name: 'Patrol schedule & model' })).toBeNull();
    expect(screen.queryByText('DeepSeek V4 Flash')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Change model' })).toBeNull();
  });

  it('unlocks paid patrol controls when the entitlement grants the features', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(loadLicenseStatusMock).toHaveBeenCalled();
      expect(getPatrolStatusMock).toHaveBeenCalled();
    });

    expect(screen.getByRole('button', { name: 'Ask first' })).not.toBeDisabled();
    expect(screen.getByRole('button', { name: 'Safe auto-fix' })).not.toBeDisabled();
    expect(screen.getByRole('button', { name: 'Autopilot' })).not.toBeDisabled();
    expect(
      screen.getByText(
        'Patrol checks infrastructure and reports issues only. It does not start fixes.',
      ),
    ).toBeInTheDocument();
    expect(screen.queryByText('Patrol handles')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Limits' })).not.toBeInTheDocument();
    expect(screen.queryByText('Hard limits')).not.toBeInTheDocument();

    fireEvent.click(screen.getByRole('button', { name: 'Autopilot' }));

    // Autopilot no longer switches directly; it opens the acknowledgement
    // dialog and the mode stays where it was until the user records one.
    expect(await screen.findByRole('dialog', { name: 'Activate Autopilot' })).toBeInTheDocument();
    expect(updatePatrolAutonomySettingsMock).not.toHaveBeenCalled();
    fireEvent.click(screen.getByRole('button', { name: 'Close Autopilot acknowledgement' }));

    expect(screen.getByRole('link', { name: 'Open Patrol settings' })).toHaveAttribute(
      'href',
      '/settings/pulse-intelligence/patrol',
    );
    expect(screen.queryByRole('dialog', { name: 'Patrol schedule & model' })).toBeNull();
    expect(screen.queryByRole('checkbox', { name: 'Container Update Risk' })).toBeNull();
    expect(screen.queryByRole('checkbox', { name: 'Alert-Triggered Patrols' })).toBeNull();
    expect(screen.queryByRole('checkbox', { name: 'Anomaly-Triggered Patrols' })).toBeNull();
    expect(screen.queryByRole('link', { name: 'Upgrade to Pro' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Upgrade' })).not.toBeInTheDocument();
  });

  it('reconciles Autopilot from the authoritative GET after a compact paid-runtime receipt', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    getPatrolAutonomySettingsMock.mockImplementation(async () =>
      updatePatrolAutonomySettingsMock.mock.calls.length > 0
        ? defaultPatrolAutonomySettings({
            autonomy_level: 'full',
            requested_autonomy_level: 'full',
            effective_autonomy_level: 'full',
            full_mode_unlocked: true,
            autopilot_acknowledgement: {
              ...defaultPatrolAutonomySettings().autopilot_acknowledgement,
              code: 'active',
              active: true,
              acknowledgementId: 'ack-test',
            },
          })
        : defaultPatrolAutonomySettings(),
    );
    // The private paid runtime returns this intentionally compact receipt.
    // Effective mode and acknowledgement truth come from the following GET.
    updatePatrolAutonomySettingsMock.mockResolvedValue({
      success: true,
      settings: {
        autonomy_level: 'full',
        full_mode_unlocked: true,
      },
    });

    render(() => <AIIntelligence />);

    const autopilotButton = await screen.findByRole('button', { name: 'Autopilot' });
    fireEvent.click(autopilotButton);
    fireEvent.click(
      await screen.findByRole('checkbox', {
        name: /I understand and accept these Autopilot limits/,
      }),
    );
    fireEvent.click(screen.getByRole('button', { name: 'Record acknowledgement and activate' }));

    await waitFor(() => {
      expect(updatePatrolAutonomySettingsMock).toHaveBeenCalledWith(
        expect.objectContaining({ autonomy_level: 'full', acknowledgement_id: 'ack-test' }),
      );
      expect(getPatrolAutonomySettingsMock.mock.calls.length).toBeGreaterThan(1);
      expect(notificationSuccessMock).toHaveBeenCalledWith(
        'Autopilot acknowledgement recorded and mode activated.',
      );
      expect(notificationErrorMock).not.toHaveBeenCalled();
    });
  });

  it('records direct Patrol mode changes after successful paid control saves', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    apiFetchJSONMock.mockImplementation(async (path: string) => {
      if (path === '/api/ai/models') {
        return { models: [] };
      }
      if (path === '/api/settings/ai') {
        return defaultAISettings;
      }
      if (path === '/api/settings/ai/update') {
        return defaultAISettings;
      }
      if (path === '/api/agent/capabilities') {
        return defaultAgentCapabilitiesManifest();
      }
      if (path === AGENT_PATROL_CONTROL_STATUS_PATH) {
        return defaultOperationsLoopStatus();
      }
      return {};
    });
    updatePatrolAutonomySettingsMock.mockResolvedValue({
      settings: defaultPatrolAutonomySettings({
        autonomy_level: 'approval',
        requested_autonomy_level: 'approval',
        effective_autonomy_level: 'approval',
      }),
    });

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('button', { name: 'Ask first' })).not.toBeDisabled();
    });
    const statusCallsBeforeModeChange = getPatrolStatusMock.mock.calls.length;
    const runHistoryCallsBeforeModeChange = getPatrolRunHistoryMock.mock.calls.length;

    fireEvent.click(screen.getByRole('button', { name: 'Ask first' }));

    await waitFor(() => {
      expect(updatePatrolAutonomySettingsMock).toHaveBeenCalledWith(
        expect.objectContaining({
          autonomy_level: 'approval',
          investigation_budget: 15,
          investigation_timeout_sec: 300,
        }),
      );
    });
    await waitFor(() => {
      expect(recordWorkflowPromptActivityMock).toHaveBeenCalledWith({
        name: 'pulse_operations_loop',
        surface: 'patrol_control',
      });
    });
    await waitFor(() => {
      expect(getPatrolStatusMock.mock.calls.length).toBeGreaterThan(statusCallsBeforeModeChange);
      expect(getPatrolRunHistoryMock.mock.calls.length).toBeGreaterThan(
        runHistoryCallsBeforeModeChange,
      );
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
      expect(screen.queryByTestId('patrol-current-work')).not.toBeInTheDocument();
      expect(screen.queryByText('Patrol ready')).not.toBeInTheDocument();
      expect(screen.queryByText(/Run Patrol now to look for issues/)).not.toBeInTheDocument();
      expect(screen.queryByText('Patrol mode ready')).not.toBeInTheDocument();
      expect(screen.queryByText(/Patrol mode is set/)).not.toBeInTheDocument();
    });
  });

  it('renders the canonical intelligence summary card with recent changes', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    intelligenceState.summary = {
      timestamp: '2026-03-01T00:00:00Z',
      overall_health: {
        score: 91,
        grade: 'A',
        trend: 'stable',
        factors: [],
        prediction: 'Stable',
      },
      findings_count: {
        critical: 1,
        warning: 2,
        watch: 0,
        info: 4,
        total: 7,
      },
      predictions_count: 3,
      recent_changes_count: 1,
      recent_changes: [
        {
          id: 'change-1',
          observedAt: '2026-03-01T00:00:00Z',
          resourceId: 'vm-100',
          kind: 'config_update',
          sourceType: 'pulse_diff',
          sourceAdapter: 'proxmox_adapter',
          confidence: 'high',
          reason: 'Updated guest configuration',
          relatedResources: ['agent-1'],
        },
      ],
      policy_posture: {
        total_resources: 4,
        sensitivity_counts: {
          public: 1,
          internal: 1,
          sensitive: 1,
          restricted: 1,
        },
        routing_counts: {
          'cloud-summary': 2,
          'local-first': 1,
          'local-only': 1,
        },
        redaction_counts: {
          hostname: 2,
          'ip-address': 1,
        },
      },
      learning: {
        resources_with_knowledge: 4,
        total_notes: 11,
        resources_with_baselines: 3,
        patterns_detected: 2,
        correlations_learned: 1,
        incidents_tracked: 5,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });

    expect(screen.queryByText('Patrol status')).not.toBeInTheDocument();
    expect(screen.queryByText(/No active issues · health score 91\/100/)).not.toBeInTheDocument();
    expect(screen.queryByText('No active issues detected')).not.toBeInTheDocument();
    expect(screen.queryByText(/Health A · 91\/100/)).not.toBeInTheDocument();
    expect(screen.queryByText('Supporting context')).not.toBeInTheDocument();
    expect(
      screen.queryByText('1 recent change · 4 resources covered by policy'),
    ).not.toBeInTheDocument();
    expect(screen.queryByText('Policy posture')).not.toBeInTheDocument();
  });

  it('keeps the Patrol page visible without a separate summary loading strip', async () => {
    getPatrolStatusMock.mockImplementation(() => new Promise(() => {}));
    intelligenceState.summary = null;

    render(() => (
      <Suspense fallback={<div>Loading view...</div>}>
        <AIIntelligence />
      </Suspense>
    ));

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
      expect(screen.getByRole('region', { name: 'Patrol decision inbox' })).toBeInTheDocument();
    });

    expect(screen.queryByText('Loading view...')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-loading')).not.toBeInTheDocument();
    expect(screen.queryByText('Loading Patrol')).not.toBeInTheDocument();
  });

  it('keeps the Patrol page visible when first-load refreshes fail', async () => {
    loadLicenseStatusMock.mockRejectedValue(new TypeError('Failed to fetch'));

    render(() => (
      <Suspense fallback={<div>Loading view...</div>}>
        <AIIntelligence />
      </Suspense>
    ));

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
      expect(screen.getByText('Patrol could not refresh')).toBeInTheDocument();
      expect(screen.getByRole('region', { name: 'Patrol decision inbox' })).toBeInTheDocument();
    });

    expect(screen.queryByText('Loading view...')).not.toBeInTheDocument();
    expect(screen.queryByText('Failed to fetch')).not.toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Retry' })).toBeInTheDocument();
  });

  it('does not present a healthy patrol summary when a retired hosted block reason is normalized', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        runtime_state: 'blocked',
        blocked_reason:
          'Quickstart credits exhausted. Connect your API key to continue using Patrol.',
        last_patrol_at: '2026-03-12T09:57:00Z',
      }),
    );
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 100,
        grade: 'A',
        trend: 'stable',
        factors: [],
        prediction: 'Infrastructure is healthy with no significant issues detected.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      recent_changes: [],
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(screen.getAllByText('Patrol paused').length).toBeGreaterThan(0);
    });

    expect(screen.getAllByText('Patrol paused').length).toBeGreaterThan(0);
    expect(
      screen.getAllByText('Connect your own AI provider or local model to use Pulse Patrol.')
        .length,
    ).toBeGreaterThan(0);
    expect(screen.queryByText(/Quickstart credits exhausted/i)).not.toBeInTheDocument();
    expect(screen.queryByText(/Health A · 100\/100/)).not.toBeInTheDocument();
  });

  it('does not show retired hosted-credit chips when patrol is active on a configured provider path', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        runtime_state: 'active',
        last_patrol_at: '2026-03-12T09:57:00Z',
      }),
    );
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 100,
        grade: 'A',
        trend: 'stable',
        factors: [],
        prediction: 'Infrastructure is healthy with no significant issues detected.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(
        screen.getByRole('heading', { name: 'Watching your infrastructure' }),
      ).toBeInTheDocument();
    });

    expect(screen.queryByText('Patrol quickstart exhausted')).not.toBeInTheDocument();
    expect(screen.queryByText(/No active issues · health score 100\/100/)).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol status')).not.toBeInTheDocument();
  });

  it('normalizes retired hosted availability copy on unactivated installs', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'expired' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        runtime_state: 'blocked',
        blocked_reason:
          'Connect your API key to use AI Patrol on this install. Hosted quickstart requires an activated entitlement.',
      }),
    );

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(
        screen.getAllByText('Connect your own AI provider or local model to use Pulse Patrol.')
          .length,
      ).toBeGreaterThan(0);
    });

    expect(screen.queryByText('Patrol quickstart exhausted')).not.toBeInTheDocument();
    expect(screen.queryByText(/Hosted quickstart requires/i)).not.toBeInTheDocument();
  });

  it('does not surface hosted-credit chips even when legacy runtime fields are present', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        runtime_state: 'active',
        last_patrol_at: '2026-03-12T09:57:00Z',
      }),
    );
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 100,
        grade: 'A',
        trend: 'stable',
        factors: [],
        prediction: 'Infrastructure is healthy with no significant issues detected.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(
        screen.getByRole('heading', { name: 'Watching your infrastructure' }),
      ).toBeInTheDocument();
    });

    expect(screen.queryByText(/Patrol quickstart/i)).not.toBeInTheDocument();
    expect(screen.queryByTitle(/No API key needed/i)).not.toBeInTheDocument();
  });

  it('surfaces coverage incomplete as the primary patrol assessment instead of no-issues copy', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        runtime_state: 'active',
        last_patrol_at: '2026-03-12T09:57:00Z',
      }),
    );
    getPatrolRunHistoryMock.mockResolvedValue([
      {
        id: 'run-scoped',
        started_at: '2026-03-12T09:56:00Z',
        completed_at: '2026-03-12T09:57:00Z',
        duration_ms: 60000,
        type: 'scoped',
        trigger_reason: 'alert_fired',
        scope_resource_ids: ['vm-100'],
        effective_scope_resource_ids: ['vm-100'],
        scope_resource_types: ['vm'],
        resources_checked: 1,
        nodes_checked: 0,
        guests_checked: 1,
        docker_checked: 0,
        storage_checked: 0,
        hosts_checked: 0,
        truenas_checked: 0,
        pbs_checked: 0,
        pmg_checked: 0,
        kubernetes_checked: 0,
        new_findings: 0,
        existing_findings: 0,
        rejected_findings: 0,
        resolved_findings: 0,
        auto_fix_count: 0,
        findings_summary: '',
        finding_ids: [],
        error_count: 1,
        status: 'error',
        triage_flags: 0,
        tool_call_count: 0,
      },
    ]);
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 70,
        grade: 'C',
        trend: 'stable',
        factors: [
          {
            name: 'Patrol coverage incomplete',
            impact: -0.35,
            description:
              'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
            category: 'coverage',
          },
        ],
        prediction:
          'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);
    await openPatrolActivityMode();

    await waitFor(() => {
      expect(screen.getByText('Open work')).toBeInTheDocument();
      expect(screen.queryByText('Next:')).not.toBeInTheDocument();
      expect(screen.queryByText('Verify full coverage')).not.toBeInTheDocument();
      expect(screen.queryByTestId('patrol-recommended-next-step-action')).not.toBeInTheDocument();
      expect(findingsPanelState.latestProps).not.toBeNull();
    });

    expect(screen.queryByText('Coverage incomplete')).not.toBeInTheDocument();
    expect(screen.queryByText(/health score 70\/100/)).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details-toggle')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details')).not.toBeInTheDocument();
    expect(screen.queryByText('No recent full patrol')).not.toBeInTheDocument();
    expect(screen.queryByText('No active issues')).not.toBeInTheDocument();
    expect(screen.queryByText(/Last patrol/i)).not.toBeInTheDocument();
    expect(screen.queryByText(/^Last:/i)).not.toBeInTheDocument();
    expect(screen.queryByText('Partial verification')).not.toBeInTheDocument();
    expect(screen.queryByText('Active findings')).not.toBeInTheDocument();
    expect(screen.queryByText('Warnings')).not.toBeInTheDocument();
    expect(screen.queryByText('Critical')).not.toBeInTheDocument();
    expect(
      screen.queryByText(
        'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
      ),
    ).not.toBeInTheDocument();
    expect(findingsPanelState.latestProps?.nextPatrolAt).toBeUndefined();
    expect(findingsPanelState.latestProps?.lastPatrolAt).toBeUndefined();
    expect(findingsPanelState.latestProps?.lastPatrolLabel).toBeUndefined();
    expect(findingsPanelState.latestProps?.patrolIntervalMs).toBeUndefined();
  });

  it('prefers last activity transport over last full patrol transport when no run history is loaded', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        runtime_state: 'active',
        last_patrol_at: '2026-03-12T09:30:00Z',
        last_activity_at: '2026-03-12T09:59:00Z',
      }),
    );
    getPatrolRunHistoryMock.mockResolvedValue([]);
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 100,
        grade: 'A',
        trend: 'stable',
        factors: [],
        prediction: 'Infrastructure is healthy with no significant issues detected.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByText(/Last activity:/i)).toBeInTheDocument();
    });

    expect(screen.queryByText(/Last full patrol:/i)).not.toBeInTheDocument();
  });

  it('describes both findings and incomplete coverage when active issues exist', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    getPatrolRunHistoryMock.mockResolvedValue([
      {
        id: 'run-full-error',
        started_at: '2026-03-12T09:56:00Z',
        completed_at: '2026-03-12T09:57:00Z',
        duration_ms: 60000,
        type: 'patrol',
        trigger_reason: 'startup',
        scope_resource_ids: [],
        effective_scope_resource_ids: [],
        scope_resource_types: [],
        resources_checked: 58,
        nodes_checked: 0,
        guests_checked: 0,
        docker_checked: 0,
        storage_checked: 0,
        hosts_checked: 0,
        truenas_checked: 0,
        pbs_checked: 0,
        pmg_checked: 0,
        kubernetes_checked: 0,
        new_findings: 1,
        existing_findings: 0,
        rejected_findings: 0,
        resolved_findings: 0,
        auto_fix_count: 0,
        findings_summary: '1 warning',
        finding_ids: ['finding-1'],
        error_count: 1,
        status: 'error',
        triage_flags: 0,
        tool_call_count: 0,
      },
    ]);
    intelligenceState.findings = [
      {
        id: 'finding-1',
        source: 'ai-patrol',
        isThreshold: false,
        status: 'active',
        severity: 'warning',
        resourceId: 'ai-service',
        resourceName: 'Pulse Patrol Service',
        resourceType: 'service',
        title: 'Pulse Patrol: Provider billing or quota issue',
        detectedAt: '2026-03-12T09:57:00Z',
      },
    ];
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 60,
        grade: 'C',
        trend: 'stable',
        factors: [
          {
            name: 'Patrol coverage incomplete',
            impact: -0.35,
            description:
              'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
            category: 'coverage',
          },
        ],
        prediction:
          'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
      },
      findings_count: {
        critical: 0,
        warning: 1,
        watch: 0,
        info: 0,
        total: 1,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Patrol needs setup' })).toBeInTheDocument();
    });

    expect(
      screen.queryByText(/1 Patrol runtime issue · health score 60\/100/),
    ).not.toBeInTheDocument();
    expect(
      screen.getByText(
        'Patrol cannot check infrastructure until its selected model passes the Patrol tool check.',
      ),
    ).toBeInTheDocument();
    expect(screen.getByRole('heading', { name: 'Patrol cannot run yet' })).toBeInTheDocument();
    expect(
      screen.queryByText('Fix the provider connection, then Patrol can check infrastructure.'),
    ).not.toBeInTheDocument();
    expect(screen.getByText('Provider billing or quota issue')).toBeInTheDocument();
    expect(screen.queryByText('Patrol setup issue')).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol readiness issue')).not.toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Check Patrol model' })).toHaveAttribute(
      'href',
      '/settings/pulse-intelligence/patrol',
    );
    expect(screen.queryByText('Runtime issue')).not.toBeInTheDocument();
    expect(screen.queryByText(/regressed \d+×/)).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /Runtime issue/i })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Check now' })).not.toBeInTheDocument();
    expect(screen.queryByRole('link', { name: 'Open Patrol settings' })).not.toBeInTheDocument();
    expect(screen.queryByText(/Automation:/)).not.toBeInTheDocument();
    const patrolControlAnchor = document.getElementById(PATROL_CONTROL_ANCHOR);
    expect(patrolControlAnchor).not.toBeNull();
    expect(patrolControlAnchor).toContainElement(
      screen.getByRole('group', { name: 'Patrol mode' }),
    );
    const patrolControl = within(patrolControlAnchor!);
    expect(patrolControl.getByText('Patrol mode')).toBeInTheDocument();
    expect(patrolControl.getAllByText('Watch only').length).toBeGreaterThan(0);
    expect(
      patrolControl.getByText(
        'Patrol checks infrastructure and reports issues only. It does not start fixes.',
      ),
    ).toBeInTheDocument();
    expect(patrolControl.getByRole('button', { name: 'Ask first' })).not.toBeDisabled();
    expect(patrolControl.getByRole('button', { name: 'Safe auto-fix' })).not.toBeDisabled();
    expect(patrolControl.getByRole('button', { name: 'Autopilot' })).not.toBeDisabled();
    expect(patrolControl.queryByRole('button', { name: 'Limits' })).not.toBeInTheDocument();
    expect(screen.queryByText(/Current mode:/)).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol status')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details-toggle')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details')).not.toBeInTheDocument();
    expect(screen.queryByText('Infrastructure findings')).not.toBeInTheDocument();
    expect(screen.queryByText('Warnings')).not.toBeInTheDocument();
    expect(screen.queryByText('Latest activity')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-status-bar')).not.toBeInTheDocument();

    expect(screen.queryByRole('heading', { name: 'Open work' })).not.toBeInTheDocument();
    expect(screen.getByRole('region', { name: 'Patrol decision inbox' })).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Active' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'All' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Resolved' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Details' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'History' })).not.toBeInTheDocument();

    expect(
      screen.queryByText(
        'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
      ),
    ).not.toBeInTheDocument();
  });

  it('keeps provider-blocked open issues to one provider action', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        license_required: false,
        readiness: {
          status: 'not_ready',
          ready: false,
          summary: 'Provider billing or quota issue.',
          provider: 'openrouter',
          model: 'openrouter:z-ai/glm-5.2',
        },
      }),
    );
    intelligenceState.findings = [
      {
        id: 'finding-storage-risk',
        source: 'ai-patrol',
        isThreshold: false,
        status: 'active',
        severity: 'critical',
        resourceId: 'storage:tower-array',
        resourceName: 'Tower Array',
        resourceType: 'storage',
        title: 'Unraid array running without parity protection',
        detectedAt: '2026-03-12T09:57:00Z',
      },
    ];

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByText('Patrol model issue')).toBeInTheDocument();
    });
    expect(screen.getByRole('region', { name: 'Patrol decision inbox' })).toBeInTheDocument();
    const providerActions = screen.getAllByRole('link', { name: /Check Patrol model/i });
    expect(providerActions.length).toBeGreaterThan(0);
    expect(providerActions[0]).toHaveAttribute('href', '/settings/pulse-intelligence/patrol');
    expect(screen.queryByRole('link', { name: 'Open Patrol settings' })).not.toBeInTheDocument();
  });

  it('does not repeat stale coverage caveats after a successful full patrol checked resources', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(
      defaultPatrolStatus({
        license_required: false,
        trust: {
          tracked: 3,
          currently_active: 1,
          resolved: 2,
          auto_resolved: 0,
          fix_verified: 0,
          fix_failed: 0,
          dismissed_as_noise: 0,
          dismissed_as_expected: 0,
          dismissed_as_later: 0,
          suppressed: 0,
          regressed_at_least_once: 2,
        },
      }),
    );
    getPatrolRunHistoryMock.mockResolvedValue([
      {
        id: 'run-full-success',
        started_at: '2026-03-12T09:56:00Z',
        completed_at: '2026-03-12T09:57:00Z',
        duration_ms: 60000,
        type: 'patrol',
        trigger_reason: 'manual',
        scope_resource_ids: [],
        effective_scope_resource_ids: [],
        scope_resource_types: [],
        resources_checked: 58,
        nodes_checked: 0,
        guests_checked: 0,
        docker_checked: 0,
        storage_checked: 0,
        hosts_checked: 0,
        truenas_checked: 0,
        pbs_checked: 0,
        pmg_checked: 0,
        kubernetes_checked: 0,
        new_findings: 1,
        existing_findings: 0,
        rejected_findings: 0,
        resolved_findings: 0,
        auto_fix_count: 0,
        findings_summary: '1 warning',
        finding_ids: ['finding-1'],
        error_count: 0,
        status: 'issues_found',
        triage_flags: 0,
        tool_call_count: 0,
      },
    ]);
    intelligenceState.findings = [
      {
        id: 'finding-1',
        source: 'ai-patrol',
        isThreshold: false,
        status: 'active',
        severity: 'warning',
        resourceId: 'backup-1',
        resourceName: 'delly',
        resourceType: 'backup',
        title: 'Backup failed',
        detectedAt: '2026-03-12T09:57:00Z',
      },
    ];
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:00:00Z',
      overall_health: {
        score: 85,
        grade: 'B',
        trend: 'stable',
        factors: [
          {
            name: 'Patrol coverage incomplete',
            impact: -0.35,
            description:
              'Recent Patrol activity only covered targeted checks and ended with errors. Run Patrol to check everything.',
            category: 'coverage',
          },
        ],
        prediction:
          'Recent Patrol runs encountered errors, so the current health summary may be incomplete.',
      },
      findings_count: {
        critical: 0,
        warning: 1,
        watch: 0,
        info: 0,
        total: 1,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(screen.getByText(/checked 58 resources/i)).toBeInTheDocument();
    });
    expect(
      screen.queryByText(/1 warning issue · 2 past regressions · health score 85\/100/),
    ).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol status')).not.toBeInTheDocument();
    expect(screen.queryByLabelText('Patrol trust summary header')).not.toBeInTheDocument();
    expect(screen.queryByLabelText('Patrol trust summary')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details-toggle')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details')).not.toBeInTheDocument();

    expect(
      screen.queryByText(
        'Recent Patrol runs encountered errors, so the current health summary may be incomplete.',
      ),
    ).not.toBeInTheDocument();
    expect(screen.queryByText(/Recent coverage is also incomplete/i)).not.toBeInTheDocument();
  });

  it('keeps recent activity mix out of the default Patrol status chrome', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    getPatrolRunHistoryMock.mockResolvedValue([
      {
        id: 'run-scoped-alert',
        started_at: '2026-03-12T10:00:00Z',
        completed_at: '2026-03-12T10:01:00Z',
        duration_ms: 60000,
        type: 'scoped',
        trigger_reason: 'alert_fired',
        scope_resource_ids: [],
        effective_scope_resource_ids: [],
        scope_resource_types: [],
        resources_checked: 1,
        nodes_checked: 0,
        guests_checked: 0,
        docker_checked: 0,
        storage_checked: 0,
        hosts_checked: 0,
        truenas_checked: 0,
        pbs_checked: 0,
        pmg_checked: 0,
        kubernetes_checked: 0,
        new_findings: 0,
        existing_findings: 0,
        rejected_findings: 0,
        resolved_findings: 0,
        auto_fix_count: 0,
        findings_summary: '',
        finding_ids: [],
        error_count: 0,
        status: 'healthy',
        triage_flags: 0,
        tool_call_count: 0,
      },
      {
        id: 'run-scoped-anomaly',
        started_at: '2026-03-12T09:58:00Z',
        completed_at: '2026-03-12T09:59:00Z',
        duration_ms: 60000,
        type: 'scoped',
        trigger_reason: 'anomaly',
        scope_resource_ids: [],
        effective_scope_resource_ids: [],
        scope_resource_types: [],
        resources_checked: 1,
        nodes_checked: 0,
        guests_checked: 0,
        docker_checked: 0,
        storage_checked: 0,
        hosts_checked: 0,
        truenas_checked: 0,
        pbs_checked: 0,
        pmg_checked: 0,
        kubernetes_checked: 0,
        new_findings: 0,
        existing_findings: 0,
        rejected_findings: 0,
        resolved_findings: 0,
        auto_fix_count: 0,
        findings_summary: '',
        finding_ids: [],
        error_count: 0,
        status: 'healthy',
        triage_flags: 0,
        tool_call_count: 0,
      },
      {
        id: 'run-full',
        started_at: '2026-03-12T09:50:00Z',
        completed_at: '2026-03-12T09:57:00Z',
        duration_ms: 420000,
        type: 'patrol',
        trigger_reason: 'scheduled',
        scope_resource_ids: [],
        effective_scope_resource_ids: [],
        scope_resource_types: [],
        resources_checked: 58,
        nodes_checked: 0,
        guests_checked: 0,
        docker_checked: 0,
        storage_checked: 0,
        hosts_checked: 0,
        truenas_checked: 0,
        pbs_checked: 0,
        pmg_checked: 0,
        kubernetes_checked: 0,
        new_findings: 0,
        existing_findings: 0,
        rejected_findings: 0,
        resolved_findings: 0,
        auto_fix_count: 0,
        findings_summary: 'No active findings',
        finding_ids: [],
        error_count: 0,
        status: 'healthy',
        triage_flags: 0,
        tool_call_count: 0,
      },
    ]);
    intelligenceState.summary = {
      timestamp: '2026-03-12T10:05:00Z',
      overall_health: {
        score: 100,
        grade: 'A',
        trend: 'stable',
        factors: [],
        prediction: 'Infrastructure is healthy with no significant issues detected.',
      },
      findings_count: {
        critical: 0,
        warning: 0,
        watch: 0,
        info: 0,
        total: 0,
      },
      predictions_count: 0,
      recent_changes_count: 0,
      learning: {
        resources_with_knowledge: 0,
        total_notes: 0,
        resources_with_baselines: 0,
        patterns_detected: 0,
        correlations_learned: 0,
        incidents_tracked: 0,
      },
    };

    render(() => <AIIntelligence />);

    await waitFor(() => {
      expect(getPatrolRunHistoryMock).toHaveBeenCalled();
      expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
    });

    expect(screen.queryByText(/No active issues · health score 100\/100/)).not.toBeInTheDocument();
    expect(screen.queryByText('Patrol status')).not.toBeInTheDocument();
    expect(screen.queryByTestId('patrol-summary-details-toggle')).not.toBeInTheDocument();
    expect(screen.queryByText(/Recent activity mix:/)).not.toBeInTheDocument();
    expect(screen.queryByText(/Trigger mode:/)).not.toBeInTheDocument();
  });

  it('treats a selected zero-finding run as an empty snapshot and uses effective scope ids', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    intelligenceState.findings = [];
    runHistoryState.selection = {
      id: 'run-empty',
      started_at: '2026-03-12T10:00:00Z',
      completed_at: '2026-03-12T10:01:00Z',
      duration_ms: 60000,
      type: 'scoped',
      trigger_reason: 'alert_fired',
      scope_resource_ids: ['seed-resource'],
      effective_scope_resource_ids: ['expanded-a', 'expanded-b'],
      scope_resource_types: ['vm'],
      resources_checked: 2,
      nodes_checked: 0,
      guests_checked: 2,
      docker_checked: 0,
      storage_checked: 0,
      hosts_checked: 0,
      truenas_checked: 0,
      pbs_checked: 0,
      pmg_checked: 0,
      kubernetes_checked: 0,
      new_findings: 0,
      existing_findings: 0,
      rejected_findings: 0,
      resolved_findings: 0,
      auto_fix_count: 0,
      findings_summary: 'All healthy',
      finding_ids: [],
      error_count: 0,
      status: 'healthy',
      tool_call_count: 0,
    };

    render(() => <AIIntelligence />);
    await openPatrolActivityMode();

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(findingsPanelState.latestProps).not.toBeNull();
    });

    fireEvent.click(screen.getByRole('button', { name: 'History' }));
    fireEvent.click(screen.getByRole('button', { name: 'Select mocked run' }));

    await waitFor(() => {
      expect(screen.getAllByText(/Patrol run/i).length).toBeGreaterThan(0);
    });

    expect(screen.queryByText('Finding record unavailable')).not.toBeInTheDocument();

    expect(findingsPanelState.latestProps).toMatchObject({
      filterOverride: 'all',
      filterFindingIds: [],
      scopeResourceIds: ['expanded-a', 'expanded-b'],
      scopeResourceTypes: ['vm'],
      showScopeWarnings: true,
      runSnapshotId: 'run-empty',
    });
  });

  it('shows the selected Patrol run outcome before the finding list', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    intelligenceState.findings = [];
    runHistoryState.selection = {
      id: 'run-provider-error',
      started_at: '2026-03-12T10:00:00Z',
      completed_at: '2026-03-12T10:58:00Z',
      duration_ms: 3_480_000,
      type: 'full',
      trigger_reason: 'manual',
      scope_resource_ids: [],
      effective_scope_resource_ids: [],
      scope_resource_types: [],
      resources_checked: 72,
      nodes_checked: 0,
      guests_checked: 72,
      docker_checked: 0,
      storage_checked: 0,
      hosts_checked: 0,
      truenas_checked: 0,
      pbs_checked: 0,
      pmg_checked: 0,
      kubernetes_checked: 0,
      new_findings: 0,
      existing_findings: 0,
      rejected_findings: 0,
      resolved_findings: 0,
      auto_fix_count: 0,
      findings_summary: 'Provider failed during analysis',
      finding_ids: ['runtime-provider'],
      error_count: 1,
      error_summary: 'Selected model does not support Patrol tools',
      error_detail:
        "agentic patrol failed: API error (404): No endpoints found that support the provided 'tool_choice' value.",
      status: 'error',
      tool_call_count: 0,
    };

    render(() => <AIIntelligence />);
    await openPatrolActivityMode();

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(findingsPanelState.latestProps).not.toBeNull();
    });

    fireEvent.click(screen.getByRole('button', { name: 'History' }));
    fireEvent.click(screen.getByRole('button', { name: 'Select mocked run' }));

    await waitFor(() => {
      expect(screen.getAllByText(/Patrol run/i).length).toBeGreaterThan(0);
    });

    expect(screen.getByText('Manual')).toBeInTheDocument();
    expect(screen.getByText('error')).toBeInTheDocument();
    expect(
      screen.getByText(
        /Checked 72 resources in 58m\. Patrol ended with a runtime issue: Selected model does not support Patrol tools/i,
      ),
    ).toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Check Patrol model' })).toHaveAttribute(
      'href',
      '/settings/pulse-intelligence/patrol',
    );
    expect(findingsPanelState.latestProps).toMatchObject({
      filterOverride: 'all',
      filterFindingIds: ['runtime-provider'],
      scopeResourceIds: [],
      runSnapshotId: 'run-provider-error',
    });
  });

  it('keeps an explicit empty effective scope instead of falling back to seed scope ids', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    runHistoryState.selection = {
      id: 'run-empty-effective-scope',
      started_at: '2026-03-12T10:05:00Z',
      completed_at: '2026-03-12T10:06:00Z',
      duration_ms: 60000,
      type: 'scoped',
      trigger_reason: 'alert_fired',
      scope_resource_ids: ['seed-resource'],
      effective_scope_resource_ids: [],
      scope_resource_types: ['vm'],
      resources_checked: 0,
      nodes_checked: 0,
      guests_checked: 0,
      docker_checked: 0,
      storage_checked: 0,
      hosts_checked: 0,
      truenas_checked: 0,
      pbs_checked: 0,
      pmg_checked: 0,
      kubernetes_checked: 0,
      new_findings: 0,
      existing_findings: 0,
      rejected_findings: 0,
      resolved_findings: 0,
      auto_fix_count: 0,
      findings_summary: 'Nothing matched',
      finding_ids: [],
      error_count: 0,
      status: 'healthy',
      tool_call_count: 0,
    };

    render(() => <AIIntelligence />);
    await openPatrolActivityMode();

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(findingsPanelState.latestProps).not.toBeNull();
    });

    fireEvent.click(screen.getByRole('button', { name: 'History' }));
    fireEvent.click(screen.getByRole('button', { name: 'Select mocked run' }));

    await waitFor(() => {
      expect(screen.getAllByText(/Patrol run/i).length).toBeGreaterThan(0);
    });

    expect(screen.queryByText('Finding record unavailable')).not.toBeInTheDocument();
    expect(findingsPanelState.latestProps).toMatchObject({
      filterOverride: 'all',
      filterFindingIds: [],
      scopeResourceIds: [],
      scopeResourceTypes: ['vm'],
      showScopeWarnings: true,
      runSnapshotId: 'run-empty-effective-scope',
    });
  });

  it('does not turn a missing snapshot finding list into an empty snapshot filter', async () => {
    hasFeatureMock.mockReturnValue(true);
    licenseStatusMock.mockReturnValue({ subscription_state: 'active' });
    getPatrolStatusMock.mockResolvedValue(defaultPatrolStatus({ license_required: false }));
    intelligenceState.findings = [];
    runHistoryState.selection = {
      id: 'run-missing-snapshot',
      started_at: '2026-03-12T10:10:00Z',
      completed_at: '2026-03-12T10:11:00Z',
      duration_ms: 60000,
      type: 'scoped',
      trigger_reason: 'alert_fired',
      scope_resource_ids: ['seed-resource'],
      effective_scope_resource_ids: ['expanded-a'],
      scope_resource_types: ['vm'],
      resources_checked: 1,
      nodes_checked: 0,
      guests_checked: 1,
      docker_checked: 0,
      storage_checked: 0,
      hosts_checked: 0,
      truenas_checked: 0,
      pbs_checked: 0,
      pmg_checked: 0,
      kubernetes_checked: 0,
      new_findings: 0,
      existing_findings: 1,
      rejected_findings: 0,
      resolved_findings: 0,
      auto_fix_count: 0,
      findings_summary: 'Legacy run without snapshot ids',
      error_count: 0,
      status: 'issues_found',
      tool_call_count: 0,
    };

    render(() => <AIIntelligence />);
    await openPatrolActivityMode();

    await waitFor(() => {
      expect(getPatrolStatusMock).toHaveBeenCalled();
      expect(findingsPanelState.latestProps).not.toBeNull();
    });

    fireEvent.click(screen.getByRole('button', { name: 'History' }));
    fireEvent.click(screen.getByRole('button', { name: 'Select mocked run' }));

    await waitFor(() => {
      expect(screen.getAllByText(/Patrol run/i).length).toBeGreaterThan(0);
    });

    expect(screen.getByText(/Finding record unavailable/)).toBeInTheDocument();

    expect(findingsPanelState.latestProps).toMatchObject({
      filterOverride: 'all',
      filterFindingIds: undefined,
      scopeResourceIds: ['expanded-a'],
      scopeResourceTypes: ['vm'],
      showScopeWarnings: true,
      runSnapshotId: 'run-missing-snapshot',
    });
  });

  describe('Patrol runtime block cause (#1789)', () => {
    // A used-up cost budget blocks Patrol while readiness still reads "none":
    // every setup action must follow the block cause to the budget field
    // instead of sending the operator to the model check.
    const budgetReadiness = {
      status: 'not_ready',
      ready: false,
      cause: 'none',
      summary: 'Patrol is paused by the 30-day cost budget.',
      provider: 'ollama',
      model: 'ollama:llama3',
      checks: [],
    };

    it('points the paused banner and the setup card at the cost budget', async () => {
      getPatrolStatusMock.mockResolvedValue(
        defaultPatrolStatus({
          runtime_state: 'blocked',
          blocked_reason:
            'Patrol is paused until the 30-day AI cost budget is raised or the 30-day window rolls on.',
          blocked_cause: 'budget_exhausted',
          readiness: budgetReadiness,
        }),
      );

      render(() => <AIIntelligence />);

      await waitFor(() => {
        expect(screen.getAllByText('Patrol paused').length).toBeGreaterThan(0);
        expect(screen.getByRole('heading', { name: 'Patrol cannot run yet' })).toBeInTheDocument();
      });
      const budget = getPatrolSetupAction('budget_exhausted');
      const links = screen.getAllByRole('link', { name: budget.label });
      // The paused banner and the setup card each offer the budget action.
      expect(links.length).toBeGreaterThanOrEqual(2);
      for (const link of links) {
        expect(link).toHaveAttribute('href', budget.href);
      }
      expect(screen.queryByRole('link', { name: 'Check Patrol model' })).not.toBeInTheDocument();
    });

    it('keeps the budget action after a restart from the persisted runtime finding', async () => {
      // The in-memory block state does not survive a restart; the saved Patrol
      // runtime finding carries the same cause until the next run.
      intelligenceState.findings = [
        {
          id: 'patrol-runtime-budget',
          source: 'ai-patrol',
          isThreshold: false,
          status: 'active',
          severity: 'critical',
          resourceId: 'ai-service',
          resourceName: 'Pulse Patrol Service',
          resourceType: 'service',
          title: 'Pulse Patrol: Cost budget exhausted',
          failureCause: 'budget_exhausted',
          detectedAt: '2026-03-12T09:57:00Z',
        },
      ];

      render(() => <AIIntelligence />);

      const budget = getPatrolSetupAction('budget_exhausted');
      await waitFor(() => {
        expect(screen.getByRole('heading', { name: 'Patrol cannot run yet' })).toBeInTheDocument();
      });
      const link = screen.getByRole('link', { name: budget.label });
      expect(link).toHaveAttribute('href', budget.href);
    });
  });

  describe('Patrol data refresh failures', () => {
    const loadPatrolFindings = () => vi.mocked(aiIntelligenceStore.loadPatrolFindings);

    it('keeps the page and offers Retry when a Patrol data load rejects, then clears the notice', async () => {
      loadPatrolFindings().mockRejectedValue(new Error('findings unavailable'));
      try {
        render(() => <AIIntelligence />);

        expect(await screen.findByText('Patrol could not refresh')).toBeInTheDocument();
        expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
        expect(screen.queryByText('findings unavailable')).not.toBeInTheDocument();

        loadPatrolFindings().mockResolvedValue(undefined);
        fireEvent.click(screen.getByRole('button', { name: 'Retry' }));

        await waitFor(() => {
          expect(screen.queryByText('Patrol could not refresh')).not.toBeInTheDocument();
        });
      } finally {
        loadPatrolFindings().mockResolvedValue(undefined);
      }
    });

    it('does not let a slow earlier refresh raise a notice after a newer one succeeded', async () => {
      let failEarlier!: (error: Error) => void;
      loadPatrolFindings().mockClear();
      loadPatrolFindings().mockImplementationOnce(
        () =>
          new Promise<undefined>((_resolve, reject) => {
            failEarlier = reject;
          }),
      );
      try {
        render(() => <AIIntelligence />);
        await waitFor(() => expect(loadPatrolFindings()).toHaveBeenCalledTimes(1));

        // The tab becoming visible again starts a newer refresh, which succeeds.
        document.dispatchEvent(new Event('visibilitychange'));
        await waitFor(() => expect(loadPatrolFindings()).toHaveBeenCalledTimes(2));

        failEarlier(new Error('stale refresh failed'));
        await new Promise((resolve) => setTimeout(resolve, 20));

        expect(screen.queryByText('Patrol could not refresh')).not.toBeInTheDocument();
        expect(screen.getByRole('heading', { name: 'Patrol' })).toBeInTheDocument();
      } finally {
        loadPatrolFindings().mockResolvedValue(undefined);
      }
    });

    it('frees Retry after the manual sync window and ignores an older retry finishing late', async () => {
      loadPatrolFindings().mockRejectedValue(new Error('findings unavailable'));
      let failFirst!: (error: Error) => void;
      let failSecond!: (error: Error) => void;
      try {
        render(() => <AIIntelligence />);
        const retry = await screen.findByRole('button', { name: 'Retry' });

        loadPatrolFindings().mockImplementationOnce(
          () =>
            new Promise<undefined>((_resolve, reject) => {
              failFirst = reject;
            }),
        );
        vi.useFakeTimers();

        fireEvent.click(retry);
        expect(retry).toBeDisabled();
        await vi.advanceTimersByTimeAsync(PATROL_MANUAL_SYNC_TIMEOUT_MS - 1);
        expect(retry).toBeDisabled();
        await vi.advanceTimersByTimeAsync(1);
        expect(retry).toBeEnabled();

        // A second retry starts while the first is still hanging.
        loadPatrolFindings().mockImplementationOnce(
          () =>
            new Promise<undefined>((_resolve, reject) => {
              failSecond = reject;
            }),
        );
        fireEvent.click(retry);
        expect(retry).toBeDisabled();

        // The older retry finishing late must not release the newer one.
        failFirst(new Error('first retry failed'));
        await vi.advanceTimersByTimeAsync(0);
        expect(retry).toBeDisabled();

        failSecond(new Error('second retry failed'));
        await vi.advanceTimersByTimeAsync(0);
        expect(retry).toBeEnabled();
      } finally {
        vi.useRealTimers();
        loadPatrolFindings().mockResolvedValue(undefined);
      }
    });
  });
});