#Requires -Version 5.1
BeforeAll {
    . (Join-Path $PSScriptRoot '..\collector\Test-SmInput.ps1')
}

Describe 'Test-SmUsername' {
    It 'accepts a simple username'          { (Test-SmUsername 'jamie').Ok | Should -BeTrue }
    It 'rejects empty'                       { (Test-SmUsername '').Ok      | Should -BeFalse }
    It 'rejects reserved name'               { (Test-SmUsername 'Administrator').Ok | Should -BeFalse }
    It 'rejects illegal chars'               { (Test-SmUsername 'a\b').Ok   | Should -BeFalse }
    It 'rejects > 20 chars'                  { (Test-SmUsername ('x'*21)).Ok| Should -BeFalse }
}

Describe 'Test-SmPassword' {
    It 'accepts matching 8+ char password'   { (Test-SmPassword 'Sup3rPass!' 'Sup3rPass!').Ok | Should -BeTrue }
    It 'rejects mismatch'                     { (Test-SmPassword 'a' 'b').Ok | Should -BeFalse }
    It 'rejects < 8 chars'                    { (Test-SmPassword 'short' 'short').Ok | Should -BeFalse }
}

Describe 'Test-SmPin' {
    It 'accepts 6-digit matching pin'        { (Test-SmPin '246810' '246810').Ok | Should -BeTrue }
    It 'rejects < 6 digits'                   { (Test-SmPin '123' '123').Ok | Should -BeFalse }
    It 'rejects non-numeric'                  { (Test-SmPin 'abcdef' 'abcdef').Ok | Should -BeFalse }
    It 'rejects mismatch'                     { (Test-SmPin '246810' '999999').Ok | Should -BeFalse }
}

Describe 'Test-SmComputerName' {
    It 'accepts a valid name'                { (Test-SmComputerName 'SILVER-01').Ok | Should -BeTrue }
    It 'rejects empty'                        { (Test-SmComputerName '').Ok | Should -BeFalse }
    It 'rejects > 15 chars'                   { (Test-SmComputerName ('A'*16)).Ok | Should -BeFalse }
    It 'rejects illegal chars'                { (Test-SmComputerName 'bad name').Ok | Should -BeFalse }
}

Describe 'New-SmAnswerFile' {
    BeforeAll {
        . (Join-Path $PSScriptRoot '..\collector\New-SmAnswerFile.ps1')
        $cfg = @{
            DisplayName  = 'Jamie'; Username = 'jamie'; Password = 'Sup3rPass!'
            ComputerName = 'SILVER-01'
            InputLocale  = '0809:00000809'; SystemLocale = 'en-GB'; UiLanguage = 'en-US'; UserLocale = 'en-GB'
            Flavour      = 'developer'; BitLockerEnable = $true; BitLockerPin = '246810'
        }
        $script:xml = New-SmAnswerFile @cfg
    }
    It 'is valid XML'                         { { [xml]$script:xml } | Should -Not -Throw }
    It 'creates the real account in Administrators' {
        $script:xml | Should -Match '<Name>jamie</Name>'
        $script:xml | Should -Match '<Group>Administrators</Group>'
    }
    It 'does NOT contain sm-bootstrap'        { $script:xml | Should -Not -Match 'sm-bootstrap' }
    It 'sets AutoLogon once as the user'      {
        $script:xml | Should -Match '<LogonCount>1</LogonCount>'
        $script:xml | Should -Match '<Username>jamie</Username>'
    }
    It 'sets the computer name'               { $script:xml | Should -Match '<ComputerName>SILVER-01</ComputerName>' }
    It 'keeps WillWipeDisk for disk 0'        { $script:xml | Should -Match '<WillWipeDisk>true</WillWipeDisk>' }
    It 'preconfig round-trips via chunked FirstLogonCommands' {
        # Gather the echo'd base64 chunks in Order, concatenate, strip whitespace, decode.
        $chunks = [regex]::Matches($script:xml, 'echo ([A-Za-z0-9+/=]+)') | ForEach-Object { $_.Groups[1].Value }
        $b64 = ($chunks -join '')
        $json = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($b64)) | ConvertFrom-Json
        $json.flavour | Should -Be 'developer'
        $json.bitlocker.pin | Should -Be '246810'
    }
    It 'has no specialize pass anymore' { $script:xml | Should -Not -Match 'pass="specialize"' }
    It 'creates the preconfig dir + decodes it at first logon' {
        $script:xml | Should -Match 'ProgramData\\SilverMetal'
        $script:xml | Should -Match 'FromBase64String'
        $script:xml | Should -Match 'preconfig\.json'
    }
    It 'launches the toolbox in FirstLogonCommands' { $script:xml | Should -Match 'SilverOS\.Welcome\.App\.exe' }
}
