Tests/Invoke-IntuneWin32AppTests.ps1

# IntuneWin32App Module - Test Runner
# This script runs the comprehensive test suite for the IntuneWin32App module
# DO NOT RUN IN PRODUCTION

param(
    [switch]$ExportResults,      # Export detailed test results to JSON
    [string]$OutputPath = "C:\IntuneWin32App\Output",
    [switch]$Verbose             # Enable verbose output for detailed diagnostics
)

#region Configuration
$TestConfig = @{
    TenantID = "cec1aa3f-dff2-48dd-8ddb-7c83e39f4547"
    ClientID = "d11ae3e7-b1aa-4b05-b769-4c6113b5263b"
    OutputPath = $OutputPath
    ModulePath = Split-Path $PSScriptRoot -Parent
}

$ComprehensiveTest = @{
    Name = "Comprehensive Function Test Suite"
    Script = "Test-ComprehensiveFunctionality.ps1"
    Description = "Complete test of all 38 public functions through lifecycle workflow"
}

$OverallResults = @{
    TestName = $ComprehensiveTest.Name
    StartTime = Get-Date
    EndTime = $null
    Duration = $null
    ExitCode = 0
}
#endregion

#region Helper Functions
function Write-TestHeader {
    param([string]$Title, [string]$Description = "")
    
    Write-Host ""
    Write-Host ("=" * 80) -ForegroundColor Cyan
    Write-Host $Title -ForegroundColor Cyan
    if ($Description) {
        Write-Host $Description -ForegroundColor Gray
    }
    Write-Host ("=" * 80) -ForegroundColor Cyan
}
#endregion

#region Main Execution
# Reimport the module to ensure latest changes are loaded
$ModulePsdPath = Join-Path $TestConfig.ModulePath "IntuneWin32App.psd1"
if (Test-Path $ModulePsdPath) {
    Import-Module $ModulePsdPath -Force -ErrorAction Stop
    Write-Host "Module loaded from: $ModulePsdPath" -ForegroundColor Cyan
}
else {
    Write-Error "Module manifest not found: $ModulePsdPath"
    exit 1
}

# Set verbose preference if requested
if ($Verbose) {
    $VerbosePreference = 'Continue'
}

Write-TestHeader -Title "IntuneWin32App Module Test Runner" -Description $ComprehensiveTest.Description

Write-Host "Configuration:" -ForegroundColor White
Write-Host " Output Path: $OutputPath" -ForegroundColor Gray
Write-Host " Export Results: $ExportResults" -ForegroundColor Gray
Write-Host " Verbose: $Verbose" -ForegroundColor Gray
Write-Host ""

# Validate output path
if (-not (Test-Path $OutputPath)) {
    try {
        New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null
        Write-Host "Created output directory: $OutputPath" -ForegroundColor Green
    }
    catch {
        Write-Warning "Could not create output directory: $OutputPath"
        $TestConfig.OutputPath = $env:TEMP
    }
}

# Run the comprehensive test
$TestScriptPath = Join-Path $PSScriptRoot $ComprehensiveTest.Script
if (-not (Test-Path $TestScriptPath)) {
    Write-Error "Test script not found: $TestScriptPath"
    exit 1
}

Write-Host "Starting comprehensive test suite..." -ForegroundColor Yellow
Write-Host ""

try {
    # Execute the test script
    & $TestScriptPath
    $OverallResults.ExitCode = $LASTEXITCODE
}
catch {
    Write-Error "Test execution failed: $_"
    $OverallResults.ExitCode = 1
}

$OverallResults.EndTime = Get-Date
$OverallResults.Duration = $OverallResults.EndTime - $OverallResults.StartTime
#endregion

#region Results Summary
Write-Host ""
Write-TestHeader -Title "TEST RUNNER SUMMARY"

Write-Host "Execution Details:" -ForegroundColor White
Write-Host " Test Suite: $($ComprehensiveTest.Name)" -ForegroundColor Gray
Write-Host " Total Duration: $($OverallResults.Duration.ToString('hh\:mm\:ss'))" -ForegroundColor Gray
Write-Host " Exit Code: $($OverallResults.ExitCode)" -ForegroundColor $(
    if ($OverallResults.ExitCode -eq 0) { "Green" } else { "Red" }
)

# Export runner results if requested
if ($ExportResults) {
    try {
        $ResultsFileName = "TestRunner_Summary_$(Get-Date -Format 'yyyyMMdd_HHmmss').json"
        $ResultsPath = Join-Path $TestConfig.OutputPath $ResultsFileName
        
        $ExportData = @{
            TestConfiguration = $TestConfig
            TestSuite = $ComprehensiveTest
            ExecutionResults = $OverallResults
            Environment = @{
                PowerShellVersion = $PSVersionTable.PSVersion.ToString()
                OS = $PSVersionTable.OS
                WindowsTerminal = ($env:WT_SESSION -ne $null)
                VSCode = ($env:VSCODE_PID -ne $null)
            }
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }
        
        $ExportData | ConvertTo-Json -Depth 10 | Out-File -FilePath $ResultsPath -Encoding UTF8
        Write-Host ""
        Write-Host "Test runner summary exported to: $ResultsPath" -ForegroundColor Cyan
    }
    catch {
        Write-Warning "Failed to export test runner summary: $_"
    }
}
#endregion

#region Exit Handling
Write-Host ""
if ($OverallResults.ExitCode -eq 0) {
    Write-Host "[SUCCESS] Comprehensive test suite completed successfully" -ForegroundColor Green
    exit 0
}
else {
    Write-Host "[FAILED] Comprehensive test suite failed - Review results above" -ForegroundColor Red
    exit 1
}
#endregion