param(
    [string]$TestPath = "e2e/tests/注册登录权限/test_e2e_saas_001_enterprise_certification.py"
)

$ErrorActionPreference = "Stop"

$ProjectRoot = Split-Path -Parent $PSScriptRoot
$Python = Join-Path $ProjectRoot ".venv\Scripts\python.exe"
$ResultsDir = Join-Path $ProjectRoot "reports\allure-results"
$ReportDir = Join-Path $ProjectRoot "reports\allure-report"

if (!(Test-Path $Python)) {
    throw "Python virtual environment not found: $Python. Please run: py -3.11 -m venv .venv"
}

Set-Location $ProjectRoot

& $Python -m pytest -s $TestPath
$pytestExitCode = $LASTEXITCODE

if (Get-Command allure -ErrorAction SilentlyContinue) {
    allure generate $ResultsDir -o $ReportDir --clean
    Write-Host "Allure report generated: $ReportDir"
} else {
    Write-Host "Allure CLI is not installed or not in PATH. Raw results generated: $ResultsDir"
    Write-Host "Install Allure CLI, then run: allure generate reports/allure-results -o reports/allure-report --clean"
}

exit $pytestExitCode
