"""
Test script for Chromium MCP Server - Core functionality (Updated)
"""
import asyncio
import base64
import httpx
import sys
import os
# Add parent directory to path to import source_fetch
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from source_fetch import extract_chromium_file_path, fetch_chromium_source_file

async def test_core_functions():
    print("Testing Chromium MCP Server core functionality...\n")
    
    # Test URL extraction with various formats
    print("1. Testing URL extraction:")
    test_urls = [
        "https://source.chromium.org/chromium/chromium/src/+/main:chrome/app/chrome_main.cc",
        "https://source.chromium.org/chromium/chromium/src/+/main:base/base.gyp",
        "https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:chrome/browser/ui/browser.cc"
    ]
    
    for test_url in test_urls:
        extracted_path = extract_chromium_file_path(test_url)
        print(f"   Input URL: {test_url}")
        print(f"   Extracted path: {extracted_path}")
        print()
    
    # Test direct file fetching
    print("2. Testing file fetching:")
    print("   Attempting to fetch chrome/app/chrome_main.cc from main branch...")
    
    try:
        result = await fetch_chromium_source_file("chrome/app/chrome_main.cc", "main")
        print(f"   Result type: {type(result)}")
        if isinstance(result, str):
            if result.startswith("Error:"):
                print(f"   Error occurred: {result}")
            else:
                print(f"   Success! Content length: {len(result)} characters")
                print(f"   First 200 chars: {result[:200]}...")
        else:
            print(f"   Result: {result}")
    except Exception as e:
        print(f"   Exception occurred: {e}")
    
    # Test another file path
    print("\n3. Testing another file path:")
    try:
        result = await fetch_chromium_source_file("base/base.gyp", "main")
        print(f"   Result type: {type(result)}")
        if isinstance(result, str):
            if result.startswith("Error:"):
                print(f"   Error occurred: {result}")
            else:
                print(f"   Success! Content length: {len(result)} characters")
                print(f"   First 200 chars: {result[:200]}...")
        else:
            print(f"   Result: {result}")
    except Exception as e:
        print(f"   Exception occurred: {e}")

if __name__ == "__main__":
    asyncio.run(test_core_functions())