param(
    [Parameter(Mandatory=$true)][string]$Executable,
    [ValidateSet('profiles','files','factory','integration')][string]$Suite,
    [Parameter(Mandatory=$true)][string]$OutputRoot,
    [Parameter(Mandatory=$true)][string]$SourceDir
)
$ErrorActionPreference='Stop'
$run=Join-Path $OutputRoot ($Suite+'-'+[DateTime]::UtcNow.ToString('yyyyMMddTHHmmssfffZ')+'-'+[guid]::NewGuid().ToString('N'))
& $Executable self-test --suite $Suite --output $run
if($LASTEXITCODE -ne 0){throw "Factory $Suite suite failed ($LASTEXITCODE); evidence: $run"}
if($Suite -eq 'profiles'){
    $expected=Join-Path $SourceDir 'Tests/FactoryPresetProfileGoldens.json'
    if(-not(Test-Path -LiteralPath $expected)){throw 'Fixed Rev1 profile goldens have not been captured/reviewed yet.'}
    $actual=Get-Content -LiteralPath (Join-Path $run 'profile-goldens.json') -Raw | ConvertFrom-Json
    $golden=Get-Content -LiteralPath $expected -Raw | ConvertFrom-Json
    if($actual.categories.Count -ne 8 -or $golden.categories.Count -ne 8){throw 'Expected eight golden categories'}
    for($i=0;$i -lt 8;$i++){
        if($actual.categories[$i].category -cne $golden.categories[$i].category -or $actual.categories[$i].sha256FloatBits -cne $golden.categories[$i].sha256FloatBits){throw "Rev1 golden mismatch at category $i"}
    }
    Write-Host 'PASS: 8192 fixed seeds match frozen raw-float goldens.'
}
if($Suite -eq 'files'){
    $attempts=@(
        @('nonsense'),
        @('generate','--category','deep','--count','1','--start-index','1','--seed','-1','--profile','rev1','--output',(Join-Path $run 'negative')),
        @('generate','--category','deep','--count','1','--start-index','1','--seed','18446744073709551616','--profile','rev1','--output',(Join-Path $run 'negative')),
        @('generate','--category','deep','--count','1x','--start-index','1','--seed','1','--profile','rev1','--output',(Join-Path $run 'negative')),
        @('validate','--input',$run,'--input',$run),
        @('generate','--category','wrong','--count','1','--start-index','1','--seed','1','--profile','rev1','--output',(Join-Path $run 'negative')),
        @('generate','--category','deep','--count','1','--start-index','1','--seed','1','--profile','rev1','--output',(Join-Path $SourceDir 'Presets/Factory'))
    )
    foreach($arguments in $attempts){
        & $Executable @arguments
        if($LASTEXITCODE -ne 1){throw 'Invalid CLI unexpectedly accepted or crashed'}
    }
    if(Test-Path -LiteralPath (Join-Path $run 'negative')){throw 'Invalid CLI created output'}
    Write-Host 'PASS: CLI negative paths, overflow, duplicate options and output isolation.'
}
