App.test.tsx 5.9 KB
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { App } from './App';
import { getAgentList } from './api/agent';

vi.mock('./api/agent', () => ({
  getAgentList: vi.fn(),
  getAgentDetail: vi.fn(),
  deleteAgent: vi.fn(),
  updateAgentStatus: vi.fn(),
  createAgent: vi.fn(),
  updateAgent: vi.fn(),
}));

const loginStorageKey = 'agent-admin-local-login';

function renderApp(path: string) {
  return render(
    <MemoryRouter initialEntries={[path]}>
      <App />
    </MemoryRouter>,
  );
}

describe('App foundation', () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.clearAllMocks();
  });

  it('renders the root layout for the React application', () => {
    renderApp('/components');

    expect(screen.getByRole('main')).toBeInTheDocument();
    expect(screen.getByText('Agent Admin Web')).toBeInTheDocument();
  });

  it('renders the independent component demo route', () => {
    renderApp('/components');

    expect(screen.getByRole('heading', { name: '组件库 Demo' })).toBeInTheDocument();
    expect(screen.getByText('全局 UI 组件库展示入口')).toBeInTheDocument();
    expect(screen.queryByRole('heading', { name: 'Agent管理' })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: '退出登录' })).not.toBeInTheDocument();
  });
});

describe('Admin login and protected routes', () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.clearAllMocks();
    vi.mocked(getAgentList).mockRejectedValue(new Error('Network error'));
  });

  it('redirects anonymous users to login and returns to the requested protected route after login', async () => {
    renderApp('/agents');

    expect(screen.getByRole('heading', { name: '岗位Agent管理后台' })).toBeInTheDocument();
    expect(screen.getByLabelText('账号')).toBeInTheDocument();
    expect(screen.getByLabelText('密码')).toBeInTheDocument();

    fireEvent.change(screen.getByLabelText('账号'), { target: { value: 'admin' } });
    fireEvent.change(screen.getByLabelText('密码'), { target: { value: '123456' } });
    fireEvent.click(screen.getByRole('button', { name: '登录' }));

    expect(window.localStorage.getItem(loginStorageKey)).toBe('true');
    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Agent管理' })).toBeInTheDocument();
    });
    await waitFor(() => {
      expect(screen.getByRole('alert', { name: '加载失败' })).toBeInTheDocument();
    });
    expect(screen.getByText('岗位Agent管理后台')).toBeInTheDocument();
    expect(screen.getByLabelText('管理员')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '退出登录' })).toBeInTheDocument();
  });

  it('shows validation and credential errors without marking the user as logged in', () => {
    renderApp('/login');

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

    expect(screen.getByText('请输入账号')).toBeInTheDocument();
    expect(screen.getByText('请输入密码')).toBeInTheDocument();
    expect(window.localStorage.getItem(loginStorageKey)).toBeNull();

    fireEvent.change(screen.getByLabelText('账号'), { target: { value: 'admin' } });
    fireEvent.change(screen.getByLabelText('密码'), { target: { value: 'wrong' } });
    fireEvent.click(screen.getByRole('button', { name: '登录' }));

    expect(screen.getByText('账号或密码错误')).toBeInTheDocument();
    expect(window.localStorage.getItem(loginStorageKey)).toBeNull();
  });

  it('keeps logged-in users on refresh and clears local state on logout', async () => {
    window.localStorage.setItem(loginStorageKey, 'true');
    vi.mocked(getAgentList).mockResolvedValue({ list: [], total: 0, page: 1, page_size: 10 });

    renderApp('/agents');

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

    fireEvent.click(screen.getByRole('button', { name: '退出登录' }));

    expect(screen.getByRole('dialog', { name: '确认退出登录?' })).toBeInTheDocument();
    expect(window.localStorage.getItem(loginStorageKey)).toBe('true');

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

    expect(screen.queryByRole('dialog', { name: '确认退出登录?' })).not.toBeInTheDocument();
    expect(window.localStorage.getItem(loginStorageKey)).toBe('true');

    fireEvent.click(screen.getByRole('button', { name: '退出登录' }));
    fireEvent.click(screen.getByRole('button', { name: '确定' }));

    expect(window.localStorage.getItem(loginStorageKey)).toBeNull();
    expect(screen.getByRole('heading', { name: '岗位Agent管理后台' })).toBeInTheDocument();
  });

  it('renders hierarchical admin navigation with the child route selected', async () => {
    window.localStorage.setItem(loginStorageKey, 'true');
    vi.mocked(getAgentList).mockResolvedValue({ list: [], total: 0, page: 1, page_size: 10 });

    renderApp('/agents');

    await waitFor(() => {
      expect(screen.getByRole('button', { name: 'Agent管理' })).toHaveAttribute('data-active-parent', 'true');
    });
    expect(screen.getByRole('button', { name: 'Agent列表' })).toHaveAttribute('aria-current', 'page');
  });

  it('renders the skill route inside the admin shell navigation', async () => {
    window.localStorage.setItem(loginStorageKey, 'true');
    vi.mocked(getAgentList).mockResolvedValue({ list: [], total: 0, page: 1, page_size: 10 });

    renderApp('/skills');

    await waitFor(() => {
      expect(screen.getByRole('heading', { name: 'Skill目录' })).toBeInTheDocument();
    });
    expect(screen.getByRole('button', { name: 'Agent管理' })).toHaveAttribute('data-active-parent', 'true');
    expect(screen.getByRole('button', { name: 'Skill目录' })).toHaveAttribute('aria-current', 'page');
    expect(screen.getByText('Agent管理 / Skill目录')).toBeInTheDocument();
  });
});