Tests/Unit/Public/Authentication/Close-SCASession.Tests.ps1

Import-Module (Join-Path $PSScriptRoot '../../../../psSCA.psd1') -Force

Describe 'Close-SCASession' {
    InModuleScope psSCA {
        BeforeEach {
            $script:SCASessions = [ordered]@{
                Default    = [pscustomobject]@{ PSTypeName = 'psSCA.Session'; Name = 'Default' }
                Production = [pscustomobject]@{ PSTypeName = 'psSCA.Session'; Name = 'Production' }
            }
            $script:SCACurrentSessionName = 'Default'
        }

        It 'removes the named session from the store' {
            Close-SCASession -Name 'Production' -Confirm:$false
            $script:SCASessions.Contains('Production') | Should -BeFalse
        }

        It 'closes the current default session when -Name is omitted' {
            Close-SCASession -Confirm:$false
            $script:SCASessions.Contains('Default') | Should -BeFalse
        }

        It 'reassigns the current session when the default is closed' {
            Close-SCASession -Name 'Default' -Confirm:$false
            $script:SCACurrentSessionName | Should -Be 'Production'
        }

        It 'clears the current session name when the last session is closed' {
            Close-SCASession -Name 'Default' -Confirm:$false
            Close-SCASession -Name 'Production' -Confirm:$false
            $script:SCACurrentSessionName | Should -BeNullOrEmpty
        }

        It 'does not remove the session when -WhatIf is specified' {
            Close-SCASession -Name 'Production' -WhatIf
            $script:SCASessions.Contains('Production') | Should -BeTrue
        }

        It 'warns and makes no change for a session name that does not exist' {
            Close-SCASession -Name 'Missing' -Confirm:$false -WarningVariable warnOut -WarningAction SilentlyContinue
            $warnOut | Should -Not -BeNullOrEmpty
            $script:SCASessions.Count | Should -Be 2
        }
    }
}