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

217 lines
7.0 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.

/**
* 智能项圈预警API集成测试
* @file test-smart-collar-alert-integration.js
* @description 测试智能项圈预警API的完整集成
*/
const axios = require('axios');
const BASE_URL = 'http://localhost:5350/api/smart-alerts/public';
// 测试数据
const testData = {
collarNumber: 'TEST_COLLAR_001',
alertType: 'battery',
alertLevel: 'high',
battery: 15,
temperature: 25.5,
dailySteps: 1200,
longitude: 106.504962,
latitude: 26.547901
};
async function testCollarAlertAPI() {
console.log('🧪 开始测试智能项圈预警API集成...\n');
try {
// 1. 测试获取统计数据
console.log('1. 测试获取统计数据...');
const statsResponse = await axios.get(`${BASE_URL}/collar/stats`);
console.log('✅ 统计数据API正常');
console.log(' 响应:', statsResponse.data);
// 2. 测试获取预警列表
console.log('\n2. 测试获取预警列表...');
const listResponse = await axios.get(`${BASE_URL}/collar`, {
params: {
page: 1,
limit: 10
}
});
console.log('✅ 预警列表API正常');
console.log(' 总数:', listResponse.data.total || 0);
console.log(' 数据条数:', listResponse.data.data ? listResponse.data.data.length : 0);
// 3. 测试搜索功能
console.log('\n3. 测试搜索功能...');
const searchResponse = await axios.get(`${BASE_URL}/collar`, {
params: {
search: 'TEST',
page: 1,
limit: 10
}
});
console.log('✅ 搜索功能正常');
console.log(' 搜索结果数:', searchResponse.data.data ? searchResponse.data.data.length : 0);
// 4. 测试预警类型筛选
console.log('\n4. 测试预警类型筛选...');
const filterResponse = await axios.get(`${BASE_URL}/collar`, {
params: {
alertType: 'battery',
page: 1,
limit: 10
}
});
console.log('✅ 预警类型筛选正常');
console.log(' 筛选结果数:', filterResponse.data.data ? filterResponse.data.data.length : 0);
// 5. 测试获取单个预警详情
if (listResponse.data.data && listResponse.data.data.length > 0) {
const firstAlert = listResponse.data.data[0];
console.log('\n5. 测试获取单个预警详情...');
const detailResponse = await axios.get(`${BASE_URL}/collar/${firstAlert.id}`);
console.log('✅ 预警详情API正常');
console.log(' 预警ID:', firstAlert.id);
}
// 6. 测试处理预警
if (listResponse.data.data && listResponse.data.data.length > 0) {
const firstAlert = listResponse.data.data[0];
console.log('\n6. 测试处理预警...');
const handleResponse = await axios.post(`${BASE_URL}/collar/${firstAlert.id}/handle`, {
action: 'acknowledged',
notes: 'API测试处理',
handler: 'test_user'
});
console.log('✅ 处理预警API正常');
console.log(' 处理结果:', handleResponse.data);
}
// 7. 测试批量处理预警
if (listResponse.data.data && listResponse.data.data.length > 0) {
const alertIds = listResponse.data.data.slice(0, 2).map(alert => alert.id);
console.log('\n7. 测试批量处理预警...');
const batchHandleResponse = await axios.post(`${BASE_URL}/collar/batch-handle`, {
alertIds: alertIds,
action: 'acknowledged',
notes: 'API批量测试处理',
handler: 'test_user'
});
console.log('✅ 批量处理预警API正常');
console.log(' 批量处理结果:', batchHandleResponse.data);
}
// 8. 测试导出数据
console.log('\n8. 测试导出数据...');
const exportResponse = await axios.get(`${BASE_URL}/collar/export`, {
params: {
format: 'json'
}
});
console.log('✅ 导出数据API正常');
console.log(' 导出数据条数:', exportResponse.data.data ? exportResponse.data.data.length : 0);
console.log('\n🎉 所有API测试通过');
console.log('\n📋 API端点总结:');
console.log(' - GET /collar/stats - 获取统计数据');
console.log(' - GET /collar - 获取预警列表');
console.log(' - GET /collar/{id} - 获取预警详情');
console.log(' - POST /collar/{id}/handle - 处理预警');
console.log(' - POST /collar/batch-handle - 批量处理预警');
console.log(' - GET /collar/export - 导出数据');
} catch (error) {
console.error('❌ API测试失败:', error.message);
if (error.response) {
console.error(' 状态码:', error.response.status);
console.error(' 响应数据:', error.response.data);
}
if (error.code === 'ECONNREFUSED') {
console.log('\n💡 建议: 请确保后端服务器已启动');
console.log(' 启动命令: cd backend && npm start');
}
}
}
// 测试前端数据服务集成
async function testFrontendIntegration() {
console.log('\n🔍 测试前端数据服务集成...');
try {
// 模拟前端API调用
const frontendTests = [
{
name: '获取统计数据',
url: `${BASE_URL}/collar/stats`,
method: 'GET'
},
{
name: '获取预警列表',
url: `${BASE_URL}/collar`,
method: 'GET',
params: { page: 1, limit: 10 }
},
{
name: '搜索预警',
url: `${BASE_URL}/collar`,
method: 'GET',
params: { search: 'TEST', page: 1, limit: 10 }
},
{
name: '筛选预警',
url: `${BASE_URL}/collar`,
method: 'GET',
params: { alertType: 'battery', page: 1, limit: 10 }
}
];
for (const test of frontendTests) {
try {
const response = await axios({
method: test.method,
url: test.url,
params: test.params
});
console.log(`${test.name}: 成功`);
if (test.name === '获取统计数据') {
console.log(` 数据:`, response.data);
} else {
console.log(` 数据条数:`, response.data.data ? response.data.data.length : 0);
}
} catch (error) {
console.log(`${test.name}: 失败 - ${error.message}`);
}
}
} catch (error) {
console.error('❌ 前端集成测试失败:', error.message);
}
}
// 主函数
async function main() {
console.log('🚀 智能项圈预警API集成测试开始\n');
await testCollarAlertAPI();
await testFrontendIntegration();
console.log('\n✅ 测试完成!');
console.log('\n📖 前端页面应该能够:');
console.log(' 1. 动态显示统计数据(低电量、离线、温度、异常运动、佩戴异常)');
console.log(' 2. 显示预警列表数据');
console.log(' 3. 支持搜索和筛选功能');
console.log(' 4. 支持处理预警操作');
console.log(' 5. 支持导出数据功能');
}
// 如果直接运行此脚本
if (require.main === module) {
main().catch(console.error);
}
module.exports = { testCollarAlertAPI, testFrontendIntegration };