/**
 * Test MCP server connection and Chrome extension availability
 */

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

async function testServerConnection() {
  console.log('🔌 Testing MCP Server Connection');
  console.log('================================');

  try {
    console.log('1️⃣ Testing server availability...');

    const response = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json, text/event-stream',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 1,
        method: 'initialize',
        params: {
          protocolVersion: '2024-11-05',
          capabilities: {},
          clientInfo: {
            name: 'connection-test',
            version: '1.0.0',
          },
        },
      }),
    });

    console.log(`   Server response status: ${response.status}`);

    if (!response.ok) {
      console.log(`❌ Server not responding properly. Status: ${response.status}`);
      return;
    }

    const result = await response.json();
    console.log('✅ Server is responding');
    console.log(`   Response:`, JSON.stringify(result, null, 2));

    // Test if Chrome extension tools are available
    console.log('\n2️⃣ Testing Chrome extension tools availability...');

    const toolsResponse = await fetch(MCP_HTTP_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json, text/event-stream',
      },
      body: JSON.stringify({
        jsonrpc: '2.0',
        id: 2,
        method: 'tools/list',
        params: {},
      }),
    });

    const toolsResult = await toolsResponse.json();

    if (toolsResult.result && toolsResult.result.tools) {
      console.log('✅ Tools are available');
      console.log(`   Available tools: ${toolsResult.result.tools.length}`);

      const chromeTools = toolsResult.result.tools.filter(
        (tool) => tool.name.includes('chrome') || tool.name.includes('navigate'),
      );

      console.log(`   Chrome-related tools: ${chromeTools.length}`);
      chromeTools.forEach((tool) => {
        console.log(`      - ${tool.name}: ${tool.description}`);
      });

      if (chromeTools.length > 0) {
        console.log('✅ Chrome extension appears to be connected');
      } else {
        console.log('❌ No Chrome tools found - extension may not be connected');
      }
    } else {
      console.log('❌ No tools available - Chrome extension likely not connected');
      console.log('   Tools response:', JSON.stringify(toolsResult, null, 2));
    }
  } catch (error) {
    console.log(`❌ Connection test failed: ${error.message}`);
    console.log('\nTroubleshooting steps:');
    console.log('1. Make sure the MCP server is running: npm start in remote-server directory');
    console.log('2. Make sure the Chrome extension is loaded and enabled');
    console.log('3. Make sure the Chrome extension is connected to the MCP server');
    console.log('4. Check the Chrome extension popup for connection status');
  }
}

// Run the connection test
testServerConnection().catch(console.error);
