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".
151 lines
6.8 KiB
PowerShell
151 lines
6.8 KiB
PowerShell
<#
|
||
MD Preview 发布前 UI 冒烟(需要普通桌面 PowerShell)
|
||
- 通过 WebView2 的 CDP 端口检查页面是否真的渲染,并截图
|
||
- 默认使用仓库里的开发构建(Low 完整性,启动时写不了 HKCU,安全)
|
||
- 不要在 DSH 沙箱里跑:沙箱禁止命名管道,Chromium/WebView2 起不来
|
||
#>
|
||
#requires -Version 5.1
|
||
[CmdletBinding()]
|
||
param(
|
||
[string]$Exe = '',
|
||
[string]$Fixture = '',
|
||
[int]$Port = 9333,
|
||
[ValidateSet('doc', 'home')]
|
||
[string]$Expect = 'doc',
|
||
[string]$OutDir = '',
|
||
[string]$Node = '',
|
||
[switch]$KeepOpen
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
$repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '..\..')).Path
|
||
Set-Location $repoRoot
|
||
|
||
if (-not $Exe) { $Exe = Join-Path $repoRoot 'target\release\md-preview.exe' }
|
||
if (-not $Fixture) { $Fixture = Join-Path $repoRoot 'agent\tests\fixtures\code-samples.md' }
|
||
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) }
|
||
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) + ')') }
|
||
|
||
New-Item -ItemType Directory -Force $OutDir | Out-Null
|
||
$configDir = Join-Path $OutDir 'config'
|
||
$logOut = Join-Path $OutDir 'app.stdout.log'
|
||
$logErr = Join-Path $OutDir 'app.stderr.log'
|
||
$reportPath = Join-Path $OutDir 'ui-smoke.json'
|
||
Remove-Item -Recurse -Force $configDir -ErrorAction SilentlyContinue
|
||
Remove-Item -Force $logOut, $logErr, $reportPath, (Join-Path $OutDir 'ui-smoke.png') -ErrorAction SilentlyContinue
|
||
|
||
# 单实例:如果目标 exe 已经在跑,新的启动会被转发出去,CDP 端口就不会出现。
|
||
$exePath = (Resolve-Path -LiteralPath $Exe).Path
|
||
$running = @()
|
||
foreach ($process in (Get-Process -ErrorAction SilentlyContinue)) {
|
||
$path = $null
|
||
try { $path = $process.Path } catch { }
|
||
if ($path -and ($path -ieq $exePath)) { $running += [string]$process.Id }
|
||
}
|
||
if ($running.Count -gt 0) {
|
||
throw ('目标 exe 已在运行(PID ' + ($running -join ', ') + '),请先关闭它;否则启动会被单实例转发。')
|
||
}
|
||
|
||
# CDP 就绪探测:TCP 端口可能只开一瞬间(WebView2 起来后立刻退出),
|
||
# 所以以 /json/list 真正能返回为准。
|
||
function Test-Cdp([int]$port) {
|
||
try {
|
||
$response = Invoke-WebRequest -UseBasicParsing -Uri ('http://127.0.0.1:' + $port + '/json/list') -TimeoutSec 2
|
||
return ($response.StatusCode -eq 200)
|
||
} catch {
|
||
return $false
|
||
}
|
||
}
|
||
|
||
$previousConfig = $env:MD_PREVIEW_CONFIG_DIR
|
||
$previousArgs = $env:WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS
|
||
$proc = $null
|
||
$exitCode = 1
|
||
try {
|
||
$env:MD_PREVIEW_CONFIG_DIR = $configDir
|
||
$env:WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS = '--remote-debugging-port=' + $Port
|
||
|
||
Write-Host ('==> launching ' + $exePath) -ForegroundColor Cyan
|
||
$proc = Start-Process -FilePath $exePath -ArgumentList @('"' + $Fixture + '"') -PassThru -RedirectStandardOutput $logOut -RedirectStandardError $logErr
|
||
|
||
$deadline = (Get-Date).AddSeconds(25)
|
||
$up = $false
|
||
while ((Get-Date) -lt $deadline) {
|
||
if ($proc.HasExited) { break }
|
||
if (Test-Cdp $Port) { $up = $true; break }
|
||
Start-Sleep -Milliseconds 300
|
||
}
|
||
|
||
if (-not $up) {
|
||
Write-Host 'ERROR: CDP 端口没有起来。' -ForegroundColor Red
|
||
Write-Host ' - 不要在 DSH 沙箱里运行本脚本:沙箱禁止命名管道,Chromium/WebView2 无法启动。'
|
||
Write-Host ' - 确认没有其它 MD Preview 实例占用同一个配置目录。'
|
||
if (Test-Path -LiteralPath $logErr) {
|
||
$err = Get-Content -LiteralPath $logErr -ErrorAction SilentlyContinue | Select-Object -Last 10
|
||
if ($err) { Write-Host ' app.stderr:'; $err | ForEach-Object { Write-Host (' ' + $_) } }
|
||
}
|
||
if ($proc.HasExited) { Write-Host (' app 已退出,exit code ' + $proc.ExitCode) }
|
||
throw 'CDP port did not open'
|
||
}
|
||
|
||
Write-Host ('==> node agent\tests\ui-smoke.mjs --port ' + $Port + ' --expect ' + $Expect) -ForegroundColor Cyan
|
||
& $node (Join-Path $repoRoot 'agent\tests\ui-smoke.mjs') --port $Port --out $OutDir --expect $Expect
|
||
$exitCode = $LASTEXITCODE
|
||
|
||
if (Test-Path -LiteralPath $reportPath) {
|
||
$report = Get-Content -LiteralPath $reportPath -Raw | ConvertFrom-Json
|
||
$failed = @($report.checks | Where-Object { -not $_.pass })
|
||
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)
|
||
}
|
||
} finally {
|
||
if ($proc -and -not $KeepOpen) {
|
||
try {
|
||
if (-not $proc.HasExited) {
|
||
$proc.CloseMainWindow() | Out-Null
|
||
if (-not $proc.WaitForExit(5000)) { $proc.Kill() }
|
||
}
|
||
} catch { }
|
||
}
|
||
if ($null -eq $previousConfig) { Remove-Item Env:\MD_PREVIEW_CONFIG_DIR -ErrorAction SilentlyContinue } else { $env:MD_PREVIEW_CONFIG_DIR = $previousConfig }
|
||
if ($null -eq $previousArgs) { Remove-Item Env:\WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS -ErrorAction SilentlyContinue } else { $env:WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS = $previousArgs }
|
||
}
|
||
|
||
exit $exitCode |