# test.ps1
#
# Runs the automated verification required by PROJECT_PLAN.md before any
# built ffmpeg.exe can be considered passing: static capability checks and
# the hardware encode/decode smoke test. Fails (non-zero exit) if any required
# check fails -- per the plan, "test.ps1 must fail the build if any required
# check fails."
#
# The plan's "Capture smoke test" and "Hardware matrix" sections (gdigrab on
# an interactive desktop, per-GPU encoder probes) are Milestone 3 concerns
# and are not run here; they need real display hardware and are driven
# separately using the same technique proven in
# baseline/capture-gyan-baseline.ps1.
#
#   powershell -ExecutionPolicy Bypass -File test.ps1 -FfmpegPath <path>
#
# Produces <WorkRoot>\manifests\test-report.json

[CmdletBinding()]
param(
    [Parameter(Mandatory)][string]$FfmpegPath,
    [string]$WorkRoot = 'C:\nnffb'
)

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
Import-Module (Join-Path $PSScriptRoot 'scripts\BuildCommon.psm1') -Force

if (-not (Test-Path $FfmpegPath)) { throw "ffmpeg.exe not found: $FfmpegPath" }

$paths = Get-BuildPaths $WorkRoot
$outDir = Join-Path $paths.manifests 'test-output'
New-Item -ItemType Directory -Force -Path $outDir | Out-Null

Write-Host "=== capability-tests ===" -ForegroundColor Cyan
$capResult = & (Join-Path $PSScriptRoot 'tests\capability-tests.ps1') -FfmpegPath $FfmpegPath -OutDir $outDir

Write-Host "`n=== encode-smoke-tests ===" -ForegroundColor Cyan
$encResult = & (Join-Path $PSScriptRoot 'tests\encode-smoke-tests.ps1') -FfmpegPath $FfmpegPath -OutDir $outDir

if (-not $encResult.passed) {
    throw 'No supported hardware H.264 encoder initialised. The LGPL LiveView build cannot be release-qualified on this machine.'
}

Write-Host "`n=== decode-smoke-tests ===" -ForegroundColor Cyan
$decResult = & (Join-Path $PSScriptRoot 'tests\decode-smoke-tests.ps1') `
    -FfmpegPath $FfmpegPath -OutDir $outDir -Encoder $encResult.selectedEncoder

$allPassed = $capResult.passed -and $encResult.passed -and $decResult.passed

$report = [ordered]@{
    schemaVersion = 1
    runUtc        = (Get-Date).ToUniversalTime().ToString('o')
    machine       = $env:COMPUTERNAME
    ffmpegPath    = $FfmpegPath
    ffmpegSha256  = Get-FileSha256 $FfmpegPath
    capabilityTests = $capResult
    encodeSmokeTests = $encResult
    decodeSmokeTests = $decResult
    overallResult = if ($allPassed) { 'passed' } else { 'failed' }
}
Write-JsonFile -Object $report -Path (Join-Path $paths.manifests 'test-report.json')

Write-Host "`n=== Result ===" -ForegroundColor Cyan
Write-Host ("capability-tests:    {0}" -f $(if ($capResult.passed) { 'PASS' } else { 'FAIL' }))
Write-Host ("encode-smoke-tests:  {0}" -f $(if ($encResult.passed) { 'PASS' } else { 'FAIL' }))
Write-Host ("decode-smoke-tests:  {0}" -f $(if ($decResult.passed) { 'PASS' } else { 'FAIL' }))
Write-Host ("Report: {0}" -f (Join-Path $paths.manifests 'test-report.json'))

if (-not $allPassed) {
    if (-not $capResult.passed) {
        if (-not $capResult.staticDumpsOk) {
            Write-Host "`nOne or more FFmpeg capability dump commands failed:" -ForegroundColor Red
            $capResult.dumps.GetEnumerator() |
                Where-Object { $_.Value.exitCode -ne 0 } |
                ForEach-Object { Write-Host "  $($_.Key): exit $($_.Value.exitCode)" -ForegroundColor Red }
        }
        if (-not $capResult.licenseMarkerOk) {
            Write-Host "`nRequired licence marker was not found:" -ForegroundColor Red
            Write-Host "  $($capResult.licenseMarker)" -ForegroundColor Red
        }
        if ($capResult.missingRequired.Count -gt 0) {
            Write-Host "`nMissing required capabilities:" -ForegroundColor Red
            $capResult.missingRequired | ForEach-Object { Write-Host "  $_" -ForegroundColor Red }
        }
        if ($capResult.forbiddenPresent.Count -gt 0) {
            Write-Host "`nForbidden items present:" -ForegroundColor Red
            $capResult.forbiddenPresent | ForEach-Object { Write-Host "  $_" -ForegroundColor Red }
        }
    }
    throw "test.ps1 FAILED -- see $(Join-Path $paths.manifests 'test-report.json')"
}

Write-Host "`nAll tests passed." -ForegroundColor Green
$report
