274 lines
7.7 KiB
JavaScript
274 lines
7.7 KiB
JavaScript
const http = require('http');
|
|
const app = require('./final-backend');
|
|
|
|
let server;
|
|
let testPurchaseRequestId;
|
|
|
|
// 简单的HTTP请求函数
|
|
function request(options, data = null) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
let body = '';
|
|
res.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
try {
|
|
const parsed = JSON.parse(body);
|
|
resolve({ status: res.statusCode, body: parsed });
|
|
} catch (e) {
|
|
resolve({ status: res.statusCode, body: body });
|
|
}
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
reject(e);
|
|
});
|
|
|
|
if (data) {
|
|
req.write(JSON.stringify(data));
|
|
}
|
|
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
// 测试采购流程
|
|
async function testPurchaseFlow() {
|
|
console.log('=== 测试采购流程 ===');
|
|
|
|
// 1. 创建采购申请
|
|
console.log('1. 创建采购申请...');
|
|
const createResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/purchase-requests',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}, {
|
|
project_id: 3,
|
|
applicant: '测试申请人',
|
|
request_date: '2026-03-25',
|
|
expense_category: 'material',
|
|
total_amount: 15000,
|
|
items: [
|
|
{
|
|
product_name: '测试商品A',
|
|
quantity: 10,
|
|
unit_price: 1000,
|
|
total_price: 10000
|
|
},
|
|
{
|
|
product_name: '测试商品B',
|
|
quantity: 5,
|
|
unit_price: 1000,
|
|
total_price: 5000
|
|
}
|
|
]
|
|
});
|
|
|
|
console.log('创建采购申请响应:', createResponse.status, createResponse.body);
|
|
if (createResponse.status === 200 && createResponse.body.success) {
|
|
testPurchaseRequestId = createResponse.body.data.id;
|
|
console.log('采购申请ID:', testPurchaseRequestId);
|
|
} else {
|
|
console.error('创建采购申请失败');
|
|
return false;
|
|
}
|
|
|
|
// 2. 获取采购申请列表
|
|
console.log('\n2. 获取采购申请列表...');
|
|
const listResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/purchase-requests',
|
|
method: 'GET'
|
|
});
|
|
console.log('获取采购申请列表响应:', listResponse.status, listResponse.body);
|
|
if (listResponse.status !== 200 || !listResponse.body.success) {
|
|
console.error('获取采购申请列表失败');
|
|
return false;
|
|
}
|
|
|
|
// 3. 获取采购申请详情
|
|
console.log('\n3. 获取采购申请详情...');
|
|
const detailResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: `/api/purchase-requests/${testPurchaseRequestId}`,
|
|
method: 'GET'
|
|
});
|
|
console.log('获取采购申请详情响应:', detailResponse.status, detailResponse.body);
|
|
if (detailResponse.status !== 200 || !detailResponse.body.success) {
|
|
console.error('获取采购申请详情失败');
|
|
return false;
|
|
}
|
|
|
|
// 4. 提交采购申请
|
|
console.log('\n4. 提交采购申请...');
|
|
const submitResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: `/api/purchase-requests/${testPurchaseRequestId}/submit`,
|
|
method: 'POST'
|
|
});
|
|
console.log('提交采购申请响应:', submitResponse.status, submitResponse.body);
|
|
if (submitResponse.status !== 200 || !submitResponse.body.success) {
|
|
console.error('提交采购申请失败');
|
|
return false;
|
|
}
|
|
|
|
// 5. 审批采购申请
|
|
console.log('\n5. 审批采购申请...');
|
|
const approveResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: `/api/purchase-requests/${testPurchaseRequestId}/approve`,
|
|
method: 'POST'
|
|
});
|
|
console.log('审批采购申请响应:', approveResponse.status, approveResponse.body);
|
|
if (approveResponse.status !== 200 || !approveResponse.body.success) {
|
|
console.error('审批采购申请失败');
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// 测试库存管理流程
|
|
async function testInventoryFlow() {
|
|
console.log('\n=== 测试库存管理流程 ===');
|
|
|
|
// 1. 获取库存记录
|
|
console.log('1. 获取库存记录...');
|
|
const recordsResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/inventory',
|
|
method: 'GET'
|
|
});
|
|
console.log('获取库存记录响应:', recordsResponse.status, recordsResponse.body);
|
|
if (recordsResponse.status !== 200 || !recordsResponse.body.success) {
|
|
console.error('获取库存记录失败');
|
|
return false;
|
|
}
|
|
|
|
// 2. 获取库存汇总
|
|
console.log('\n2. 获取库存汇总...');
|
|
const summaryResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/inventory/summary',
|
|
method: 'GET'
|
|
});
|
|
console.log('获取库存汇总响应:', summaryResponse.status, summaryResponse.body);
|
|
if (summaryResponse.status !== 200 || !summaryResponse.body.success) {
|
|
console.error('获取库存汇总失败');
|
|
return false;
|
|
}
|
|
|
|
// 3. 创建出库记录
|
|
console.log('3. 创建出库记录...');
|
|
const outResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/inventory/out',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}, {
|
|
project_id: 3,
|
|
product_id: 1,
|
|
quantity: 10,
|
|
operator: '测试操作员'
|
|
});
|
|
console.log('创建出库记录响应:', outResponse.status, outResponse.body);
|
|
if (outResponse.status !== 200 || !outResponse.body.success) {
|
|
console.error('创建出库记录失败');
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// 测试成本统计功能
|
|
async function testCostStatistics() {
|
|
console.log('\n=== 测试成本统计功能 ===');
|
|
|
|
// 获取项目成本统计
|
|
console.log('1. 获取项目成本统计...');
|
|
const costResponse = await request({
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/projects/3/cost-summary',
|
|
method: 'GET'
|
|
});
|
|
console.log('获取项目成本统计响应:', costResponse.status, costResponse.body);
|
|
if (costResponse.status !== 200 || !costResponse.body.success) {
|
|
console.error('获取项目成本统计失败');
|
|
return false;
|
|
}
|
|
|
|
// 验证成本统计数据结构
|
|
const costData = costResponse.body.data;
|
|
if (!costData.project_name || !costData.purchase_cost || costData.payment_cost === undefined || costData.total_cost === undefined || costData.profit === undefined) {
|
|
console.error('成本统计数据结构不完整');
|
|
return false;
|
|
}
|
|
|
|
console.log('成本统计数据验证通过:', {
|
|
project_name: costData.project_name,
|
|
purchase_cost: costData.purchase_cost,
|
|
payment_cost: costData.payment_cost,
|
|
total_cost: costData.total_cost,
|
|
profit: costData.profit
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
// 主测试函数
|
|
async function runTests() {
|
|
try {
|
|
// 启动服务器
|
|
server = app.listen(3005, () => {
|
|
console.log('测试服务器启动在端口3005');
|
|
});
|
|
|
|
// 等待服务器启动
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
// 运行测试
|
|
const purchaseResult = await testPurchaseFlow();
|
|
const inventoryResult = await testInventoryFlow();
|
|
const costResult = await testCostStatistics();
|
|
|
|
// 输出测试结果
|
|
console.log('\n=== 测试结果 ===');
|
|
console.log('采购流程测试:', purchaseResult ? '通过' : '失败');
|
|
console.log('库存管理流程测试:', inventoryResult ? '通过' : '失败');
|
|
console.log('成本统计功能测试:', costResult ? '通过' : '失败');
|
|
|
|
if (purchaseResult && inventoryResult && costResult) {
|
|
console.log('\n🎉 所有测试通过!');
|
|
} else {
|
|
console.log('\n❌ 部分测试失败!');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('测试过程中发生错误:', error);
|
|
} finally {
|
|
// 关闭服务器
|
|
if (server) {
|
|
server.close();
|
|
console.log('测试服务器已关闭');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
runTests(); |