| 1 | # Cross-platform regression for the real smoke-report and CI artifact contract. |
| 2 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 3 | $directory = Join-Path ([IO.Path]::GetTempPath()) ('cage-smoke-contract-' + [guid]::NewGuid().ToString('N')) |
| 4 | New-Item -ItemType Directory -Path $directory | Out-Null |
| 5 | $savedRunnerTemp = $env:RUNNER_TEMP |
| 6 | function Assert-SmokeContract([bool]$Condition, [string]$Message) { if (-not $Condition) { throw $Message } } |
| 7 | try { |
| 8 | $env:RUNNER_TEMP = Join-Path $directory 'runner-temp' |
| 9 | Assert-SmokeContract ((Get-WindowsWorkbenchTestBase) -eq [IO.Path]::GetFullPath($env:RUNNER_TEMP)) 'CI report directory ignores RUNNER_TEMP.' |
| 10 | $savedTemp = $env:TEMP; $savedTmp = $env:TMP |
| 11 | $envFile = Join-Path $directory 'child-env.json' |
| 12 | $envScript = Join-Path $directory 'child-env.ps1' |
| 13 | [IO.File]::WriteAllText($envScript, '@{temp=$env:TEMP;tmp=$env:TMP} | ConvertTo-Json | Set-Content -LiteralPath $args[0]') |
| 14 | $failed = $false |
| 15 | try { |
| 16 | Invoke-InIsolatedTestTemp { |
| 17 | Invoke-BuildTool (Get-Process -Id $PID).Path @('-NoProfile','-NonInteractive','-File',$envScript,$envFile) |
| 18 | throw 'injected test failure' |
| 19 | } |
| 20 | } catch { $failed = $_.Exception.Message -eq 'injected test failure' } |
| 21 | $childEnv = Get-Content -LiteralPath $envFile -Raw | ConvertFrom-Json |
| 22 | Assert-SmokeContract $failed 'Isolated test temp swallowed its test failure.' |
| 23 | Assert-SmokeContract ($childEnv.temp -eq $childEnv.tmp -and (Split-Path $childEnv.temp -Parent) -eq $env:RUNNER_TEMP) 'Child tests did not inherit the isolated runner temp.' |
| 24 | Assert-SmokeContract ($env:TEMP -eq $savedTemp -and $env:TMP -eq $savedTmp) 'Failed tests changed the caller temp environment.' |
| 25 | Assert-SmokeContract (-not (Test-Path -LiteralPath $childEnv.temp)) 'Owned test temp was not cleaned.' |
| 26 | Remove-Item Env:RUNNER_TEMP -ErrorAction SilentlyContinue |
| 27 | Assert-SmokeContract ((Get-WindowsWorkbenchTestBase) -eq [IO.Path]::GetTempPath()) 'Local report directory lost its OS temp fallback.' |
| 28 | $report = Join-Path $directory 'smoke.json' |
| 29 | $fixture = Join-Path $directory 'fixture.json' |
| 30 | $childScript = Join-Path $directory 'child.ps1' |
| 31 | [IO.File]::WriteAllText($childScript, '[IO.File]::Copy($args[0],$args[1],$true); exit 17') |
| 32 | @{ |
| 33 | success = $false; nativeBridgeReady = $false; phase = 'loading_workbench' |
| 34 | prepareExit = 1; prepareStatus = 'stop_required' |
| 35 | nativeStatus = 'failed http://localhost:1234/?token=private-token#private-fragment' |
| 36 | entryJSON = 'private-entry'; pageURL = 'http://localhost:1234/#private-launch' |
| 37 | page = @{ text = 'private-page-body'; errors = @( |
| 38 | @{ type = 'module-load'; source = '/missing.js?session=private-query#private-fragment' }, |
| 39 | @{ type = 'error'; source = '/main.js'; message = 'syntax error'; stack = @('at http://localhost:1234/main.js?session=private-query#private-fragment') } |
| 40 | ) } |
| 41 | } | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath $fixture |
| 42 | $start = [Diagnostics.ProcessStartInfo]::new((Get-Process -Id $PID).Path) |
| 43 | $start.UseShellExecute = $false |
| 44 | foreach ($argument in @('-NoProfile','-NonInteractive','-File',$childScript,$fixture,$report)) { $start.ArgumentList.Add($argument) } |
| 45 | $child = [Diagnostics.Process]::Start($start) |
| 46 | try { |
| 47 | $child.WaitForExit() |
| 48 | Assert-SmokeContract ($child.ExitCode -eq 17) 'Failure fixture did not exit unsuccessfully.' |
| 49 | $failed = $false |
| 50 | $captured = & { |
| 51 | try { Assert-WindowsWorkbenchReport -ExitCode $child.ExitCode -Report $report } |
| 52 | catch { $script:observedFailure = $_.Exception.Message; Write-Output ('Rejected smoke: ' + $_.Exception.Message) } |
| 53 | } 6>&1 |
| 54 | $text = ($captured | ForEach-Object { $_.ToString() }) -join "`n" |
| 55 | Assert-SmokeContract ($observedFailure -like '*loading_workbench*') 'Failure report was not parsed before rejecting its process exit.' |
| 56 | foreach ($expected in @('loading_workbench', 'module-load', '/missing.js', 'syntax error', '"prepareExit":1', '"prepareStatus":"stop_required"')) { |
| 57 | Assert-SmokeContract ($text.Contains($expected)) "Missing safe smoke evidence: $expected" |
| 58 | } |
| 59 | foreach ($private in @('private-token','private-fragment','private-query','private-entry','private-launch','private-page-body')) { |
| 60 | Assert-SmokeContract (-not $text.Contains($private)) "Private smoke field leaked: $private" |
| 61 | } |
| 62 | } finally { $child.Dispose() } |
| 63 | [IO.File]::WriteAllText($report, '{"success":true,"nativeBridgeReady":true}') |
| 64 | Assert-WindowsWorkbenchReport -ExitCode 0 -Report $report |
| 65 | $rejected = $false |
| 66 | try { Assert-WindowsWorkbenchReport -ExitCode 17 -Report $report } catch { $rejected = $true } |
| 67 | Assert-SmokeContract $rejected 'Nonzero child exit was accepted because its report said success.' |
| 68 | Write-Output 'PASS: RUNNER_TEMP/local selection, real failed child with existing diagnostic, safe evidence logging, and success/exit contract.' |
| 69 | } finally { |
| 70 | $env:RUNNER_TEMP = $savedRunnerTemp |
| 71 | Remove-Item -LiteralPath $directory -Recurse |
| 72 | } |