# test-mcp-servers.ps1
# Automated testing script for all configured MCP servers

Write-Host "=== MCP Server Testing Suite ===" -ForegroundColor Cyan
Write-Host "Testing all configured MCP servers..." -ForegroundColor White
Write-Host ""

$results = @()

# Test 1: Chrome DevTools
Write-Host "[1/8] Testing Chrome DevTools..." -ForegroundColor Yellow
try {
    $chromeProc = Start-Process -FilePath "npx" -ArgumentList "-y","chrome-devtools-mcp@latest","--headless","true" -PassThru -NoNewWindow -RedirectStandardOutput "$env:TEMP\chrome-test.log" -ErrorAction Stop
    Start-Sleep -Seconds 5
    if (!$chromeProc.HasExited) {
        Write-Host "  âœ“ Chrome DevTools server started successfully" -ForegroundColor Green
        $results += [PSCustomObject]@{Server="Chrome DevTools"; Status="PASS"; Details="Server started"}
        Stop-Process -Id $chromeProc.Id -Force -ErrorAction SilentlyContinue
    } else {
        Write-Host "  âœ— Chrome DevTools server exited immediately" -ForegroundColor Red
        $results += [PSCustomObject]@{Server="Chrome DevTools"; Status="FAIL"; Details="Server exited"}
    }
} catch {
    Write-Host "  âœ— Chrome DevTools failed: $($_.Exception.Message)" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Chrome DevTools"; Status="FAIL"; Details=$_.Exception.Message}
}

# Test 2: Markitdown
Write-Host "`n[2/8] Testing Markitdown..." -ForegroundColor Yellow
$markitdownPath = "C:\Users\games3\.local\bin\uvx.exe"
if (Test-Path $markitdownPath) {
    Write-Host "  âœ“ Markitdown executable found at: $markitdownPath" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Markitdown"; Status="PASS"; Details="Executable exists"}
} else {
    Write-Host "  âœ— Markitdown executable not found at: $markitdownPath" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Markitdown"; Status="FAIL"; Details="Executable not found"}
}

# Test 3: Gitea Torbonium
Write-Host "`n[3/8] Testing Gitea Torbonium (gitea.torbonium.com)..." -ForegroundColor Yellow
try {
    $headers = @{Authorization="token 391c9ddbe113378bc87bb8184800ba954648fcf8"}
    $response = Invoke-RestMethod -Uri "https://gitea.torbonium.com/api/v1/user" -Headers $headers -TimeoutSec 10
    Write-Host "  âœ“ Gitea Torbonium authenticated as: $($response.login)" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Gitea Torbonium"; Status="PASS"; Details="Authenticated as $($response.login)"}
} catch {
    Write-Host "  âœ— Gitea Torbonium failed: $($_.Exception.Message)" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Gitea Torbonium"; Status="FAIL"; Details=$_.Exception.Message}
}

# Test 4: Gitea LAN
Write-Host "`n[4/8] Testing Gitea LAN (gitea.torbolan.com)..." -ForegroundColor Yellow
Write-Host "  âš  Token needs replacement - SKIPPING" -ForegroundColor Yellow
$results += [PSCustomObject]@{Server="Gitea LAN"; Status="SKIP"; Details="Token placeholder needs update"}

# Test 5: Gitea Projectium
Write-Host "`n[5/8] Testing Gitea Projectium (gitea.projectium.com)..." -ForegroundColor Yellow
try {
    $headers = @{Authorization="token c72bc0f14f623fec233d3c94b3a16397fe3649ef"}
    $response = Invoke-RestMethod -Uri "https://gitea.projectium.com/api/v1/user" -Headers $headers -TimeoutSec 10
    Write-Host "  âœ“ Gitea Projectium authenticated as: $($response.login)" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Gitea Projectium"; Status="PASS"; Details="Authenticated as $($response.login)"}
} catch {
    Write-Host "  âœ— Gitea Projectium failed: $($_.Exception.Message)" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Gitea Projectium"; Status="FAIL"; Details=$_.Exception.Message}
}

