| 1 | param( |
| 2 | [Parameter(Mandatory)][string]$Terminal, |
| 3 | [Parameter(Mandatory)][string]$Binary, |
| 4 | [Parameter(Mandatory)][string]$Workspace, |
| 5 | [Parameter(Mandatory)][string]$Title, |
| 6 | [Parameter(Mandatory)][string]$Evidence |
| 7 | ) |
| 8 | $ErrorActionPreference = 'Stop' |
| 9 | [IO.File]::WriteAllText([IO.Path]::Combine($Evidence, 'desktop-started.txt'), "startedAt=$([DateTime]::UtcNow.ToString('o'))`npid=$PID`npowershellVersion=$($PSVersionTable.PSVersion)`n") |
| 10 | Write-Output 'desktop stage: loading automation' |
| 11 | Add-Type -AssemblyName UIAutomationClient, UIAutomationTypes, System.Windows.Forms, System.Drawing |
| 12 | Add-Type @' |
| 13 | using System; |
| 14 | using System.Runtime.InteropServices; |
| 15 | public static class ClipboardFixtureWindow { |
| 16 | [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr h); |
| 17 | [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); |
| 18 | [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr h, int command); |
| 19 | } |
| 20 | '@ |
| 21 | $window = $null |
| 22 | $ownedPid = $null |
| 23 | Write-Output 'desktop stage: automation loaded' |
| 24 | function Read-Terminal { |
| 25 | $texts = $window.FindAll([System.Windows.Automation.TreeScope]::Descendants, |
| 26 | [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::IsTextPatternAvailableProperty, $true)) |
| 27 | return (@($texts | ForEach-Object { |
| 28 | $_.GetCurrentPattern([System.Windows.Automation.TextPattern]::Pattern).DocumentRange.GetText(-1) |
| 29 | }) -join "`n") |
| 30 | } |
| 31 | function Wait-For([scriptblock]$Check, [string]$Label) { |
| 32 | $deadline = [DateTime]::UtcNow.AddSeconds(45) |
| 33 | while ([DateTime]::UtcNow -lt $deadline) { |
| 34 | if (Test-Path (Join-Path $Evidence 'cancel')) { throw 'Clipboard check canceled' } |
| 35 | if (& $Check) { return } |
| 36 | Start-Sleep -Milliseconds 200 |
| 37 | } |
| 38 | throw "Deadline waiting for $Label" |
| 39 | } |
| 40 | function Capture([string]$Name) { |
| 41 | (Read-Terminal) | Set-Content -Encoding UTF8 (Join-Path $Evidence "$Name.txt") |
| 42 | $r = $window.Current.BoundingRectangle |
| 43 | if ($r.IsEmpty -or $r.Width -le 0 -or $r.Height -le 0) { throw 'Terminal has no visible window' } |
| 44 | $bitmap = [System.Drawing.Bitmap]::new([int]$r.Width, [int]$r.Height) |
| 45 | $graphics = [System.Drawing.Graphics]::FromImage($bitmap) |
| 46 | try { |
| 47 | $graphics.CopyFromScreen([int]$r.X, [int]$r.Y, 0, 0, $bitmap.Size) |
| 48 | $bitmap.Save((Join-Path $Evidence "$Name.png"), [System.Drawing.Imaging.ImageFormat]::Png) |
| 49 | } finally { $graphics.Dispose(); $bitmap.Dispose() } |
| 50 | } |
| 51 | function Focus-OwnedWindow { |
| 52 | $process = Get-Process -Id $ownedPid |
| 53 | if ($process.Path -ne $Terminal) { throw 'Terminal process identity changed' } |
| 54 | $handle = [IntPtr]$window.Current.NativeWindowHandle |
| 55 | [void][ClipboardFixtureWindow]::ShowWindow($handle, 9) |
| 56 | [void][ClipboardFixtureWindow]::SetForegroundWindow($handle) |
| 57 | Wait-For { [ClipboardFixtureWindow]::GetForegroundWindow() -eq $handle } 'owned Terminal focus' |
| 58 | } |
| 59 | try { |
| 60 | # Reject quotes in the owned fixture arguments. |
| 61 | foreach ($arg in @($Terminal, $Binary, $Workspace, $Title)) { |
| 62 | if ($arg.Contains('"')) { throw 'Unexpected quote in fixture argument' } |
| 63 | } |
| 64 | $arguments = "-w new new-tab --title `"$Title`" --suppressApplicationTitle --startingDirectory `"$Workspace`" `"$Binary`" --workspace `"$Workspace`" --no-project-config --skip-onboarding --fresh" |
| 65 | Write-Output 'desktop stage: starting owned Terminal' |
| 66 | Start-Process -FilePath $Terminal -ArgumentList $arguments | Out-Null |
| 67 | Write-Output 'desktop stage: locating owned Terminal window' |
| 68 | Wait-For { |
| 69 | $windows = [System.Windows.Automation.AutomationElement]::RootElement.FindAll( |
| 70 | [System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition) |
| 71 | foreach ($candidate in $windows) { |
| 72 | if ($candidate.Current.Name.Contains($Title)) { |
| 73 | $process = Get-Process -Id $candidate.Current.ProcessId |
| 74 | if ($process.Path -eq $Terminal) { |
| 75 | $script:window = $candidate; $script:ownedPid = $process.Id; return $true |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return $false |
| 80 | } 'owned Windows Terminal window' |
| 81 | Write-Output 'desktop stage: focusing owned Terminal' |
| 82 | Focus-OwnedWindow |
| 83 | Write-Output 'desktop stage: reading composer accessibility' |
| 84 | Wait-For { (Read-Terminal).Contains('Describe a task') } 'Codewhale composer' |
| 85 | Write-Output 'desktop stage: composer ready' |
| 86 | Capture 'ready' |
| 87 | @{ processId = $ownedPid; readyAt = [DateTime]::UtcNow.ToString('o') } | ConvertTo-Json | |
| 88 | Set-Content -Encoding UTF8 (Join-Path $Evidence 'ready.json') |
| 89 | $payload = [System.IO.File]::ReadAllText((Join-Path $Evidence 'expected.txt'), [System.Text.Encoding]::UTF8) |
| 90 | [System.Windows.Forms.Clipboard]::SetText($payload) |
| 91 | if ([System.Windows.Forms.Clipboard]::GetText() -ne $payload) { throw 'Clipboard readback mismatch' } |
| 92 | Focus-OwnedWindow |
| 93 | [System.Windows.Forms.SendKeys]::SendWait('+{INS}') |
| 94 | # Let every pasted line arrive before stashing the exact composer text. |
| 95 | Start-Sleep -Milliseconds 1800 |
| 96 | Capture 'pasted' |
| 97 | @{ processId = $ownedPid; sessionId = (Get-Process -Id $ownedPid).SessionId; chars = $payload.Length; input = 'Windows clipboard + Shift+Insert' } | |
| 98 | ConvertTo-Json | Set-Content -Encoding UTF8 (Join-Path $Evidence 'pasted.json') |
| 99 | Wait-For { Test-Path (Join-Path $Evidence 'stash') } 'draft stash observation' |
| 100 | Focus-OwnedWindow |
| 101 | [System.Windows.Forms.SendKeys]::SendWait('^g') |
| 102 | Wait-For { (Read-Terminal).Contains('Draft stashed') } 'actual composer stash' |
| 103 | Capture 'completed' |
| 104 | @{ stashKey = 'Ctrl+G'; harnessSubmitKeys = 0; completedAt = [DateTime]::UtcNow.ToString('o') } | ConvertTo-Json | |
| 105 | Set-Content -Encoding UTF8 (Join-Path $Evidence 'completed.json') |
| 106 | } catch { |
| 107 | if ($window) { try { Capture 'failure' } catch { Write-Warning $_ } } |
| 108 | throw |
| 109 | } finally { |
| 110 | # Retire only processes at this fixture's unique portable Terminal path. |
| 111 | $stopped = @() |
| 112 | foreach ($process in @(Get-Process | Where-Object { $_.Path -eq $Terminal })) { |
| 113 | Stop-Process -InputObject $process -Force |
| 114 | if (!$process.WaitForExit(5000)) { throw 'Owned Terminal did not exit' } |
| 115 | $stopped += $process.Id |
| 116 | } |
| 117 | $remaining = @(Get-Process | Where-Object { $_.Path -eq $Terminal }).Count |
| 118 | if ($remaining) { throw 'Owned Terminal is still running' } |
| 119 | @{ terminal = $Terminal; stopped = $stopped; remaining = $remaining } | ConvertTo-Json | |
| 120 | Set-Content -Encoding UTF8 (Join-Path $Evidence 'cleanup.json') |
| 121 | } |
| 122 |