// 测试账号池相关模型 require('module-alias/register'); const Db = require("@src/config/Db"); const MAccountPool = require("@src/modes/MAccountPool"); const MAccountHealth = require("@src/modes/MAccountHealth"); const MAccountUsageLog = require("@src/modes/MAccountUsageLog"); const MTgAccount = require("@src/modes/MTgAccount"); const initAssociations = require("@src/modes/initAssociations"); async function testModels() { try { console.log("开始测试账号池模型..."); // 初始化数据库 await Db.getInstance(); // 初始化关联关系 initAssociations(); // 同步模型(创建表)- 使用 {alter: true} 来处理已存在的表 await MAccountPool.sync({ alter: false }); await MAccountHealth.sync({ alter: false }); await MAccountUsageLog.sync({ alter: false }); console.log("✅ 数据表创建成功"); // 清理已有的测试数据 await MAccountUsageLog.destroy({ where: {} }); await MAccountHealth.destroy({ where: {} }); await MAccountPool.destroy({ where: {} }); console.log("✅ 已清理旧数据"); // 获取第一个TG账号用于测试 const tgAccount = await MTgAccount.findOne(); if (!tgAccount) { console.log("❌ 没有找到TG账号,请先创建TG账号"); return; } // 1. 创建账号池记录 const accountPool = await MAccountPool.create({ accountId: tgAccount.id, phone: tgAccount.phone, status: 'active', tier: 'new', dailyLimit: 30, hourlyLimit: 5, intervalSeconds: 120, tags: ['test', 'new_account'], metadata: { source: 'test_script', createdBy: 'system' } }); console.log("✅ 账号池记录创建成功:", { id: accountPool.id, phone: accountPool.phone, status: accountPool.status, tier: accountPool.tier }); // 2. 创建健康度记录 const healthRecord = await MAccountHealth.create({ accountPoolId: accountPool.id, healthScore: 95, successRate: 98.5, errorCount: 2, warningCount: 1, activeHours: [9, 10, 11, 14, 15, 16, 17, 18], evaluationDetails: { lastCheck: new Date(), metrics: { responseTime: 250, successRate: 98.5 } }, recommendations: [ "建议减少发送频率", "避免在凌晨发送消息" ] }); console.log("✅ 健康度记录创建成功:", { id: healthRecord.id, healthScore: healthRecord.healthScore, trend: healthRecord.trend }); // 3. 创建使用记录 const usageLog = await MAccountUsageLog.create({ accountPoolId: accountPool.id, taskId: 1, // 假设的任务ID taskType: 'group_send', groupId: 1, messageContent: '测试消息内容', status: 'success', startTime: new Date(), endTime: new Date(Date.now() + 5000), duration: 5000, riskLevel: 'low', behaviorSimulation: { typingSpeed: 80, pauseTime: 1000, readTime: 2000 }, recipientCount: 50, readCount: 45, replyCount: 5 }); console.log("✅ 使用记录创建成功:", { id: usageLog.id, status: usageLog.status, duration: usageLog.duration }); // 4. 测试关联查询 const poolWithRelations = await MAccountPool.findOne({ where: { id: accountPool.id }, include: [ { model: MTgAccount, as: 'tgAccount' }, { model: MAccountHealth, as: 'healthRecords', limit: 5, order: [['createdAt', 'DESC']] }, { model: MAccountUsageLog, as: 'usageLogs', limit: 10, order: [['createdAt', 'DESC']] } ] }); console.log("✅ 关联查询成功:", { accountPhone: poolWithRelations.tgAccount?.phone, healthRecordCount: poolWithRelations.healthRecords?.length || 0, usageLogCount: poolWithRelations.usageLogs?.length || 0 }); console.log("\n✅ 所有测试通过!账号池模型工作正常。"); } catch (error) { console.error("❌ 测试失败:", error); } finally { process.exit(); } } // 运行测试 testModels();