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

65 lines
1.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.

/**
* 直接测试API
* @file test-direct-api.js
* @description 直接测试智能项圈预警API不通过HTTP请求
*/
const { getCollarAlerts } = require('./controllers/smartCollarAlertController');
async function testDirectApi() {
console.log('🔍 直接测试智能项圈预警API...\n');
try {
// 模拟请求对象
const mockReq = {
query: {
page: 1,
limit: 5,
search: '22012000107'
}
};
// 模拟响应对象
const mockRes = {
json: (data) => {
console.log('API响应数据:');
console.log(JSON.stringify(data, null, 2));
if (data.success && data.data) {
const targetCollar = data.data.find(item => item.collarNumber == 22012000107);
if (targetCollar) {
console.log('\n找到项圈22012000107的数据:');
console.log('电量:', targetCollar.battery);
console.log('温度:', targetCollar.temperature);
console.log('预警类型:', targetCollar.alertType);
console.log('预警级别:', targetCollar.alertLevel);
} else {
console.log('\n未找到项圈22012000107的数据');
console.log('可用的项圈编号:');
data.data.forEach(item => {
console.log(`- ${item.collarNumber}`);
});
}
}
},
status: (code) => ({
json: (data) => {
console.log('错误响应:', code, data);
}
})
};
// 调用API函数
await getCollarAlerts(mockReq, mockRes);
} catch (error) {
console.error('❌ 测试失败:', error.message);
console.error('错误详情:', error);
} finally {
process.exit(0);
}
}
// 运行测试
testDirectApi().catch(console.error);