/** * 预警检测逻辑测试 * @file test-alert-detection-logic.js * @description 测试智能项圈预警的自动检测逻辑 */ // 模拟前端判断函数 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 } // 获取预警类型文本 function getAlertTypeText(type) { const typeMap = { 'battery': '低电量预警', 'offline': '离线预警', 'temperature_low': '温度过低预警', 'temperature_high': '温度过高预警', 'movement': '异常运动预警', 'wear': '佩戴异常预警' } return typeMap[type] || '未知预警' } // 测试数据 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' }, { name: '佩戴异常预警', data: { battery: 85, temperature: 25, is_connect: 1, bandge_status: 0, steps: 1000, y_steps: 500 }, expected: 'wear' }, { name: '温度过低预警', data: { battery: 85, temperature: 15, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: 'temperature_low' }, { name: '温度过高预警', data: { battery: 85, temperature: 45, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: 'temperature_high' }, { name: '异常运动预警', data: { battery: 85, temperature: 25, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 1000 }, expected: 'movement' }, { name: '多重预警(低电量+离线)', data: { battery: 15, temperature: 25, is_connect: 0, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: 'battery' // 应该返回第一个预警 }, { name: '边界值测试 - 电量20', data: { battery: 20, temperature: 25, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: null // 20不算低电量 }, { name: '边界值测试 - 电量19', data: { battery: 19, temperature: 25, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: 'battery' // 19算低电量 }, { name: '边界值测试 - 温度20', data: { battery: 85, temperature: 20, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: null // 20不算温度过低 }, { name: '边界值测试 - 温度19', data: { battery: 85, temperature: 19, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: 'temperature_low' // 19算温度过低 }, { name: '边界值测试 - 温度40', data: { battery: 85, temperature: 40, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: null // 40不算温度过高 }, { name: '边界值测试 - 温度41', data: { battery: 85, temperature: 41, is_connect: 1, bandge_status: 1, steps: 1000, y_steps: 500 }, expected: 'temperature_high' // 41算温度过高 } ]; // 运行测试 function runTests() { console.log('🧪 开始测试预警检测逻辑...\n'); let passed = 0; let failed = 0; testCases.forEach((testCase, index) => { const result = determineAlertType(testCase.data); const expected = testCase.expected; const success = result === expected; console.log(`测试 ${index + 1}: ${testCase.name}`); console.log(` 输入数据:`, testCase.data); console.log(` 预期结果: ${expected ? getAlertTypeText(expected) : '正常'}`); console.log(` 实际结果: ${result ? getAlertTypeText(result) : '正常'}`); console.log(` 测试结果: ${success ? '✅ 通过' : '❌ 失败'}`); console.log(''); if (success) { passed++; } else { failed++; } }); console.log('📊 测试总结:'); console.log(` 总测试数: ${testCases.length}`); console.log(` 通过: ${passed}`); console.log(` 失败: ${failed}`); console.log(` 成功率: ${((passed / testCases.length) * 100).toFixed(1)}%`); if (failed === 0) { console.log('\n🎉 所有测试通过!预警检测逻辑工作正常。'); } else { console.log('\n⚠️ 有测试失败,请检查预警检测逻辑。'); } } // 运行测试 runTests();