Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

44.8 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen, waitFor, within } from '@solidjs/testing-library';
import { Route, Router } from '@solidjs/router';

import type { APITokenRecord } from '@/api/security';
import type { Resource } from '@/types/resource';
import {
  AI_EXECUTE_SCOPE,
  AGENT_CONFIG_READ_SCOPE,
  AGENT_REPORT_SCOPE,
  AUDIT_READ_SCOPE,
  DOCKER_MANAGE_SCOPE,
  DOCKER_REPORT_SCOPE,
  MONITORING_READ_SCOPE,
  MONITORING_WRITE_SCOPE,
  SETTINGS_READ_SCOPE,
  SETTINGS_WRITE_SCOPE,
} from '@/constants/apiScopes';
import { API_TOKEN_CREATE_ANCHOR, PULSE_MCP_TOKEN_SETUP_PATH } from '@/routing/resourceLinks';
import apiTokenManagerSource from '../APITokenManager.tsx?raw';
import { APIAccessPanel } from '../APIAccessPanel';
import { APITokenManager } from '../APITokenManager';

const listTokensMock = vi.fn();
const createTokenMock = vi.fn();
const updateTokenScopesMock = vi.fn();
const renameTokenMock = vi.fn();
const deleteTokenMock = vi.fn();
const fetchAgentCapabilitiesManifestMock = vi.fn();
const notificationSuccessMock = vi.fn();
const notificationErrorMock = vi.fn();
const showTokenRevealMock = vi.fn();
const loggerErrorMock = vi.fn();
const markDockerRuntimesTokenRevokedMock = vi.fn();
const markAgentsTokenRevokedMock = vi.fn();
const scrollIntoViewMock = vi.fn();

let mockResources: Resource[] = [];

vi.mock('@/api/security', () => ({
  SecurityAPI: {
    listTokens: (...args: unknown[]) => listTokensMock(...args),
    createToken: (...args: unknown[]) => createTokenMock(...args),
    updateTokenScopes: (...args: unknown[]) => updateTokenScopesMock(...args),
    renameToken: (...args: unknown[]) => renameTokenMock(...args),
    deleteToken: (...args: unknown[]) => deleteTokenMock(...args),
  },
}));

vi.mock('@/api/agentCapabilities', async (importOriginal) => {
  const actual = await importOriginal<typeof import('@/api/agentCapabilities')>();
  return {
    ...actual,
    fetchAgentCapabilitiesManifest: (...args: unknown[]) =>
      fetchAgentCapabilitiesManifestMock(...args),
  };
});

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

vi.mock('@/stores/tokenReveal', () => ({
  dismissTokenReveal: vi.fn(),
  showTokenReveal: (...args: unknown[]) => showTokenRevealMock(...args),
  useTokenRevealState: () => () => null,
}));

vi.mock('@/utils/logger', () => ({
  logger: {
    error: (...args: unknown[]) => loggerErrorMock(...args),
    debug: vi.fn(),
    warn: vi.fn(),
  },
}));

vi.mock('@/utils/url', () => ({
  getPulseBaseUrl: () => 'https://pulse.example.com',
}));

vi.mock('@/contexts/appRuntime', () => ({
  useWebSocket: () => ({
    markDockerRuntimesTokenRevoked: (...args: unknown[]) =>
      markDockerRuntimesTokenRevokedMock(...args),
    markAgentsTokenRevoked: (...args: unknown[]) => markAgentsTokenRevokedMock(...args),
  }),
}));

vi.mock('@/hooks/useResources', () => ({
  useResources: () => ({
    resources: () => mockResources,
    byType: (type: string) => mockResources.filter((resource) => resource.type === type),
  }),
}));

const makeToken = (overrides: Partial<APITokenRecord> = {}): APITokenRecord => ({
  id: 'token-1',
  name: 'Runtime token',
  prefix: 'pulse',
  suffix: '1234',
  createdAt: '2026-03-12T10:00:00.000Z',
  lastUsedAt: '2026-03-12T11:00:00.000Z',
  scopes: [DOCKER_REPORT_SCOPE],
  ...overrides,
});

const makeResource = (overrides: Partial<Resource> = {}): Resource => ({
  id: 'resource-1',
  type: 'agent',
  name: 'Resource One',
  displayName: 'Resource One',
  platformId: 'agent-1',
  platformType: 'agent',
  sourceType: 'agent',
  status: 'online',
  lastSeen: Date.now(),
  tags: [],
  ...overrides,
});

const renderAPIAccessPanel = () =>
  render(() => (
    <Router>
      <Route
        path="/*"
        component={() => <APIAccessPanel onTokensChanged={vi.fn()} refreshing={false} canManage />}
      />
    </Router>
  ));

const getTokenTableRow = (name: string): HTMLTableRowElement => {
  const row = screen
    .getAllByText(name)
    .map((element) => element.closest('tr'))
    .find(
      (candidate): candidate is HTMLTableRowElement => candidate instanceof HTMLTableRowElement,
    );
  if (!row) throw new Error(`Could not find desktop token row for ${name}`);
  return row;
};

const tokenCell = (row: HTMLTableRowElement, label: string) => {
  const headers = [...row.closest('table')!.querySelectorAll('thead th')];
  const index = headers.findIndex((header) => header.textContent?.trim() === label);
  if (index < 0) throw new Error(`Token table has no ${label} header`);
  return row.cells[index];
};

const createdTokenCell = (row: HTMLTableRowElement) => tokenCell(row, 'Created');

