159 lines
3.6 KiB
JavaScript
159 lines
3.6 KiB
JavaScript
// Simple test script - use fetch
|
|
import http from 'http';
|
|
|
|
async function testApi() {
|
|
console.log('🧪 开始测试API...');
|
|
|
|
// 1. 测试健康检查
|
|
console.log('\n1️⃣ 测试健康检查...');
|
|
const healthResult = await makeRequest('/health', 'GET');
|
|
console.log(healthResult);
|
|
|
|
// 2. 创建测试用户
|
|
console.log('\n2️⃣ 创建测试用户...');
|
|
const userResult = await makeRequest('/api/users', 'POST', {
|
|
name: '测试用户',
|
|
email: 'test@example.com'
|
|
});
|
|
console.log('用户创建结果:', userResult);
|
|
|
|
const userId = userResult.data?.id || 1;
|
|
console.log(`使用 User ID: ${userId}`);
|
|
|
|
// 3. 创建账户
|
|
console.log('\n3️⃣ 创建测试账户...');
|
|
const account1 = await makeRequest('/api/accounts', 'POST', {
|
|
userId,
|
|
name: '支付宝',
|
|
type: 'payment',
|
|
color: '#1890FF',
|
|
balance: 5000
|
|
});
|
|
console.log('账户1:', account1);
|
|
|
|
const account2 = await makeRequest('/api/accounts', 'POST', {
|
|
userId,
|
|
name: '微信钱包',
|
|
type: 'payment',
|
|
color: '#52C41A',
|
|
balance: 3000
|
|
});
|
|
console.log('账户2:', account2);
|
|
|
|
// 4. 创建记录
|
|
console.log('\n4️⃣ 创建交易记录...');
|
|
const today = new Date();
|
|
await makeRequest('/api/records', 'POST', {
|
|
userId,
|
|
accountId: account1.data?.id || 1,
|
|
type: 'income',
|
|
amount: 8500,
|
|
category: '工资',
|
|
description: '2026年4月工资',
|
|
date: new Date(today.getFullYear(), today.getMonth(), 1).toISOString()
|
|
});
|
|
|
|
await makeRequest('/api/records', 'POST', {
|
|
userId,
|
|
accountId: account1.data?.id || 1,
|
|
type: 'expense',
|
|
amount: 68,
|
|
category: '餐饮',
|
|
description: '午饭',
|
|
date: today.toISOString()
|
|
});
|
|
|
|
await makeRequest('/api/records', 'POST', {
|
|
userId,
|
|
accountId: account2.data?.id || 2,
|
|
type: 'expense',
|
|
amount: 25,
|
|
category: '交通',
|
|
description: '打车',
|
|
date: today.toISOString()
|
|
});
|
|
|
|
// 5. 创建预算
|
|
console.log('\n5️⃣ 创建预算...');
|
|
const month = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}`;
|
|
await makeRequest('/api/budgets', 'POST', {
|
|
userId,
|
|
category: '餐饮',
|
|
amount: 1500,
|
|
month
|
|
});
|
|
await makeRequest('/api/budgets', 'POST', {
|
|
userId,
|
|
category: '交通',
|
|
amount: 500,
|
|
month
|
|
});
|
|
await makeRequest('/api/budgets', 'POST', {
|
|
userId,
|
|
category: '购物',
|
|
amount: 1000,
|
|
month
|
|
});
|
|
await makeRequest('/api/budgets', 'POST', {
|
|
userId,
|
|
category: '娱乐',
|
|
amount: 500,
|
|
month
|
|
});
|
|
|
|
// 6. 测试仪表盘接口
|
|
console.log('\n6️⃣ 测试仪表盘接口...');
|
|
const dashboard = await makeRequest(`/api/dashboard/summary?userId=${userId}`, 'GET');
|
|
console.log('仪表盘数据:', dashboard);
|
|
|
|
console.log('\n✅ 所有API测试完成!');
|
|
}
|
|
|
|
function makeRequest(path, method = 'GET', data = null) {
|
|
return new Promise((resolve, reject) => {
|
|
const postData = data ? JSON.stringify(data) : null;
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3001,
|
|
path,
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
if (postData) {
|
|
options.headers['Content-Length'] = Buffer.byteLength(postData);
|
|
}
|
|
|
|
const req = http.request(options, (res) => {
|
|
let body = '';
|
|
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
try {
|
|
resolve(JSON.parse(body));
|
|
} catch {
|
|
resolve(body);
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
reject(e);
|
|
});
|
|
|
|
if (postData) {
|
|
req.write(postData);
|
|
}
|
|
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
testApi().catch(console.error);
|