174 lines
4.5 KiB
JavaScript
174 lines
4.5 KiB
JavaScript
import { productService } from '../src/renderer/services';
|
||
import { orderService } from '../src/renderer/services';
|
||
import { authService } from '../src/renderer/services';
|
||
|
||
// 模拟测试环境
|
||
const setupTestEnvironment = () => {
|
||
// 模拟localStorage
|
||
const localStorageMock = {
|
||
getItem: jest.fn(),
|
||
setItem: jest.fn(),
|
||
removeItem: jest.fn(),
|
||
clear: jest.fn(),
|
||
};
|
||
global.localStorage = localStorageMock;
|
||
|
||
// 模拟fetch
|
||
global.fetch = jest.fn();
|
||
|
||
// 设置默认用户信息
|
||
const mockUser = {
|
||
id: 1,
|
||
username: 'admin',
|
||
name: '管理员',
|
||
role: 'admin'
|
||
};
|
||
|
||
localStorageMock.getItem.mockImplementation((key) => {
|
||
if (key === 'userInfo') {
|
||
return JSON.stringify(mockUser);
|
||
}
|
||
return null;
|
||
});
|
||
};
|
||
|
||
// 测试API连接
|
||
const testApiConnection = async () => {
|
||
console.log('测试API连接...');
|
||
try {
|
||
const commonPorts = [3000, 3001, 3002, 9876, 8080];
|
||
|
||
for (const port of commonPorts) {
|
||
try {
|
||
const response = await fetch(`http://localhost:${port}/api/health`, {
|
||
method: 'GET',
|
||
signal: AbortSignal.timeout(1000)
|
||
});
|
||
if (response.ok) {
|
||
console.log(`✅ API连接成功,端口: ${port}`);
|
||
return port;
|
||
}
|
||
} catch (error) {
|
||
// 端口不可用,继续尝试下一个
|
||
}
|
||
}
|
||
|
||
console.error('❌ 无法连接到API服务器');
|
||
return null;
|
||
} catch (error) {
|
||
console.error('❌ API连接测试失败:', error);
|
||
return null;
|
||
}
|
||
};
|
||
|
||
// 测试认证功能
|
||
const testAuth = async () => {
|
||
console.log('测试认证功能...');
|
||
try {
|
||
// 测试登录
|
||
const loginData = await authService.login('admin', 'admin123');
|
||
if (loginData && loginData.id) {
|
||
console.log('✅ 用户登录功能正常');
|
||
} else {
|
||
console.error('❌ 用户登录功能异常');
|
||
}
|
||
|
||
// 测试获取当前用户
|
||
const currentUser = authService.getCurrentUser();
|
||
if (currentUser) {
|
||
console.log('✅ 获取当前用户功能正常');
|
||
} else {
|
||
console.error('❌ 获取当前用户功能异常');
|
||
}
|
||
} catch (error) {
|
||
console.error('❌ 认证测试失败:', error);
|
||
}
|
||
};
|
||
|
||
// 测试商品功能
|
||
const testProducts = async () => {
|
||
console.log('测试商品功能...');
|
||
try {
|
||
// 测试获取商品列表
|
||
const products = await productService.getProducts();
|
||
if (products && Array.isArray(products.data)) {
|
||
console.log(`✅ 商品列表获取成功,共 ${products.data.length} 个商品`);
|
||
|
||
// 测试获取商品分类
|
||
const categories = await productService.getCategories();
|
||
if (categories && Array.isArray(categories)) {
|
||
console.log(`✅ 商品分类获取成功,共 ${categories.length} 个分类`);
|
||
} else {
|
||
console.error('❌ 商品分类获取失败');
|
||
}
|
||
} else {
|
||
console.error('❌ 商品列表获取失败');
|
||
}
|
||
} catch (error) {
|
||
console.error('❌ 商品功能测试失败:', error);
|
||
}
|
||
};
|
||
|
||
// 测试订单功能
|
||
const testOrders = async () => {
|
||
console.log('测试订单功能...');
|
||
try {
|
||
// 测试获取订单列表
|
||
const orders = await orderService.getOrders();
|
||
if (orders && Array.isArray(orders.data)) {
|
||
console.log(`✅ 订单列表获取成功,共 ${orders.data.length} 个订单`);
|
||
} else {
|
||
console.error('❌ 订单列表获取失败');
|
||
}
|
||
|
||
// 测试获取挂单列表
|
||
const heldOrders = await orderService.getHeldOrders();
|
||
if (heldOrders && Array.isArray(heldOrders)) {
|
||
console.log(`✅ 挂单列表获取成功,共 ${heldOrders.length} 个挂单`);
|
||
} else {
|
||
console.error('❌ 挂单列表获取失败');
|
||
}
|
||
} catch (error) {
|
||
console.error('❌ 订单功能测试失败:', error);
|
||
}
|
||
};
|
||
|
||
// 运行所有测试
|
||
const runTests = async () => {
|
||
console.log('开始迷你收银台系统功能测试...\n');
|
||
|
||
setupTestEnvironment();
|
||
|
||
// 测试API连接
|
||
const apiPort = await testApiConnection();
|
||
if (!apiPort) {
|
||
console.error('\n❌ API连接失败,无法继续测试');
|
||
return;
|
||
}
|
||
|
||
console.log('\n');
|
||
|
||
// 测试各项功能
|
||
await testAuth();
|
||
await testProducts();
|
||
await testOrders();
|
||
|
||
console.log('\n测试完成');
|
||
};
|
||
|
||
// 如果直接运行此文件,执行测试
|
||
if (require.main === module) {
|
||
runTests().catch(error => {
|
||
console.error('测试执行失败:', error);
|
||
process.exit(1);
|
||
});
|
||
}
|
||
|
||
module.exports = {
|
||
setupTestEnvironment,
|
||
testApiConnection,
|
||
testAuth,
|
||
testProducts,
|
||
testOrders,
|
||
runTests
|
||
}; |