tests/Unit/Show-ConsoleMenu.Tests.ps1

# Unit Tests for Show-ConsoleMenu - HermesConsoleUI v2.1.0

BeforeAll {
    # Import the module
    $modulePath = Join-Path $PSScriptRoot ".." "HermesConsoleUI.psm1"
    Import-Module $modulePath -Force
}

Describe "Show-ConsoleMenu - Parameter Validation" {
    
    It "Should have Header parameter" {
        $cmd = Get-Command Show-ConsoleMenu
        $cmd.Parameters.Keys | Should -Contain "Header"
    }
    
    It "Should have ReturnIndex parameter" {
        $cmd = Get-Command Show-ConsoleMenu
        $cmd.Parameters.Keys | Should -Contain "ReturnIndex"
    }
    
    It "Header parameter should be string type" {
        $cmd = Get-Command Show-ConsoleMenu
        $cmd.Parameters['Header'].ParameterType.Name | Should -Be "String"
    }
    
    It "ReturnIndex parameter should be switch type" {
        $cmd = Get-Command Show-ConsoleMenu
        $cmd.Parameters['ReturnIndex'].ParameterType.Name | Should -Be "SwitchParameter"
    }
}

Describe "Show-ConsoleMenu - Legacy Behavior (Without ReturnIndex)" {
    
    It "Should return count of selectable options" {
        $options = @("Option 1", "Option 2", "Option 3")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 3
    }
    
    It "Should return correct count with headers and separators" {
        $options = @(
            "[HEADER 1]",
            "Option 1",
            "Option 2",
            "---",
            "[HEADER 2]",
            "Option 3"
        )
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 3  # Only 3 selectable options
    }
    
    It "Should return 0 for empty options array" {
        $options = @()
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 0
    }
    
    It "Should return 0 when all options are headers/separators" {
        $options = @("[HEADER]", "---", "[ANOTHER HEADER]")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 0
    }
}

Describe "Show-ConsoleMenu - Cassiel Compatibility (With ReturnIndex)" {
    
    BeforeAll {
        # Mock Read-ConsoleChoice to simulate user input
        Mock Read-ConsoleChoice {
            param($MaxChoice, $Prompt, $AllowZero)
            return 2  # Simulate user selecting option 2
        } -ModuleName HermesConsoleUI
    }
    
    It "Should call Read-ConsoleChoice when ReturnIndex is present" {
        $options = @("Option 1", "Option 2", "Option 3")
        Show-ConsoleMenu -Title "Test" -Options $options -ReturnIndex -NoClear
        
        Should -Invoke Read-ConsoleChoice -ModuleName HermesConsoleUI -Times 1
    }
    
    It "Should return user's selected index" {
        $options = @("Option 1", "Option 2", "Option 3")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -ReturnIndex -NoClear
        $result | Should -Be 2
    }
    
    It "Should handle exit selection (0)" {
        Mock Read-ConsoleChoice {
            return 0  # User selects exit
        } -ModuleName HermesConsoleUI
        
        $options = @("Option 1", "Option 2")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -ReturnIndex -NoClear
        $result | Should -Be 0
    }
}

Describe "Show-ConsoleMenu - Header Parameter" {
    
    It "Should accept Header parameter" {
        $options = @("Option 1", "Option 2")
        { Show-ConsoleMenu -Title "Test" -Options $options -Header "Select an option:" -NoClear } | Should -Not -Throw
    }
    
    It "Should work without Header parameter" {
        $options = @("Option 1", "Option 2")
        { Show-ConsoleMenu -Title "Test" -Options $options -NoClear } | Should -Not -Throw
    }
    
    It "Should work with empty Header string" {
        $options = @("Option 1", "Option 2")
        { Show-ConsoleMenu -Title "Test" -Options $options -Header "" -NoClear } | Should -Not -Throw
    }
}

Describe "Show-ConsoleMenu - Non-Selectable Items" {
    
    It "Should not count headers starting with '[' as selectable" {
        $options = @("[CATEGORY]", "Option 1", "Option 2")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 2
    }
    
    It "Should not count separators '---' as selectable" {
        $options = @("Option 1", "---", "Option 2")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 2
    }
    
    It "Should handle multiple headers and separators" {
        $options = @(
            "[SECTION 1]",
            "Option 1",
            "Option 2",
            "---",
            "[SECTION 2]",
            "Option 3",
            "---",
            "[SECTION 3]",
            "Option 4"
        )
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 4
    }
}

Describe "Show-ConsoleMenu - Color Tags" {
    
    It "Should handle color tags in options" {
        $options = @("{Red}Danger", "{Green}Safe", "Normal")
        { Show-ConsoleMenu -Title "Test" -Options $options -NoClear } | Should -Not -Throw
    }
    
    It "Should count color-tagged options as selectable" {
        $options = @("{Red}Option 1", "{Green}Option 2")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -Be 2
    }
}

Describe "Show-ConsoleMenu - Backward Compatibility" {
    
    It "Should work with minimal parameters (legacy usage)" {
        $options = @("Option 1", "Option 2")
        { Show-ConsoleMenu -Title "Test" -Options $options } | Should -Not -Throw
    }
    
    It "Should work with all legacy parameters" {
        $options = @("Option 1", "Option 2")
        { Show-ConsoleMenu -Title "Test" -Options $options -Color "Cyan" -OptionColor "Gray" -Footer "Footer text" -NoClear } | Should -Not -Throw
    }
    
    It "Should maintain legacy return behavior without ReturnIndex" {
        $options = @("A", "B", "C")
        $result = Show-ConsoleMenu -Title "Test" -Options $options -NoClear
        $result | Should -BeOfType [Int32]
        $result | Should -Be 3
    }
}

AfterAll {
    Remove-Module HermesConsoleUI -Force -ErrorAction SilentlyContinue
}