-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
266 lines (226 loc) · 8.6 KB
/
test.js
File metadata and controls
266 lines (226 loc) · 8.6 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
#!/usr/bin/env node
/**
* Claude Code StatusBar 测试脚本
* 验证核心功能和依赖是否正常工作
*/
const fs = require('fs');
const path = require('path');
class StatusBarTester {
constructor() {
this.testResults = [];
this.config = this.loadTestConfig();
}
/**
* 加载测试配置
*/
loadTestConfig() {
try {
const configPath = path.join(process.cwd(), 'config.json');
if (fs.existsSync(configPath)) {
return JSON.parse(fs.readFileSync(configPath, 'utf8'));
}
} catch (error) {
console.warn('⚠️ 无法加载config.json,使用默认测试配置');
}
return {
api: {
url: 'https://your-domain.com/admin-next/api-stats?apiId=your-api-id',
timeout: 15000
}
};
}
/**
* 测试基础模块依赖
*/
async testDependencies() {
console.log('🧪 测试模块依赖...');
const requiredModules = [
'fs',
'path',
'os'
];
const optionalModules = [
{ name: 'axios', package: 'axios' },
{ name: 'cheerio', package: 'cheerio' }
];
let requiredPassed = 0;
let optionalPassed = 0;
let total = requiredModules.length + optionalModules.length;
// 测试必需模块
for (const module of requiredModules) {
try {
require(module);
console.log(` ✅ ${module}`);
requiredPassed++;
} catch (error) {
console.log(` ❌ ${module}: ${error.message}`);
this.testResults.push({ test: `dependency-${module}`, passed: false, error: error.message });
}
}
// 测试可选模块(通过try-catch避免中断)
for (const { name, package: pkg } of optionalModules) {
try {
require(pkg);
console.log(` ✅ ${name}`);
optionalPassed++;
} catch (error) {
console.log(` ⚠️ ${name}: ${error.message}`);
this.testResults.push({ test: `optional-dependency-${name}`, passed: false, warning: error.message });
}
}
const totalPassed = requiredPassed + optionalPassed;
console.log(`📊 依赖测试结果: ${totalPassed}/${total} 通过\n`);
// 只要求所有必需模块通过,可选模块通过与否不影响结果
return requiredPassed === requiredModules.length;
}
/**
* 测试核心文件是否存在
*/
async testCoreFiles() {
console.log('📁 测试核心文件...');
const coreFiles = [
'statusline.js',
'api-service.js',
'data-parser.js',
'admin-html-provider.js',
'package.json'
];
let passed = 0;
for (const file of coreFiles) {
const filePath = path.join(process.cwd(), file);
if (fs.existsSync(filePath)) {
console.log(` ✅ ${file}`);
passed++;
} else {
console.log(` ❌ ${file} - 文件不存在`);
this.testResults.push({ test: `file-${file}`, passed: false, error: '文件不存在' });
}
}
console.log(`📊 文件测试结果: ${passed}/${coreFiles.length} 通过\n`);
return passed === coreFiles.length;
}
/**
* 测试配置文件
*/
async testConfiguration() {
console.log('⚙️ 测试配置...');
let passed = 0;
let total = 3;
// 测试 package.json
try {
const pkg = require(path.join(process.cwd(), 'package.json'));
if (pkg.name && pkg.version && pkg.main) {
console.log(` ✅ package.json 格式正确`);
passed++;
} else {
console.log(` ⚠️ package.json 缺少必要字段`);
}
} catch (error) {
console.log(` ❌ package.json: ${error.message}`);
}
// 测试 config.json
try {
const config = require(path.join(process.cwd(), 'config.json'));
console.log(` ✅ config.json 加载成功`);
passed++;
} catch (error) {
console.log(` ⚠️ config.json: ${error.message}`);
}
// 测试 .claude 目录
const claudeDir = path.join(process.cwd(), '.claude');
if (fs.existsSync(claudeDir)) {
console.log(` ✅ .claude 目录存在`);
passed++;
} else {
console.log(` ⚠️ .claude 目录不存在`);
}
console.log(`📊 配置测试结果: ${passed}/${total} 通过\n`);
return passed >= 2; // 至少package.json和config.json要正常
}
/**
* 测试状态栏基础功能(不依赖网络)
*/
async testStatusLineBasics() {
console.log('🔧 测试状态栏基础功能...');
try {
// 检查能否加载StatusLine类
const statusLinePath = path.join(process.cwd(), 'statusline.js');
if (fs.existsSync(statusLinePath)) {
// 使用简单的模块加载测试,避免执行网络请求
const content = fs.readFileSync(statusLinePath, 'utf8');
// 适配当前实现(类名与方法)
const hasClass = content.includes('class StatusLine');
const hasMethod = content.includes('formatStatus(');
if (hasClass) {
console.log(' ✅ StatusLine 类定义存在');
} else {
console.log(' ❌ StatusLine 类定义缺失');
return false;
}
if (hasMethod) {
console.log(' ✅ formatStatus 方法存在');
} else {
console.log(' ❌ formatStatus 方法缺失');
return false;
}
console.log(' ✅ 状态栏基础结构完整');
return true;
}
} catch (error) {
console.log(` ❌ 状态栏测试失败: ${error.message}`);
return false;
}
return false;
}
/**
* 运行所有测试
*/
async runAllTests() {
console.log('🚀 开始 Claude Code StatusBar 功能测试\n');
const results = {
dependencies: await this.testDependencies(),
files: await this.testCoreFiles(),
configuration: await this.testConfiguration(),
statusline: await this.testStatusLineBasics()
};
// 汇总结果
console.log('📋 测试总结:');
console.log('========================================');
const totalTests = Object.keys(results).length;
const passedTests = Object.values(results).filter(Boolean).length;
for (const [testName, passed] of Object.entries(results)) {
const status = passed ? '✅' : '❌';
const testDisplayName = {
dependencies: '模块依赖',
files: '核心文件',
configuration: '配置检查',
statusline: '状态栏功能'
}[testName];
console.log(`${status} ${testDisplayName}: ${passed ? '通过' : '失败'}`);
}
console.log('========================================');
console.log(`🎯 总体结果: ${passedTests}/${totalTests} 项测试通过`);
if (passedTests === totalTests) {
console.log('🎉 所有测试通过!StatusBar 应该可以正常工作');
process.exit(0);
} else if (passedTests >= totalTests - 1) {
console.log('⚠️ 大部分测试通过,可能存在轻微问题');
process.exit(0);
} else {
console.log('❌ 发现严重问题,请检查依赖和配置');
console.log('\n💡 修复建议:');
console.log(' 1. 运行 npm install 重新安装依赖');
console.log(' 2. 检查 config.json 配置文件');
console.log(' 3. 确保网络连接正常');
process.exit(1);
}
}
}
if (require.main === module) {
const tester = new StatusBarTester();
tester.runAllTests().catch(error => {
console.error('❌ 测试运行异常:', error);
process.exit(1);
});
}
module.exports = StatusBarTester;