tests/maproom/scripts/Test-S2DFromSyntheticCluster.ps1

<#
.SYNOPSIS
    Manually validates S2DCartographer output rendering against the IIC synthetic cluster fixture.
 
.DESCRIPTION
    Loads tests/maproom/Fixtures/synthetic-cluster.json (generated by New-S2DSyntheticCluster.ps1)
    and validates capacity math, unit conversion, and health check outputs.
    Use for visual inspection and regression testing without needing a live cluster.
 
.PARAMETER Open
    If specified, opens the output folder in Explorer after any file-based outputs are generated.
 
.EXAMPLE
    .\tests\maproom\scripts\Test-S2DFromSyntheticCluster.ps1
 
.NOTES
    Modeled on the AzureLocalRanger Test-RangerFromSyntheticManifest.ps1 pattern.
#>

[CmdletBinding()]
param(
    [switch]$Open
)

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

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

if (-not (Test-Path $fixturePath)) {
    Write-Warning "Synthetic cluster fixture not found at $fixturePath"
    Write-Warning "Run .\tests\maproom\scripts\New-S2DSyntheticCluster.ps1 first."
    exit 1
}

Write-Host ''
Write-Host '======================================================================' -ForegroundColor Cyan
Write-Host ' S2DCartographer — Synthetic Cluster Validation Test ' -ForegroundColor Cyan
Write-Host ' Company: Infinite Improbability Corp (IIC) ' -ForegroundColor Cyan
Write-Host '======================================================================' -ForegroundColor Cyan
Write-Host ''

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

# Load fixture
Write-Host '[2/4] Loading synthetic cluster fixture...' -ForegroundColor DarkGray
$cluster = Get-Content $fixturePath -Raw | ConvertFrom-Json -Depth 20
Write-Host " Cluster : $($cluster.clusterName)" -ForegroundColor Gray
Write-Host " Nodes : $($cluster.nodeCount)" -ForegroundColor Gray
Write-Host " Disks : $($cluster.physicalDisks.Count) total" -ForegroundColor Gray
Write-Host " Raw : $($cluster.capacityWaterfall.Raw.TiB) TiB ($($cluster.capacityWaterfall.Raw.TB) TB)" -ForegroundColor Gray
Write-Host " Usable : $($cluster.capacityWaterfall.Usable.TiB) TiB ($($cluster.capacityWaterfall.Usable.TB) TB)" -ForegroundColor Gray
Write-Host ''

# Validate capacity conversions
Write-Host '[3/4] Validating ConvertTo-S2DCapacity math...' -ForegroundColor DarkGray
$knownPairs = @(
    @{ TB = 0.96;  ExpectedTiB = 0.873 }
    @{ TB = 1.92;  ExpectedTiB = 1.747 }
    @{ TB = 3.84;  ExpectedTiB = 3.493 }
    @{ TB = 7.68;  ExpectedTiB = 6.986 }
    @{ TB = 15.36; ExpectedTiB = 13.97 }
)

$conversionPassed = 0
$conversionFailed = 0
foreach ($pair in $knownPairs) {
    $result = ConvertTo-S2DCapacity -TB $pair.TB
    $diff   = [Math]::Abs($result.TiB - $pair.ExpectedTiB)
    if ($diff -le 0.01) {
        Write-Host " ✅ $($pair.TB) TB → $($result.TiB) TiB (expected ~$($pair.ExpectedTiB))" -ForegroundColor Green
        $conversionPassed++
    } else {
        Write-Host " ❌ $($pair.TB) TB → $($result.TiB) TiB (expected ~$($pair.ExpectedTiB), diff=$diff)" -ForegroundColor Red
        $conversionFailed++
    }
}

# Validate health checks
Write-Host ''
Write-Host '[4/4] Health check summary...' -ForegroundColor DarkGray
foreach ($check in $cluster.healthChecks) {
    $icon = if ($check.Status -eq 'Pass') { '✅' } else { '❌' }
    Write-Host " $icon [$($check.Severity.PadRight(8))] $($check.CheckName.PadRight(22)) — $($check.Details)" -ForegroundColor $(if ($check.Status -eq 'Pass') { 'Green' } else { 'Red' })
}

Write-Host ''
Write-Host '----------------------------------------------------------------------' -ForegroundColor DarkGray
Write-Host " Conversion checks : $conversionPassed passed, $conversionFailed failed" -ForegroundColor $(if ($conversionFailed -eq 0) { 'Green' } else { 'Red' })
Write-Host " Health checks : $(@($cluster.healthChecks | Where-Object Status -eq 'Pass').Count) passed" -ForegroundColor Green
Write-Host '----------------------------------------------------------------------' -ForegroundColor DarkGray
Write-Host ''

if ($conversionFailed -gt 0) {
    Write-Warning "Conversion failures detected. Review capacity math in Modules/Classes/S2DCapacity.ps1"
    exit 1
}

Write-Host "Simulation test complete." -ForegroundColor Green