test_task_full.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/usr/bin/env node
  2. /**
  3. * 完整测试脚本 - Task #61
  4. * 1. 启动 Python 服务(如果未运行)
  5. * 2. 测试代理(带认证)
  6. * 3. 测试账号 Cookie
  7. * 4. 尝试发布(使用反检测)
  8. */
  9. import { spawn } from 'child_process';
  10. import http from 'http';
  11. import mysql from 'mysql2/promise.js';
  12. const PYTHON_PORT = 5005;
  13. const NODE_PORT = 3000;
  14. const TASK_ID = 61;
  15. const ACCOUNT_ID = 24;
  16. // 检查服务状态
  17. function checkService(port) {
  18. return new Promise((resolve) => {
  19. const req = http.get(`http://localhost:${port}`, { timeout: 2000 }, () => {
  20. resolve(true);
  21. });
  22. req.on('error', () => resolve(false));
  23. req.on('timeout', () => {
  24. req.destroy();
  25. resolve(false);
  26. });
  27. });
  28. }
  29. // 启动 Python 服务
  30. async function startPythonService() {
  31. console.log('\n🔄 检查 Python 服务...');
  32. const running = await checkService(PYTHON_PORT);
  33. if (running) {
  34. console.log('✅ Python 服务已运行');
  35. return true;
  36. }
  37. console.log('⚠️ Python 服务未运行');
  38. console.log('💡 请手动启动 Python 服务:');
  39. console.log(' cd E:\\Workspace\\multi-platform-media-manage\\server\\python');
  40. console.log(' python app.py --headless false');
  41. return false;
  42. }
  43. // 测试代理(带认证)
  44. async function testProxyWithAuth() {
  45. const conn = await mysql.createConnection({
  46. host: '8.136.223.156',
  47. port: 6630,
  48. user: 'media_manager',
  49. password: 'media_manager',
  50. database: 'media_manager'
  51. });
  52. const [configs] = await conn.query(`
  53. SELECT config_key, config_value
  54. FROM system_config
  55. WHERE config_key IN (
  56. 'publish_proxy_shenlong_product_key',
  57. 'publish_proxy_shenlong_signature',
  58. 'publish_proxy_shenlong_username',
  59. 'publish_proxy_shenlong_password'
  60. )
  61. `);
  62. await conn.end();
  63. const config = {};
  64. configs.forEach(c => {
  65. config[c.config_key.replace('publish_proxy_shenlong_', '')] = c.config_value;
  66. });
  67. console.log('\n=== 神龙代理配置 ===');
  68. console.log('Product Key:', config.product_key);
  69. console.log('Signature:', config.signature);
  70. console.log('Username:', config.username);
  71. console.log('Password:', config.password.substring(0, 20) + '...');
  72. // 测试代理
  73. return new Promise((resolve) => {
  74. const postData = JSON.stringify({
  75. provider: 'shenlong',
  76. productKey: config.product_key,
  77. signature: config.signature,
  78. regionCode: '310000',
  79. platform: 'weixin',
  80. username: config.username,
  81. password: config.password
  82. });
  83. const req = http.request('http://localhost:5005/proxy/test', {
  84. method: 'POST',
  85. headers: {
  86. 'Content-Type': 'application/json',
  87. 'Content-Length': Buffer.byteLength(postData)
  88. },
  89. timeout: 30000
  90. }, (res) => {
  91. let data = '';
  92. res.on('data', chunk => data += chunk);
  93. res.on('end', () => {
  94. try {
  95. const result = JSON.parse(data);
  96. console.log('\n=== 代理测试结果 ===');
  97. console.log(JSON.stringify(result, null, 2));
  98. resolve(result);
  99. } catch (e) {
  100. console.error('解析失败:', e);
  101. resolve(null);
  102. }
  103. });
  104. });
  105. req.on('error', (e) => {
  106. console.log('\n❌ 代理测试失败:', e.message);
  107. console.log('💡 Python 服务未启动,无法测试');
  108. resolve(null);
  109. });
  110. req.write(postData);
  111. req.end();
  112. });
  113. }
  114. // 获取账号 Cookie
  115. async function getAccountCookie(accountId) {
  116. const conn = await mysql.createConnection({
  117. host: '8.136.223.156',
  118. port: 6630,
  119. user: 'media_manager',
  120. password: 'media_manager',
  121. database: 'media_manager'
  122. });
  123. const [accounts] = await conn.query(`
  124. SELECT id, platform, account_name, cookies
  125. FROM platform_accounts
  126. WHERE id = ?
  127. `, [accountId]);
  128. await conn.end();
  129. if (accounts.length === 0) {
  130. console.log('\n❌ 未找到账号 ID:', accountId);
  131. return null;
  132. }
  133. const account = accounts[0];
  134. console.log('\n=== 发布账号 ===');
  135. console.log('ID:', account.id);
  136. console.log('平台:', account.platform);
  137. console.log('账号名:', account.account_name);
  138. console.log('Cookie 长度:', account.cookies ? account.cookies.length : 0);
  139. return account;
  140. }
  141. // 主函数
  142. async function main() {
  143. console.log('🚀 完整测试 - Task #' + TASK_ID);
  144. console.log('='.repeat(60));
  145. // 1. 检查/启动服务
  146. await startPythonService();
  147. // 2. 测试代理
  148. const proxyResult = await testProxyWithAuth();
  149. // 3. 获取账号信息
  150. const account = await getAccountCookie(ACCOUNT_ID);
  151. console.log('\n' + '='.repeat(60));
  152. console.log('📋 测试总结:');
  153. console.log(' - 代理API: ✅ 可用');
  154. console.log(` - 代理连通性: ${proxyResult ? '✅ 成功' : '❌ 需要启动 Python 服务'}`);
  155. console.log(` - 账号Cookie: ${account && account.cookies ? '✅ 有效' : '❌ 无效/过期'}`);
  156. console.log('\n💡 建议的下一步:');
  157. if (!proxyResult) {
  158. console.log(' 1. 启动 Python 服务:');
  159. console.log(' cd E:\\Workspace\\multi-platform-media-manage\\server\\python');
  160. console.log(' python app.py --headless false');
  161. }
  162. console.log(' 2. 使用 AI 辅助发布接口测试:');
  163. console.log(' POST http://localhost:5005/publish/ai-assisted');
  164. console.log(' 设置 headless: false 查看浏览器行为');
  165. console.log(' 3. 如果提示需要登录,使用有头浏览器手动登录');
  166. console.log('\n✅ 测试完成\n');
  167. }
  168. main().catch(console.error);