test: add a CDP-based pre-release UI smoke harness

The DSH sandbox blocks named pipes, so Chromium/WebView2 cannot start inside
it (window renders white, msedgewebview2 exits immediately; Edge headless
fails with a Mojo platform-channel access-denied fatal). UI verification
therefore moves to a script the user runs from a normal desktop shell.

- agent/tests/ui-smoke.ps1: launches the built exe with an isolated config
  and --remote-debugging-port, waits for the port, runs the driver, closes.
- agent/tests/ui-smoke.mjs: connects over CDP, snapshots the page (preview
  content, headings, fold toggles, highlighted code, window.ipc bridge),
  saves a screenshot and a JSON report for the agent to read.
- The driver was validated against a mock CDP server; the real end-to-end run
  needs a desktop shell (sandbox cannot host WebView2).
This commit is contained in:
2026-11-04 15:15:24 +08:00
parent fadba614a8
commit 0b97bcb6dc
5 changed files with 360 additions and 1 deletions

129
agent/tests/ui-smoke.ps1 Normal file
View File

@@ -0,0 +1,129 @@
<#
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 = '',
[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) }
$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)' }
& $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 起来之前不要跑断言。
function Test-Port([int]$port) {
$client = New-Object System.Net.Sockets.TcpClient
try {
$task = $client.ConnectAsync('127.0.0.1', $port)
if (-not $task.Wait(400)) { return $false }
return $client.Connected
} catch {
return $false
} finally {
$client.Dispose()
}
}
$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-Port $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 ($failed.Count -gt 0) {
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
}
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