AgentEditorFlow.test.tsx
9.14 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
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('线索跟进');
});
});
});