OfficialSiteAgentDisplayConfigEdit.test.tsx 14.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
import { fireEvent, render, screen, within, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { App } from '../../App';
import { getOfficialSiteTabDetail, getOfficialSiteTabList, type OfficialSiteTabDetail, type OfficialSiteTabListItem } from '../../api/officialSiteTab';
import { getAvailableAgents, getAgentDisplayDetail, updateAgentDisplayConfig, uploadOfficialSiteAsset, type OfficialSiteAvailableAgentsResponse, type OfficialSiteAgentDisplayDetail } from '../../api/officialSiteAgentDisplay';

vi.mock('../../api/officialSiteTab', () => ({
  getOfficialSiteTabDetail: vi.fn(),
  getOfficialSiteTabList: vi.fn(),
  updateOfficialSiteTabStatus: vi.fn(),
  deleteOfficialSiteTab: vi.fn(),
  createOfficialSiteTab: vi.fn(),
  updateOfficialSiteTab: vi.fn(),
  getAvailableQRCodes: vi.fn(),
}));

vi.mock('../../api/officialSiteAgentDisplay', () => ({
  getAvailableAgents: vi.fn(),
  getAgentDisplayDetail: vi.fn(),
  updateAgentDisplayConfig: vi.fn(),
  uploadOfficialSiteAsset: vi.fn(),
}));

const mockTabDetail1: OfficialSiteTabDetail = {
  tab_id: 1,
  position_code: 'store_sales',
  position_name: '店销顾问',
  qrcode_id: 1,
  qrcode_name: '店销顾问渠道活码',
  channel_code: 'DSC-QR-001',
  employee_name: '张小萌',
  qrcode_employee_name: '张小萌',
  employee_role_text: '店销主管',
  main_title: 'AI 智能助手提升店销效率',
  sub_title: '通过 AI 智能问答帮助店销顾问快速响应客户需求',
  display_status: 'enable',
  sort_order: 10,
};

const mockTabDetail2: OfficialSiteTabDetail = {
  tab_id: 2,
  position_code: 'after_sales_service',
  position_name: '售后客服',
  qrcode_id: 2,
  qrcode_name: '售后客服渠道活码',
  channel_code: 'ASC-QR-002',
  employee_name: '李小燕',
  qrcode_employee_name: '李小燕',
  employee_role_text: '客服专员',
  main_title: 'AI 客服机器人赋能售后服务',
  sub_title: 'AI 客服机器人自动解答常见问题',
  display_status: 'enable',
  sort_order: 20,
};

const mockTabListItem: OfficialSiteTabListItem = {
  tab_id: 1,
  position_code: 'store_sales',
  position_name: '店销顾问',
  main_title: 'AI 智能助手提升店销效率',
  sub_title: '通过 AI 智能问答帮助店销顾问快速响应客户需求',
  qrcode_id: 1,
  qrcode_name: '店销顾问渠道活码',
  qrcode_url: 'https://cdn.example.com/qrcode.png',
  qrcode_status: 'enable',
  channel_code: 'DSC-QR-001',
  qrcode_employee_name: '张小萌',
  display_status: 'enable',
  sort_order: 10,
  agent_count: 3,
  created_at: '2026-05-10 09:00:00',
  operations: ['edit', 'disable', 'delete', 'configureAgent'],
};

const mockDisplayDetail1: OfficialSiteAgentDisplayDetail = {
  display_id: 1,
  agent_id: 101,
  agent_name: '复盘Agent',
  agent_code: 'review',
  media_url: 'https://cdn.example.com/media.mp4',
  media_type: 'mp4',
  media_size: 1000,
  icon_url: 'https://cdn.example.com/icons/review-agent.png',
  speech_title: '复盘Agent',
  speech_content: '帮助店销顾问完成每日复盘',
  sort_order: 10,
  display_status: 'enable',
  agent_status: 'enable',
};

const mockDisplayDetail2: OfficialSiteAgentDisplayDetail = {
  display_id: 2,
  agent_id: 102,
  agent_name: '质检Agent',
  agent_code: 'quality',
  media_size: 0,
  sort_order: 30,
  display_status: 'disable',
  agent_status: 'disable',
};

const mockDisplayDetail3: OfficialSiteAgentDisplayDetail = {
  display_id: 3,
  agent_id: 103,
  agent_name: '销售助手Agent',
  agent_code: 'sales-assist',
  media_url: 'https://cdn.example.com/media/sales-assist.gif',
  media_type: 'gif',
  media_size: 1000,
  icon_url: 'https://cdn.example.com/icons/sales-assist.png',
  speech_title: '销售助手Agent',
  speech_content: '智能推荐话术',
  sort_order: 20,
  display_status: 'disable',
  agent_status: 'enable',
};

const mockDisplayDetail5: OfficialSiteAgentDisplayDetail = {
  display_id: 5,
  agent_id: 105,
  agent_name: '客诉安抚Agent',
  agent_code: 'complaint',
  media_size: 0,
  icon_url: '',
  speech_title: '客诉安抚Agent',
  speech_content: '',
  sort_order: 10,
  display_status: 'disable',
  agent_status: 'enable',
};

const mockAvailableAgentsResponse1: OfficialSiteAvailableAgentsResponse = {
  tab_id: 1,
  position_name: '店销顾问',
  qrcode_id: 1,
  available_agents: [
    {
      display_id: 1,
      agent_id: 101,
      agent_name: '复盘Agent',
      agent_code: 'review',
      is_configured: true,
      is_steward: false,
      icon_url: 'https://cdn.example.com/icons/review-agent.png',
      media_url: 'https://cdn.example.com/media.mp4',
      summary: '帮助店销顾问完成每日复盘',
      display_status: 'enable',
      agent_status: 'enable',
    },
    {
      display_id: 2,
      agent_id: 102,
      agent_name: '质检Agent',
      agent_code: 'quality',
      is_configured: false,
      is_steward: false,
      display_status: 'disable',
      agent_status: 'disable',
    },
    {
      display_id: 3,
      agent_id: 103,
      agent_name: '销售助手Agent',
      agent_code: 'sales-assist',
      is_configured: true,
      is_steward: false,
      icon_url: 'https://cdn.example.com/icons/sales-assist.png',
      media_url: 'https://cdn.example.com/media/sales-assist.gif',
      summary: '智能推荐话术',
      display_status: 'disable',
      agent_status: 'enable',
    },
  ],
};

const mockAvailableAgentsResponse2: OfficialSiteAvailableAgentsResponse = {
  tab_id: 2,
  position_name: '售后客服',
  qrcode_id: 2,
  available_agents: [
    {
      display_id: 5,
      agent_id: 105,
      agent_name: '客诉安抚Agent',
      agent_code: 'complaint',
      is_configured: false,
      is_steward: false,
      display_status: 'disable',
      agent_status: 'enable',
    },
  ],
};

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();
  vi.clearAllMocks();
}

