Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

run-windows-packaged-lifecycle-smoke.ps1

218 lines10.3 KB

1param(
2 [Parameter(Mandatory = $true)][string]$ArtifactDir,
3 [Parameter(Mandatory = $true)][string]$Distro,
4 [int]$TimeoutSeconds = 300
5)
6​
7$ErrorActionPreference = "Stop"
8Set-StrictMode -Version Latest
9​
10function Get-FreeTcpPort {
11 $listener = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Loopback, 0)
12 try {
13 $listener.Start()
14 return ([System.Net.IPEndPoint]$listener.LocalEndpoint).Port
15 } finally {
16 $listener.Stop()
17 }
19​
20function Assert-ProcessGone {
21 param([Nullable[int]]$ProcessId, [string]$Label)
22 if ($null -eq $ProcessId -or $ProcessId -le 0) { return }
23 $existing = Get-Process -Id $ProcessId -ErrorAction SilentlyContinue
24 if ($null -ne $existing) {
25 throw "$Label process $ProcessId survived packaged lifecycle shutdown ($($existing.ProcessName))."
26 }
28​
29function Assert-PortBindable {
30 param([int]$Port)
31 $listener = [System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Loopback, $Port)
32 try {
33 $listener.Start()
34 } catch {
35 throw "Windows backend port $Port remained unavailable after packaged lifecycle shutdown: $($_.Exception.Message)"
36 } finally {
37 try { $listener.Stop() } catch {}
38 }
40​
41function Write-SmokeLogs {
42 param([string]$StdoutPath, [string]$StderrPath)
43 if (Test-Path $StdoutPath) {
44 Write-Host "=== packaged smoke stdout ==="
45 Get-Content $StdoutPath
46 }
47 if (Test-Path $StderrPath) {
48 Write-Host "=== packaged smoke stderr ==="
49 Get-Content $StderrPath
50 }
52​
53$resolvedArtifactDir = (Resolve-Path $ArtifactDir).Path
54$artifacts = @(Get-ChildItem -Path $resolvedArtifactDir -Filter "T3-Code-*.exe" -File)
55if ($artifacts.Count -ne 1) {
56 throw "Expected exactly one portable T3 Code executable under '$resolvedArtifactDir', found $($artifacts.Count)."
58​
59$smokeRoot = Join-Path $env:RUNNER_TEMP "T3 Code José QA\packaged lifecycle"
60if (Test-Path $smokeRoot) {
61 Remove-Item -Recurse -Force $smokeRoot
63$artifactRunDir = Join-Path $smokeRoot "artifact üñíçødé path"
64$stateDir = Join-Path $smokeRoot "state home Ω"
65$receiptPath = Join-Path $smokeRoot "receipt lifecycle.json"
66$stdoutPath = Join-Path $smokeRoot "packaged stdout.log"
67$stderrPath = Join-Path $smokeRoot "packaged stderr.log"
68New-Item -ItemType Directory -Force -Path $artifactRunDir, $stateDir | Out-Null
69$executablePath = Join-Path $artifactRunDir $artifacts[0].Name
70Copy-Item -Force $artifacts[0].FullName $executablePath
71​
72$port = Get-FreeTcpPort
73$environmentNames = @(
74 "T3CODE_DESKTOP_PACKAGED_SMOKE",
75 "T3CODE_DESKTOP_PACKAGED_SMOKE_RECEIPT",
76 "T3CODE_DESKTOP_PACKAGED_SMOKE_WSL_DISTRO",
77 "T3CODE_HOME",
78 "T3CODE_PORT",
79 "T3CODE_DISABLE_AUTO_UPDATE"
81$previousEnvironment = @{}
82foreach ($name in $environmentNames) {
83 $previousEnvironment[$name] = [Environment]::GetEnvironmentVariable($name, "Process")
85​
86try {
87 $env:T3CODE_DESKTOP_PACKAGED_SMOKE = "1"
88 $env:T3CODE_DESKTOP_PACKAGED_SMOKE_RECEIPT = $receiptPath
89 $env:T3CODE_DESKTOP_PACKAGED_SMOKE_WSL_DISTRO = $Distro
90 $env:T3CODE_HOME = $stateDir
91 $env:T3CODE_PORT = [string]$port
92 $env:T3CODE_DISABLE_AUTO_UPDATE = "true"
93​
94 Write-Host "Launching packaged artifact from: $executablePath"
95 Write-Host "Smoke state path: $stateDir"
96 Write-Host "Requested Windows backend port: $port"
97 Write-Host "Requested WSL distro: $Distro"
98​
99 $startArgs = @{
100 FilePath = $executablePath
101 WorkingDirectory = $artifactRunDir
102 RedirectStandardOutput = $stdoutPath
103 RedirectStandardError = $stderrPath
104 PassThru = $true
105 }
106 $process = Start-Process @startArgs
107​
108 $deadline = [DateTime]::UtcNow.AddSeconds($TimeoutSeconds)
109 $wrapperExitedAt = $null
110 while (-not (Test-Path $receiptPath)) {
111 $process.Refresh()
112 if ($process.HasExited) {
113 if ($process.ExitCode -ne 0) {
114 Write-SmokeLogs -StdoutPath $stdoutPath -StderrPath $stderrPath
115 throw "Packaged portable wrapper exited with code $($process.ExitCode) before producing a lifecycle receipt."
116 }
117 if ($null -eq $wrapperExitedAt) {
118 # electron-builder's portable wrapper may hand off to the extracted app.
119 # Give the real Electron child a bounded window to publish its receipt.
120 $wrapperExitedAt = [DateTime]::UtcNow
121 } elseif ([DateTime]::UtcNow -ge $wrapperExitedAt.AddSeconds(30)) {
122 Write-SmokeLogs -StdoutPath $stdoutPath -StderrPath $stderrPath
123 throw "Portable wrapper exited successfully, but no packaged lifecycle receipt appeared within 30 seconds."
124 }
125 }
126 if ([DateTime]::UtcNow -ge $deadline) {
127 if (-not $process.HasExited) { & taskkill.exe /PID $process.Id /T /F 2>$null | Out-Null }
128 Write-SmokeLogs -StdoutPath $stdoutPath -StderrPath $stderrPath
129 throw "Timed out after $TimeoutSeconds seconds waiting for packaged lifecycle receipt."
130 }
131 Start-Sleep -Milliseconds 500
132 }
133​
134 $receipt = Get-Content -Raw $receiptPath | ConvertFrom-Json -Depth 20
135 if ($receipt.status -ne "success") { throw "Packaged lifecycle receipt status was '$($receipt.status)'." }
136 if ($receipt.schemaVersion -ne 1) { throw "Unexpected packaged lifecycle receipt schema $($receipt.schemaVersion)." }
137 if ($receipt.app.isPackaged -ne $true) { throw "Smoke did not execute from a packaged Electron artifact." }
138 if ($receipt.app.platform -ne "win32") { throw "Smoke platform was '$($receipt.app.platform)', expected win32." }
139 if ($receipt.requestedWslDistro -ne $Distro) { throw "Receipt WSL distro mismatch." }
140 if ($receipt.windowsBackend.snapshot.ready -ne $true) { throw "Windows backend never reached ready state." }
141 if ($null -eq $receipt.windowsBackend.pid) { throw "Windows backend receipt is missing its PID." }
142 if (-not ([string]$receipt.app.appRoot).EndsWith("app.asar", [StringComparison]::OrdinalIgnoreCase)) { throw "Packaged app root was not app.asar: $($receipt.app.appRoot)" }
143 if ([string]$receipt.app.stateDir -notlike "*state home Ω*") { throw "Packaged state directory did not preserve the Unicode/spaces smoke path." }
144 if ([string]$receipt.windowsBackend.entryPath -notlike "*app.asar*apps*server*dist*bin.mjs") { throw "Windows backend did not launch through app.asar: $($receipt.windowsBackend.entryPath)" }
145 if ($null -eq $receipt.wslBackend) { throw "WSL backend receipt is missing." }
146 if ($receipt.wslBackend.snapshot.ready -ne $true) { throw "WSL backend never reached ready state." }
147 if ($receipt.wslBackend.runningDistro -ne $Distro) { throw "WSL backend ran in '$($receipt.wslBackend.runningDistro)', expected '$Distro'." }
148 if ([string]$receipt.wslBackend.entryPath -notlike "*app.asar.unpacked*apps*server*dist*bin.mjs") { throw "WSL backend did not launch through app.asar.unpacked: $($receipt.wslBackend.entryPath)" }
149 if ([string]::IsNullOrWhiteSpace([string]$receipt.wslBackend.wslNodePath)) { throw "WSL backend did not report the bundled Linux Node path." }
150 if ([string]$receipt.wslBackend.wslNodePath -like "/mnt/*") { throw "WSL backend executed Node from a Windows mount instead of its Linux cache: $($receipt.wslBackend.wslNodePath)" }
151 if ($receipt.restartCycle.windowsBackend.snapshot.ready -ne $true) { throw "Windows backend failed packaged restart verification." }
152 if ($null -eq $receipt.restartCycle.wslBackend -or $receipt.restartCycle.wslBackend.snapshot.ready -ne $true) { throw "WSL backend failed packaged restart verification." }
153 if ($receipt.restartCycle.wslBackend.runningDistro -ne $Distro) { throw "Restarted WSL backend changed distro unexpectedly." }
154​
155 foreach ($entry in @($receipt.afterFirstStop)) {
156 if ($entry.snapshot.ready -eq $true -or $entry.snapshot.desiredRunning -eq $true -or $null -ne $entry.snapshot.activePid) {
157 throw "Backend '$($entry.id)' remained active after first stop: $($entry.snapshot | ConvertTo-Json -Compress)"
158 }
159 }
160 foreach ($entry in @($receipt.afterStop)) {
161 if ($entry.snapshot.ready -eq $true -or $entry.snapshot.desiredRunning -eq $true -or $null -ne $entry.snapshot.activePid) {
162 throw "Backend '$($entry.id)' remained active after stop: $($entry.snapshot | ConvertTo-Json -Compress)"
163 }
164 }
165​
166 # The receipt is written immediately before app.quit(). Use the actual Electron
167 # PID from that receipt as the authority; a portable NSIS wrapper may be a
168 # different process and may already have handed off.
169 $appExitDeadline = [DateTime]::UtcNow.AddSeconds(30)
170 while ($null -ne (Get-Process -Id $receipt.app.pid -ErrorAction SilentlyContinue)) {
171 if ([DateTime]::UtcNow -ge $appExitDeadline) {
172 & taskkill.exe /PID $receipt.app.pid /T /F 2>$null | Out-Null
173 throw "Packaged Electron process $($receipt.app.pid) did not exit within 30 seconds after the lifecycle receipt."
174 }
175 Start-Sleep -Milliseconds 250
176 }
177​
178 $process.Refresh()
179 if (-not $process.HasExited) {
180 if (-not $process.WaitForExit(30000)) {
181 & taskkill.exe /PID $process.Id /T /F 2>$null | Out-Null
182 throw "Packaged portable wrapper did not exit within 30 seconds after Electron shutdown."
183 }
184 }
185 if ($process.ExitCode -ne 0) {
186 Write-SmokeLogs -StdoutPath $stdoutPath -StderrPath $stderrPath
187 throw "Packaged portable wrapper exited with code $($process.ExitCode)."
188 }
189​
190 Assert-ProcessGone -ProcessId $receipt.app.pid -Label "Electron"
191 Assert-ProcessGone -ProcessId $receipt.windowsBackend.pid -Label "Windows backend first cycle"
192 Assert-ProcessGone -ProcessId $receipt.wslBackend.pid -Label "WSL launcher first cycle"
193 Assert-ProcessGone -ProcessId $receipt.restartCycle.windowsBackend.pid -Label "Windows backend restart cycle"
194 Assert-ProcessGone -ProcessId $receipt.restartCycle.wslBackend.pid -Label "WSL launcher restart cycle"
195 Assert-PortBindable -Port ([int]$receipt.windowsBackend.port)
196​
197 $wslUri = [Uri]$receipt.wslBackend.httpBaseUrl
198 $tcp = [System.Net.Sockets.TcpClient]::new()
199 try {
200 $connect = $tcp.ConnectAsync($wslUri.Host, $wslUri.Port)
201 if ($connect.Wait(2000) -and $tcp.Connected) {
202 throw "WSL backend TCP endpoint remained reachable after shutdown: $($receipt.wslBackend.httpBaseUrl)"
203 }
204 } catch {
205 if ($_.Exception.Message -like "WSL backend TCP endpoint remained reachable*") { throw }
206 } finally {
207 $tcp.Dispose()
208 }
209​
210 Write-SmokeLogs -StdoutPath $stdoutPath -StderrPath $stderrPath
211 Write-Host "=== packaged lifecycle receipt ==="
212 Get-Content $receiptPath
213 Write-Host "Packaged Windows + WSL lifecycle smoke passed."
214} finally {
215 foreach ($name in $environmentNames) {
216 [Environment]::SetEnvironmentVariable($name, $previousEnvironment[$name], "Process")
217 }