Tests/Private/Mount-SmbDrive.Tests.ps1

BeforeAll {
    . "$PSScriptRoot/../../Private/Get-Timestamp.ps1"
    . "$PSScriptRoot/../../Private/Invoke-NetUse.ps1"
    . "$PSScriptRoot/../../Private/Mount-SmbDrive.ps1"
}

Describe 'Mount-SmbDrive' {
    BeforeEach {
        Mock Write-Host {}
    }

    Context 'When mount succeeds' {
        BeforeEach {
            Mock Invoke-NetUse {
                $global:LASTEXITCODE = 0
                'The command completed successfully.'
            }
        }

        It 'Does not throw' {
            { Mount-SmbDrive -AccountName 'teststorage' -AccountKey 'key123==' -ShareName 'data' -DriveLetter 'X' } | Should -Not -Throw
        }

        It 'Writes success confirmation' {
            Mount-SmbDrive -AccountName 'teststorage' -AccountKey 'key123==' -ShareName 'data' -DriveLetter 'X'
            Should -Invoke Write-Host -ParameterFilter { $Object -like "*Mounted*" }
        }
    }

    Context 'When mount fails' {
        BeforeEach {
            Mock Invoke-NetUse {
                $global:LASTEXITCODE = 1
                'System error 53 has occurred.'
            }
        }

        It 'Throws with the UNC path in the error' {
            { Mount-SmbDrive -AccountName 'teststorage' -AccountKey 'key123==' -ShareName 'data' -DriveLetter 'X' } | Should -Throw '*teststorage*'
        }
    }

    Context 'Parameter validation' {
        It 'Rejects multi-character drive letters' {
            { Mount-SmbDrive -AccountName 'test' -AccountKey 'key' -ShareName 'share' -DriveLetter 'XY' } | Should -Throw
        }

        It 'Rejects numeric drive letters' {
            { Mount-SmbDrive -AccountName 'test' -AccountKey 'key' -ShareName 'share' -DriveLetter '1' } | Should -Throw
        }

        It 'Accepts lowercase drive letter' {
            Mock Invoke-NetUse { $global:LASTEXITCODE = 0; 'OK' }
            { Mount-SmbDrive -AccountName 'test' -AccountKey 'key' -ShareName 'share' -DriveLetter 'x' } | Should -Not -Throw
        }
    }
}