Scripts/Run-ModuleTests.ps1

<#
.SYNOPSIS
    Quick test runner for Export-ModuleInfoForLLM module
.DESCRIPTION
    Provides a simple interface to run module tests with common scenarios
#>


param(
    [switch]$Full,
    [switch]$Quick,
    [switch]$Cleanup
)

$ErrorActionPreference = "Stop"

# Set paths
$modulePath = Join-Path $PSScriptRoot "Export-ModuleInfoForLLM.psm1"
$testScriptPath = Join-Path $PSScriptRoot "Test-ExportModuleInfo.ps1"
$outputDir = Join-Path $PSScriptRoot "TestOutput"

# Cleanup if requested
if ($Cleanup) {
    Write-Host "Cleaning up test outputs..." -ForegroundColor Yellow
    if (Test-Path $outputDir) {
        Remove-Item $outputDir -Recurse -Force
        Write-Host "Test output directory cleaned." -ForegroundColor Green
    }
}

# Run tests
if ($Quick) {
    Write-Host "Running quick smoke tests..." -ForegroundColor Cyan
    
    # Import module
    Import-Module $modulePath -Force
    
    # Quick test with small module
    try {
        # Temporarily suppress verbose output for cleaner quick test
        $originalVerbosePreference = $VerbosePreference
        $VerbosePreference = 'SilentlyContinue'
        
        # Capture the output and filter for the actual file path
        $output = Export-ModuleCommandsForLLM `
            -ModuleName "Microsoft.PowerShell.Host" `
            -OutputDirectory $outputDir `
            -FileName "QuickTest" `
            -Format "JSON" `
            -MaxConcurrentJobs 2
        
        # Restore verbose preference
        $VerbosePreference = $originalVerbosePreference
        
        # Extract just the file path from the output
        # The function returns the path as the last item
        if ($output -is [array]) {
            $result = $output[-1]
        } else {
            $result = $output
        }
        
        # Validate the path
        if ($result -and (Test-Path $result)) {
            Write-Host "✓ Quick test passed!" -ForegroundColor Green
            Write-Host " Output: $result" -ForegroundColor Gray
            
            # Show basic stats - Fix for PowerShell version compatibility
            $jsonContent = Get-Content $result
            $json = $jsonContent | Out-String | ConvertFrom-Json
            Write-Host " Commands exported: $($json.Commands.Count)" -ForegroundColor Gray
            
            # Display sample command info
            if ($json.Commands.Count -gt 0) {
                Write-Host " Sample command: $($json.Commands[0].Name)" -ForegroundColor Gray
                Write-Host " Module version: $($json.ModuleVersion)" -ForegroundColor Gray
            }
        } else {
            Write-Host "✗ Quick test failed - no valid output file" -ForegroundColor Red
            Write-Host " Raw output: $output" -ForegroundColor Gray
        }
    }
    catch {
        Write-Host "✗ Quick test failed: $_" -ForegroundColor Red
        Write-Host " Error details: $($_.Exception.Message)" -ForegroundColor Gray
    }
    
    Remove-Module Export-ModuleInfoForLLM -ErrorAction SilentlyContinue
}
elseif ($Full) {
    Write-Host "Running full test suite..." -ForegroundColor Cyan
    
    if (Test-Path $testScriptPath) {
        & $testScriptPath -ModulePath $modulePath -TestOutputDir $outputDir -Verbose
    } else {
        Write-Host "Test script not found at: $testScriptPath" -ForegroundColor Red
    }
}
else {
    Write-Host "Module Test Runner" -ForegroundColor Cyan
    Write-Host "=================" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "Usage:" -ForegroundColor Yellow
    Write-Host " .\Run-ModuleTests.ps1 -Quick # Run quick smoke test"
    Write-Host " .\Run-ModuleTests.ps1 -Full # Run full test suite"
    Write-Host " .\Run-ModuleTests.ps1 -Cleanup # Clean test outputs"
    Write-Host ""
    Write-Host "Current module: $modulePath" -ForegroundColor Gray
    
    # Show module info if available
    try {
        Import-Module $modulePath -Force
        $module = Get-Module Export-ModuleInfoForLLM
        Write-Host "Module loaded: v$($module.Version)" -ForegroundColor Green
        Write-Host "Exported functions:" -ForegroundColor Gray
        $module.ExportedFunctions.Keys | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray }
        Remove-Module Export-ModuleInfoForLLM
    }
    catch {
        Write-Host "Module not loadable: $_" -ForegroundColor Red
    }
}