const findTokenTableRow = async (name: string): Promise<HTMLTableRowElement> => {
  await screen.findAllByText(name);
  return getTokenTableRow(name);
};

describe('APITokenManager security surface', () => {
  // The API Access tab is the canonical security surface for
  // operator-controlled machine access. Pulse Intelligence owns
  // external-agent setup; API Access only mints and manages the
  // scoped token that setup links to when credentials are needed.
  it('keeps API Access separate from Pulse Intelligence external-agent setup', () => {
    expect(apiTokenManagerSource).toContain(
      'flex flex-col items-stretch gap-4 sm:flex-row sm:items-start sm:justify-between',
    );
    expect(apiTokenManagerSource).toContain('min-h-11 w-full items-center justify-center');
    expect(apiTokenManagerSource).toContain('sm:min-h-10 sm:w-auto');
  });
});

describe('APITokenManager', () => {
  it('ages Created in both layouts without another list read, token write or losing a draft name', async () => {
    const start = Date.parse('2026-10-11T11:00:00Z');
    vi.useFakeTimers({ now: start });
    const token = makeToken({
      createdAt: new Date(start - 60_000).toISOString(),
      lastUsedAt: undefined,
    });
    listTokensMock.mockResolvedValue([token]);
    const view = render(() => <APITokenManager canManage />);
    await vi.advanceTimersByTimeAsync(0);
    const row = getTokenTableRow(token.name!);
    const card = screen.getByRole('article', { name: `API token ${token.name}` });
    expect(createdTokenCell(row)).toHaveTextContent('1 min ago');
    expect(within(card).getByText('Created').nextElementSibling).toHaveTextContent('1 min ago');
    const input = screen.getByLabelText('Token name');
    fireEvent.input(input, { target: { value: 'Unsaved token name' } });
    input.focus();
    await vi.advanceTimersByTimeAsync(2 * 3_600_000);
    expect(createdTokenCell(row)).toHaveTextContent('2 hours ago');
    expect(within(card).getByText('Created').nextElementSibling).toHaveTextContent('2 hours ago');
    expect(input).toHaveValue('Unsaved token name');
    expect(input).toHaveFocus();
    expect(getTokenTableRow(token.name!)).toBe(row);
    expect(within(card).getByText('Never')).toBeInTheDocument();
    expect(listTokensMock).toHaveBeenCalledTimes(1);
    expect(createTokenMock).not.toHaveBeenCalled();
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(updateTokenScopesMock).not.toHaveBeenCalled();
    expect(renameTokenMock).not.toHaveBeenCalled();
    expect(token.createdAt).toBe(new Date(start - 60_000).toISOString());
    view.unmount();
  });

  it('ages Last used in both layouts without another list read', async () => {
    const start = Date.parse('2026-10-16T22:00:00Z');
    vi.useFakeTimers({ now: start });
    const token = makeToken({
      createdAt: new Date(start - 86_400_000).toISOString(),
      lastUsedAt: new Date(start - 60_000).toISOString(),
    });
    listTokensMock.mockResolvedValue([token]);
    const view = render(() => <APITokenManager canManage />);
    await vi.advanceTimersByTimeAsync(0);
    const row = getTokenTableRow(token.name!);
    const card = screen.getByRole('article', { name: `API token ${token.name}` });
    const cardLastUsed = () => within(card).getByText('Last used').nextElementSibling;
    expect(tokenCell(row, 'Last used')).toHaveTextContent('1 min ago');
    expect(cardLastUsed()).toHaveTextContent('1 min ago');
    await vi.advanceTimersByTimeAsync(2 * 3_600_000);
    expect(tokenCell(row, 'Last used')).toHaveTextContent('2 hours ago');
    expect(cardLastUsed()).toHaveTextContent('2 hours ago');
    expect(getTokenTableRow(token.name!)).toBe(row);
    expect(listTokensMock).toHaveBeenCalledTimes(1);
    view.unmount();
  });

  beforeEach(() => {
    listTokensMock.mockReset();
    createTokenMock.mockReset();
    updateTokenScopesMock.mockReset();
    renameTokenMock.mockReset();
    deleteTokenMock.mockReset();
    notificationSuccessMock.mockReset();
    notificationErrorMock.mockReset();
    showTokenRevealMock.mockReset();
    loggerErrorMock.mockReset();
    markDockerRuntimesTokenRevokedMock.mockReset();
    markAgentsTokenRevokedMock.mockReset();
    fetchAgentCapabilitiesManifestMock.mockReset();
    scrollIntoViewMock.mockReset();
    Element.prototype.scrollIntoView = scrollIntoViewMock;

    mockResources = [];
    listTokensMock.mockResolvedValue([]);
    fetchAgentCapabilitiesManifestMock.mockResolvedValue({
      version: 'v1',
      surfaceContract: {
        core: { id: 'pulse_intelligence_core', label: 'Pulse Intelligence Core', description: '' },
        proactiveEngine: { id: 'pulse_patrol', label: 'Pulse Patrol', description: '' },
        operatorSurfaces: [],
      },
      mcpAdapter: {
        serverName: 'pulse',
        command: 'pulse-mcp',
        baseUrlFlag: '--base-url',
        defaultBaseUrl: 'http://localhost:7655',
        tokenEnv: 'PULSE_API_TOKEN',
        configFamilies: [],
      },
      requiredScopes: [
        MONITORING_READ_SCOPE,
        MONITORING_WRITE_SCOPE,
        SETTINGS_READ_SCOPE,
        SETTINGS_WRITE_SCOPE,
        AI_EXECUTE_SCOPE,
      ],
      categories: [],
      capabilities: [],
    });
    createTokenMock.mockResolvedValue({
      token: 'pulse_secret_value',
      record: makeToken({
        id: 'token-created',
        name: 'Container automation',
        scopes: [DOCKER_MANAGE_SCOPE, DOCKER_REPORT_SCOPE],
      }),
    });
    deleteTokenMock.mockResolvedValue(undefined);
    updateTokenScopesMock.mockImplementation(
      async (id: string, scopes: string[]): Promise<APITokenRecord> => makeToken({ id, scopes }),
    );
    renameTokenMock.mockImplementation(async (id: string, name: string) => makeToken({ id, name }));
  });

  afterEach(() => {
    cleanup();
    vi.useRealTimers();
    window.history.pushState({}, '', '/');
  });

  it('keeps the inventory unknown while loading, then admits a confirmed empty list', async () => {
    let completeRead!: (tokens: APITokenRecord[]) => void;
    listTokensMock.mockReturnValueOnce(
      new Promise<APITokenRecord[]>((resolve) => {
        completeRead = resolve;
      }),
    );
    render(() => <APITokenManager canManage />);

    expect(screen.getByRole('status', { name: 'Loading token inventory' })).toBeInTheDocument();
    expect(screen.getAllByText('Loading…')).toHaveLength(3);
    expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
    expect(screen.queryByText(/no wildcard credentials detected/i)).not.toBeInTheDocument();
    expect(screen.queryByText('No full access tokens in this inventory.')).not.toBeInTheDocument();

    completeRead([]);
    await screen.findByText(/No tokens yet/);
    expect(screen.getAllByText('0')).toHaveLength(3);
    expect(screen.getByText('No full access tokens in this inventory.')).toBeInTheDocument();
    expect(listTokensMock).toHaveBeenCalledTimes(1);
  });

  it.each([403, 500, undefined])('keeps a failed inventory read unknown (%s)', async (status) => {
    listTokensMock.mockRejectedValueOnce(
      Object.assign(new Error('private-proxy-body'), { status }),
    );
    mockResources = [
      makeResource({
        platformData: { agent: { agentId: 'agent-1', tokenId: 'unread-token' } },
      }),
    ];
    render(() => <APITokenManager canManage />);

    const notice = await screen.findByRole('alert');
    expect(notice).toHaveTextContent('Unable to load API tokens.');
    expect(notice).toHaveTextContent('Existing credentials have not been changed.');
    expect(screen.getAllByText('Unknown')).toHaveLength(3);
    expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
    expect(screen.queryByText(/no wildcard credentials detected/i)).not.toBeInTheDocument();
    expect(screen.queryByText('No full access tokens in this inventory.')).not.toBeInTheDocument();
    expect(screen.queryByText(/private-proxy-body/)).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Create one' })).not.toBeInTheDocument();
    expect(notificationErrorMock).toHaveBeenCalledWith('Unable to load API tokens.');
    expect(listTokensMock).toHaveBeenCalledTimes(1);
    expect(markAgentsTokenRevokedMock).not.toHaveBeenCalled();
    expect(markDockerRuntimesTokenRevokedMock).not.toHaveBeenCalled();
    expect(createTokenMock).not.toHaveBeenCalled();
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(updateTokenScopesMock).not.toHaveBeenCalled();
    expect(renameTokenMock).not.toHaveBeenCalled();
  });

  it('retries only the inventory read once at a time without losing token drafts', async () => {
    let completeRetry!: (tokens: APITokenRecord[]) => void;
    listTokensMock.mockRejectedValueOnce(new Error('unavailable'));
    listTokensMock.mockReturnValueOnce(
      new Promise<APITokenRecord[]>((resolve) => {
        completeRetry = resolve;
      }),
    );
    render(() => <APITokenManager canManage />);
    const retry = await screen.findByRole('button', { name: 'Retry token inventory' });
    fireEvent.input(screen.getByLabelText('Token name'), { target: { value: 'Unsent draft' } });
    fireEvent.click(screen.getByRole('button', { name: 'Kiosk / Monitoring' }));

    fireEvent.click(retry);
    fireEvent.click(retry);
    expect(listTokensMock).toHaveBeenCalledTimes(2);
    expect(screen.getByRole('status', { name: 'Loading token inventory' })).toBeInTheDocument();
    expect(screen.queryByRole('alert')).not.toBeInTheDocument();
    expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
    expect(screen.getByLabelText('Token name')).toHaveValue('Unsent draft');

    completeRetry([
      makeToken({ id: 'full-token', name: 'Full token', scopes: ['*'] }),
      makeToken(),
    ]);
    await screen.findAllByText('Full token');
    expect(screen.queryAllByText('Unknown')).toHaveLength(0);
    expect(screen.queryByRole('button', { name: 'Retry token inventory' })).not.toBeInTheDocument();
    expect(screen.getByText(/1 full access token – consider/)).toBeInTheDocument();
    expect(screen.getByText('Credentials visible to this account')).toBeInTheDocument();
    expect(screen.getByLabelText('Token name')).toHaveValue('Unsent draft');
    expect(screen.getByRole('button', { name: 'Kiosk / Monitoring' })).toHaveAttribute(
      'aria-pressed',
      'true',
    );
    expect(listTokensMock).toHaveBeenCalledTimes(2);
    expect(createTokenMock).not.toHaveBeenCalled();
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(updateTokenScopesMock).not.toHaveBeenCalled();
    expect(renameTokenMock).not.toHaveBeenCalled();
  });

  it('lets a read-only account retry inventory without gaining token writes', async () => {
    listTokensMock
      .mockRejectedValueOnce(new Error('unavailable'))
      .mockResolvedValueOnce([makeToken()]);
    render(() => <APITokenManager canManage={false} />);
    fireEvent.click(await screen.findByRole('button', { name: 'Retry token inventory' }));
    await screen.findAllByText('Runtime token');
    for (const name of [
      'New token',
      'Generate new',
      'Generate',
      'Rename',
      'Edit scopes',
      'Revoke',
    ]) {
      for (const button of screen.getAllByRole('button', { name })) {
        expect(button).toBeDisabled();
      }
    }
    expect(listTokensMock).toHaveBeenCalledTimes(2);
    expect(createTokenMock).not.toHaveBeenCalled();
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(updateTokenScopesMock).not.toHaveBeenCalled();
    expect(renameTokenMock).not.toHaveBeenCalled();
  });

  it('keeps inventory recovery explicit across repeated failures before accepting empty', async () => {
    listTokensMock
      .mockRejectedValueOnce(new Error('unavailable'))
      .mockRejectedValueOnce(new Error('still unavailable'))
      .mockResolvedValueOnce([]);
    mockResources = [
      makeResource({
        platformData: { agent: { agentId: 'agent-1', tokenId: 'unread-token' } },
      }),
    ];
    render(() => <APITokenManager canManage />);
    fireEvent.click(await screen.findByRole('button', { name: 'Retry token inventory' }));
    await screen.findByRole('button', { name: 'Retry token inventory' });
    expect(listTokensMock).toHaveBeenCalledTimes(2);
    expect(screen.getAllByText('Unknown')).toHaveLength(3);
    expect(screen.queryByText(/No tokens yet/)).not.toBeInTheDocument();
    expect(markAgentsTokenRevokedMock).not.toHaveBeenCalled();
    fireEvent.click(screen.getByRole('button', { name: 'Retry token inventory' }));
    await screen.findByText(/No tokens yet/);
    expect(listTokensMock).toHaveBeenCalledTimes(3);
    expect(screen.getAllByText('0')).toHaveLength(3);
    expect(screen.queryByRole('alert')).not.toBeInTheDocument();
    expect(markAgentsTokenRevokedMock).toHaveBeenCalledWith('unread-token', ['agent-1']);
    expect(createTokenMock).not.toHaveBeenCalled();
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(updateTokenScopesMock).not.toHaveBeenCalled();
    expect(renameTokenMock).not.toHaveBeenCalled();
  });

  it('does not call a partially known inventory complete after creating a token', async () => {
    listTokensMock.mockRejectedValueOnce(new Error('unavailable'));
    render(() => <APITokenManager canManage />);
    await screen.findByRole('alert');
    fireEvent.click(screen.getByRole('button', { name: 'Kiosk / Monitoring' }));
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));
    await waitFor(() => expect(createTokenMock).toHaveBeenCalledTimes(1));
    expect(screen.getAllByText('Unknown')).toHaveLength(3);
    expect(screen.getByRole('alert')).toHaveTextContent('Unable to load API tokens.');
    expect(screen.queryByTestId('api-token-compact-list')).not.toBeInTheDocument();
    expect(screen.queryByText('No full access tokens in this inventory.')).not.toBeInTheDocument();
    expect(listTokensMock).toHaveBeenCalledTimes(1);
  });

  it.each(['', 'not-a-date'])(
    'does not invent Created for an unusable timestamp (%s)',
    async (createdAt) => {
      vi.useFakeTimers({ now: Date.parse('2026-10-11T11:00:00Z') });
      const token = makeToken({ createdAt });
      listTokensMock.mockResolvedValue([token]);
      render(() => <APITokenManager canManage />);
      await vi.advanceTimersByTimeAsync(0);
      const row = getTokenTableRow(token.name!);
      expect(createdTokenCell(row).textContent).toBe('');
      await vi.advanceTimersByTimeAsync(2 * 3_600_000);
      expect(createdTokenCell(row).textContent).toBe('');
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    },
  );

  it('keeps API Access focused on token management', async () => {
    window.history.pushState({}, '', '/settings/security/api');

    renderAPIAccessPanel();

    const tokenHeading = await screen.findByRole('heading', { name: 'API tokens' });
    const tokenSection = tokenHeading.closest('[data-testid="api-access-token-section"]');

    expect(tokenSection).not.toBeNull();
    expect(screen.queryByRole('heading', { name: 'External agents' })).not.toBeInTheDocument();
    expect(document.querySelector('[data-testid="api-access-external-agent-section"]')).toBeNull();
  });

  it('creates scoped tokens from the canonical preset path', async () => {
    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });

    fireEvent.input(screen.getByPlaceholderText('e.g. Docker / Podman automation'), {
      target: { value: 'Container automation' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Docker / Podman manage' }));
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'Container automation',
        [DOCKER_MANAGE_SCOPE, DOCKER_REPORT_SCOPE],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });

    expect(showTokenRevealMock).toHaveBeenCalledWith(
      expect.objectContaining({
        token: 'pulse_secret_value',
        source: 'security',
        record: expect.objectContaining({
          id: 'token-created',
          name: 'Container automation',
          scopes: [DOCKER_MANAGE_SCOPE, DOCKER_REPORT_SCOPE],
        }),
        note: 'Copy this token now. You can reopen this dialog from Settings → API Access while this page stays open.',
      }),
    );
    expect(notificationSuccessMock).toHaveBeenCalledWith(
      'New API token generated. Copy it below while it is still visible.',
    );

    await waitFor(() => {
      expect(screen.getAllByText('Container automation')).toHaveLength(3);
      expect(screen.getByText(/Token generated:/)).toBeInTheDocument();
      expect(
        screen.getAllByText('Docker / Podman lifecycle management').length,
      ).toBeGreaterThanOrEqual(2);
      expect(screen.getAllByText('Docker / Podman reporting').length).toBeGreaterThanOrEqual(2);
    });
  });

  it('creates manually cycled agent tokens with bound collector scopes', async () => {
    createTokenMock.mockResolvedValue(
      makeToken({
        name: 'Replacement agent token',
        scopes: [AGENT_REPORT_SCOPE, AGENT_CONFIG_READ_SCOPE],
      }),
    );

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    fireEvent.input(screen.getByPlaceholderText('e.g. Docker / Podman automation'), {
      target: { value: 'Replacement agent token' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Agent' }));
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'Replacement agent token',
        [AGENT_CONFIG_READ_SCOPE, AGENT_REPORT_SCOPE],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });
  });

  it('offers agent lifecycle management when editing token scopes', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'agent-token-edit',
        name: 'Agent token',
        scopes: [AGENT_REPORT_SCOPE],
      }),
    ]);

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const row = await findTokenTableRow('Agent token');
    fireEvent.click(within(row).getByRole('button', { name: 'Edit scopes' }));

    const dialog = await screen.findByRole('dialog', { name: 'Edit API token scopes' });
    expect(
      within(dialog).getByRole('checkbox', { name: /Agent lifecycle management/i }),
    ).not.toBeChecked();
  });

  it('requires an explicit scope selection before generating a token', async () => {
    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });

    expect(screen.getByRole('button', { name: 'Full access' })).toHaveAttribute(
      'aria-pressed',
      'false',
    );
    expect(screen.getByRole('button', { name: 'Generate' })).toBeDisabled();
    expect(
      screen.getByText(
        'Choose a scoped preset for least privilege, or deliberately choose Full access.',
      ),
    ).toBeInTheDocument();

    fireEvent.click(screen.getByRole('button', { name: 'Kiosk / Monitoring' }));

    expect(screen.getByRole('button', { name: 'Generate' })).not.toBeDisabled();
    expect(screen.getByRole('button', { name: 'Kiosk / Monitoring' })).toHaveAttribute(
      'aria-pressed',
      'true',
    );
    expect(screen.getByRole('button', { name: 'Full access' })).toHaveAttribute(
      'aria-pressed',
      'false',
    );
  });

  it('creates Patrol external agent tokens from the live manifest scopes', async () => {
    createTokenMock.mockResolvedValueOnce({
      token: 'pulse_agent_secret',
      record: makeToken({
        id: 'token-agent',
        name: 'OpenCode Pulse',
        scopes: [
          AI_EXECUTE_SCOPE,
          MONITORING_READ_SCOPE,
          MONITORING_WRITE_SCOPE,
          SETTINGS_READ_SCOPE,
          SETTINGS_WRITE_SCOPE,
        ],
      }),
    });

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(fetchAgentCapabilitiesManifestMock).toHaveBeenCalledTimes(1);
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });

    const preset = await screen.findByRole('button', { name: 'Patrol external agent' });
    expect(preset).toHaveAttribute(
      'title',
      'Scopes for connected agents that read Pulse context and request Patrol work.',
    );

    fireEvent.input(screen.getByPlaceholderText('e.g. Docker / Podman automation'), {
      target: { value: 'OpenCode Pulse' },
    });
    fireEvent.click(preset);
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'OpenCode Pulse',
        [
          AI_EXECUTE_SCOPE,
          MONITORING_READ_SCOPE,
          MONITORING_WRITE_SCOPE,
          SETTINGS_READ_SCOPE,
          SETTINGS_WRITE_SCOPE,
        ],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });

    expect(showTokenRevealMock).toHaveBeenCalledWith(
      expect.objectContaining({
        token: 'pulse_agent_secret',
        source: 'security',
        record: expect.objectContaining({
          id: 'token-agent',
          name: 'OpenCode Pulse',
        }),
      }),
    );
  });

  it('preselects the Patrol external agent preset from the MCP setup route', async () => {
    window.history.pushState({}, '', PULSE_MCP_TOKEN_SETUP_PATH);
    createTokenMock.mockResolvedValueOnce({
      token: 'pulse_mcp_secret',
      record: makeToken({
        id: 'token-mcp',
        name: 'Patrol external agent',
        scopes: [
          AI_EXECUTE_SCOPE,
          MONITORING_READ_SCOPE,
          MONITORING_WRITE_SCOPE,
          SETTINGS_READ_SCOPE,
          SETTINGS_WRITE_SCOPE,
        ],
      }),
    });

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(fetchAgentCapabilitiesManifestMock).toHaveBeenCalledTimes(1);
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });
    await screen.findByDisplayValue('Patrol external agent');
    await waitFor(() => {
      expect(scrollIntoViewMock).toHaveBeenCalledWith({ behavior: 'smooth', block: 'start' });
    });
    expect(document.getElementById(API_TOKEN_CREATE_ANCHOR)?.className).toContain('ring-2');

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

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'Patrol external agent',
        [
          AI_EXECUTE_SCOPE,
          MONITORING_READ_SCOPE,
          MONITORING_WRITE_SCOPE,
          SETTINGS_READ_SCOPE,
          SETTINGS_WRITE_SCOPE,
        ],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });
  });

  it('surfaces the dedicated audit scope in presets and grouped custom scopes', async () => {
    createTokenMock.mockResolvedValueOnce({
      token: 'pulse_audit_secret',
      record: makeToken({
        id: 'token-audit',
        name: 'Audit export',
        scopes: [AUDIT_READ_SCOPE],
      }),
    });

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });

    expect(screen.getByRole('button', { name: 'Audit read' })).toBeInTheDocument();

    fireEvent.click(screen.getByText('Custom scopes'));
    expect(screen.getByText('AI')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Pulse Mobile chat' })).toHaveAttribute(
      'title',
      'Use Pulse Mobile chat sessions, models, and read knowledge.',
    );
    expect(screen.getByRole('button', { name: 'Pulse Intelligence actions' })).toHaveAttribute(
      'title',
      'Use governed Patrol actions for plans, approvals, policy-allowed fixes, verification, history, and knowledge changes.',
    );
    expect(screen.getByText('Security')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Audit logs (read)' })).toBeInTheDocument();

    fireEvent.input(screen.getByPlaceholderText('e.g. Docker / Podman automation'), {
      target: { value: 'Audit export' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Audit read' }));
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'Audit export',
        [AUDIT_READ_SCOPE],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });
  });

  it('maps token usage from unified resources and fans revocation out to affected runtimes and agents', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-runtime',
        name: 'Runtime token',
        scopes: [DOCKER_REPORT_SCOPE, AGENT_REPORT_SCOPE],
      }),
      makeToken({
        id: 'token-unused',
        name: 'Unused token',
        suffix: '9999',
        scopes: [DOCKER_REPORT_SCOPE],
      }),
    ]);

    mockResources = [
      makeResource({
        id: 'docker-resource',
        type: 'docker-host',
        name: 'Docker Edge',
        displayName: 'Docker Edge',
        platformType: 'docker',
        sourceType: 'agent',
        platformData: {
          docker: {
            hostSourceId: 'docker-runtime-1',
            tokenId: 'token-runtime',
          },
        } as Record<string, unknown>,
      }),
      makeResource({
        id: 'agent-resource',
        type: 'agent',
        name: 'Edge Agent',
        displayName: 'Edge Agent',
        platformData: {
          agent: {
            agentId: 'agent-007',
            tokenId: 'token-runtime',
          },
        } as Record<string, unknown>,
      }),
    ];

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const row = await findTokenTableRow('Runtime token');
    expect(within(row).getByText('Docker Edge • Edge Agent')).toBeInTheDocument();
    expect(within(row).queryByText(/container runtime/i)).not.toBeInTheDocument();
    expect(within(row).getByText('Agent reporting')).toBeInTheDocument();

    fireEvent.click(within(row).getByRole('button', { name: 'Revoke' }));

    // Confirm modal opens — click "Revoke token" to actually trigger the delete.
    const confirmBtn = await screen.findByRole('button', { name: 'Revoke token' });
    fireEvent.click(confirmBtn);

    await waitFor(() => {
      expect(deleteTokenMock).toHaveBeenCalledWith(
        'token-runtime',
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });

    expect(markDockerRuntimesTokenRevokedMock).toHaveBeenCalledWith('token-runtime', [
      'docker-runtime-1',
    ]);
    expect(markAgentsTokenRevokedMock).toHaveBeenCalledWith('token-runtime', ['agent-007']);
    expect(notificationSuccessMock).toHaveBeenCalledWith(
      expect.stringContaining('Token "Runtime token" was previously used by'),
    );
    expect(notificationSuccessMock).toHaveBeenCalledWith(
      expect.stringContaining('Docker / Podman runtime: Docker Edge'),
    );

    await waitFor(() => {
      expect(screen.queryByText('Runtime token')).not.toBeInTheDocument();
      expect(screen.getAllByText('Unused token')).toHaveLength(2);
    });
  });

  it('presents the complete token inventory as manageable compact cards below 1600px', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-compact',
        name: 'Phone-managed token',
        scopes: [DOCKER_REPORT_SCOPE, AGENT_REPORT_SCOPE],
      }),
    ]);

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const compactList = await screen.findByTestId('api-token-compact-list');
    expect(compactList).toHaveClass('min-[1600px]:hidden');
    const card = compactList.querySelector('[data-api-token-card="token-compact"]');
    expect(card).not.toBeNull();
    const compactCard = within(card as HTMLElement);
    expect(compactCard.getByRole('heading', { name: 'Phone-managed token' })).toBeInTheDocument();
    expect(compactCard.getByText('Scopes')).toBeInTheDocument();
    expect(compactCard.getByText('Docker / Podman reporting')).toBeInTheDocument();
    expect(compactCard.getByText('Agent reporting')).toBeInTheDocument();
    expect(compactCard.getByText('Usage')).toBeInTheDocument();
    expect(compactCard.getByText('Created')).toBeInTheDocument();
    expect(compactCard.getByText('Last used')).toBeInTheDocument();
    expect(compactCard.getByRole('button', { name: 'Edit scopes' })).toBeInTheDocument();
    expect(compactCard.getByRole('button', { name: 'Revoke' })).toBeInTheDocument();
  });

  it('keeps the reconciled desktop row actionable after a confirmed scope edit', async () => {
    const token = makeToken({ name: 'Sequential token', scopes: [MONITORING_READ_SCOPE] });
    listTokensMock.mockResolvedValue([token]);
    updateTokenScopesMock.mockResolvedValue({
      ...token,
      scopes: [MONITORING_READ_SCOPE, SETTINGS_READ_SCOPE],
    });
    render(() => <APITokenManager canManage />);
    const row = await findTokenTableRow('Sequential token');
    fireEvent.click(within(row).getByRole('button', { name: 'Edit scopes' }));
    const dialog = await screen.findByRole('dialog', { name: 'Edit API token scopes' });
    fireEvent.click(within(dialog).getByRole('checkbox', { name: /^Settings \(read\)/ }));
    fireEvent.click(within(dialog).getByRole('button', { name: 'Save scopes' }));
    await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
    const updatedRow = getTokenTableRow('Sequential token');
    fireEvent.click(within(updatedRow).getByRole('button', { name: 'Revoke' }));
    const revoke = await screen.findByRole('dialog', { name: 'Revoke API token' });
    fireEvent.click(within(revoke).getByRole('button', { name: 'Revoke token' }));
    await waitFor(() => {
      expect(deleteTokenMock).toHaveBeenCalledWith(
        token.id,
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });
    await screen.findByText(/No tokens yet/);
    expect(updateTokenScopesMock).toHaveBeenCalledTimes(1);
    expect(deleteTokenMock).toHaveBeenCalledTimes(1);
  });

  it('edits a token scope set in place without rotating or revoking it', async () => {
    const onTokensChanged = vi.fn();
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-edit',
        name: 'Editable token',
        scopes: [DOCKER_REPORT_SCOPE],
      }),
    ]);
    updateTokenScopesMock.mockResolvedValue(
      makeToken({
        id: 'token-edit',
        name: 'Editable token',
        scopes: [MONITORING_READ_SCOPE],
      }),
    );

    render(() => <APITokenManager onTokensChanged={onTokensChanged} canManage />);

    const row = await findTokenTableRow('Editable token');
    fireEvent.click(within(row).getByRole('button', { name: 'Edit scopes' }));

    const dialog = await screen.findByRole('dialog', { name: 'Edit API token scopes' });
    expect(within(dialog).getByText(/take effect on its next request/i)).toBeInTheDocument();
    const dockerReport = within(dialog).getByRole('checkbox', {
      name: /Docker \/ Podman reporting/i,
    });
    const monitoringRead = within(dialog).getByRole('checkbox', {
      name: /Monitoring & alerts \(read\)/i,
    });
    expect(dockerReport).toBeChecked();
    expect(monitoringRead).not.toBeChecked();

    fireEvent.click(dockerReport);
    fireEvent.click(monitoringRead);
    fireEvent.click(within(dialog).getByRole('button', { name: 'Save scopes' }));

    await waitFor(() => {
      expect(updateTokenScopesMock).toHaveBeenCalledWith(
        'token-edit',
        [MONITORING_READ_SCOPE],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(createTokenMock).not.toHaveBeenCalled();
    expect(onTokensChanged).toHaveBeenCalledTimes(1);
    expect(notificationSuccessMock).toHaveBeenCalledWith('Scopes updated for Editable token.');

    await waitFor(() => {
      expect(
        screen.queryByRole('dialog', { name: 'Edit API token scopes' }),
      ).not.toBeInTheDocument();
    });
    const updatedRow = getTokenTableRow('Editable token');
    expect(within(updatedRow).getByText('Monitoring & alerts (read)')).toBeInTheDocument();
    expect(within(updatedRow).queryByText('Docker / Podman reporting')).not.toBeInTheDocument();
  });

  it('returns focus to token action triggers after dialogs close', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-focus-return',
        name: 'Keyboard token',
        scopes: [DOCKER_REPORT_SCOPE],
      }),
    ]);

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const row = await findTokenTableRow('Keyboard token');
    const trigger = within(row).getByRole('button', { name: 'Edit scopes' });
    fireEvent.click(trigger);

    const dialog = await screen.findByRole('dialog', { name: 'Edit API token scopes' });
    fireEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }));

    await waitFor(() => expect(trigger).toHaveFocus());

    fireEvent.click(trigger);
    await screen.findByRole('dialog', { name: 'Edit API token scopes' });
    fireEvent.keyDown(document, { key: 'Escape' });

    await waitFor(() => expect(trigger).toHaveFocus());

    const revokeTrigger = within(row).getByRole('button', { name: 'Revoke' });
    fireEvent.click(revokeTrigger);

    const revokeDialog = await screen.findByRole('dialog', { name: 'Revoke API token' });
    fireEvent.click(within(revokeDialog).getByRole('button', { name: 'Cancel' }));

    await waitFor(() => expect(revokeTrigger).toHaveFocus());
  });

  it('requires at least one changed scope and keeps failed edits open', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-edit-failure',
        name: 'Protected token',
        scopes: [DOCKER_REPORT_SCOPE],
      }),
    ]);
    updateTokenScopesMock.mockRejectedValueOnce(new Error('Cannot grant scope "monitoring:read"'));

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const row = await findTokenTableRow('Protected token');
    fireEvent.click(within(row).getByRole('button', { name: 'Edit scopes' }));

    const dialog = await screen.findByRole('dialog', { name: 'Edit API token scopes' });
    const save = within(dialog).getByRole('button', { name: 'Save scopes' });
    expect(save).toBeDisabled();

    fireEvent.click(within(dialog).getByRole('checkbox', { name: /Docker \/ Podman reporting/i }));
    expect(
      within(dialog).getByText('Select at least one scope before saving.'),
    ).toBeInTheDocument();
    expect(save).toBeDisabled();

    fireEvent.click(
      within(dialog).getByRole('checkbox', { name: /Monitoring & alerts \(read\)/i }),
    );
    expect(save).not.toBeDisabled();
    fireEvent.click(save);

    await waitFor(() => {
      expect(updateTokenScopesMock).toHaveBeenCalledWith(
        'token-edit-failure',
        [MONITORING_READ_SCOPE],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });
    expect(notificationErrorMock).toHaveBeenCalledWith('Unable to update API token scopes.');
    expect(screen.getByRole('dialog', { name: 'Edit API token scopes' })).toBeInTheDocument();
  });

  it('keeps governed infrastructure token usage labels on local operator identity', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-runtime',
        name: 'Runtime token',
        scopes: [AGENT_REPORT_SCOPE],
      }),
    ]);

    mockResources = [
      makeResource({
        id: 'pbs-resource',
        type: 'pbs',
        name: 'redacted-pbs',
        displayName: 'PBS Main',
        platformType: 'proxmox-pbs',
        sourceType: 'api',
        policy: {
          sensitivity: 'restricted',
          routing: { scope: 'local-only', redact: ['hostname'] },
        },
        platformData: {
          pbs: {
            hostname: 'pbs.local',
            instanceId: 'pbs-main',
          },
          agent: {
            agentId: 'pbs-agent-1',
            tokenId: 'token-runtime',
          },
        } as Record<string, unknown>,
      }),
    ];

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const row = await findTokenTableRow('Runtime token');
    expect(within(row).getByText('PBS Main')).toBeInTheDocument();
    expect(
      within(row).queryByText('backup server resource; status online; sources pbs'),
    ).not.toBeInTheDocument();
  });

  it('renames an existing token without revoking it', async () => {
    listTokensMock.mockResolvedValue([
      makeToken({
        id: 'token-pbs',
        name: 'proxmox-agent-pbs-1785306083',
        scopes: [AGENT_REPORT_SCOPE],
      }),
    ]);

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    const row = await findTokenTableRow('proxmox-agent-pbs-1785306083');
    const renameButton = within(row).getByRole('button', { name: 'Rename' });
    fireEvent.click(renameButton);

    const dialog = await screen.findByRole('dialog', { name: 'Rename API token' });
    const input = within(dialog).getByRole('textbox', { name: 'Token name' });
    fireEvent.input(input, { target: { value: 'PBS 01 telemetry' } });
    fireEvent.click(within(dialog).getByRole('button', { name: 'Save' }));

    await waitFor(() => {
      expect(renameTokenMock).toHaveBeenCalledWith(
        'token-pbs',
        'PBS 01 telemetry',
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
      expect(screen.getAllByText('PBS 01 telemetry').length).toBeGreaterThan(0);
      expect(document.activeElement).toHaveTextContent('Rename');
    });
    expect(deleteTokenMock).not.toHaveBeenCalled();
    expect(notificationSuccessMock).toHaveBeenCalledWith('Token renamed');
  });

  it('surfaces scope denial when token generation is blocked by caller scope', async () => {
    createTokenMock.mockRejectedValueOnce(
      Object.assign(
        new Error('Cannot grant scope "monitoring:read": your token does not have this scope'),
        {
          status: 403,
        },
      ),
    );

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });

    fireEvent.input(screen.getByPlaceholderText('e.g. Docker / Podman automation'), {
      target: { value: 'Blocked token' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Kiosk / Monitoring' }));
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'Blocked token',
        ['monitoring:read'],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });

    expect(notificationErrorMock).toHaveBeenCalledWith(
      'Cannot grant scope "monitoring:read": your token does not have this scope',
    );
    expect(notificationSuccessMock).not.toHaveBeenCalled();
    expect(showTokenRevealMock).not.toHaveBeenCalled();
  });

  it('surfaces required scope when middleware rejects token generation', async () => {
    createTokenMock.mockRejectedValueOnce(
      Object.assign(new Error('missing_scope'), {
        status: 403,
        requiredScope: 'settings:write',
      }),
    );

    render(() => <APITokenManager onTokensChanged={vi.fn()} canManage />);

    await waitFor(() => {
      expect(listTokensMock).toHaveBeenCalledTimes(1);
    });

    fireEvent.input(screen.getByPlaceholderText('e.g. Docker / Podman automation'), {
      target: { value: 'Needs settings scope' },
    });
    fireEvent.click(screen.getByRole('button', { name: 'Docker / Podman report' }));
    fireEvent.click(screen.getByRole('button', { name: 'Generate' }));

    await waitFor(() => {
      expect(createTokenMock).toHaveBeenCalledWith(
        'Needs settings scope',
        [DOCKER_REPORT_SCOPE],
        expect.objectContaining({ expectedOrgID: 'default', signal: expect.any(AbortSignal) }),
      );
    });

    expect(notificationErrorMock).toHaveBeenCalledWith(
      'This token is missing the required scope: Settings (write) (settings:write).',
    );
    expect(notificationSuccessMock).not.toHaveBeenCalled();
    expect(showTokenRevealMock).not.toHaveBeenCalled();
  });
});