# 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 {
    # Use Start-Job to run npx in background since npx is a PowerShell script on Windows
    $chromeJob = Start-Job -ScriptBlock {
        & npx -y chrome-devtools-mcp@latest --headless true 2>&1
    }
    Start-Sleep -Seconds 5

    $jobState = Get-Job -Id $chromeJob.Id | Select-Object -ExpandProperty State
    if ($jobState -eq "Running") {
        Write-Host "  [PASS] Chrome DevTools server started successfully" -ForegroundColor Green
        $results += [PSCustomObject]@{Server="Chrome DevTools"; Status="PASS"; Details="Server started"}
        Stop-Job -Id $chromeJob.Id -ErrorAction SilentlyContinue
        Remove-Job -Id $chromeJob.Id -Force -ErrorAction SilentlyContinue
    } else {
        Receive-Job -Id $chromeJob.Id -ErrorAction SilentlyContinue | Out-Null
        Write-Host "  [FAIL] Chrome DevTools server failed to start" -ForegroundColor Red
        $results += [PSCustomObject]@{Server="Chrome DevTools"; Status="FAIL"; Details="Server failed to start"}
        Remove-Job -Id $chromeJob.Id -Force -ErrorAction SilentlyContinue
    }
} catch {
    Write-Host "  [FAIL] 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 "  [PASS] Markitdown executable found at: $markitdownPath" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Markitdown"; Status="PASS"; Details="Executable exists"}
} else {
    Write-Host "  [FAIL] 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 "  [PASS] Gitea Torbonium authenticated as: $($response.login)" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Gitea Torbonium"; Status="PASS"; Details="Authenticated as $($response.login)"}
} catch {
    Write-Host "  [FAIL] 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 "  [SKIP] 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 "  [PASS] Gitea Projectium authenticated as: $($response.login)" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Gitea Projectium"; Status="PASS"; Details="Authenticated as $($response.login)"}
} catch {
    Write-Host "  [FAIL] 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 {
    # Try podman first, then docker
    & podman ps 2>$null | Out-Null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "  [PASS] Podman daemon accessible and responding" -ForegroundColor Green
        $results += [PSCustomObject]@{Server="Docker/Podman"; Status="PASS"; Details="Podman running"}
    } else {
        & docker ps 2>$null | Out-Null
        if ($LASTEXITCODE -eq 0) {
            Write-Host "  [PASS] Docker daemon accessible" -ForegroundColor Green
            $results += [PSCustomObject]@{Server="Docker/Podman"; Status="PASS"; Details="Docker running"}
        } else {
            Write-Host "  [FAIL] Neither Podman nor Docker available" -ForegroundColor Red
            $results += [PSCustomObject]@{Server="Docker/Podman"; Status="FAIL"; Details="No container runtime found"}
        }
    }
} catch {
    Write-Host "  [FAIL] Container runtime test failed: $($_.Exception.Message)" -ForegroundColor Red
    $results += [PSCustomObject]@{Server="Docker/Podman"; Status="FAIL"; Details=$_.Exception.Message}
}

# 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 "  [PASS] Project directory accessible ($fileCount files)" -ForegroundColor Green
    $results += [PSCustomObject]@{Server="Filesystem"; Status="PASS"; Details="Path accessible, $fileCount files"}
} else {
    Write-Host "  [FAIL] 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 "  [PASS] Fetch server prerequisites met (network accessible)" -ForegroundColor Green
        $results += [PSCustomObject]@{Server="Fetch"; Status="PASS"; Details="Network accessible, can fetch data"}
    } else {
        Write-Host "  [FAIL] Fetch server test failed" -ForegroundColor Red
        $results += [PSCustomObject]@{Server="Fetch"; Status="FAIL"; Details="Could not fetch test data"}
    }
} catch {
    Write-Host "  [FAIL] 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[WARNING] Some tests failed. Review the results above." -ForegroundColor Yellow
    exit 1
} elseif ($passed -eq ($total - $skipped)) {
    Write-Host "`n[SUCCESS] All tests passed!" -ForegroundColor Green
    exit 0
} else {
    Write-Host "`n[WARNING] Tests completed with warnings." -ForegroundColor Yellow
    exit 0
}
