#!/usr/bin/env npx tsx import { OrderCleanupService } from './src/modules/cleanup/OrderCleanupService.js' import { AccountManager } from './src/modules/account/AccountManager.js' import { CacheManager } from './src/modules/cache/CacheManager.js' import { PacificaProxyClient } from './src/exchanges/pacifica/PacificaProxyClient.js' import { logger } from './src/utils/logger.js' async function testManualCleanup() { logger.info('🧹 测试手动订单清理功能') try { // 1. 创建基础服务 const cacheManager = new CacheManager() const accountManager = new AccountManager(cacheManager) // 2. 创建 Pacifica 客户端 const client1 = new PacificaProxyClient({ privateKey: process.env.PACIFICA_PRIVATE_KEY_1 || '', account: process.env.PACIFICA_ACCOUNT_1 || '3v2fE8y6uPVu5pmNCpmygpGNgdP3kGL3SMoVa86uvLLu', }) const client2 = new PacificaProxyClient({ privateKey: process.env.PACIFICA_PRIVATE_KEY_2 || '', account: process.env.PACIFICA_ACCOUNT_2 || 'GkFi4YUFVTYKVqzsT98QiiUVbwuTiXWe8XsbRZRYafv3', }) // 禁用WebSocket,使用HTTP client1.setWebSocketEnabled(false) client2.setWebSocketEnabled(false) // 3. 配置账户管理器 const accounts = [ { account: process.env.PACIFICA_ACCOUNT_1, privateKey: process.env.PACIFICA_PRIVATE_KEY_1 }, { account: process.env.PACIFICA_ACCOUNT_2, privateKey: process.env.PACIFICA_PRIVATE_KEY_2 } ] accountManager.setAccounts(accounts) accountManager.addClient('pacifica-1', client1) accountManager.addClient('pacifica-2', client2) // 4. 创建订单清理服务(5秒间隔,60秒超时,最多3个订单用于测试) const cleanupInterval = 5000 // 5秒测试间隔 const maxOrderAge = 60000 // 60秒超时 const maxOrderCount = 3 // 最多3个订单(用于快速测试) const orderCleanupService = new OrderCleanupService( accountManager, cleanupInterval, maxOrderAge, maxOrderCount ) // 5. 初始化和启动服务 await cacheManager.initialize() await accountManager.initialize() await orderCleanupService.initialize() await cacheManager.start() await accountManager.start() await orderCleanupService.start() logger.info('🚀 服务已启动,开始手动清理测试...') // 6. 手动触发清理 logger.info('🔧 手动触发第一次清理...') try { await orderCleanupService.manualCleanup() logger.info('✅ 第一次清理完成') } catch (cleanupError: any) { logger.error('❌ 第一次清理失败:', { error: cleanupError.message, stack: cleanupError.stack, name: cleanupError.name, details: cleanupError }) throw cleanupError } logger.info('⏱️ 等待5秒后再次清理...') await new Promise(resolve => setTimeout(resolve, 5000)) logger.info('🔧 手动触发第二次清理...') await orderCleanupService.manualCleanup() // 7. 获取清理统计 const stats = orderCleanupService.getCleanupStats() logger.info('📊 清理统计:', stats) // 8. 停止服务 await orderCleanupService.stop() await accountManager.stop() await cacheManager.stop() logger.info('✅ 手动清理测试完成') } catch (error: any) { logger.error('❌ 测试失败:', { error: error.message, stack: error.stack, }) } } // 立即执行 testManualCleanup().then(() => { logger.info('🎯 测试完成') process.exit(0) }).catch(error => { logger.error('🚨 测试脚本执行失败:', error) process.exit(1) })