CallAssemblyDetailInputVariables.test.tsx
4.87 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import CallAssemblyDetail from '../pages/Calls/CallAssemblyDetail';
const mockMessageSuccess = vi.hoisted(() => vi.fn());
/** 构建一个带模板变量和对应请求体的 mock 数据 */
function buildDetailWithVars(vars: Record<string, string>) {
const entries = Object.entries(vars);
const systemPrompt = entries.map(([k]) => `{{${k}}}`).join('\n');
const userPrompt = '';
// 请求体内容直接包含变量值(模拟真实场景)
const content = entries.map(([, v]) => v).join('\n');
return {
id: '1',
request_body: { model: 'gpt-4', messages: [{ role: 'user', content }] },
template_id: 'tpl-1',
template_info: {
id: 1,
title: '测试模板',
code: 'test',
template_type: 'text_generation',
version: 1,
system_prompt: systemPrompt,
user_prompt: userPrompt,
},
provider_name: 'OpenAI',
account_name: '主账号',
sensitive_replacements: null,
response_body: { choices: [{ message: { content: 'ok' } }] },
status: 'success',
prompt_tokens: 10,
completion_tokens: 20,
total_tokens: 30,
duration_ms: 500,
created_at: '2026-06-25T12:00:00',
called_at: '2026-06-25T12:00:00',
model_name: 'gpt-4',
biz_type: null,
error_message: null,
};
}
const mockNoTemplateDetail = vi.hoisted(() => ({
id: '2',
request_body: { model: 'gpt-4', messages: [{ role: 'user', content: '你好' }] },
template_id: null,
template_info: null,
provider_name: 'OpenAI',
account_name: '主账号',
sensitive_replacements: null,
response_body: { choices: [{ message: { content: '你好!' } }] },
status: 'success',
prompt_tokens: 10,
completion_tokens: 20,
total_tokens: 30,
duration_ms: 500,
created_at: '2026-06-25T12:00:00',
called_at: '2026-06-25T12:00:00',
model_name: 'gpt-4',
biz_type: null,
error_message: null,
}));
let mockDetail: any = null;
vi.mock('../stores/callRecordStore', () => ({
useCallRecordStore: () => ({
detail: mockDetail,
detailLoading: false,
getCallDetail: vi.fn(),
}),
}));
vi.mock('antd', async () => {
const actual = await vi.importActual('antd');
return {
...actual,
message: {
success: mockMessageSuccess,
error: vi.fn(),
},
};
});
describe('CallAssemblyDetail — 输入变量展示与复制', () => {
beforeEach(() => {
vi.clearAllMocks();
Object.assign(navigator, {
clipboard: {
writeText: vi.fn().mockResolvedValue(undefined),
},
});
});
it('有模板变量时渲染变量卡片', async () => {
mockDetail = buildDetailWithVars({ question: '你好世界', language: '中文' });
render(<CallAssemblyDetail open={true} callId="1" onClose={() => {}} />);
await waitFor(() => {
expect(screen.getByText('输入变量')).toBeInTheDocument();
});
// 检查变量卡片元素
const cards = document.querySelectorAll('.variable-card');
expect(cards.length).toBe(2);
// 检查变量值
const values = document.querySelectorAll('.variable-card-value');
expect(values[0].textContent).toBe('你好世界');
expect(values[1].textContent).toBe('中文');
});
it('点击变量复制按钮复制变量值', async () => {
mockDetail = buildDetailWithVars({ question: '你好世界' });
render(<CallAssemblyDetail open={true} callId="1" onClose={() => {}} />);
await waitFor(() => {
expect(screen.getByText('输入变量')).toBeInTheDocument();
});
// 找到变量卡片上的复制按钮
const copyBtns = document.querySelectorAll('.variable-copy-btn');
expect(copyBtns.length).toBe(1);
fireEvent.click(copyBtns[0]);
await waitFor(() => {
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('你好世界');
});
await waitFor(() => {
expect(mockMessageSuccess).toHaveBeenCalledWith('已复制');
});
});
it('无模板时不显示输入变量区域', async () => {
mockDetail = mockNoTemplateDetail;
render(<CallAssemblyDetail open={true} callId="2" onClose={() => {}} />);
await waitFor(() => {
expect(screen.getByText('最终请求体')).toBeInTheDocument();
});
expect(screen.queryByText('输入变量')).not.toBeInTheDocument();
});
it('点击请求体复制按钮复制完整请求 JSON', async () => {
mockDetail = buildDetailWithVars({ question: '你好' });
render(<CallAssemblyDetail open={true} callId="1" onClose={() => {}} />);
await waitFor(() => {
const copyBtn = screen.getByTitle('复制请求体');
fireEvent.click(copyBtn);
});
const expectedJson = JSON.stringify(mockDetail.request_body, null, 2);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(expectedJson);
expect(mockMessageSuccess).toHaveBeenCalledWith('请求体已复制');
});
});