// Test script to verify background page navigation functionality
// This tests the new backgroundPage parameter for chrome_navigate and chrome_navigate_natural tools

console.log('🧪 Testing Background Page Navigation Functionality\n');

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

async function testBackgroundPageNavigation() {
  try {
    console.log('🚀 Starting background page navigation tests...\n');

    // Initialize MCP connection
    console.log('1️⃣ Initializing MCP connection...');
    const initResponse = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 0,
        method: 'initialize',
        params: {
          protocolVersion: '2024-11-05',
          capabilities: {},
          clientInfo: {
            name: 'background-page-test',
            version: '1.0.0',
          },
        },
      }),
    });

    const initResult = await initResponse.json();
    console.log('✅ Initialization response:', initResult.result ? 'Success' : 'Failed');

    // Test 1: Regular navigation with backgroundPage=true
    console.log('\n2️⃣ Testing chrome_navigate with backgroundPage=true...');
    const backgroundNavResponse = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 1,
        method: 'tools/call',
        params: {
          name: 'chrome_navigate',
          arguments: {
            url: 'https://www.example.com',
            backgroundPage: true,
          },
        },
      }),
    });

    const backgroundNavResult = await backgroundNavResponse.json();
    console.log('✅ Background navigation result:', JSON.stringify(backgroundNavResult, null, 2));

    // Test 2: Natural language navigation with backgroundPage=true
    console.log('\n3️⃣ Testing chrome_navigate_natural with backgroundPage=true...');
    const naturalBackgroundResponse = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 2,
        method: 'tools/call',
        params: {
          name: 'chrome_navigate_natural',
          arguments: {
            query: 'google',
            backgroundPage: true,
          },
        },
      }),
    });

    const naturalBackgroundResult = await naturalBackgroundResponse.json();
    console.log('✅ Natural language background navigation result:', JSON.stringify(naturalBackgroundResult, null, 2));

    // Test 3: Compare with regular navigation (backgroundPage=false)
    console.log('\n4️⃣ Testing chrome_navigate with backgroundPage=false for comparison...');
    const regularNavResponse = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 3,
        method: 'tools/call',
        params: {
          name: 'chrome_navigate',
          arguments: {
            url: 'https://www.google.com',
            backgroundPage: false,
          },
        },
      }),
    });

    const regularNavResult = await regularNavResponse.json();
    console.log('✅ Regular navigation result:', JSON.stringify(regularNavResult, null, 2));

    // Test 4: Test with both newWindow and backgroundPage (backgroundPage should take precedence)
    console.log('\n5️⃣ Testing chrome_navigate with both newWindow=true and backgroundPage=true...');
    const conflictTestResponse = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 4,
        method: 'tools/call',
        params: {
          name: 'chrome_navigate',
          arguments: {
            url: 'https://www.github.com',
            newWindow: true,
            backgroundPage: true,
          },
        },
      }),
    });

    const conflictTestResult = await conflictTestResponse.json();
    console.log('✅ Conflict test result:', JSON.stringify(conflictTestResult, null, 2));

    console.log('\n✨ All background page navigation tests completed!');
    
    // Summary
    console.log('\n📊 Test Summary:');
    console.log('✅ Background page navigation with chrome_navigate tested');
    console.log('✅ Background page navigation with chrome_navigate_natural tested');
    console.log('✅ Regular navigation comparison tested');
    console.log('✅ Parameter conflict handling tested');
    
    console.log('\n🎯 Expected Behavior:');
    console.log('- backgroundPage=true should create a minimized window');
    console.log('- backgroundPage=false should use normal tab/window behavior');
    console.log('- backgroundPage should take precedence over newWindow when both are true');
    console.log('- Natural language navigation should support backgroundPage parameter');

  } catch (error) {
    console.error('❌ Test failed:', error.message);
    console.error('Stack trace:', error.stack);
  }
}

// Run the tests
testBackgroundPageNavigation().catch(console.error);
