/**
 * Test script for background page navigation functionality
 * Tests the improved behavior where windows open full-size then minimize
 */

const MCP_HTTP_URL = 'http://localhost:3001/mcp';

async function testBackgroundNavigation() {
  console.log('🧪 Testing Background Page Navigation');
  console.log('=====================================');

  const testCases = [
    {
      name: 'Basic background navigation',
      params: {
        url: 'https://example.com',
        backgroundPage: true
      }
    },
    {
      name: 'Background navigation with custom dimensions',
      params: {
        url: 'https://httpbin.org/html',
        backgroundPage: true,
        width: 1920,
        height: 1080
      }
    },
    {
      name: 'Background navigation to automation-friendly site',
      params: {
        url: 'https://httpbin.org/forms/post',
        backgroundPage: true,
        width: 1280,
        height: 720
      }
    }
  ];

  for (const testCase of testCases) {
    console.log(`\n📝 Testing: ${testCase.name}`);
    console.log(`URL: ${testCase.params.url}`);
    console.log(`Dimensions: ${testCase.params.width || 1280}x${testCase.params.height || 720}`);
    
    try {
      const response = await fetch(MCP_HTTP_URL, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
        },
        body: JSON.stringify({
          jsonrpc: '2.0',
          id: Math.random(),
          method: 'tools/call',
          params: {
            name: 'chrome_navigate',
            arguments: testCase.params,
          },
        }),
      });

      const result = await response.json();
      
      if (result.error) {
        console.log(`❌ Error: ${result.error.message}`);
      } else if (result.result && result.result.content) {
        const content = JSON.parse(result.result.content[0].text);
        console.log(`✅ Success: ${content.message}`);
        console.log(`   Window ID: ${content.windowId}`);
        console.log(`   Dimensions: ${content.width}x${content.height}`);
        console.log(`   Tab Count: ${content.tabs?.length || 0}`);
        
        // Wait a moment to see the behavior
        console.log('   Waiting 3 seconds to observe window behavior...');
        await new Promise(resolve => setTimeout(resolve, 3000));
      } else {
        console.log(`⚠️  Unexpected response format`);
      }
    } catch (error) {
      console.log(`❌ Test failed: ${error.message}`);
    }
  }

  console.log('\n🎯 Test Summary');
  console.log('===============');
  console.log('Expected behavior:');
  console.log('1. Window opens with full dimensions (visible briefly)');
  console.log('2. Window minimizes to taskbar after 1 second');
  console.log('3. Web automation tools can still interact with minimized window');
  console.log('4. Window maintains proper viewport dimensions for automation');
}

// Run the test
testBackgroundNavigation().catch(console.error);
