# Package-free selftests. All bytes/processes are synthetic tooling fixtures. # Run with Windows PowerShell 5.1 or pwsh; Linux pwsh is NOT Windows evidence. [CmdletBinding()] param([string]$FixtureOutput) Set-StrictMode -Version 2 $ErrorActionPreference = 'Stop' $collector = Join-Path (Split-Path -Parent $PSScriptRoot) 'scripts/collect-fidelity-windows.ps1' . $collector -LibraryOnly $temporary = Join-Path ([IO.Path]::GetTempPath()) ('fidelity-selftest-' + [guid]::NewGuid().ToString('N')) $null = [IO.Directory]::CreateDirectory($temporary) $script:Checks = 0 function Assert-True($Condition, [string]$Message) { if (-not $Condition) { throw $Message } } function Assert-Throws([scriptblock]$Body) { $thrown = $false try { & $Body | Out-Null } catch { $thrown = $true } Assert-True $thrown 'Expected rejection did not occur' } function Check([string]$Name, [scriptblock]$Body) { & $Body $script:Checks++ Write-Host "PASS $Name" } function Fixture-File([string]$Name, [string]$Content) { $path = Join-Path $temporary $Name $null = [IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($path)) [IO.File]::WriteAllText($path,$Content,(New-Object Text.UTF8Encoding($false))) return $path } try { Check 'stable IDs reject reserved/path aliases' { Assert-True ((Test-FidelityId 'session-01') -eq 'session-01') 'ID changed' foreach ($bad in @('../x','CON','a/b','NUL.txt','')) { Assert-Throws { Test-FidelityId $bad } } } Check 'paths reject traversal, drive paths and Windows aliases' { foreach ($bad in @('../x','a//x','C:/x','/x','a\x','aux.txt','a./x','a /x')) { Assert-Throws { Get-FidelityPath $temporary $bad } } Assert-True ((Get-FidelityPath $temporary 'space dir/a.txt').StartsWith($temporary)) 'Contained path missing' } $sample = Fixture-File 'sample.txt' 'abc' Check 'streamed SHA256 and size match actual bytes' { $id = Get-FidelityIdentity $sample Assert-True ($id.size -eq 3 -and $id.sha256 -eq 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad') 'Wrong identity' } Check 'copies preserve bytes and never overwrite evidence' { $id = Copy-FidelityEvidence $sample $temporary 'copied/space name.txt' Assert-True ($id.sha256 -eq (Get-FidelityIdentity $sample).sha256) 'Copy differs' Assert-Throws { Copy-FidelityEvidence $sample $temporary 'copied/space name.txt' } } Check 'JSON keeps empty arrays, Unicode, singleton arrays and write-once results' { $path = Join-Path $temporary 'json.json' Write-FidelityJson $path @{empty=@();single=@('one');text='café'} $value = Read-FidelityJson $path Assert-True ($value.empty -is [array] -and $value.empty.Count -eq 0) 'Empty list lost' Assert-True ($value.single -is [array] -and $value.single.Count -eq 1 -and $value.text -eq 'café') 'JSON changed' Assert-Throws { Write-FidelityJson $path @{} } $path2 = Join-Path $temporary 'empty.json' Write-FidelityJson $path2 @() Assert-True ([IO.File]::ReadAllText($path2).Trim() -eq '[]') 'Top-level empty list lost' } Check 'argument quoting preserves empty/spaces/quotes/trailing backslashes' { Assert-True ((ConvertTo-FidelityArgument '') -ceq '""') 'Empty quoting' Assert-True ((ConvertTo-FidelityArgument 'a b') -ceq '"a b"') 'Space quoting' Assert-True ((ConvertTo-FidelityArgument 'a"b') -ceq '"a\"b"') 'Quote escaping' Assert-True ((ConvertTo-FidelityArgument 'C:\space dir\') -ceq '"C:\space dir\\"') 'Trailing slash escaping' Assert-Throws { ConvertTo-FidelityArgument "bad`nargument" } } $hostExe = [Diagnostics.Process]::GetCurrentProcess().MainModule.FileName $child = Fixture-File 'child space.ps1' 'param([string]$Value) [Console]::Out.Write($Value); [Console]::Error.Write("child stderr"); exit 7' Check 'real child arguments, logs and nonzero exit remain distinct from success' { $logs = Join-Path $temporary 'process' $null = [IO.Directory]::CreateDirectory($logs) $result = Invoke-FidelityProcess $hostExe @('-NoProfile','-File',$child,'value with spaces') $temporary $logs 30 Assert-True ($result.exit_code -eq 7 -and $result.elapsed_seconds -ge 0) 'Exit result changed' Assert-True ([IO.File]::ReadAllText((Join-Path $logs 'stdout.txt')) -ceq 'value with spaces') 'Arguments not preserved' Assert-True ([IO.File]::ReadAllText((Join-Path $logs 'stderr.txt')) -ceq 'child stderr') 'Stderr missing' } Check 'process waits are bounded and timeout preserves logs' { $sleeper = Fixture-File 'sleep.ps1' 'Start-Sleep -Seconds 30' $logs = Join-Path $temporary 'timeout' $null = [IO.Directory]::CreateDirectory($logs) Assert-Throws { Invoke-FidelityProcess $hostExe @('-NoProfile','-File',$sleeper) $temporary $logs 1 } Assert-True (Test-Path -LiteralPath (Join-Path $logs 'stdout.txt')) 'Timeout logs lost' Assert-Throws { Invoke-FidelityProcess $hostExe @() $temporary $logs 0 } } Check 'failed process start produces original exception without unstarted process access' { $notExe = Fixture-File 'not-executable.bin' 'synthetic non executable' $logs = Join-Path $temporary 'bad-process' $null = [IO.Directory]::CreateDirectory($logs) Assert-Throws { Invoke-FidelityProcess $notExe @() $temporary $logs 1 } } Check 'failure skip abort cannot become captured and require reasons' { foreach ($state in @('failed','skipped','aborted')) { Assert-True ((Get-FidelityOutcome 'incomplete' $state 'observed reason') -eq $state) 'Outcome changed' Assert-Throws { Get-FidelityOutcome $state 'captured' '' } Assert-Throws { Get-FidelityOutcome 'incomplete' $state '' } } } Check 'result categories and open acceptance remain explicit' { foreach ($kind in @('native_port','original_dxwnd','synthetic')) { $r = New-FidelityReceipt 'selftest' 'fresh-natural-ordinary' 'continuous' $kind 'incomplete' @{} @{} 'fixture' Assert-True ($r.evidence_kind -eq $kind -and $r.acceptance.state -eq 'open' -and $r.missing_evidence.Count -gt 0) 'False acceptance' } Assert-Throws { New-FidelityReceipt 'selftest' 'case' 'continuous' 'wine-as-native' 'captured' @{} @{} '' } } $bundleRoot = Join-Path $temporary 'bundle' $null = [IO.Directory]::CreateDirectory((Join-Path $bundleRoot 'protocol')) $material = Fixture-File 'bundle/protocol/test.txt' 'synthetic immutable material' $entry = Get-FidelityIdentity $material; $entry.path = 'protocol/test.txt' Write-FidelityJson (Join-Path $bundleRoot 'manifest.json') @{schema_version=1;tooling_only=$true;mutable_roots=@('results','original-runs');files=@($entry)} Write-FidelityJson (Join-Path $bundleRoot 'READY.json') @{schema_version=1;status='prepared';manifest=(Get-FidelityIdentity (Join-Path $bundleRoot 'manifest.json'))} Check 'prepared bundle validates immutable bytes and permits only declared mutable roots' { $null = Test-FidelityBundle $bundleRoot $null = Fixture-File 'bundle/results/local-result.txt' 'diagnostic' $null = Test-FidelityBundle $bundleRoot $extra = Fixture-File 'bundle/unlisted.txt' 'unexpected' Assert-Throws { Test-FidelityBundle $bundleRoot } [IO.File]::Delete($extra) } Check 'tampered immutable bytes fail preflight' { [IO.File]::AppendAllText($material,'tampered') Assert-Throws { Test-FidelityBundle $bundleRoot } [IO.File]::WriteAllText($material,'synthetic immutable material',(New-Object Text.UTF8Encoding($false))) } $script:Bundle = $bundleRoot $script:SessionId = 'fixture-finish' $run = Join-Path $bundleRoot 'results/fixture-finish' $null = [IO.Directory]::CreateDirectory($run) $clock = @{wall_start=[DateTime]::UtcNow.ToString('o');wall_end=[DateTime]::UtcNow.ToString('o');monotonic_start=0;monotonic_end=2} $r = New-FidelityReceipt $SessionId 'fresh-natural-ordinary' 'continuous' 'synthetic' 'incomplete' @{os='test';execution='synthetic'} $clock 'Tooling-only synthetic fixture' Write-FidelityJson (Join-Path $run 'run.json') $r $script:ArtifactsDirectory = Join-Path $temporary 'media' $null = Fixture-File 'media/raw.mkv' 'synthetic video bytes, no gameplay' $null = Fixture-File 'media/audio.wav' 'synthetic audio bytes, no gameplay' $script:AnnotationPath = Join-Path $temporary 'annotations.json' $annotation = @{schema_version=1;artifacts=@(@{path='raw.mkv';role='video';duration_seconds=2},@{path='audio.wav';role='audio';duration_seconds=2});events=@(@{name='startup';artifact='raw.mkv';media_seconds=0;elapsed_seconds=0;method='manual';uncertainty_seconds=0.1});derivatives=@();limitations=@('synthetic tooling only');continuity=@{prerequisite_artifact='raw.mkv'}} $script:Outcome = 'captured' Check 'capture rejects an empty annotation before any media copy' { Write-FidelityJson $AnnotationPath @{schema_version=1;artifacts=@();events=@();derivatives=@();limitations=@()} Assert-Throws { Complete-FidelityRun } Assert-True (-not (Test-Path -LiteralPath (Join-Path $run 'media'))) 'Invalid annotation copied data' [IO.File]::Delete($AnnotationPath) } Check 'capture rejects synthetic promotion and missing event targets before writes' { $annotation.artifacts[0].synthetic = $true Write-FidelityJson $AnnotationPath $annotation Assert-Throws { Complete-FidelityRun } [IO.File]::Delete($AnnotationPath) $annotation.artifacts[0].Remove('synthetic') $annotation.events[0].artifact = 'absent.mkv' Write-FidelityJson $AnnotationPath $annotation Assert-Throws { Complete-FidelityRun } [IO.File]::Delete($AnnotationPath) $annotation.events[0].artifact = 'raw.mkv' } Check 'finish maps hashed media/event/continuity paths and preserves synthetic category' { Write-FidelityJson $AnnotationPath $annotation Complete-FidelityRun $result = Read-FidelityJson (Join-Path $run 'receipt.json') Assert-True ($result.status -eq 'captured' -and $result.evidence_kind -eq 'synthetic' -and $result.acceptance.state -eq 'open') 'Fixture promoted' Assert-True ($result.artifacts.Count -eq 3 -and $result.events[0].artifact -eq 'media/raw.mkv' -and $result.continuity.prerequisite_artifact -eq 'media/raw.mkv') 'Broken result links' Assert-True ($result.artifacts[0].sha256 -eq (Get-FidelityIdentity (Join-Path $run 'media/raw.mkv')).sha256) 'Media identity differs' Assert-Throws { Complete-FidelityRun } } Check 'terminal failed skipped aborted results retain empty artifact lists and cannot be promoted' { foreach ($state in @('failed','skipped','aborted')) { $script:SessionId = 'fixture-' + $state $terminalRun = Join-Path $bundleRoot ('results/' + $SessionId) $null = [IO.Directory]::CreateDirectory($terminalRun) $terminal = New-FidelityReceipt $SessionId 'fresh-natural-ordinary' 'continuous' 'synthetic' $state @{os='test';execution='synthetic'} $clock 'synthetic terminal fixture' Write-FidelityJson (Join-Path $terminalRun 'run.json') $terminal $script:AnnotationPath = Join-Path $temporary ($state + '-notes.json') Write-FidelityJson $AnnotationPath @{schema_version=1;artifacts=@();events=@();derivatives=@();limitations=@('synthetic tooling only')} $script:Outcome = $state; $script:Reason = 'Tooling fixture; no actual capture' Complete-FidelityRun $saved = Read-FidelityJson (Join-Path $terminalRun 'receipt.json') Assert-True ($saved.status -eq $state -and $saved.artifacts.Count -eq 1 -and $saved.events -is [array] -and $saved.events.Count -eq 0 -and $saved.acceptance.state -eq 'open') 'Terminal fixture changed' } } if ($FixtureOutput) { if (Test-Path -LiteralPath $FixtureOutput) { throw 'Fixture output must be new' } $null = [IO.Directory]::CreateDirectory($FixtureOutput) foreach ($item in Get-ChildItem -LiteralPath (Join-Path $bundleRoot 'results') -Directory) { Copy-Item -LiteralPath $item.FullName -Destination $FixtureOutput -Recurse } } Assert-True ($script:Checks -ge 8) 'Fewer than eight executed checks' Write-Host "PASS: $script:Checks checks; 0 failures; synthetic tooling only; native Windows/original gameplay acceptance OPEN" } finally { if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Recurse -Force } }