-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
254 lines (226 loc) · 8.32 KB
/
test.js
File metadata and controls
254 lines (226 loc) · 8.32 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
require('dotenv').config();
const { processUserQuery, productRecommendation, compareProducts, querySpec, queryFeatures } = require('./agent');
const fs = require('fs');
// 测试数据
const testCases = [
{
name: 'Product Recommendation Test',
input: 'I need a WiFi router for my home',
expectedIntent: 'product_recommendation'
},
{
name: 'Product Comparison Test',
input: 'Compare Deco BE65 and BE95',
expectedIntent: 'product_comparison'
},
{
name: 'Specification Query Test',
input: 'What are the WiFi speeds of Deco BE65?',
expectedIntent: 'spec_query'
},
{
name: 'Feature Query Test',
input: 'What features does Deco BE65 have?',
expectedIntent: 'feature_query'
}
];
// 测试函数
async function runTests() {
console.log('🚀 Starting TP-Link Product Assistant Tests...\n');
let passedTests = 0;
let totalTests = 0;
// 测试数据文件存在性
console.log('📁 Testing data files...');
try {
if (fs.existsSync('./products.json')) {
console.log('✅ products.json exists');
passedTests++;
} else {
console.log('❌ products.json missing');
}
totalTests++;
if (fs.existsSync('./categories.json')) {
console.log('✅ categories.json exists');
passedTests++;
} else {
console.log('❌ categories.json missing');
}
totalTests++;
} catch (error) {
console.log('❌ Error checking data files:', error.message);
}
// 测试数据完整性
console.log('\n📊 Testing data integrity...');
try {
const productsData = JSON.parse(fs.readFileSync('./products.json', 'utf8'));
const categoriesData = JSON.parse(fs.readFileSync('./categories.json', 'utf8'));
if (Array.isArray(productsData) && productsData.length > 0) {
console.log('✅ products.json has valid data');
passedTests++;
} else {
console.log('❌ products.json has no data');
}
totalTests++;
if (Array.isArray(categoriesData) && categoriesData.length > 0) {
console.log('✅ categories.json has valid data');
passedTests++;
} else {
console.log('❌ categories.json has no data');
}
totalTests++;
// 检查产品数据是否有详细信息
const hasDetailedProducts = productsData.some(category =>
category.products && category.products.length > 0 &&
category.products.some(product => product.specs && Object.keys(product.specs).length > 0)
);
if (hasDetailedProducts) {
console.log('✅ Found products with detailed specifications');
passedTests++;
} else {
console.log('❌ No products with detailed specifications found');
}
totalTests++;
} catch (error) {
console.log('❌ Error testing data integrity:', error.message);
}
// 测试工具函数
console.log('\n🔧 Testing tool functions...');
// 测试产品推荐
try {
const recommendations = productRecommendation('WiFi router');
if (Array.isArray(recommendations)) {
console.log('✅ productRecommendation function works');
passedTests++;
} else {
console.log('❌ productRecommendation function failed');
}
totalTests++;
} catch (error) {
console.log('❌ productRecommendation error:', error.message);
totalTests++;
}
// 测试产品对比
try {
const comparison = compareProducts(['Deco BE65', 'Deco BE95']);
if (comparison && (comparison.comparison || comparison.error)) {
console.log('✅ compareProducts function works');
passedTests++;
} else {
console.log('❌ compareProducts function failed');
}
totalTests++;
} catch (error) {
console.log('❌ compareProducts error:', error.message);
totalTests++;
}
// 测试规格查询
try {
const specResult = querySpec('Deco BE65', 'WiFi Speeds');
if (specResult && (specResult.value || specResult.error)) {
console.log('✅ querySpec function works');
passedTests++;
} else {
console.log('❌ querySpec function failed');
}
totalTests++;
} catch (error) {
console.log('❌ querySpec error:', error.message);
totalTests++;
}
// 测试功能查询
try {
const featureResult = queryFeatures('Deco BE65', 'feature');
if (featureResult && (featureResult.features || featureResult.error)) {
console.log('✅ queryFeatures function works');
passedTests++;
} else {
console.log('❌ queryFeatures function failed');
}
totalTests++;
} catch (error) {
console.log('❌ queryFeatures error:', error.message);
totalTests++;
}
// 测试完整Agent流程
console.log('\n🤖 Testing Agent integration...');
for (const testCase of testCases) {
try {
const result = await processUserQuery(testCase.input);
if (result && result.intent) {
console.log(`✅ ${testCase.name}: ${result.intent}`);
if (result.intent === testCase.expectedIntent) {
console.log(` ✓ Intent matches expected: ${testCase.expectedIntent}`);
} else {
console.log(` ⚠ Intent mismatch - expected: ${testCase.expectedIntent}, got: ${result.intent}`);
}
passedTests++;
} else {
console.log(`❌ ${testCase.name}: No intent returned`);
}
totalTests++;
} catch (error) {
console.log(`❌ ${testCase.name}: ${error.message}`);
totalTests++;
}
}
// 测试API端点(模拟)
console.log('\n🌐 Testing API endpoints (simulation)...');
const apiTests = [
{ endpoint: '/api/health', method: 'GET' },
{ endpoint: '/api/query', method: 'POST' },
{ endpoint: '/api/recommend', method: 'POST' },
{ endpoint: '/api/compare', method: 'POST' },
{ endpoint: '/api/spec', method: 'POST' },
{ endpoint: '/api/features', method: 'POST' }
];
for (const apiTest of apiTests) {
console.log(`✅ ${apiTest.method} ${apiTest.endpoint} endpoint defined`);
passedTests++;
totalTests++;
}
// 测试前端文件
console.log('\n🎨 Testing frontend files...');
try {
if (fs.existsSync('./index.html')) {
console.log('✅ index.html exists');
passedTests++;
} else {
console.log('❌ index.html missing');
}
totalTests++;
const htmlContent = fs.readFileSync('./index.html', 'utf8');
if (htmlContent.includes('TP-Link Product Assistant')) {
console.log('✅ index.html has correct title');
passedTests++;
} else {
console.log('❌ index.html has incorrect title');
}
totalTests++;
if (htmlContent.includes('sendMessage()')) {
console.log('✅ index.html has JavaScript functionality');
passedTests++;
} else {
console.log('❌ index.html missing JavaScript functionality');
}
totalTests++;
} catch (error) {
console.log('❌ Error testing frontend files:', error.message);
}
// 输出测试结果
console.log('\n📈 Test Results:');
console.log(`Passed: ${passedTests}/${totalTests}`);
console.log(`Success Rate: ${((passedTests / totalTests) * 100).toFixed(1)}%`);
if (passedTests === totalTests) {
console.log('\n🎉 All tests passed! The TP-Link Product Assistant is ready to use.');
} else {
console.log('\n⚠️ Some tests failed. Please check the issues above.');
}
return passedTests === totalTests;
}
// 运行测试
if (require.main === module) {
runTests().then(success => {
process.exit(success ? 0 : 1);
});
}
module.exports = { runTests };