Tests/Test-AgentHandoff.ps1

if (-not (Get-Command Invoke-AgentHandoff -ErrorAction SilentlyContinue)) {
    . "$PSScriptRoot\Invoke-AgentHandoff.ps1"
}

$ApiKey = $env:HANDOFF_API_KEY
if (-not $ApiKey) { throw "Set `$env:HANDOFF_API_KEY first" }

$testCases = @(
    @{
        Name      = "Basic handoff - simple task"
        Message   = @{ task = "Summarize the quarterly report" }
        NextAgent = "summarizer"
    },
    @{
        Name      = "Handoff with structured data"
        Message   = @{ task = "Analyze sentiment"; data = "Customer feedback text here"; priority = "high" }
        NextAgent = "analyzer"
    },
    @{
        Name      = "Handoff to different agent"
        Message   = @{ task = "Translate document"; language = "Spanish" }
        NextAgent = "translator"
    }
)

$passed = 0
$failed = 0
$results = @()

Write-Host ""
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host " Agent Handoff Middleware - Test Suite" -ForegroundColor Cyan
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""

# Health check first
Write-Host "HEALTH CHECK" -ForegroundColor Yellow
try {
    $health = Get-HandoffHealth
    Write-Host " Status: $($health.status)" -ForegroundColor Green
} catch {
    Write-Host " FAILED: $_" -ForegroundColor Red
}
Write-Host ""

foreach ($test in $testCases) {
    Write-Host "TEST: $($test.Name)" -ForegroundColor Yellow

    try {
        $result = Invoke-AgentHandoff -Message $test.Message -NextAgent $test.NextAgent -ApiKey $ApiKey

        $testPassed = $true
        $issues = @()

        if (-not $result.Success)   { $testPassed = $false; $issues += "Success was false" }
        if (-not $result.HandoffId) { $testPassed = $false; $issues += "Missing HandoffId" }
        if (-not $result.NextAgent) { $testPassed = $false; $issues += "Missing NextAgent" }

        if ($testPassed) {
            Write-Host " PASSED" -ForegroundColor Green
            $passed++
        } else {
            Write-Host " FAILED: $($issues -join ', ')" -ForegroundColor Red
            $failed++
        }

        Write-Host " HandoffId : $($result.HandoffId)" -ForegroundColor DarkGray
        Write-Host " NextAgent : $($result.NextAgent)" -ForegroundColor DarkGray
        Write-Host " IsPro : $($result.IsPro)" -ForegroundColor DarkGray
        Write-Host " Total : $($result.TotalHandoffs)" -ForegroundColor DarkGray

        $results += [PSCustomObject]@{
            TestName  = $test.Name
            Passed    = $testPassed
            HandoffId = $result.HandoffId
            NextAgent = $result.NextAgent
            IsPro     = $result.IsPro
            Issues    = ($issues -join "; ")
        }

    } catch {
        Write-Host " ERROR: $_" -ForegroundColor Red
        $failed++
        $results += [PSCustomObject]@{
            TestName  = $test.Name
            Passed    = $false
            HandoffId = ""
            NextAgent = ""
            IsPro     = $false
            Issues    = "Exception: $_"
        }
    }

    Write-Host ""
}

Write-Host "=================================================" -ForegroundColor Cyan
Write-Host " RESULTS: $passed passed, $failed failed" -ForegroundColor $(if ($failed -eq 0) { 'Green' } else { 'Red' })
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""

$csvPath = ".\test-results-handoff-$(Get-Date -Format 'yyyyMMdd-HHmmss').csv"
$results | Export-Csv -Path $csvPath -NoTypeInformation
Write-Host "Results saved to: $csvPath" -ForegroundColor DarkGray