原始资料 · reports/evidence-windows-launcher/windows-smoke-checker.ps1 · 以可阅读文本展示,HTML 与脚本不会在页面中执行。
$ErrorActionPreference='Stop' $project='D:\codex小项目\go\web3' $evidence=Join-Path $project 'docs\reports\evidence-windows-launcher' $testRoot=Join-Path $env:TEMP ('exchange windows smoke '+[Guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $testRoot | Out-Null $listener=New-Object System.Net.Sockets.TcpListener -ArgumentList ([System.Net.IPAddress]::Loopback), 0 $listener.Start();$port=$listener.LocalEndpoint.Port;$listener.Stop() $url="http://127.0.0.1:$port" $launcher=$null $results=@() # 只跟踪并停止本次 CMD 根进程的后代,避免清理用户原有服务。 function Stop-TestTree { param([int]$RootId) $children=@(Get-CimInstance Win32_Process -Filter "ParentProcessId=$RootId") foreach($child in $children){Stop-TestTree -RootId ([int]$child.ProcessId)} Stop-Process -Id $RootId -ErrorAction SilentlyContinue } function Start-TestLauncher { param([int]$Iteration) $command='""'+(Join-Path $project 'run-windows.cmd')+'" -NoBrowser -Port '+$port+' -DataDir "'+(Join-Path $testRoot 'data')+'""' $p=Start-Process cmd.exe -ArgumentList @('/c',$command) -WorkingDirectory $env:TEMP -PassThru -RedirectStandardOutput (Join-Path $evidence "windows-run-$Iteration.txt") -RedirectStandardError (Join-Path $evidence "windows-run-$Iteration-errors.txt") $deadline=[DateTime]::UtcNow.AddSeconds(120) while([DateTime]::UtcNow-lt $deadline){ if($p.HasExited){throw "Launcher failed: $($p.ExitCode)"} try {$req=[System.Net.HttpWebRequest]::Create("$url/health/ready");$req.Proxy=$null;$req.Timeout=1000;$resp=$req.GetResponse();$resp.Close();return $p}catch{} Start-Sleep -Milliseconds 200 } throw 'Windows readiness timeout' } function Assert-True {param([bool]$Condition,[string]$Message) if(-not $Condition){throw $Message};$script:results+=$Message} try{ $launcher=Start-TestLauncher -Iteration 1 $session=New-Object Microsoft.PowerShell.Commands.WebRequestSession $auth=Invoke-RestMethod -Uri "$url/api/v1/auth/login" -Method Post -WebSession $session -ContentType 'application/json' -Body '{"email":"pinkode","password":"2312a132"}' Assert-True ($auth.user.role-eq 'ADMIN'-and $auth.user.email-eq 'pinkode@localhost') 'native Windows alias login returns ADMIN' $headers=@{'X-CSRF-Token'=$auth.csrf_token;'X-Dataset-ID'=$auth.dataset_id;'Idempotency-Key'='windows-smoke-order'} $users=Invoke-RestMethod -Uri "$url/api/v1/admin/users" -WebSession $session Assert-True ($users.items.Count-eq 1) 'ADMIN management authorization' $body='{"market":"ETH-USDT","side":"BUY","type":"LIMIT","time_in_force":"GTC","price":"1","quantity":"1"}' $order=Invoke-RestMethod -Uri "$url/api/v1/orders" -Method Post -Headers $headers -WebSession $session -ContentType 'application/json' -Body $body $replay=Invoke-RestMethod -Uri "$url/api/v1/orders" -Method Post -Headers $headers -WebSession $session -ContentType 'application/json' -Body $body Assert-True ($replay.replayed-and $replay.order.id-eq $order.order.id) 'native Windows original-key confirmation' $balance=Invoke-RestMethod -Uri "$url/api/v1/balances?min_seq=$($replay.processed_seq)" -WebSession $session Assert-True (($balance|Where-Object asset -eq 'USDT').available-eq '99999'-and ($balance|Where-Object asset -eq 'USDT').frozen-eq '1') 'native Windows precise funding and frozen balance' $rejected=$false try{Invoke-RestMethod -Uri "$url/api/v1/auth/register" -Method Post -ContentType 'application/json' -Body '{"email":"short@example.com","password":"2312a132"}'|Out-Null}catch{$rejected=([int]$_.Exception.Response.StatusCode-eq 400)} Assert-True $rejected 'public registration still rejects 8-byte password' $dataset=$auth.dataset_id;$userId=$auth.user.id # 模拟非正常退出后重新双击入口;不将 Stop-Process 说成优雅关闭。 Stop-TestTree -RootId $launcher.Id;$launcher=$null Start-Sleep -Milliseconds 300 $launcher=Start-TestLauncher -Iteration 2 $me=Invoke-RestMethod -Uri "$url/api/v1/auth/me" -WebSession $session Assert-True ($me.user.id-eq $userId-and $me.dataset_id-eq $dataset) 'native Windows restart preserves identity, dataset and session' $after=Invoke-RestMethod -Uri "$url/api/v1/orders" -Method Post -Headers $headers -WebSession $session -ContentType 'application/json' -Body $body Assert-True ($after.replayed-and $after.order.id-eq $order.order.id) 'native Windows restart preserves original-key result' $page=Invoke-RestMethod -Uri "$url/api/v1/orders?min_seq=$($after.processed_seq)" -WebSession $session Assert-True ($page.items.Count-eq 1) 'restart does not duplicate orders' $login=Invoke-RestMethod -Uri "$url/api/v1/auth/login" -Method Post -WebSession $session -ContentType 'application/json' -Body '{"email":"pinkode","password":"2312a132"}' Assert-True ($login.user.id-eq $userId) 'specified password persists after reinitialization' $report=@{checks=$results;data_path=$testRoot;port=$port;windows=[Environment]::OSVersion.VersionString;powershell=$PSVersionTable.PSVersion.ToString();go=(& go.exe version);node=(& node.exe --version);stop_method='forced termination of owned process tree; not graceful Ctrl+C';browser_auto_open_tested=$false} $report|ConvertTo-Json -Depth 8|Set-Content -LiteralPath (Join-Path $evidence 'windows-smoke.json') -Encoding UTF8 $results|ForEach-Object{Write-Host "PASS: $_"} }finally{ if($null-ne $launcher){Stop-TestTree -RootId $launcher.Id} # 仅删除本脚本新建的随机 TEMP 目录,日志在独立 evidence 路径保留。 Remove-Item -LiteralPath $testRoot -Recurse -Force -ErrorAction SilentlyContinue } |