OfficialSiteAgentDisplayConfigEdit.test.tsx
7.52 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
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 },
);
});
});
});