$ErrorActionPreference = 'Stop'

$BootstrapRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$SourceZip = Join-Path $BootstrapRoot 'source\qne-node-v00023-windows-ui-source.zip'
$ExtractRoot = Join-Path $env:USERPROFILE 'QNEDEV\qne-node-v00023-windows-ui-source'
$ProjectRoot = Join-Path $ExtractRoot 'qne-node-v00023'
$TestCycle = Join-Path $ProjectRoot 'scripts\windows-test-cycle.ps1'
$Results = Join-Path $BootstrapRoot 'results'

Write-Host 'QNE node v00023 Windows test cycle'
Write-Host "bootstrap_root = $BootstrapRoot"
Write-Host "project_root = $ProjectRoot"
Write-Host "results = $Results"

if (!(Test-Path $SourceZip)) {
  throw "Bundled source zip missing: $SourceZip"
}

if (!(Test-Path $ProjectRoot) -or !(Test-Path $TestCycle)) {
  Remove-Item -Recurse -Force $ExtractRoot -ErrorAction SilentlyContinue
  New-Item -ItemType Directory -Force -Path $ExtractRoot | Out-Null
  Expand-Archive -Force -Path $SourceZip -DestinationPath $ExtractRoot
}

if (!(Test-Path $TestCycle)) {
  throw "test-cycle script missing after extract: $TestCycle"
}

New-Item -ItemType Directory -Force -Path $Results | Out-Null
Set-Location $ProjectRoot
powershell.exe -NoProfile -ExecutionPolicy Bypass -File $TestCycle -Version v00023 -UsbOut $Results
