Modules/businessdev.ALbuild.Apps/Private/Resolve-BcCoverageData.ps1

function Resolve-BcCoverageData {
    <#
    .SYNOPSIS
        Loads ALbuild coverage data (Summary + Objects) from either an existing coverage-summary.json or raw
        coverage .dat files, so the summary/threshold/delta cmdlets share one input contract.
 
    .PARAMETER SummaryPath
        Path to a coverage-summary.json (or a folder containing one) produced by Convert-BcCodeCoverage.
 
    .PARAMETER CoveragePath
        Folder of raw *.dat files (or a single .dat) -- converted in-memory (no files written).
 
    .PARAMETER WorkspaceRoot
        AL source root, enabling the honest source-based denominator when converting raw .dat.
 
    .PARAMETER DenominatorMode
        Passed through to Convert-BcCodeCoverage when converting raw .dat.
 
    .OUTPUTS
        PSCustomObject: Summary, Objects.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Summary')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory, ParameterSetName = 'Summary')] [string] $SummaryPath,
        [Parameter(Mandatory, ParameterSetName = 'Raw')] [string] $CoveragePath,
        [Parameter(ParameterSetName = 'Raw')] [string] $WorkspaceRoot,
        [Parameter(ParameterSetName = 'Raw')] [ValidateSet('Auto', 'Source', 'CoveredOnly')] [string] $DenominatorMode = 'Auto'
    )

    if ($PSCmdlet.ParameterSetName -eq 'Summary') {
        $file = $SummaryPath
        if (Test-Path -LiteralPath $file -PathType Container) { $file = Join-Path $file 'coverage-summary.json' }
        if (-not (Test-Path -LiteralPath $file)) { throw "No coverage-summary.json found at '$SummaryPath'." }
        $doc = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json
        return [PSCustomObject]@{ Summary = $doc.summary; Objects = @($doc.objects) }
    }

    # Convert raw .dat in-memory to a temp folder (cleaned up), returning the live objects.
    $tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("albuild-covtmp-" + [guid]::NewGuid())
    try {
        $convertArgs = @{ CoveragePath = $CoveragePath; Format = @('ALbuildJson'); OutputFolder = $tmp; DenominatorMode = $DenominatorMode }
        if ($WorkspaceRoot) { $convertArgs['WorkspaceRoot'] = $WorkspaceRoot }
        $r = Convert-BcCodeCoverage @convertArgs
        return [PSCustomObject]@{ Summary = $r.Summary; Objects = @($r.Objects) }
    }
    finally { if (Test-Path $tmp) { Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue } }
}