tests/maproom/scripts/Test-RangerFromSyntheticManifest.ps1

<#
.SYNOPSIS
    Manually validates all Ranger output rendering against the IIC synthetic manifest.

.DESCRIPTION
    Loads tests/maproom/Fixtures/synthetic-manifest.json (generated by New-RangerSyntheticManifest.ps1)
    and runs Export-AzureLocalRangerReport with all output formats. Prints a summary of generated
    reports and diagrams. Use this for visual inspection of rendering quality without needing
    live Azure or WinRM connections.

    Modeled on the Azure Scout Test-ExcelFromDataDump.ps1 pattern.

.PARAMETER OutputPath
    Root path for rendered output. Defaults to a timestamped folder in $env:TEMP.

.PARAMETER Open
    If specified, opens the output folder in Explorer after rendering.

.EXAMPLE
    .\tests\maproom\scripts\Test-RangerFromSyntheticManifest.ps1

.EXAMPLE
    .\tests\maproom\scripts\Test-RangerFromSyntheticManifest.ps1 -OutputPath C:\Temp\ranger-sim-test -Open
#>

[CmdletBinding()]
param(
    [string]$OutputPath = (Join-Path $env:TEMP "ranger-sim-$(Get-Date -Format 'yyyyMMdd-HHmmss')"),
    [switch]$Open
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$repoRoot     = Resolve-Path (Join-Path $PSScriptRoot '..\..\..') | Select-Object -ExpandProperty Path
$manifestPath = Join-Path $repoRoot 'tests\maproom\Fixtures\synthetic-manifest.json'

if (-not (Test-Path $manifestPath)) {
    Write-Warning "Synthetic manifest not found at $manifestPath"
    Write-Warning "Run .\tests\maproom\scripts\New-RangerSyntheticManifest.ps1 first."
    exit 1
}

Write-Host ''
Write-Host '======================================================================' -ForegroundColor Cyan
Write-Host ' Azure Local Ranger — Synthetic Manifest Simulation Test ' -ForegroundColor Cyan
Write-Host ' Company: Infinite Improbability Corp (IIC) ' -ForegroundColor Cyan
Write-Host '======================================================================' -ForegroundColor Cyan
Write-Host ''

# Import module
Write-Host '[1/3] Importing AzureLocalRanger module...' -ForegroundColor DarkGray
Import-Module (Join-Path $repoRoot 'AzureLocalRanger.psd1') -Force

# Load and preview manifest
Write-Host '[2/3] Loading synthetic manifest...' -ForegroundColor DarkGray
$manifest = Get-Content $manifestPath -Raw | ConvertFrom-Json -Depth 20
Write-Host " Cluster: $($manifest.target.clusterName)" -ForegroundColor Gray
Write-Host " Mode: $($manifest.run.mode)" -ForegroundColor Gray
Write-Host " Nodes: $($manifest.target.nodeList -join ', ')" -ForegroundColor Gray
Write-Host " VMs: $($manifest.domains.virtualMachines.summary.totalVms) total ($($manifest.domains.virtualMachines.workloadFamilies | ForEach-Object { "$($_.count) $($_.name)" } | Join-String -Separator ', '))" -ForegroundColor Gray
Write-Host " Findings: $(@($manifest.findings | Where-Object { $_.severity -eq 'warning' }).Count) warnings, $(@($manifest.findings | Where-Object { $_.severity -eq 'informational' }).Count) informationals" -ForegroundColor Gray
Write-Host ''

# Render
Write-Host '[3/3] Rendering reports and diagrams...' -ForegroundColor DarkGray
$result = Export-AzureLocalRangerReport `
    -ManifestPath $manifestPath `
    -OutputPath   $OutputPath `
    -Formats      @('html', 'markdown', 'svg')

# Summary
Write-Host ''
Write-Host '----------------------------------------------------------------------' -ForegroundColor DarkGray
Write-Host ' Render Summary' -ForegroundColor White
Write-Host '----------------------------------------------------------------------' -ForegroundColor DarkGray

$reports  = @(Get-ChildItem -Path (Join-Path $OutputPath 'reports')  -Recurse -File -ErrorAction SilentlyContinue)
$diagrams = @(Get-ChildItem -Path (Join-Path $OutputPath 'diagrams') -Recurse -File -ErrorAction SilentlyContinue)

Write-Host " Reports: $($reports.Count) files" -ForegroundColor Green
foreach ($r in $reports | Sort-Object Name) {
    Write-Host " $($r.Name)" -ForegroundColor Gray
}

Write-Host ''
Write-Host " Diagrams: $($diagrams.Count) files" -ForegroundColor Green
foreach ($d in $diagrams | Sort-Object Name) {
    Write-Host " $($d.Name)" -ForegroundColor Gray
}

$generated = @($result.Artifacts | Where-Object { $_.status -eq 'generated' })
$skipped   = @($result.Artifacts | Where-Object { $_.status -eq 'skipped' })
$errors    = @($result.Artifacts | Where-Object { $_.status -eq 'error' })

Write-Host ''
Write-Host " Artifacts: $($generated.Count) generated, $($skipped.Count) skipped, $($errors.Count) errors" -ForegroundColor $(if ($errors.Count -gt 0) { 'Red' } else { 'Green' })

if ($skipped.Count -gt 0) {
    Write-Host " Skipped diagrams:" -ForegroundColor DarkYellow
    foreach ($s in $skipped) {
        Write-Host " $($s.name)" -ForegroundColor DarkYellow
    }
}

if ($errors.Count -gt 0) {
    Write-Host " ERROR artifacts:" -ForegroundColor Red
    foreach ($e in $errors) {
        Write-Host " $($e.name): $($e.error)" -ForegroundColor Red
    }
}

Write-Host ''
Write-Host " Output: $OutputPath" -ForegroundColor Cyan

if ($Open -and (Test-Path $OutputPath)) {
    Start-Process 'explorer.exe' $OutputPath
}

Write-Host ''