fix: resolve node correctly and wait for a real CDP endpoint

The smoke script passed a CommandInfo from Get-Command straight to
Test-Path, so Node was reported missing even when installed. Resolve the
executable path (Get-Command .Path, then the usual install directories) and
add a -Node override.

Wait for /json/list to answer instead of a TCP connect: WebView2 opens the
port briefly and dies under the sandbox, which made the script run the
driver against a dead endpoint. The summary now reports report.errors
instead of printing "OK: 0 checks".
This commit is contained in:
2026-11-04 17:28:37 +08:00
parent 9a568774d8
commit a4616d0228

View File

@@ -13,6 +13,7 @@ param(
[ValidateSet('doc', 'home')] [ValidateSet('doc', 'home')]
[string]$Expect = 'doc', [string]$Expect = 'doc',
[string]$OutDir = '', [string]$OutDir = '',
[string]$Node = '',
[switch]$KeepOpen [switch]$KeepOpen
) )
@@ -26,9 +27,27 @@ if (-not $OutDir) { $OutDir = Join-Path $repoRoot 'target\ui-test' }
if (-not (Test-Path -LiteralPath $Exe)) { throw ('missing exe: ' + $Exe + '(先运行 agent\scripts\build.ps1)') } if (-not (Test-Path -LiteralPath $Exe)) { throw ('missing exe: ' + $Exe + '(先运行 agent\scripts\build.ps1)') }
if (-not (Test-Path -LiteralPath $Fixture)) { throw ('missing fixture: ' + $Fixture) } if (-not (Test-Path -LiteralPath $Fixture)) { throw ('missing fixture: ' + $Fixture) }
$node = Get-Command node -ErrorAction SilentlyContinue function Resolve-Node {
if (-not $node) { $node = 'C:\Program Files\nodejs\node.exe' } $command = Get-Command node -ErrorAction SilentlyContinue
if (-not (Test-Path -LiteralPath $node)) { throw 'Node.js 未找到(需要 Node 21+,用其内置 WebSocket)' } if ($command) {
if ($command.Path) { return $command.Path }
if ($command.Source) { return $command.Source }
}
$candidates = @()
if ($env:ProgramFiles) { $candidates += (Join-Path $env:ProgramFiles 'nodejs\node.exe') }
if (${env:ProgramFiles(x86)}) { $candidates += (Join-Path ${env:ProgramFiles(x86)} 'nodejs\node.exe') }
if ($env:LOCALAPPDATA) { $candidates += (Join-Path $env:LOCALAPPDATA 'Programs\nodejs\node.exe') }
foreach ($candidate in $candidates) {
if ($candidate -and (Test-Path -LiteralPath $candidate)) { return $candidate }
}
return $null
}
if ($Node) { $node = $Node } else { $node = Resolve-Node }
if (-not $node) {
throw 'Node.js 未找到(需要 Node 21+)。用 -Node 指定路径,或安装 Node 后重试。'
}
if (-not (Test-Path -LiteralPath $node)) { throw ('Node 路径无效:' + $node) }
& $node -e "process.exit(('WebSocket' in globalThis) ? 0 : 1)" 2>$null & $node -e "process.exit(('WebSocket' in globalThis) ? 0 : 1)" 2>$null
if ($LASTEXITCODE -ne 0) { throw ('Node 版本太旧:需要 Node 21+ 的内置 WebSocket(当前 ' + (& $node --version) + ')') } if ($LASTEXITCODE -ne 0) { throw ('Node 版本太旧:需要 Node 21+ 的内置 WebSocket(当前 ' + (& $node --version) + ')') }
@@ -52,17 +71,14 @@ if ($running.Count -gt 0) {
throw ('目标 exe 已在运行(PID ' + ($running -join ', ') + '),请先关闭它;否则启动会被单实例转发。') throw ('目标 exe 已在运行(PID ' + ($running -join ', ') + '),请先关闭它;否则启动会被单实例转发。')
} }
# 端口探测:CDP 起来之前不要跑断言。 # CDP 就绪探测:TCP 端口可能只开一瞬间(WebView2 起来后立刻退出),
function Test-Port([int]$port) { # 所以以 /json/list 真正能返回为准。
$client = New-Object System.Net.Sockets.TcpClient function Test-Cdp([int]$port) {
try { try {
$task = $client.ConnectAsync('127.0.0.1', $port) $response = Invoke-WebRequest -UseBasicParsing -Uri ('http://127.0.0.1:' + $port + '/json/list') -TimeoutSec 2
if (-not $task.Wait(400)) { return $false } return ($response.StatusCode -eq 200)
return $client.Connected
} catch { } catch {
return $false return $false
} finally {
$client.Dispose()
} }
} }
@@ -81,7 +97,7 @@ try {
$up = $false $up = $false
while ((Get-Date) -lt $deadline) { while ((Get-Date) -lt $deadline) {
if ($proc.HasExited) { break } if ($proc.HasExited) { break }
if (Test-Port $Port) { $up = $true; break } if (Test-Cdp $Port) { $up = $true; break }
Start-Sleep -Milliseconds 300 Start-Sleep -Milliseconds 300
} }
@@ -104,11 +120,17 @@ try {
if (Test-Path -LiteralPath $reportPath) { if (Test-Path -LiteralPath $reportPath) {
$report = Get-Content -LiteralPath $reportPath -Raw | ConvertFrom-Json $report = Get-Content -LiteralPath $reportPath -Raw | ConvertFrom-Json
$failed = @($report.checks | Where-Object { -not $_.pass }) $failed = @($report.checks | Where-Object { -not $_.pass })
if ($failed.Count -gt 0) { if ($report.ok) {
Write-Host 'FAILED checks:' -ForegroundColor Red
$failed | ForEach-Object { Write-Host (' - ' + $_.name + ': ' + $_.detail) -ForegroundColor Red }
} else {
Write-Host ('OK: ' + @($report.checks).Count + ' checks') -ForegroundColor Green Write-Host ('OK: ' + @($report.checks).Count + ' checks') -ForegroundColor Green
} else {
if ($failed.Count -gt 0) {
Write-Host 'FAILED checks:' -ForegroundColor Red
$failed | ForEach-Object { Write-Host (' - ' + $_.name + ': ' + $_.detail) -ForegroundColor Red }
}
if ($report.errors) {
Write-Host 'errors:' -ForegroundColor Red
$report.errors | ForEach-Object { Write-Host (' - ' + $_) -ForegroundColor Red }
}
} }
if ($report.screenshot) { Write-Host ('screenshot: ' + $report.screenshot) } if ($report.screenshot) { Write-Host ('screenshot: ' + $report.screenshot) }
Write-Host ('report : ' + $reportPath) Write-Host ('report : ' + $reportPath)