diff --git a/agent/tests/ui-smoke.ps1 b/agent/tests/ui-smoke.ps1 index 83f9eec..833243e 100644 --- a/agent/tests/ui-smoke.ps1 +++ b/agent/tests/ui-smoke.ps1 @@ -13,6 +13,7 @@ param( [ValidateSet('doc', 'home')] [string]$Expect = 'doc', [string]$OutDir = '', + [string]$Node = '', [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 $Fixture)) { throw ('missing fixture: ' + $Fixture) } -$node = Get-Command node -ErrorAction SilentlyContinue -if (-not $node) { $node = 'C:\Program Files\nodejs\node.exe' } -if (-not (Test-Path -LiteralPath $node)) { throw 'Node.js 未找到(需要 Node 21+,用其内置 WebSocket)' } +function Resolve-Node { + $command = Get-Command node -ErrorAction SilentlyContinue + 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 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 ', ') + '),请先关闭它;否则启动会被单实例转发。') } -# 端口探测:CDP 起来之前不要跑断言。 -function Test-Port([int]$port) { - $client = New-Object System.Net.Sockets.TcpClient +# CDP 就绪探测:TCP 端口可能只开一瞬间(WebView2 起来后立刻退出), +# 所以以 /json/list 真正能返回为准。 +function Test-Cdp([int]$port) { try { - $task = $client.ConnectAsync('127.0.0.1', $port) - if (-not $task.Wait(400)) { return $false } - return $client.Connected + $response = Invoke-WebRequest -UseBasicParsing -Uri ('http://127.0.0.1:' + $port + '/json/list') -TimeoutSec 2 + return ($response.StatusCode -eq 200) } catch { return $false - } finally { - $client.Dispose() } } @@ -81,7 +97,7 @@ try { $up = $false while ((Get-Date) -lt $deadline) { if ($proc.HasExited) { break } - if (Test-Port $Port) { $up = $true; break } + if (Test-Cdp $Port) { $up = $true; break } Start-Sleep -Milliseconds 300 } @@ -104,11 +120,17 @@ try { if (Test-Path -LiteralPath $reportPath) { $report = Get-Content -LiteralPath $reportPath -Raw | ConvertFrom-Json $failed = @($report.checks | Where-Object { -not $_.pass }) - if ($failed.Count -gt 0) { - Write-Host 'FAILED checks:' -ForegroundColor Red - $failed | ForEach-Object { Write-Host (' - ' + $_.name + ': ' + $_.detail) -ForegroundColor Red } - } else { + if ($report.ok) { 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) } Write-Host ('report : ' + $reportPath)