#Requires -Version 5.1 param([Parameter(Mandatory)] [string]$Executable) $ErrorActionPreference = 'Stop' if (!$env:REIMS_TMP_DIR) { throw 'Run with node scripts/test.mjs webview2 ' } Add-Type -AssemblyName UIAutomationClient, UIAutomationTypes $labels = Get-Content -Raw -Encoding UTF8 -LiteralPath (Join-Path $PSScriptRoot '../locales/en-US/wizard.json') | ConvertFrom-Json $scratch = $env:REIMS_TMP_DIR $dataRoot = Join-Path $scratch 'data' $location = Join-Path $scratch 'location.json' $start = [Diagnostics.ProcessStartInfo]::new((Get-Item -LiteralPath $Executable).FullName) $start.WorkingDirectory = $scratch $start.UseShellExecute = $false $start.RedirectStandardOutput = $true $start.RedirectStandardError = $true $settings = @{ REIMS_GO_FIRST_RUN = '1' REIMS_GO_LOCATION_FILE = $location REIMS_GO_DATA_ROOT = $dataRoot REIMS_GO_LANG = 'en-US' REIMS_GO_PLUGIN_MARKET_URL = 'off' # Refuse external requests while exercising the offline wizard. HTTP_PROXY = 'http://127.0.0.1:0' HTTPS_PROXY = 'http://127.0.0.1:0' NO_PROXY = '' # The Wails loader uses APPDATA for its default WebView2 profile. APPDATA = (Join-Path $scratch 'roaming') LOCALAPPDATA = (Join-Path $scratch 'local') } New-Item -ItemType Directory -Path $settings.APPDATA, $settings.LOCALAPPDATA | Out-Null foreach ($name in $settings.Keys) { $start.EnvironmentVariables[$name] = $settings[$name] } function Wait-Element { param([string]$Name, [System.Windows.Automation.ControlType]$Type, [int]$Seconds = 10, [switch]$Enabled) $ae = [System.Windows.Automation.AutomationElement] $conditions = @([System.Windows.Automation.PropertyCondition]::new($ae::NameProperty, $Name)) if ($Type) { $conditions += [System.Windows.Automation.PropertyCondition]::new($ae::ControlTypeProperty, $Type) } if ($Enabled) { $conditions += [System.Windows.Automation.PropertyCondition]::new($ae::IsEnabledProperty, $true) } $condition = if ($conditions.Count -eq 1) { $conditions[0] } else { [System.Windows.Automation.AndCondition]::new([System.Windows.Automation.Condition[]]$conditions) } $watch = [Diagnostics.Stopwatch]::StartNew() while ($watch.Elapsed.TotalSeconds -lt $Seconds) { $app.Refresh() if ($app.HasExited) { throw "Reims-Go exited before '$Name' (exit $($app.ExitCode))" } if ($app.MainWindowHandle -ne [IntPtr]::Zero) { $window = $ae::FromHandle($app.MainWindowHandle) $element = $window.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $condition) if ($element) { return $element } } Start-Sleep -Milliseconds 150 } throw "Timed out waiting for '$Name' in Reims-Go (PID $($app.Id))" } $app = [Diagnostics.Process]::Start($start) $stdout = $app.StandardOutput.ReadToEndAsync() $stderr = $app.StandardError.ReadToEndAsync() $passed = $false try { $button = [System.Windows.Automation.ControlType]::Button $invoke = [System.Windows.Automation.InvokePattern]::Pattern $value = [System.Windows.Automation.ValuePattern]::Pattern Wait-Element -Name $labels.setupTitle -Seconds 45 | Out-Null Wait-Element -Name $labels.setupContinue -Type $button -Enabled | Out-Null if (Test-Path -LiteralPath $location) { throw 'Setup persisted a location before confirmation' } $other = Wait-Element -Name $labels.setupOther -Type $button -Enabled $other.GetCurrentPattern($invoke).Invoke() $field = Wait-Element -Name $labels.setupOther -Type ([System.Windows.Automation.ControlType]::Edit) -Enabled $field.GetCurrentPattern($value).SetValue($dataRoot) $next = Wait-Element -Name $labels.setupContinue -Type $button -Enabled $next.GetCurrentPattern($invoke).Invoke() Wait-Element -Name $labels.setupRuntimeTitle | Out-Null foreach ($file in @($location, (Join-Path $dataRoot 'reims-go.json'))) { $saved = Get-Content -Raw -Encoding UTF8 -LiteralPath $file | ConvertFrom-Json if ($saved.data_root -cne $dataRoot) { throw "Unexpected data folder in $file" } } $passed = $true } finally { try { if (!$app.HasExited) { & taskkill.exe /PID $app.Id /T /F | Out-Null if (!$app.WaitForExit(10000)) { throw "Could not stop Reims-Go smoke process $($app.Id)" } } $log = $stdout.GetAwaiter().GetResult() + $stderr.GetAwaiter().GetResult() [IO.File]::WriteAllText((Join-Path $scratch 'app.log'), $log) if (!$passed -and $log) { Write-Host $log } } finally { $app.Dispose() } } Write-Host 'PASS WebView2 first launch, custom folder persisted, runtime setup rendered'