Tests/Private/Config.Tests.ps1

BeforeDiscovery {
    Import-Module (Resolve-Path "$PSScriptRoot\..\..\Posh-YBKPIV.psd1") -Force
}

BeforeAll {
    Import-Module (Resolve-Path "$PSScriptRoot\..\..\Posh-YBKPIV.psd1") -Force
}

AfterAll {
    Remove-Module Posh-YBKPIV -ErrorAction SilentlyContinue
}

InModuleScope Posh-YBKPIV {

    Describe 'Read-PWSHYBKPIVConfigFile' -Tag Unit {
        BeforeEach {
            $script:OriginalConfigPath = $script:ConfigPath
        }

        AfterEach {
            $script:ConfigPath = $script:OriginalConfigPath
            if ($tempPath) { Remove-Item -Path $tempPath -ErrorAction SilentlyContinue }
        }

        It 'Throws when the configuration file does not exist' {
            $script:ConfigPath = Join-Path $env:TEMP "PoshYBKPIV-Missing-$([guid]::NewGuid()).json"
            { Read-PWSHYBKPIVConfigFile } | Should -Throw -ExpectedMessage "*$($script:ConfigPath)*"
        }

        It 'Returns the parsed configuration when the file exists' {
            $testJson = @'
{
  "version": "1.0",
  "installation": { "version": "2.7.3", "architecture": "auto" },
  "Logging": { "Enabled": false }
}
'@

            $tempPath = [IO.Path]::GetTempFileName()
            Set-Content -Path $tempPath -Value $testJson -Encoding UTF8
            $script:ConfigPath = $tempPath

            $result = Read-PWSHYBKPIVConfigFile
            $result.version                    | Should -Be '1.0'
            $result.installation.version        | Should -Be '2.7.3'
            $result.installation.architecture   | Should -Be 'auto'
        }
    }
}