PositionAbilityArea.test.tsx
13.9 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
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent, within } from '@testing-library/react';
import { PositionAbilityArea } from '../components/PositionAbilityArea';
import { createMockPositionTab, createMockAgent } from '../mocks/data';
import type { PositionTab, Agent } from '../types/home';
describe('岗位能力区测试', () => {
const createTestProps = (
tabOverrides: Partial<PositionTab> = {},
agentOverrides: Partial<Agent> = {}
) => {
const tab = createMockPositionTab({
agents: [createMockAgent(agentOverrides)],
...tabOverrides,
});
const agent = tab.agents[0];
return {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
};
it('应展示核心能力标签', () => {
const props = createTestProps();
render(<PositionAbilityArea {...props} />);
expect(screen.getByText('核心能力')).toBeInTheDocument();
});
it('应展示岗位标题', () => {
const props = createTestProps({ main_title: '岗位标题' });
render(<PositionAbilityArea {...props} />);
expect(screen.getByText('岗位标题')).toBeInTheDocument();
});
it('应展示岗位概述', () => {
const props = createTestProps({ sub_title: '岗位概述说明' });
render(<PositionAbilityArea {...props} />);
expect(screen.getByText('岗位概述说明')).toBeInTheDocument();
});
it('应展示立即体验按钮', () => {
const props = createTestProps();
render(<PositionAbilityArea {...props} />);
expect(screen.getByRole('button', { name: /立即体验/ })).toBeInTheDocument();
});
it('点击立即体验应打开弹窗', () => {
const props = createTestProps();
render(<PositionAbilityArea {...props} />);
const button = screen.getByRole('button', { name: /立即体验/ });
fireEvent.click(button);
expect(props.onOpenQrcodeModal).toHaveBeenCalled();
});
it('立即体验按钮应展示在左侧素材容器之外', () => {
const props = createTestProps();
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByRole('button', { name: /立即体验/ })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /立即体验/ })).toBeInTheDocument();
});
it('左侧素材容器不应保留内边距,并应让素材铺满区域', () => {
const agent = createMockAgent({ media_type: 'image', media_url: 'https://example.com/image.png' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const mediaFrame = screen.getByTestId('agent-media-frame');
expect(mediaFrame.className).not.toMatch(/\bp-4\b/);
expect(mediaFrame.className).not.toMatch(/\bp-5\b/);
expect(screen.getByTestId('agent-image')).toHaveClass('object-contain');
});
it('点击 Agent 卡片应触发选择', () => {
const agent1 = createMockAgent({ agent_id: 'agent-001', speech_title: 'Agent 1' });
const agent2 = createMockAgent({ agent_id: 'agent-002', speech_title: 'Agent 2' });
const tab = createMockPositionTab({ agents: [agent1, agent2] });
const props = {
tab,
currentAgent: agent1,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const cards = screen.getAllByRole('button');
const agent2Card = cards.find((card) => within(card).queryByText('Agent 2'));
if (agent2Card) {
fireEvent.click(agent2Card);
expect(props.onAgentSelect).toHaveBeenCalled();
}
});
it('当前岗位无 Agent 应展示素材占位', () => {
const tab = createMockPositionTab({ agents: [] });
const props = {
tab,
currentAgent: undefined,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
expect(screen.getByText('暂无 Agent 素材')).toBeInTheDocument();
});
it('素材类型为 image 时应展示图片', () => {
const agent = createMockAgent({ media_type: 'image', media_url: 'https://example.com/image.png' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const img = screen.getByTestId('agent-image');
expect(img).toHaveAttribute('src', 'https://example.com/image.png');
expect(img).toHaveClass('object-contain');
});
it('素材类型为 gif 时应展示 GIF', () => {
const agent = createMockAgent({ media_type: 'gif', media_url: 'https://example.com/gif.gif' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const img = screen.getByTestId('agent-image');
expect(img).toHaveAttribute('src', 'https://example.com/gif.gif');
expect(img).toHaveClass('object-contain');
});
it('素材类型为 mp4 时应展示视频', () => {
const agent = createMockAgent({ media_type: 'mp4', media_url: 'https://example.com/video.mp4' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const video = screen.getByTestId('agent-video');
expect(video).toHaveAttribute('src', 'https://example.com/video.mp4');
expect(video).toHaveClass('object-contain');
});
it('素材类型为 mov 时应展示视频', () => {
const agent = createMockAgent({ media_type: 'mov', media_url: 'https://example.com/video.mov' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const video = screen.getByTestId('agent-video');
expect(video).toHaveAttribute('src', 'https://example.com/video.mov');
expect(video).toHaveClass('object-contain');
});
it('素材缺失时应展示占位', () => {
const agent = createMockAgent({ media_url: null, media_type: null });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
expect(screen.getByText('素材不可用')).toBeInTheDocument();
});
it('当 Agent 卡片超过 4 张时右侧列表应启用纵向滚动', () => {
const agents = Array.from({ length: 5 }, (_, index) =>
createMockAgent({
agent_id: `agent-${index + 1}`,
speech_title: `Agent ${index + 1}`,
})
);
const tab = createMockPositionTab({ agents });
const props = {
tab,
currentAgent: agents[0],
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const list = screen.getByTestId('agent-card-list');
expect(list.className).toContain('overflow-y-auto');
expect(list.className).toContain('max-h-');
});
describe('Agent 真实字段绑定', () => {
it('左侧素材应取当前 Agent 的 media_url', () => {
const agent = createMockAgent({
media_url: 'https://example.com/current-agent-media.png',
media_type: 'image',
});
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
expect(screen.getByTestId('agent-image')).toHaveAttribute('src', 'https://example.com/current-agent-media.png');
});
it('右侧 Agent 卡片应取 icon_url 和 speech_content', () => {
const agent = createMockAgent({
agent_name: '真实 Agent',
icon_url: 'https://example.com/real-agent-icon.png',
speech_title: '真实标题',
speech_content: '真实 Agent 概述',
});
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const card = screen.getByText('真实标题').closest('button');
expect(card).not.toBeNull();
expect(within(card as HTMLElement).getByAltText('真实 Agent')).toHaveAttribute('src', 'https://example.com/real-agent-icon.png');
expect(within(card as HTMLElement).queryByText('★')).not.toBeInTheDocument();
expect(within(card as HTMLElement).getByText('真实 Agent 概述')).toBeInTheDocument();
});
});
describe('左侧素材下方不展示标题和描述', () => {
it('不展示 Agent 标题 speech_title', () => {
const agent = createMockAgent({ speech_title: 'Agent 标题' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByText('Agent 标题')).not.toBeInTheDocument();
});
it('不展示英文副标题 / Position Agent', () => {
const agent = createMockAgent();
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByText(/Position Agent/)).not.toBeInTheDocument();
});
it('不展示 Agent 描述 speech_content', () => {
const agent = createMockAgent({ speech_content: 'Agent 描述内容' });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByText('Agent 描述内容')).not.toBeInTheDocument();
});
it('不展示兜底标题 "岗位智能体"', () => {
const agent = createMockAgent({ speech_title: null });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByText('岗位智能体')).not.toBeInTheDocument();
});
it('不展示兜底描述', () => {
const agent = createMockAgent({ speech_content: null });
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByText('为每个岗位配置专属 AI 助手')).not.toBeInTheDocument();
});
it('仅保留"立即体验"按钮', () => {
const agent = createMockAgent();
const tab = createMockPositionTab({ agents: [agent] });
const props = {
tab,
currentAgent: agent,
currentAgentIndex: 0,
onAgentSelect: vi.fn(),
onOpenQrcodeModal: vi.fn(),
qrcodeModalOpen: false,
onCloseQrcodeModal: vi.fn(),
};
render(<PositionAbilityArea {...props} />);
const leftMockup = screen.getByTestId('left-mockup');
expect(within(leftMockup).queryByRole('button', { name: /立即体验/ })).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: /立即体验/ })).toBeInTheDocument();
});
});
});