describe('Agent display config edit modal', () => {
  beforeEach(() => {
    resetAllMockState();
    vi.mocked(getOfficialSiteTabDetail).mockImplementation(async (tabId: number) => {
      if (tabId === 1) return mockTabDetail1;
      if (tabId === 2) return mockTabDetail2;
      throw new Error('岗位展示配置不存在');
    });
    vi.mocked(getOfficialSiteTabList).mockResolvedValue({
      list: [mockTabListItem],
      total: 1,
      page: 1,
      page_size: 10,
    });
    vi.mocked(getAvailableAgents).mockImplementation(async (tabId: number) => {
      if (tabId === 1) return mockAvailableAgentsResponse1;
      if (tabId === 2) return mockAvailableAgentsResponse2;
      return { tab_id: tabId, position_name: '岗位', qrcode_id: 0, available_agents: [] };
    });
    vi.mocked(getAgentDisplayDetail).mockImplementation(async (displayId: number) => {
      if (displayId === 1) return mockDisplayDetail1;
      if (displayId === 2) return mockDisplayDetail2;
      if (displayId === 3) return mockDisplayDetail3;
      if (displayId === 5) return mockDisplayDetail5;
      throw new Error('Agent配置不存在');
    });
    vi.mocked(updateAgentDisplayConfig).mockResolvedValue({
      media_url: 'https://cdn.example.com/media.gif',
      media_type: 'gif',
      media_size: 0,
      speech_title: '复盘Agent',
      speech_content: '帮助店销顾问完成每日复盘',
      display_status: 'enable',
      sort_order: 10,
    });
  });

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

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

    it('closes modal when clicking cancel', async () => {
      renderApp();
      await waitFor(() => {
        expect(screen.getByRole('button', { name: '编辑配置-复盘Agent' })).toBeInTheDocument();
      });
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-复盘Agent' }));
      await waitFor(() => {
        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();
      await waitFor(() => {
        expect(screen.getByRole('button', { name: '编辑配置-复盘Agent' })).toBeInTheDocument();
      });
      fireEvent.click(screen.getByRole('button', { name: '编辑配置-复盘Agent' }));
      await waitFor(() => {
        expect(screen.getByRole('dialog')).toBeInTheDocument();
      });
      // Wait for modal content to load (not showing loading state)
      await waitFor(() => {
        expect(screen.queryByText('加载中...')).not.toBeInTheDocument();
      });
    });

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

    it('shows Agent 名称 field (speech_title) disabled/read-only', async () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByText('Agent 名称')).toBeInTheDocument();
      await waitFor(() => {
        const nameInput = within(modal).getByDisplayValue('复盘Agent');
        expect(nameInput).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).getByText('展示素材')).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();
      expect(within(modal).queryByText('已停用')).not.toBeInTheDocument();
    });
  });

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

    it('shows default display_status as 不展示 for unconfigured agent', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByRole('combobox', { name: '展示状态' })).toHaveAttribute('value', '不展示');
    });

    it('shows empty icon for unconfigured agent', () => {
      const modal = screen.getByRole('dialog');
      expect(within(modal).getByText('图标')).toBeInTheDocument();
    });

    it('shows empty speech_content for unconfigured agent', () => {
      const modal = screen.getByRole('dialog');
      const contentInput = within(modal).getByPlaceholderText('请输入Agent概述');
      expect(contentInput).toHaveValue('');
    });
  });

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

    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(
        () => {
          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 and shows length count', async () => {
      const user = userEvent.setup();
      const modal = screen.getByRole('dialog');
      const contentInput = within(modal).getByPlaceholderText('请输入Agent概述');

      const longContent = '测'.repeat(101);
      await user.clear(contentInput);
      fireEvent.change(contentInput, { target: { value: longContent } });
      expect(contentInput).toHaveValue('测'.repeat(100));
      expect(within(modal).getByText('100/100')).toBeInTheDocument();
      await user.click(within(modal).getByRole('button', { name: '确定' }));
      expect(within(modal).queryByText(/不能超过100字/)).not.toBeInTheDocument();
    });

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

      await user.clear(sortInput);
      await user.type(sortInput, '100');
      const contentInput = within(modal).getByPlaceholderText('请输入Agent概述');
      await user.clear(contentInput);
      await user.type(contentInput, '测试概述');
      await user.click(within(modal).getByRole('button', { name: '确定' }));
      await waitFor(
        () => {
          expect(within(modal).getByText(/0-99/)).toBeInTheDocument();
        },
        { timeout: 2000 },
      );
    });
  });
});