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

import { resetAgentDisplayConfigMockState } from '../../../packages/contracts/official-site-agent-display-config-mock';
import { resetOfficialSiteJobTabMockState } from '../../../packages/contracts/official-site-job-tab-mock';
import { resetChannelCodeMockState } from '../../../packages/contracts/channel-code-mock';
import { resetAgentTagMockState } from '../../../packages/contracts/agent-tag-mock';
import { App } from '../../App';

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

function resetAllMockState() {
  window.localStorage.clear();
  resetOfficialSiteJobTabMockState();
  resetAgentDisplayConfigMockState();
  resetChannelCodeMockState();
  resetAgentTagMockState();
}

describe('Agent display config edit modal', () => {
  beforeEach(() => {
    resetAllMockState();
  });

  describe('modal opening and permissions', () => {
    it('opens edit modal when clicking 编辑配置 on enabled agent', async () => {
      renderApp();
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-复盘Agent' }));
      expect(screen.getByRole('dialog')).toBeInTheDocument();
      expect(screen.getByText('编辑展示配置 - 复盘Agent')).toBeInTheDocument();
    });

    it('disabled agent has no edit button', () => {
      renderApp();
      expect(screen.queryByRole('button', { name: '编辑配置-质检Agent' })).not.toBeInTheDocument();
    });

    it('closes modal when clicking cancel', async () => {
      renderApp();
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-复盘Agent' }));
      expect(screen.getByRole('dialog')).toBeInTheDocument();
      fireEvent.click(screen.getByRole('button', { name: '取消' }));
      await waitFor(() => {
        expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
      });
    });
  });

  describe('modal fields', () => {
    beforeEach(async () => {
      renderApp();
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-复盘Agent' }));
    });

    it('shows 图标 field with upload component', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByTestId('form-item-图标')).toBeInTheDocument();
    });

    it('shows Agent 名称 field (speech_title) editable', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByText('Agent 名称')).toBeInTheDocument();
      // Agent 名称 should be editable
      const nameInput = within(modal).getByDisplayValue('复盘Agent');
      expect(nameInput).not.toBeDisabled();
    });

    it('shows Agent 概述 field (speech_content) editable', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByText('Agent 概述')).toBeInTheDocument();
    });

    it('shows 展示素材 field with upload component', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByTestId('form-item-展示素材')).toBeInTheDocument();
    });

    it('shows 展示排序 field editable', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByText('展示排序')).toBeInTheDocument();
    });

    it('shows 展示状态 field with 已下架 and 展示中 options only', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByText('展示状态')).toBeInTheDocument();
      // Check that 已停用 is not an option
      expect(within(modal).queryByText('已停用')).not.toBeInTheDocument();
    });
  });

  describe('default values for unconfigured agent', () => {
    beforeEach(async () => {
      renderApp('/official-site/job-tabs/2/agents');
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-客诉安抚Agent' }));
    });

    it('shows default display_status as 已下架 for unconfigured agent', () => {
      const modal = screen.getByRole('dialog');
      const statusSelect = within(modal).getByDisplayValue('已下架');
      expect(statusSelect).toBeInTheDocument();
    });

    it('shows empty icon for unconfigured agent', () => {
      const modal = screen.getByRole('dialog');
      // Check that 图标 field exists (MediaWall will show upload button for empty state)
      expect(within(modal).getByText('图标')).toBeInTheDocument();
    });

    it('shows empty speech_content for unconfigured agent', () => {
      const modal = screen.getByRole('dialog');
      // Find the Agent概述 input by placeholder
      const contentInput = within(modal).getByPlaceholderText('请输入Agent概述');
      // Check that it's empty
      expect(contentInput).toHaveValue('');
    });
  });

  describe('validation', () => {
    beforeEach(async () => {
      renderApp('/official-site/job-tabs/2/agents');
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-客诉安抚Agent' }));
    });

    it('validates all required fields on empty form', async () => {
      const modal = screen.getByRole('dialog');
      const confirmBtn = within(modal).getByRole('button', { name: '确定' });
      fireEvent.click(confirmBtn);
      await waitFor(
        () => {
          // 图标、Agent概述、展示素材必填 (Agent名称已有默认值,不会触发错误)
          expect(within(modal).getByText('图标必填')).toBeInTheDocument();
          expect(within(modal).getByText('Agent概述必填')).toBeInTheDocument();
          expect(within(modal).getByText('展示素材必填')).toBeInTheDocument();
        },
        { timeout: 2000 },
      );
    });

    it('validates speech_content max 100 chars', async () => {
      const modal = screen.getByRole('dialog');
      const contentInput = within(modal).getByPlaceholderText('请输入Agent概述');

      const longContent = '这是一段超过一百字的概述内容这是一段超过一百字的概述内容这是一段超过一百字的概述内容这是一段超过一百字的概述内容这是一段超过一百字的概述内容这是一段超过一百字的概述内容这是一段超过一百字的概述内容这是一段超过一百字的概述内容';
      fireEvent.change(contentInput, { target: { value: longContent } });
      fireEvent.click(within(modal).getByRole('button', { name: '确定' }));
      await waitFor(
        () => {
          expect(within(modal).getByText(/不能超过100字/)).toBeInTheDocument();
        },
        { timeout: 2000 },
      );
    });

    it('validates sort_order 0-99', async () => {
      const modal = screen.getByRole('dialog');
      const inputs = within(modal).getAllByRole('spinbutton');
      const sortInput = inputs[0];
      if (!sortInput) throw new Error('Sort input not found');

      fireEvent.change(sortInput, { target: { value: '100' } });
      // Fill in required fields
      const nameInput = within(modal).getByPlaceholderText('请输入Agent名称');
      fireEvent.change(nameInput, { target: { value: '测试名称' } });
      const contentInput = within(modal).getByPlaceholderText('请输入Agent概述');
      fireEvent.change(contentInput, { target: { value: '测试概述' } });
      fireEvent.click(within(modal).getByRole('button', { name: '确定' }));
      await waitFor(
        () => {
          expect(within(modal).getByText(/0-99/)).toBeInTheDocument();
        },
        { timeout: 2000 },
      );
    });
  });
});