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

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

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

function getSelectOption(name: string) {
  const option = screen.getAllByText(name).map((element) => element.closest('li')).find(Boolean);
  if (!option) throw new Error(`Select option ${name} was not found`);
  return option as HTMLElement;
}

function getSelectRoot(name: string) {
  return screen.getByRole('combobox', { name }).closest('.t-select') as HTMLElement;
}

function getSelectWrap(name: string) {
  return screen.getByRole('combobox', { name }).closest('.t-select__wrap') as HTMLElement;
}

describe('Agent editor routes and detail drawer', () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it('renders the new page with agent navigation selected and returns to the list context on cancel', () => {
    renderAgents('/agents/new?keyword=复盘&page=2&pageSize=20');

    expect(screen.getByRole('heading', { name: '新建Agent' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Agent列表' })).toHaveAttribute('aria-current', 'page');
    expect(screen.getByRole('button', { name: '取消' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: '保存' })).toBeInTheDocument();

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

    expect(screen.getByRole('heading', { name: 'Agent管理' })).toBeInTheDocument();
    expect(screen.getByLabelText('搜索Agent名称')).toHaveValue('复盘');
    expect(screen.getByLabelText('每页条数')).toHaveValue('20');
  });

  it('opens the detail drawer from the list entry and closes without losing list context', () => {
    renderAgents('/agents?keyword=复盘&page=1&pageSize=10');

    fireEvent.click(screen.getByRole('button', { name: '查看复盘Agent详情' }));

    const drawer = screen.getByRole('dialog', { name: 'Agent详情' });
    expect(within(drawer).getByText('复盘Agent')).toBeInTheDocument();
    expect(screen.getByLabelText('搜索Agent名称')).toHaveValue('复盘');

    fireEvent.click(screen.getByRole('button', { name: '关闭Agent详情' }));

    expect(screen.queryByRole('dialog', { name: 'Agent详情' })).not.toBeInTheDocument();
    expect(screen.getByLabelText('搜索Agent名称')).toHaveValue('复盘');
  });

  it('renders the edit page with preloaded values and restores the detail drawer context after cancel', () => {
    renderAgents('/agents/101/edit?keyword=复盘&detail=101');

    expect(screen.getByRole('heading', { name: '编辑Agent' })).toBeInTheDocument();
    expect(screen.getByDisplayValue('复盘Agent')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Agent列表' })).toHaveAttribute('aria-current', 'page');

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

    const drawer = screen.getByRole('dialog', { name: 'Agent详情' });
    expect(drawer).toBeInTheDocument();
    expect(within(drawer).getByText('复盘Agent')).toBeInTheDocument();
  });
});

describe('Agent editor form rules', () => {
  beforeEach(() => {
    window.localStorage.clear();
  });

  it('shows required validation for a normal agent and keeps saving disabled during submit', () => {
    renderAgents('/agents/new');

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

    expect(screen.getByText('请输入 Agent 名称')).toBeInTheDocument();
    expect(screen.getByText('请选择所属岗位')).toBeInTheDocument();
    expect(screen.getByText('请选择托管企微员工')).toBeInTheDocument();
    expect(screen.getByText('请输入 Agent 描述')).toBeInTheDocument();
    expect(screen.getByText('请至少绑定一个 Skill')).toBeInTheDocument();
  });

  it('locks fixed fields for manager agents while leaving description editable', () => {
    renderAgents('/agents/1/edit');

    expect(screen.getByDisplayValue('管家Agent')).toBeDisabled();
    expect(screen.getByRole('combobox', { name: '所属岗位' })).toHaveAttribute('aria-disabled', 'true');
    expect(screen.getByRole('combobox', { name: '托管企微员工' })).toHaveAttribute('aria-disabled', 'true');
    expect(screen.getByRole('switch', { name: '状态' })).toBeDisabled();
    expect(screen.getByLabelText('Agent描述')).toBeEnabled();
  });

  it('uses consistent responsive widths, shows required state label, and keeps employee tips visible before selecting positions', () => {
    renderAgents('/agents/new');

    expect(screen.getByLabelText('Agent名称').parentElement).toHaveClass('w-full', 'max-w-[450px]');
    expect(getSelectWrap('所属岗位')).toHaveClass('w-full', 'max-w-[450px]');
    expect(getSelectWrap('托管企微员工')).toHaveClass('w-full', 'max-w-[450px]');
    expect(getSelectWrap('绑定Skill')).toHaveClass('w-full', 'max-w-[450px]');
    expect(screen.getByTestId('form-label-状态')).toHaveTextContent('*状态');
    expect(screen.getByRole('combobox', { name: '托管企微员工' })).toHaveAttribute('aria-disabled', 'true');
    expect(screen.getByPlaceholderText('请先选择所属岗位')).toBeInTheDocument();
    expect(screen.getByText('请先选择所属岗位')).toBeInTheDocument();
  });

  it('creates a new agent and returns to the list with refreshed data', async () => {
    renderAgents('/agents');

    const initialRowCount = screen.getAllByRole('row').length;

    fireEvent.click(screen.getByRole('button', { name: '新建Agent' }));

    expect(screen.getByRole('heading', { name: '新建Agent' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Agent列表' })).toHaveAttribute('aria-current', 'page');

    // 填写表单
    fireEvent.change(screen.getByLabelText('Agent名称'), { target: { value: '新测试Agent' } });
    fireEvent.click(screen.getByRole('combobox', { name: '所属岗位' }));
    await waitFor(() => expect(getSelectOption('驻店销售岗')).toBeInTheDocument());
    fireEvent.click(getSelectOption('驻店销售岗'));
    await waitFor(() => expect(screen.getByRole('combobox', { name: '托管企微员工' })).not.toHaveAttribute('aria-disabled', 'true'));
    fireEvent.click(screen.getByRole('combobox', { name: '托管企微员工' }));
    await waitFor(() => expect(getSelectOption('陈晓岚')).toBeInTheDocument());
    fireEvent.click(getSelectOption('陈晓岚'));
    fireEvent.change(screen.getByLabelText('Agent描述'), { target: { value: '这是一个测试Agent的描述' } });
    fireEvent.click(screen.getByRole('combobox', { name: '绑定Skill' }));
    await waitFor(() => expect(getSelectOption('会话总结')).toBeInTheDocument());
    fireEvent.click(getSelectOption('会话总结'));

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

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

    // 验证列表数据已刷新(新增了一条数据)
    const rowsAfterCreate = screen.getAllByRole('row');
    expect(rowsAfterCreate.length).toBeGreaterThan(initialRowCount);
    expect(screen.getByRole('button', { name: 'Agent列表' })).toHaveAttribute('aria-current', 'page');
  }, 10000);

  it('edits an agent and returns to the list with detail drawer context preserved', async () => {
    renderAgents('/agents?detail=101');

    // 详情抽屉已打开
    const drawer = screen.getByRole('dialog', { name: 'Agent详情' });
    expect(within(drawer).getByText('复盘Agent')).toBeInTheDocument();

    fireEvent.click(within(drawer).getByRole('button', { name: '编辑' }));

    expect(screen.getByRole('heading', { name: '编辑Agent' })).toBeInTheDocument();
    expect(screen.getByDisplayValue('复盘Agent')).toBeInTheDocument();

    // 修改描述
    fireEvent.change(screen.getByLabelText('Agent描述'), { target: { value: '更新后的复盘Agent描述' } });

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

    await waitFor(() => {
      expect(screen.getByRole('dialog', { name: 'Agent详情' })).toBeInTheDocument();
    });

    // 验证详情抽屉上下文已恢复
    const restoredDrawer = screen.getByRole('dialog', { name: 'Agent详情' });
    expect(within(restoredDrawer).getByText('更新后的复盘Agent描述')).toBeInTheDocument();
  });

  it('renders selected skills as tag content instead of summary mode in the editor form', async () => {
    renderAgents('/agents/new');

    const combobox = screen.getByRole('combobox', { name: '绑定Skill' });
    fireEvent.click(combobox);

    await waitFor(() => {
      expect(getSelectOption('会话总结')).toBeInTheDocument();
    });

    fireEvent.click(getSelectOption('会话总结'));
    fireEvent.click(getSelectOption('线索跟进'));

    await waitFor(() => {
      const skillCombobox = screen.getByRole('combobox', { name: '绑定Skill' });
      expect(skillCombobox.closest('.t-select')).toHaveTextContent('会话总结');
      expect(skillCombobox.closest('.t-select')).toHaveTextContent('线索跟进');
    });
  });
});