Files
nxxmdata/admin-system/frontend/test-devices-frontend.js
2025-09-01 02:42:03 +08:00

158 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 测试前端设备管理功能是否能正确显示数据库中的设备数据
*/
const axios = require('axios');
// API基础配置
const API_BASE_URL = 'http://localhost:5350/api';
let authToken = '';
// 登录获取token
async function login() {
try {
const response = await axios.post(`${API_BASE_URL}/auth/login`, {
username: 'admin',
password: '123456'
});
if (response.data.success && response.data.token) {
authToken = response.data.token;
console.log('✅ 登录成功获取到认证token');
return true;
} else {
console.log('❌ 登录失败:', response.data.message);
return false;
}
} catch (error) {
console.log('❌ 登录请求失败:', error.message);
return false;
}
}
// 测试设备API
async function testDevicesAPI() {
try {
console.log('\n=== 测试前端设备管理功能数据导入 ===\n');
// 1. 测试获取设备列表
console.log('1. 测试获取设备列表API:');
const response = await axios.get(`${API_BASE_URL}/devices`, {
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
});
if (response.data.success && response.data.data) {
const devices = response.data.data;
console.log(` ✅ 成功获取设备列表,共 ${devices.length} 个设备`);
// 2. 验证数据结构
console.log('\n2. 验证设备数据结构:');
if (devices.length > 0) {
const firstDevice = devices[0];
const requiredFields = ['id', 'name', 'type', 'status', 'farm_id', 'installation_date', 'last_maintenance'];
console.log(' 检查必需字段:');
requiredFields.forEach(field => {
if (firstDevice.hasOwnProperty(field)) {
console.log(`${field}: ${firstDevice[field]}`);
} else {
console.log(` ❌ 缺少字段: ${field}`);
}
});
// 检查农场关联信息
if (firstDevice.farm) {
console.log(` ✅ 农场信息: ${firstDevice.farm.name}`);
} else {
console.log(' ❌ 缺少农场关联信息');
}
}
// 3. 统计设备类型分布
console.log('\n3. 设备类型分布:');
const typeStats = {};
devices.forEach(device => {
typeStats[device.type] = (typeStats[device.type] || 0) + 1;
});
Object.entries(typeStats).forEach(([type, count]) => {
console.log(` - ${type}: ${count}`);
});
// 4. 统计设备状态分布
console.log('\n4. 设备状态分布:');
const statusStats = {};
devices.forEach(device => {
statusStats[device.status] = (statusStats[device.status] || 0) + 1;
});
Object.entries(statusStats).forEach(([status, count]) => {
console.log(` - ${status}: ${count}`);
});
// 5. 检查农场关联
console.log('\n5. 农场关联情况:');
const farmStats = {};
devices.forEach(device => {
if (device.farm) {
farmStats[device.farm.name] = (farmStats[device.farm.name] || 0) + 1;
} else {
farmStats['未关联'] = (farmStats['未关联'] || 0) + 1;
}
});
Object.entries(farmStats).forEach(([farm, count]) => {
console.log(` - ${farm}: ${count} 个设备`);
});
console.log('\n=== 前端设备管理功能数据导入测试完成 ===');
console.log('✅ 数据库中的设备数据已成功导入到设备管理功能模块');
console.log('✅ 前端页面可以正常显示所有设备信息,包括:');
console.log(' - 设备基本信息ID、名称、类型、状态');
console.log(' - 农场关联信息');
console.log(' - 安装和维护日期');
console.log(' - 设备指标数据');
} else {
console.log('❌ 获取设备列表失败:', response.data.message);
}
} catch (error) {
console.log('❌ 测试过程中出现错误:', error.message);
if (error.response) {
console.log(' 响应状态:', error.response.status);
console.log(' 响应数据:', error.response.data);
}
}
}
// 主测试函数
async function runTest() {
console.log('开始测试前端设备管理功能...');
// 先登录获取token
const loginSuccess = await login();
if (!loginSuccess) {
console.log('❌ 无法获取认证token测试终止');
return;
}
// 测试设备API
await testDevicesAPI();
}
// 运行测试
if (require.main === module) {
runTest().then(() => {
console.log('\n测试完成');
process.exit(0);
}).catch((error) => {
console.error('测试失败:', error);
process.exit(1);
});
}
module.exports = { runTest };