Tests/Private/Config.Tests.ps1

BeforeDiscovery {
    Import-Module (Join-Path $PSScriptRoot '../../Posh-SecretRotation.psd1') -Force
}

Describe 'Private/Config' -Tag Unit {
    InModuleScope 'Posh-SecretRotation' {
        Describe 'Get-SecretRotationConfigPath' {
            It 'returns a path under the user profile ending in Posh-SecretRotation-Config.json' {
                $path = Get-SecretRotationConfigPath
                $path | Should -BeLike "$home*Posh-SecretRotation*"
                $path | Should -BeLike '*Posh-SecretRotation-Config.json'
            }
        }

        Describe 'Read-SecretRotationConfigFile' {
            BeforeAll {
                $script:fixturePath = Join-Path ([System.IO.Path]::GetTempPath()) "SecretRotationConfigTest-$([guid]::NewGuid()).json"
                $script:missingPath = Join-Path ([System.IO.Path]::GetTempPath()) "SecretRotationConfigTest-$([guid]::NewGuid())-missing.json"

                @'
{
    "version": "1.0",
    "targets": { "t1": { "backend": "Custom" } }
}
'@
 | Set-Content -Path $script:fixturePath -Encoding UTF8
            }

            AfterAll {
                Remove-Item -Path $script:fixturePath -Force -ErrorAction SilentlyContinue
            }

            It 'returns a default version/empty-targets object when the file does not exist' {
                $result = Read-SecretRotationConfigFile -Path $script:missingPath
                $result.version | Should -Be '1.0'
                # Not @(...).Count -eq 0: PSObject.Properties.Name on a property-less PSCustomObject
                # returns $null (not an empty array), and @($null) has Count 1, not 0.
                $result.targets.PSObject.Properties.Name | Should -BeNullOrEmpty
            }

            It 'parses an existing JSON file' {
                $result = Read-SecretRotationConfigFile -Path $script:fixturePath
                $result.version | Should -Be '1.0'
                $result.targets.t1.backend | Should -Be 'Custom'
            }
        }

        Describe 'Get-SecretRotationTargetConfig' {
            BeforeAll {
                $script:testConfig = [PSCustomObject]@{
                    targets = [PSCustomObject]@{
                        t1 = [PSCustomObject]@{ backend = 'ActiveDirectory' }
                    }
                }
            }

            It 'returns the named target block' {
                $result = Get-SecretRotationTargetConfig -Config $testConfig -TargetName 't1'
                $result.backend | Should -Be 'ActiveDirectory'
            }

            It 'throws listing available targets when the name is not found' {
                { Get-SecretRotationTargetConfig -Config $testConfig -TargetName 'missing' } |
                    Should -Throw '*t1*'
            }

            It 'throws cleanly (no error about $null) when Config.targets has zero targets' {
                $emptyConfig = [PSCustomObject]@{ targets = [PSCustomObject]@{} }
                { Get-SecretRotationTargetConfig -Config $emptyConfig -TargetName 'anything' } |
                    Should -Throw "*Available targets: *"
            }

            It 'still resolves correctly when Config.targets has exactly one target' {
                # Regression guard: PSObject.Properties.Name collapses to a bare scalar (not a
                # 1-element array) when there is exactly one property - Get-SecretRotationTargetConfig
                # must still find/reject names correctly in that case.
                $singleTargetConfig = [PSCustomObject]@{
                    targets = [PSCustomObject]@{ 'only-target' = [PSCustomObject]@{ backend = 'Custom' } }
                }
                (Get-SecretRotationTargetConfig -Config $singleTargetConfig -TargetName 'only-target').backend | Should -Be 'Custom'
                { Get-SecretRotationTargetConfig -Config $singleTargetConfig -TargetName 'missing' } |
                    Should -Throw '*only-target*'
            }
        }
    }
}