tests/Test-LocalModule.ps1

# Test script for local OfficeScrubC2R module
# Run this from the repository root

Write-Host "`n=== Testing Local OfficeScrubC2R Module ===" -ForegroundColor Cyan

# Remove any installed versions
Write-Host "`n1. Removing PowerShell Gallery version..." -ForegroundColor Yellow
Get-Module OfficeScrubC2R | Remove-Module -Force -ErrorAction SilentlyContinue
if (Get-Module -ListAvailable OfficeScrubC2R) {
    Write-Host " Warning: PSGallery version still installed. Use: Uninstall-Module OfficeScrubC2R" -ForegroundColor Yellow
}

# Import local version
Write-Host "`n2. Importing LOCAL version..." -ForegroundColor Yellow
try {
    Import-Module .\OfficeScrubC2R.psd1 -Force -Verbose -ErrorAction Stop
    Write-Host " SUCCESS: Module imported" -ForegroundColor Green
}
catch {
    Write-Host " FAILED: $_" -ForegroundColor Red
    exit 1
}

# Verify module location
Write-Host "`n3. Verifying module location..." -ForegroundColor Yellow
$module = Get-Module OfficeScrubC2R
Write-Host " Module Path: $($module.Path)" -ForegroundColor Gray
if ($module.Path -like "*Source*OfficeScrubC2R*") {
    Write-Host " SUCCESS: Using local module" -ForegroundColor Green
}
else {
    Write-Host " WARNING: Not using local module!" -ForegroundColor Red
}

# Test exported commands
Write-Host "`n4. Checking exported commands..." -ForegroundColor Yellow
$commands = Get-Command -Module OfficeScrubC2R
Write-Host " Exported commands: $($commands.Count)" -ForegroundColor Gray
$commands.Name | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray }

# Test Initialize-Environment (should already be called)
Write-Host "`n5. Testing Initialize-Environment..." -ForegroundColor Yellow
try {
    Initialize-Environment -ErrorAction Stop
    Write-Host " SUCCESS: Environment initialized" -ForegroundColor Green
}
catch {
    Write-Host " INFO: Already initialized (this is normal): $_" -ForegroundColor Cyan
}

# Test Test-IsC2R (previously failed with null reference)
Write-Host "`n6. Testing Test-IsC2R..." -ForegroundColor Yellow
try {
    $result = Test-IsC2R -Path "C:\Program Files\Microsoft Office\root"
    Write-Host " Result: $result" -ForegroundColor Gray
    Write-Host " SUCCESS: No null reference error" -ForegroundColor Green
}
catch {
    Write-Host " FAILED: $_" -ForegroundColor Red
}

# Test Get-InstalledOfficeProducts
Write-Host "`n7. Testing Get-InstalledOfficeProducts..." -ForegroundColor Yellow
try {
    $products = Get-InstalledOfficeProducts
    Write-Host " Found $($products.Count) product(s)" -ForegroundColor Gray
    $products.GetEnumerator() | ForEach-Object {
        Write-Host " - $($_.Key): $($_.Value)" -ForegroundColor Gray
    }
    Write-Host " SUCCESS: Detection works" -ForegroundColor Green
}
catch {
    Write-Host " FAILED: $_" -ForegroundColor Red
}

# Test Stop-OfficeProcesses
Write-Host "`n8. Testing Stop-OfficeProcesses..." -ForegroundColor Yellow
try {
    Stop-OfficeProcesses
    Write-Host " SUCCESS: Process termination works" -ForegroundColor Green
}
catch {
    Write-Host " FAILED: $_" -ForegroundColor Red
}

# Test Invoke-OfficeScrubC2R -DetectOnly (the actual issue)
Write-Host "`n9. Testing Invoke-OfficeScrubC2R -DetectOnly..." -ForegroundColor Yellow
try {
    # Use -Confirm:$false to avoid the prompt
    Invoke-OfficeScrubC2R -DetectOnly -Confirm:$false -ErrorAction Stop
    Write-Host " SUCCESS: Main function works without path error!" -ForegroundColor Green
}
catch {
    Write-Host " FAILED: $_" -ForegroundColor Red
    Write-Host " Error details: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n=== Test Complete ===" -ForegroundColor Cyan
Write-Host "`nTo use local version in future sessions, run:" -ForegroundColor Yellow
Write-Host " Import-Module .\OfficeScrubC2R.psd1 -Force" -ForegroundColor Cyan
Write-Host "`nTo publish to PowerShell Gallery after testing:" -ForegroundColor Yellow
Write-Host " 1. Update version in OfficeScrubC2R.psd1" -ForegroundColor Cyan
Write-Host " 2. Run: Publish-Module -Path . -NuGetApiKey `$apiKey" -ForegroundColor Cyan