# Test 6: Podman/Docker
Write-Host "`n[6/8] Testing Docker/Podman..." -ForegroundColor Yellow
try {
    $dockerOutput = & docker version 2>$null
    if ($LASTEXITCODE -eq 0 -and $dockerOutput) {
        Write-Host "  âœ“ Docker daemon accessible" -ForegroundColor Green
        $results += [PSCustomObject]@{Server="Docker/Podman"; Status="PASS"; Details="Docker daemon running"}
    } else {
        Write-Host "  âœ— Docker daemon not accessible" -ForegroundColor Red
        $results += [PSCustomObject]@{Server="Docker/Podman"; Status="FAIL"; Details="Cannot connect to daemon"}
    }
} catch {
    Write-Host "  âœ— Docker not available: $($_.Exception.Message)" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Docker/Podman"; Status="FAIL"; Details="Docker not installed"}
}

# Test 7: Filesystem
Write-Host "`n[7/8] Testing Filesystem..." -ForegroundColor Yellow
$projectPath = "D:\gitea\flyer-crawler.projectium.com\flyer-crawler.projectium.com"
if (Test-Path $projectPath) {
    $fileCount = (Get-ChildItem $projectPath -File -Recurse -ErrorAction SilentlyContinue | Measure-Object).Count
    Write-Host "  âœ“ Project directory accessible ($fileCount files)" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Filesystem"; Status="PASS"; Details="Path accessible, $fileCount files"}
} else {
    Write-Host "  âœ— Project directory not accessible" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Filesystem"; Status="FAIL"; Details="Path not accessible"}
}

# Test 8: Fetch MCP Server
Write-Host "`n[8/8] Testing Fetch MCP Server..." -ForegroundColor Yellow
try {
    # Test by attempting to fetch a simple public API
    $testUrl = "https://api.github.com/zen"
    $response = Invoke-RestMethod -Uri $testUrl -TimeoutSec 10 -ErrorAction Stop
    if ($response) {
        Write-Host "  âœ“ Fetch server prerequisites met (network accessible)" -ForegroundColor Green
        $results += [PSCustomObject]@{Server="Fetch"; Status="PASS"; Details="Network accessible, can fetch data"}
    } else {
        Write-Host "  âœ— Fetch server test failed" -ForegroundColor Red
        $results += [PSCustomObject]@{Server="Fetch"; Status="FAIL"; Details="Could not fetch test data"}
    }
} catch {
    Write-Host "  âœ— Fetch server test failed: $($_.Exception.Message)" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Fetch"; Status="FAIL"; Details=$_.Exception.Message}
}

# Display Results Summary
Write-Host "`n`n=== Test Results Summary ===" -ForegroundColor Cyan
Write-Host ""

$results | Format-Table -AutoSize

# Count results
$passed = ($results | Where-Object Status -eq "PASS").Count
$failed = ($results | Where-Object Status -eq "FAIL").Count
$skipped = ($results | Where-Object Status -eq "SKIP").Count
$total = $results.Count

Write-Host "`nOverall Results:" -ForegroundColor White
Write-Host "  Total Tests:  $total" -ForegroundColor White
Write-Host "  Passed:       $passed" -ForegroundColor Green
Write-Host "  Failed:       $failed" -ForegroundColor Red
Write-Host "  Skipped:      $skipped" -ForegroundColor Yellow

# Exit code based on results
if ($failed -gt 0) {
    Write-Host "`nâš ï¸ Some tests failed. Review the results above." -ForegroundColor Yellow
    exit 1
} elseif ($passed -eq ($total - $skipped)) {
    Write-Host "`nâœ“ All tests passed!" -ForegroundColor Green
    exit 0
} else {
    Write-Host "`nâš ï¸ Tests completed with warnings." -ForegroundColor Yellow
    exit 0
}

