OfficialSiteMethodologyRoute.test.tsx 3.65 KB
import { fireEvent, render, screen, waitFor, within } 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';
import { getMethodologyConfig, saveMethodologyConfig } from '../../api/methodology';

vi.mock('../../api/agent', () => ({
  getAgentList: vi.fn(),
  getAgentDetail: vi.fn(),
  deleteAgent: vi.fn(),
  updateAgentStatus: vi.fn(),
  createAgent: vi.fn(),
  updateAgent: vi.fn(),
  getEmployeeMounts: vi.fn(() => Promise.resolve({ employee_count: 0, employees: [] })),
}));

vi.mock('../../api/methodology', () => ({
  getMethodologyConfig: vi.fn(),
  saveMethodologyConfig: vi.fn(),
}));

vi.mock('../../api/analytics', () => ({
  reportPageView: vi.fn().mockResolvedValue({ success: true }),
  reportAnalyticsEvents: vi.fn().mockResolvedValue({ success: true }),
}));

const mockMethodologyConfig = {
  config_id: 'methodology_001',
  display_status: 'enable' as const,
  main_title: '四步打造可复用的岗位 Agent',
  sub_title: '从目标澄清到上线复盘,帮助团队沉淀一套稳定可复制的方法论。',
  steps: [
    {
      step_id: 's1',
      step_icon: 'https://cdn.example.com/icon1.png',
      step_name: '需求澄清',
      step_order: 1,
      step_image: 'https://cdn.example.com/image1.png',
    },
  ],
  updated_at: '2026-05-16T10:00:00Z',
};

function renderApp(path = '/official-site/methodology') {
  window.localStorage.setItem('agent-admin-local-login', 'true');
  return render(
    <MemoryRouter initialEntries={[path]}>
      <App />
    </MemoryRouter>,
  );
}

function getMenuItem(label: string) {
  const item = within(screen.getByLabelText('左侧导航')).getByText(label).closest('li');
  if (!item) throw new Error(`Menu item ${label} was not found`);
  return item;
}

describe('official site methodology route and navigation', () => {
  beforeEach(() => {
    window.localStorage.clear();
    vi.clearAllMocks();
    vi.mocked(getAgentList).mockResolvedValue({ list: [], total: 0, page: 1, page_size: 10 });
    vi.mocked(getMethodologyConfig).mockResolvedValue(mockMethodologyConfig);
    vi.mocked(saveMethodologyConfig).mockResolvedValue({ config_id: 'methodology_001', updated_at: '2026-05-16T12:00:00Z' });
  });

  it('renders the methodology page inside admin shell with correct menu ownership', async () => {
    renderApp();

    expect(await screen.findByRole('heading', { name: '方法论展示配置' })).toBeInTheDocument();
    expect(screen.getAllByText('官网配置').length).toBeGreaterThan(0);
    expect(screen.getAllByText('方法论展示配置').length).toBeGreaterThan(0);
    expect(getMenuItem('方法论展示配置')).toHaveClass('t-is-active');
    expect(getMenuItem('Agent列表')).not.toHaveClass('t-is-active');
  });

  it('shows the official site navigation entry and can navigate to the methodology page from agents', async () => {
    renderApp('/agents');

    await waitFor(() => {
      expect(getMenuItem('Agent列表')).toHaveClass('t-is-active');
    });
    fireEvent.click(getMenuItem('方法论展示配置'));

    expect(await screen.findByRole('heading', { name: '方法论展示配置' })).toBeInTheDocument();
    expect(screen.getAllByText('官网配置').length).toBeGreaterThan(0);
    expect(screen.getAllByText('方法论展示配置').length).toBeGreaterThan(0);
  });

  it('keeps the protected index redirect pointing to agents by default', async () => {
    renderApp('/');

    expect(await screen.findByRole('heading', { name: 'Agent管理' })).toBeInTheDocument();
    expect(getMenuItem('Agent列表')).toHaveClass('t-is-active');
  });
});