#!/usr/bin/env tsx /** * 健康检查系统测试 * 测试生产环境模块的健康检查功能 */ import { HealthChecker, HealthAPI } from '../src/infrastructure/health/index.js' import { ProductionLogger } from '../src/utils/ProductionLogger.js' class MockAdapter { name = 'mock-exchange' async ping() { return { status: 'ok', timestamp: Date.now() } } async time() { return Date.now() } async balances() { return { USDT: 1000, BTC: 0.5 } } } class MockAccountManager { getRegisteredAccounts() { return [ { exchange: 'mock-exchange', accountId: 'account1' }, { exchange: 'mock-exchange', accountId: 'account2' }, ] } async getBalances(exchange: string, accountId: string) { return { USDT: 500, BTC: 0.25 } } } async function testHealthCheck() { console.log('🏥 测试健康检查系统') console.log('='.repeat(50)) // 初始化日志器 const logger = new ProductionLogger({ level: 'info', enableConsole: true, enableFile: false, }) // 初始化健康检查器 const healthChecker = new HealthChecker() // 注册模拟适配器和账户管理器 const mockAdapter = new MockAdapter() const mockAccountManager = new MockAccountManager() healthChecker.registerExchange('mock-exchange', mockAdapter as any) healthChecker.registerAccountManager('mock-manager', mockAccountManager) // 监听健康检查事件 healthChecker.on('health_check', health => { logger.info('健康检查完成', { status: health.status, exchangeCount: Object.keys(health.exchanges).length, accountCount: Object.keys(health.accounts).length, }) }) // 执行健康检查 console.log('📊 执行健康检查...') const health = await healthChecker.performHealthCheck() console.log('\n✅ 健康检查结果:') console.log(`整体状态: ${health.status}`) console.log(`运行时间: ${Math.round(health.uptime / 1000)}秒`) console.log( `交易所状态:`, Object.entries(health.exchanges) .map(([name, status]) => `${name}: ${status.status} (连接: ${status.connected ? '✅' : '❌'})`) .join(', '), ) console.log( `账户状态:`, Object.entries(health.accounts) .map(([key, status]) => `${key}: ${status.status} (余额可访问: ${status.balanceAccessible ? '✅' : '❌'})`) .join(', '), ) console.log(`内存使用: ${Math.round((health.memory.used / health.memory.total) * 100)}%`) // 获取健康检查摘要 const summary = healthChecker.getHealthSummary() console.log('\n📋 系统摘要:') console.log(JSON.stringify(summary, null, 2)) return health } async function testHealthAPI() { console.log('\n🌐 测试健康检查 API') console.log('='.repeat(50)) const healthChecker = new HealthChecker() const mockAdapter = new MockAdapter() healthChecker.registerExchange('mock-exchange', mockAdapter as any) const healthAPI = new HealthAPI(healthChecker, { port: 3002, // 使用不同端口避免冲突 host: 'localhost', }) try { // 启动健康检查 API await healthAPI.start() console.log('✅ 健康检查 API 已启动') // 等待一段时间让系统稳定 await new Promise(resolve => setTimeout(resolve, 1000)) // 测试健康检查端点 const endpoints = [ 'http://localhost:3002/health', 'http://localhost:3002/health/summary', 'http://localhost:3002/health/live', 'http://localhost:3002/health/ready', ] for (const endpoint of endpoints) { try { const response = await fetch(endpoint) const data = await response.json() console.log(`\n📡 ${endpoint}:`) console.log(`状态码: ${response.status}`) console.log('响应:', JSON.stringify(data, null, 2)) } catch (error) { console.error(`❌ ${endpoint} 请求失败:`, error) } } } finally { // 关闭 API await healthAPI.stop() console.log('✅ 健康检查 API 已关闭') } } async function main() { try { console.log('🚀 开始生产模块健康检查测试\n') // 测试健康检查核心功能 await testHealthCheck() // 测试健康检查 API await testHealthAPI() console.log('\n🎉 所有健康检查测试完成!') } catch (error) { console.error('❌ 测试失败:', error) process.exit(1) } } // 运行测试 if (import.meta.url === `file://${process.argv[1]}`) { main() }