Files
nxxmdata/backend/test-smart-eartag-alert-api.js
2025-09-22 19:09:45 +08:00

327 lines
12 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

/**
* 智能耳标预警API测试脚本
* @file test-smart-eartag-alert-api.js
* @description 测试智能耳标预警相关的API接口功能
*/
const axios = require('axios');
// 配置基础URL
const BASE_URL = 'http://localhost:5350/api/smart-alerts/public';
// 创建axios实例
const api = axios.create({
baseURL: BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
});
// 测试结果统计
let testResults = {
total: 0,
passed: 0,
failed: 0,
errors: []
};
// 测试辅助函数
function logTest(testName, success, message = '') {
testResults.total++;
if (success) {
testResults.passed++;
console.log(`${testName}: ${message}`);
} else {
testResults.failed++;
testResults.errors.push(`${testName}: ${message}`);
console.log(`${testName}: ${message}`);
}
}
// 测试函数
async function testGetEartagAlertStats() {
try {
console.log('\n=== 测试获取智能耳标预警统计 ===');
const response = await api.get('/eartag/stats');
if (response.status === 200 && response.data.success) {
const data = response.data.data;
logTest('获取预警统计', true, `成功获取统计,设备总数: ${data.totalDevices}, 预警总数: ${data.totalAlerts}`);
// 验证数据结构
const requiredFields = ['totalDevices', 'lowBattery', 'offline', 'highTemperature', 'abnormalMovement', 'totalAlerts'];
const hasAllFields = requiredFields.every(field => data.hasOwnProperty(field));
logTest('统计数据结构验证', hasAllFields, hasAllFields ? '数据结构正确' : '缺少必要字段');
} else {
logTest('获取预警统计', false, `请求失败: ${response.data.message || '未知错误'}`);
}
} catch (error) {
logTest('获取预警统计', false, `请求异常: ${error.message}`);
}
}
async function testGetEartagAlerts() {
try {
console.log('\n=== 测试获取智能耳标预警列表 ===');
// 测试基础列表获取
const response = await api.get('/eartag?page=1&limit=5');
if (response.status === 200 && response.data.success) {
const data = response.data;
logTest('获取预警列表', true, `成功获取列表,共 ${data.total} 条预警`);
// 验证分页信息
const hasPagination = data.pagination && typeof data.pagination.page === 'number';
logTest('分页信息验证', hasPagination, hasPagination ? '分页信息正确' : '分页信息缺失');
// 验证统计数据
const hasStats = data.stats && typeof data.stats.lowBattery === 'number';
logTest('统计信息验证', hasStats, hasStats ? '统计信息正确' : '统计信息缺失');
} else {
logTest('获取预警列表', false, `请求失败: ${response.data.message || '未知错误'}`);
}
} catch (error) {
logTest('获取预警列表', false, `请求异常: ${error.message}`);
}
}
async function testGetEartagAlertsWithFilters() {
try {
console.log('\n=== 测试预警列表筛选功能 ===');
// 测试按预警类型筛选
const batteryResponse = await api.get('/eartag?alertType=battery&limit=3');
if (batteryResponse.status === 200 && batteryResponse.data.success) {
const batteryAlerts = batteryResponse.data.data;
const allBattery = batteryAlerts.every(alert => alert.alertType === 'battery');
logTest('按预警类型筛选', allBattery, `筛选结果: ${batteryAlerts.length} 条低电量预警`);
} else {
logTest('按预警类型筛选', false, `筛选失败: ${batteryResponse.data.message || '未知错误'}`);
}
// 测试搜索功能
const searchResponse = await api.get('/eartag?search=EARTAG&limit=3');
if (searchResponse.status === 200 && searchResponse.data.success) {
const searchAlerts = searchResponse.data.data;
logTest('搜索功能', true, `搜索到 ${searchAlerts.length} 条相关预警`);
} else {
logTest('搜索功能', false, `搜索失败: ${searchResponse.data.message || '未知错误'}`);
}
} catch (error) {
logTest('筛选功能测试', false, `请求异常: ${error.message}`);
}
}
async function testGetEartagAlertById() {
try {
console.log('\n=== 测试获取单个预警详情 ===');
// 首先获取一个预警ID
const listResponse = await api.get('/eartag?limit=1');
if (listResponse.status === 200 && listResponse.data.success && listResponse.data.data.length > 0) {
const alertId = listResponse.data.data[0].id;
// 测试获取详情
const detailResponse = await api.get(`/eartag/${alertId}`);
if (detailResponse.status === 200 && detailResponse.data.success) {
const alert = detailResponse.data.data;
logTest('获取预警详情', true, `成功获取预警 ${alertId} 的详情`);
// 验证详情数据结构
const hasRequiredFields = alert.id && alert.alertType && alert.alertLevel;
logTest('详情数据结构验证', hasRequiredFields, hasRequiredFields ? '详情数据结构正确' : '缺少必要字段');
} else {
logTest('获取预警详情', false, `获取详情失败: ${detailResponse.data.message || '未知错误'}`);
}
} else {
logTest('获取预警详情', false, '没有可用的预警数据用于测试');
}
} catch (error) {
logTest('获取预警详情', false, `请求异常: ${error.message}`);
}
}
async function testHandleEartagAlert() {
try {
console.log('\n=== 测试处理预警功能 ===');
// 首先获取一个预警ID
const listResponse = await api.get('/eartag?limit=1');
if (listResponse.status === 200 && listResponse.data.success && listResponse.data.data.length > 0) {
const alertId = listResponse.data.data[0].id;
// 测试处理预警
const handleData = {
action: 'acknowledged',
notes: 'API测试处理',
handler: 'test-user'
};
const handleResponse = await api.post(`/eartag/${alertId}/handle`, handleData);
if (handleResponse.status === 200 && handleResponse.data.success) {
const result = handleResponse.data.data;
logTest('处理预警', true, `成功处理预警 ${alertId}`);
// 验证处理结果
const hasProcessedInfo = result.alertId && result.action && result.processedAt;
logTest('处理结果验证', hasProcessedInfo, hasProcessedInfo ? '处理结果正确' : '处理结果不完整');
} else {
logTest('处理预警', false, `处理失败: ${handleResponse.data.message || '未知错误'}`);
}
} else {
logTest('处理预警', false, '没有可用的预警数据用于测试');
}
} catch (error) {
logTest('处理预警', false, `请求异常: ${error.message}`);
}
}
async function testBatchHandleEartagAlerts() {
try {
console.log('\n=== 测试批量处理预警功能 ===');
// 首先获取一些预警ID
const listResponse = await api.get('/eartag?limit=3');
if (listResponse.status === 200 && listResponse.data.success && listResponse.data.data.length > 0) {
const alertIds = listResponse.data.data.map(alert => alert.id);
// 测试批量处理
const batchData = {
alertIds: alertIds,
action: 'acknowledged',
notes: 'API批量测试处理',
handler: 'test-user'
};
const batchResponse = await api.post('/eartag/batch-handle', batchData);
if (batchResponse.status === 200 && batchResponse.data.success) {
const result = batchResponse.data.data;
logTest('批量处理预警', true, `成功批量处理 ${result.processedCount} 个预警`);
// 验证批量处理结果
const hasBatchResult = result.processedCount > 0 && Array.isArray(result.results);
logTest('批量处理结果验证', hasBatchResult, hasBatchResult ? '批量处理结果正确' : '批量处理结果不完整');
} else {
logTest('批量处理预警', false, `批量处理失败: ${batchResponse.data.message || '未知错误'}`);
}
} else {
logTest('批量处理预警', false, '没有足够的预警数据用于测试');
}
} catch (error) {
logTest('批量处理预警', false, `请求异常: ${error.message}`);
}
}
async function testExportEartagAlerts() {
try {
console.log('\n=== 测试导出预警数据功能 ===');
// 测试JSON格式导出
const exportResponse = await api.get('/eartag/export?format=json&limit=5');
if (exportResponse.status === 200 && exportResponse.data.success) {
const exportData = exportResponse.data.data;
logTest('导出预警数据', true, `成功导出 ${exportData.length} 条预警数据`);
// 验证导出数据
const hasExportData = Array.isArray(exportData) && exportData.length > 0;
logTest('导出数据验证', hasExportData, hasExportData ? '导出数据正确' : '导出数据为空');
} else {
logTest('导出预警数据', false, `导出失败: ${exportResponse.data.message || '未知错误'}`);
}
} catch (error) {
logTest('导出预警数据', false, `请求异常: ${error.message}`);
}
}
async function testErrorHandling() {
try {
console.log('\n=== 测试错误处理 ===');
// 测试无效的预警ID
const invalidIdResponse = await api.get('/eartag/invalid_id');
if (invalidIdResponse.status === 400 || invalidIdResponse.status === 404) {
logTest('无效ID错误处理', true, '正确处理无效ID错误');
} else {
logTest('无效ID错误处理', false, '未正确处理无效ID错误');
}
// 测试无效的筛选参数
const invalidFilterResponse = await api.get('/eartag?alertType=invalid_type');
if (invalidFilterResponse.status === 200) {
logTest('无效筛选参数处理', true, '正确处理无效筛选参数');
} else {
logTest('无效筛选参数处理', false, '未正确处理无效筛选参数');
}
} catch (error) {
logTest('错误处理测试', false, `请求异常: ${error.message}`);
}
}
// 主测试函数
async function runAllTests() {
console.log('🚀 开始智能耳标预警API测试...\n');
try {
await testGetEartagAlertStats();
await testGetEartagAlerts();
await testGetEartagAlertsWithFilters();
await testGetEartagAlertById();
await testHandleEartagAlert();
await testBatchHandleEartagAlerts();
await testExportEartagAlerts();
await testErrorHandling();
// 输出测试结果
console.log('\n' + '='.repeat(50));
console.log('📊 测试结果汇总:');
console.log(`总测试数: ${testResults.total}`);
console.log(`通过: ${testResults.passed}`);
console.log(`失败: ${testResults.failed}`);
console.log(`成功率: ${((testResults.passed / testResults.total) * 100).toFixed(2)}%`);
if (testResults.errors.length > 0) {
console.log('\n❌ 失败详情:');
testResults.errors.forEach((error, index) => {
console.log(`${index + 1}. ${error}`);
});
}
if (testResults.failed === 0) {
console.log('\n🎉 所有测试通过智能耳标预警API功能正常。');
} else {
console.log('\n⚠ 部分测试失败,请检查相关功能。');
}
} catch (error) {
console.error('❌ 测试执行异常:', error.message);
}
}
// 如果直接运行此脚本
if (require.main === module) {
runAllTests().catch(console.error);
}
module.exports = {
runAllTests,
testGetEartagAlertStats,
testGetEartagAlerts,
testGetEartagAlertById,
testHandleEartagAlert,
testBatchHandleEartagAlerts,
testExportEartagAlerts
};