const http = require('http');

function httpRequest(method, path, data) {
  return new Promise((resolve, reject) => {
    const options = {
      hostname: 'localhost',
      port: 3000,
      path: path,
      method: method,
      headers: { 'Content-Type': 'application/json' }
    };
    const req = http.request(options, (res) => {
      let body = '';
      res.on('data', chunk => body += chunk);
      res.on('end', () => {
        try { resolve({ status: res.statusCode, data: JSON.parse(body) }); }
        catch { resolve({ status: res.statusCode, data: body }); }
      });
    });
    req.on('error', reject);
    if (data) req.write(JSON.stringify(data));
    req.end();
  });
}

(async () => {
  console.log('=== 完整功能测试 ===\n');
  
  try {
    console.log('1. 测试API连接...');
    const apiTest = await httpRequest('GET', '/api/video/projects');
    console.log('   状态:', apiTest.status === 200 ? '✅' : '❌', apiTest.status);
    
    console.log('\n2. 创建测试项目...');
    const create = await httpRequest('POST', '/api/video/projects', {
      title: '自动化测试',
      config: {
        images: [{ url: '/uploads/materials/dnoz9actogl6q0kp9togy2fym.png', duration: 5 }],
        audio: { url: '/uploads/materials/xlgman1g6tyt53zl5k3jhc42n.mp3', volume: 1 },
        video: { width: 720, height: 1280, fps: 30 },
        kenburns: { enabled: true, minZoom: 1, maxZoom: 1.2 }
      }
    });
    console.log('   状态:', create.status === 200 ? '✅' : '❌', create.status);
    if (create.data.success) {
      console.log('   项目ID:', create.data.data.id);
      
      console.log('\n3. 测试视频生成...');
      const gen = await httpRequest('POST', `/api/video/projects/${create.data.data.id}/generate`);
      console.log('   状态:', gen.status === 200 ? '✅' : '❌', gen.status);
      console.log('   结果:', JSON.stringify(gen.data).substring(0, 150));
    }
    
    console.log('\n=== 测试完成 ===');
  } catch (e) {
    console.error('测试失败:', e.message);
  }
})();
