Tests/Private/Password.Tests.ps1

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

Describe 'Private/Password' -Tag Unit {
    InModuleScope 'Posh-SecretSharing' {
        Describe 'Get-SecretSharingPasswordConstant' {
            It 'returns a 75-character alphabet with no duplicate entries' {
                $constant = Get-SecretSharingPasswordConstant
                $constant.Alphabet.Length | Should -Be 75
                (@($constant.Alphabet) | Select-Object -Unique).Count | Should -Be 75
            }

            It 'excludes characters known to need escaping in shells, URLs, CSV, JSON, or SQL' {
                $constant = Get-SecretSharingPasswordConstant
                $excluded = [byte[]]([System.Text.Encoding]::ASCII.GetBytes('''"`\,;|<>()[]{}/:$?'))
                foreach ($byte in $excluded) {
                    $constant.Alphabet | Should -Not -Contain $byte
                }
            }
        }

        Describe 'Get-SecretSharingRandomAlphabetByte' {
            It 'returns Length bytes, each present in Alphabet' {
                $alphabet = [byte[]](65, 66, 67)
                $result = Get-SecretSharingRandomAlphabetByte -Alphabet $alphabet -Length 50
                $result.Length | Should -Be 50
                foreach ($byte in $result) {
                    $alphabet | Should -Contain $byte
                }
            }

            It 'covers every alphabet entry given enough draws (no entry starved by rejection)' {
                $alphabet = [byte[]](1..7)
                $result = Get-SecretSharingRandomAlphabetByte -Alphabet $alphabet -Length 500
                $seen = @($result | Select-Object -Unique)
                $seen.Count | Should -Be $alphabet.Count
            }

            It 'handles a 256-entry alphabet without ever rejecting' {
                $alphabet = [byte[]](0..255)
                $result = Get-SecretSharingRandomAlphabetByte -Alphabet $alphabet -Length 32
                $result.Length | Should -Be 32
            }
        }
    }
}