Files
nxxmdata/backend/simple-test.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

// 简化的测试脚本
console.log('开始测试预警检测逻辑...');
// 模拟前端判断函数
function determineAlertType(record) {
const alerts = []
// 检查电量预警
if (record.battery !== undefined && record.battery !== null && record.battery < 20) {
alerts.push('battery')
}
// 检查脱落预警 (bandge_status为0)
if (record.bandge_status !== undefined && record.bandge_status !== null && record.bandge_status === 0) {
alerts.push('wear')
}
// 检查离线预警 (is_connect为0)
if (record.is_connect !== undefined && record.is_connect !== null && record.is_connect === 0) {
alerts.push('offline')
}
// 检查温度预警
if (record.temperature !== undefined && record.temperature !== null) {
if (record.temperature < 20) {
alerts.push('temperature_low')
} else if (record.temperature > 40) {
alerts.push('temperature_high')
}
}
// 检查运动异常预警 (steps - y_steps为0)
if (record.steps !== undefined && record.y_steps !== undefined &&
record.steps !== null && record.y_steps !== null) {
const movementDiff = record.steps - record.y_steps
if (movementDiff === 0) {
alerts.push('movement')
}
}
// 返回第一个预警类型如果没有预警则返回null
return alerts.length > 0 ? alerts[0] : null
}
// 测试用例
const testCases = [
{
name: '正常设备',
data: { battery: 85, temperature: 25, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 },
expected: null
},
{
name: '低电量预警',
data: { battery: 15, temperature: 25, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 },
expected: 'battery'
},
{
name: '离线预警',
data: { battery: 85, temperature: 25, is_connect: 0, bandge_status: 1, steps: 1000, y_steps: 500 },
expected: 'offline'
}
];
// 运行测试
testCases.forEach((testCase, index) => {
const result = determineAlertType(testCase.data);
const success = result === testCase.expected;
console.log(`测试 ${index + 1}: ${testCase.name} - ${success ? '✅ 通过' : '❌ 失败'}`);
console.log(` 预期: ${testCase.expected}, 实际: ${result}`);
});
console.log('测试完成!');