/**
 * Test script to verify the background URL opening functionality
 * This script tests the new browser setting for opening URLs in background pages
 */

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

async function testBackgroundUrlSetting() {
  console.log('🧪 Testing Background URL Opening Setting...\n');

  try {
    // Test 1: 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-url-test',
            version: '1.0.0',
          },
        },
      }),
    });

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

    // Test 2: Navigate without explicit backgroundPage parameter (should use setting)
    console.log('\n2️⃣ Testing navigation without explicit backgroundPage parameter...');
    console.log("   This should use the user's browser setting preference");

    const defaultNavResponse = 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',
            // No backgroundPage parameter - should use user setting
          },
        },
      }),
    });

    const defaultNavResult = await defaultNavResponse.json();
    console.log('✅ Default navigation result:', JSON.stringify(defaultNavResult, null, 2));

    // Test 3: Navigate with explicit backgroundPage=true (should override setting)
    console.log('\n3️⃣ Testing navigation with explicit backgroundPage=true...');
    console.log('   This should override the user setting and open in background');

    const explicitBackgroundResponse = 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',
          arguments: {
            url: 'https://www.google.com',
            backgroundPage: true, // Explicit override
          },
        },
      }),
    });

    const explicitBackgroundResult = await explicitBackgroundResponse.json();
    console.log(
      '✅ Explicit background navigation result:',
      JSON.stringify(explicitBackgroundResult, null, 2),
    );

    // Test 4: Navigate with explicit backgroundPage=false (should override setting)
    console.log('\n4️⃣ Testing navigation with explicit backgroundPage=false...');
    console.log('   This should override the user setting and open in foreground');

    const explicitForegroundResponse = 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.github.com',
            backgroundPage: false, // Explicit override
          },
        },
      }),
    });

    const explicitForegroundResult = await explicitForegroundResponse.json();
    console.log(
      '✅ Explicit foreground navigation result:',
      JSON.stringify(explicitForegroundResult, null, 2),
    );

    console.log('\n🎉 All tests completed!');
    console.log('\n📋 Test Summary:');
    console.log('   • Default navigation uses browser setting preference');
    console.log('   • Explicit backgroundPage=true overrides setting');
    console.log('   • Explicit backgroundPage=false overrides setting');
    console.log('\n💡 To test the setting:');
    console.log('   1. Open the Chrome extension popup');
    console.log('   2. Go to Browser Settings section');
    console.log('   3. Toggle "Open URLs in background pages by default"');
    console.log('   4. Run this test script again to see the difference');
  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

// Run the test
testBackgroundUrlSetting();
