TestInstallation.ps1

# Test Installation Script for Action.Logging Module
# This script validates the Action.Logging module installation and basic functionality

[CmdletBinding()]
param(
    [Parameter(Mandatory = $false)]
    [switch]$FromLocal,
    
    [Parameter(Mandatory = $false)]
    [switch]$DetailedOutput
)

Write-Host "Action.Logging Module Installation Test" -ForegroundColor Cyan
Write-Host "===========================================" -ForegroundColor Cyan

# Test 1: Module Import
Write-Host "`nTest 1: Module Import" -ForegroundColor Yellow

try {
    if ($FromLocal) {
        Write-Host " Importing from local path..." -ForegroundColor Gray
        Import-Module ".\Action.Logging\Action.Logging.psd1" -Force -ErrorAction Stop
    } else {
        Write-Host " Importing from PowerShell Gallery..." -ForegroundColor Gray
        Import-Module Action.Logging -Force -ErrorAction Stop
    }
    
    $Module = Get-Module Action.Logging
    Write-Host " [PASS] Module imported successfully" -ForegroundColor Green
    Write-Host " Version: $($Module.Version)" -ForegroundColor Gray
    Write-Host " Path: $($Module.Path)" -ForegroundColor Gray
} catch {
    Write-Host " [FAIL] Module import failed: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# Test 2: Function Availability
Write-Host "`nTest 2: Function Availability" -ForegroundColor Yellow

$ExpectedFunctions = @(
    'Write-EnhancedLog',
    'Write-EnhancedLogAsync', 
    'Start-AsyncLogger',
    'Stop-AsyncLogger',
    'Clear-OldLog',
    'Set-LoggingConsoleOutput'
)

$AvailableFunctions = Get-Command -Module Action.Logging
Write-Host " Found $($AvailableFunctions.Count) functions" -ForegroundColor Gray

foreach ($Function in $ExpectedFunctions) {
    if ($Function -in $AvailableFunctions.Name) {
        Write-Host " [PASS] $Function" -ForegroundColor Green
    } else {
        Write-Host " [FAIL] $Function - Missing!" -ForegroundColor Red
    }
}

# Test 3: Basic Logging Functionality
Write-Host "`nTest 3: Basic Logging Functionality" -ForegroundColor Yellow

try {
    # Test basic logging
    Write-EnhancedLog -Message "Test INFO message" -Level INFO
    Write-Host " [PASS] INFO level logging works" -ForegroundColor Green
    
    Write-EnhancedLog -Message "Test WARNING message" -Level WARN
    Write-Host " [PASS] WARN level logging works" -ForegroundColor Green
    
    Write-EnhancedLog -Message "Test ERROR message" -Level ERROR
    Write-Host " [PASS] ERROR level logging works" -ForegroundColor Green
    
    Write-EnhancedLog -Message "Test DEBUG message" -Level DEBUG
    Write-Host " [PASS] DEBUG level logging works" -ForegroundColor Green
    
    Write-EnhancedLog -Message "Test TRACE message" -Level TRACE
    Write-Host " [PASS] TRACE level logging works" -ForegroundColor Green
    
} catch {
    Write-Host " [FAIL] Basic logging failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 4: Console Output Control
Write-Host "`nTest 4: Console Output Control" -ForegroundColor Yellow

try {
    # Test disabling console output
    Set-LoggingConsoleOutput -Enabled $false -WhatIf
    Write-Host " [PASS] Console output control function works" -ForegroundColor Green
    
    # Re-enable console output
    Set-LoggingConsoleOutput -Enabled $true -WhatIf
    Write-Host " [PASS] Console output re-enabled" -ForegroundColor Green
    
} catch {
    Write-Host " [FAIL] Console output control failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 5: Async Logger Functions
Write-Host "`nTest 5: Async Logger Functions" -ForegroundColor Yellow

try {
    # Test starting async logger
    Start-AsyncLogger -WhatIf
    Write-Host " [PASS] Start-AsyncLogger function works" -ForegroundColor Green
    
    # Test stopping async logger
    Stop-AsyncLogger -WhatIf
    Write-Host " [PASS] Stop-AsyncLogger function works" -ForegroundColor Green
    
} catch {
    Write-Host " [FAIL] Async logger functions failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 6: Log File Management
Write-Host "`nTest 6: Log File Management" -ForegroundColor Yellow

try {
    # Test log cleanup function
    Clear-OldLog -LogPath "C:\temp\test" -WhatIf
    Write-Host " [PASS] Clear-OldLog function works" -ForegroundColor Green
    
} catch {
    Write-Host " [FAIL] Log file management failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 7: Error Handling
Write-Host "`nTest 7: Error Handling" -ForegroundColor Yellow

try {
    # Test with invalid log level (should handle gracefully)
    Write-EnhancedLog -Message "Test with default level"
    Write-Host " [PASS] Default level handling works" -ForegroundColor Green
    
} catch {
    Write-Host " [FAIL] Error handling failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Summary
Write-Host "`nTest Summary" -ForegroundColor Cyan
Write-Host "=================" -ForegroundColor Cyan

if ($FromLocal) {
    Write-Host "[PASS] Local module installation test completed successfully!" -ForegroundColor Green
    Write-Host " The module is ready for PowerShell Gallery publication." -ForegroundColor Gray
} else {
    Write-Host "[PASS] PowerShell Gallery module test completed successfully!" -ForegroundColor Green
    Write-Host " The module is working correctly from the gallery." -ForegroundColor Gray
}

Write-Host "`nQuick Usage Example:" -ForegroundColor Cyan
Write-Host @"
   Import-Module Action.Logging
   Write-EnhancedLog -Message "Application started" -Level INFO
   Write-EnhancedLog -Message "Warning: Low disk space" -Level WARN
   Write-EnhancedLog -Message "Critical error occurred" -Level ERROR
"@
 -ForegroundColor Gray

Write-Host "`nReady to use Action.Logging!" -ForegroundColor Green