Files
akmon/push-receiver-service/test.js
2026-01-20 08:04:15 +08:00

155 lines
5.3 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.
/**
* 推送消息接收服务测试脚本
*/
const SupabaseDatabaseManager = require('./lib/supabase-database');
require('dotenv').config();
async function testService() {
console.log('🧪 开始测试推送消息接收服务...');
const db = new SupabaseDatabaseManager();
try {
// 测试 1: 数据库连接
console.log('\n📡 测试 1: 数据库连接');
await db.testConnection();
console.log('✅ 数据库连接测试通过');
// 测试 2: 插入健康数据消息
console.log('\n💊 测试 2: 健康数据消息');
const healthMessage = {
pushType: 'HEALTH',
userId: 'test_user_001',
deviceId: 'test_device_001',
H: 75,
O: 98,
T: 36.5,
source_ip: '127.0.0.1',
user_agent: 'Test Client'
};
const healthResult = await db.insertPushMessage(healthMessage);
console.log('✅ 健康数据消息插入成功:', healthResult.messageId);
// 测试 3: 插入 SOS 消息
console.log('\n🆘 测试 3: SOS 紧急消息');
const sosMessage = {
pushType: 'SOS',
userId: 'test_user_002',
deviceId: 'test_device_002',
emergencyLevel: 'HIGH',
lat: 39.9042,
lng: 116.4074,
source_ip: '127.0.0.1',
user_agent: 'Emergency Device'
};
const sosResult = await db.insertPushMessage(sosMessage);
console.log('✅ SOS 消息插入成功:', sosResult.messageId);
// 测试 4: 批量插入消息
console.log('\n📦 测试 4: 批量消息插入');
const batchMessages = [
{
pushType: 'LOCATION',
userId: 'test_user_003',
deviceId: 'test_device_003',
lat: 31.2304,
lng: 121.4737,
accuracy: 10,
source_ip: '127.0.0.1',
user_agent: 'GPS Device'
},
{
pushType: 'DEVICE_STATUS',
userId: 'test_user_004',
deviceId: 'test_device_004',
status: 'online',
batteryLevel: 85,
source_ip: '127.0.0.1',
user_agent: 'IoT Device'
},
{
pushType: 'ACTIVITY',
userId: 'test_user_005',
deviceId: 'test_device_005',
activityType: 'running',
duration: 1800,
calories: 250,
source_ip: '127.0.0.1',
user_agent: 'Fitness Tracker'
}
];
const batchResult = await db.insertPushMessagesBatch(batchMessages);
console.log('✅ 批量消息插入成功:', `${batchResult.successCount}/${batchResult.totalCount}`);
// 测试 5: 重复消息检测
console.log('\n🔄 测试 5: 重复消息检测');
const duplicateResult = await db.insertPushMessage(healthMessage);
console.log('✅ 重复消息检测:', duplicateResult.isDuplicate ? '检测到重复' : '未检测到重复');
// 测试 6: 获取统计信息
console.log('\n📊 测试 6: 统计信息获取');
const stats = await db.getMessageStats(24);
console.log('✅ 统计信息获取成功:', stats.length, '种消息类型');
stats.forEach(stat => {
console.log(` - ${stat.push_type}: ${stat.total_count} 条消息`);
});
// 测试 7: 获取健康状态
console.log('\n🏥 测试 7: 系统健康状态');
const health = await db.getHealthStatus();
console.log('✅ 系统健康状态:', health.database.connected ? '正常' : '异常');
console.log(` - 总消息数: ${health.database.stats.total_messages}`);
console.log(` - 最近1小时: ${health.database.stats.messages_last_hour}`);
// 测试 8: 获取消息列表
console.log('\n📋 测试 8: 消息列表获取');
const messages = await db.getPushMessages({ limit: 5 });
console.log('✅ 消息列表获取成功:', messages.length, '条消息');
// 测试 9: HTTP API 测试
console.log('\n🌐 测试 9: HTTP API 测试');
await testHttpAPI();
console.log('\n🎉 所有测试完成!');
} catch (error) {
console.error('❌ 测试失败:', error.message);
throw error;
} finally {
await db.close();
}
}
async function testHttpAPI() {
try {
// 测试健康检查端点
const healthResponse = await fetch('http://localhost:3001/api/health');
if (healthResponse.ok) {
console.log('✅ 健康检查 API 可用');
} else {
console.log('❌ 健康检查 API 不可用');
}
} catch (error) {
console.log(' HTTP API 测试跳过 (服务未运行)');
}
}
// 运行测试
if (require.main === module) {
testService()
.then(() => {
console.log('\n✅ 测试完成,服务运行正常!');
process.exit(0);
})
.catch((error) => {
console.error('\n❌ 测试失败:', error.message);
process.exit(1);
});
}
module.exports = { testService };