test_auto_speedtest.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { Subscription, Node } = require('./src/models');
  2. const MultiSubscriptionManager = require('./src/core/multiSubscriptionManager');
  3. async function testAutoSpeedTest() {
  4. try {
  5. console.log('=== 自动测速功能测试 ===');
  6. // 创建测试订阅
  7. const testSubscription = await Subscription.create({
  8. name: '自动测速测试订阅',
  9. url: 'http://so.xfxssr.me/api/v1/client/subscribe?token=7854d59f38ac51700730b9e782c5160c',
  10. description: '用于测试自动测速功能的订阅',
  11. speedTestConfig: {
  12. testCount: 3,
  13. timeout: 10000,
  14. testUrls: ['https://www.google.com'],
  15. speedTestEnabled: true
  16. },
  17. notifyConfig: {
  18. enabled: true,
  19. notifyOnSpeedTest: true,
  20. notifyOnNodeUpdate: true,
  21. webhookUrl: '',
  22. emailConfig: null
  23. }
  24. });
  25. console.log('✅ 创建测试订阅成功');
  26. // 创建多订阅管理器
  27. const manager = new MultiSubscriptionManager();
  28. // 设置测速触发器(模拟)
  29. let speedTestTriggered = false;
  30. manager.setSpeedTestTrigger(() => {
  31. console.log('🚀 测速触发器被调用!');
  32. speedTestTriggered = true;
  33. });
  34. // 模拟一些测试节点
  35. console.log('\n2. 创建测试节点...');
  36. const testNodes = [
  37. { name: '自动测速节点1', type: 'ss', server: 'auto1.example.com', port: 443, method: 'aes-256-gcm', password: 'password1', subscriptionId: testSubscription.id },
  38. { name: '自动测速节点2', type: 'ss', server: 'auto2.example.com', port: 443, method: 'aes-256-gcm', password: 'password2', subscriptionId: testSubscription.id },
  39. { name: '自动测速节点3', type: 'ss', server: 'auto3.example.com', port: 443, method: 'aes-256-gcm', password: 'password3', subscriptionId: testSubscription.id }
  40. ];
  41. for (const nodeData of testNodes) {
  42. await Node.create({
  43. ...nodeData,
  44. isActive: true,
  45. status: 'offline'
  46. });
  47. }
  48. console.log(`✅ 创建了 ${testNodes.length} 个测试节点`);
  49. // 更新订阅的节点数量
  50. await testSubscription.update({
  51. nodeCount: testNodes.length,
  52. lastUpdateTime: new Date()
  53. });
  54. // 直接测试触发器功能
  55. console.log('\n3. 直接测试测速触发器...');
  56. manager.triggerSpeedTest();
  57. // 等待触发器执行
  58. await new Promise(resolve => setTimeout(resolve, 1000));
  59. // 检查触发器是否被调用
  60. console.log('\n4. 检查触发器状态...');
  61. if (speedTestTriggered) {
  62. console.log('✅ 自动测速功能正常!');
  63. } else {
  64. console.log('❌ 自动测速功能未触发');
  65. }
  66. // 清理测试数据
  67. console.log('\n5. 清理测试数据...');
  68. await testSubscription.destroy();
  69. console.log('✅ 测试数据清理完成');
  70. console.log('\n=== 自动测速功能测试完成 ===');
  71. } catch (error) {
  72. console.error('❌ 测试失败:', error.message);
  73. console.error(error.stack);
  74. }
  75. }
  76. // 运行测试
  77. testAutoSpeedTest();