Tests/Unit/Export-ModuleCommandsForLLM.Tests.ps1

#Requires -Module Pester
#Requires -Version 5.1

BeforeAll {
    $modulePath = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
    Import-Module "$modulePath\Export-ModuleInfoForLLM.psd1" -Force
    
    # Create test output directory
    $script:TestOutputDir = Join-Path $TestDrive "TestOutput"
    New-Item -Path $script:TestOutputDir -ItemType Directory -Force | Out-Null
}

Describe 'Export-ModuleCommandsForLLM' {
    Context 'Parameter Validation' {
        It 'Should have mandatory parameters' {
            $command = Get-Command Export-ModuleCommandsForLLM
            $command.Parameters['ModuleName'].Attributes.Mandatory | Should -Contain $true
            $command.Parameters['OutputDirectory'].Attributes.Mandatory | Should -Contain $true
            $command.Parameters['FileName'].Attributes.Mandatory | Should -Contain $true
        }
        
        It 'Should validate Format parameter' {
            { Export-ModuleCommandsForLLM -ModuleName "Test" -OutputDirectory $TestDrive `
                -FileName "Test" -Format "Invalid" -ErrorAction Stop } | Should -Throw
        }
        
        It 'Should validate MaxConcurrentJobs range' {
            $command = Get-Command Export-ModuleCommandsForLLM
            $validateRange = $command.Parameters['MaxConcurrentJobs'].Attributes | 
                Where-Object { $_ -is [System.Management.Automation.ValidateRangeAttribute] }
            $validateRange.MinRange | Should -Be 1
            $validateRange.MaxRange | Should -Be 32
        }
    }
    
    Context 'Functionality' {
        It 'Should export a module to JSON format' {
            $result = Export-ModuleCommandsForLLM -ModuleName "Microsoft.PowerShell.Host" `
                -OutputDirectory $script:TestOutputDir `
                -FileName "Test-Host" `
                -Format "JSON"
            
            $result | Should -Not -BeNullOrEmpty
            Test-Path $result | Should -Be $true
            
            $json = Get-Content $result | ConvertFrom-Json
            $json.ModuleName | Should -Be "Microsoft.PowerShell.Host"
            $json.Commands | Should -Not -BeNullOrEmpty
        }
        
        It 'Should create output directory if it does not exist' {
            $newDir = Join-Path $TestDrive "NewDirectory"
            $result = Export-ModuleCommandsForLLM -ModuleName "Microsoft.PowerShell.Host" `
                -OutputDirectory $newDir `
                -FileName "Test" `
                -Format "JSON"
            
            Test-Path $newDir | Should -Be $true
        }
        
        It 'Should support WhatIf' {
            $result = Export-ModuleCommandsForLLM -ModuleName "Microsoft.PowerShell.Host" `
                -OutputDirectory $script:TestOutputDir `
                -FileName "WhatIfTest" `
                -Format "JSON" `
                -WhatIf
            
            $expectedPath = Join-Path $script:TestOutputDir "WhatIfTest.json"
            Test-Path $expectedPath | Should -Be $false
        }
    }
    
    Context 'Error Handling' {
        It 'Should throw for non-existent module' {
            { Export-ModuleCommandsForLLM -ModuleName "NonExistentModule12345" `
                -OutputDirectory $script:TestOutputDir `
                -FileName "Test" `
                -ErrorAction Stop } | Should -Throw
        }
        
        It 'Should throw for invalid output directory' {
            { Export-ModuleCommandsForLLM -ModuleName "Microsoft.PowerShell.Host" `
                -OutputDirectory "Z:\InvalidPath\DoesNotExist" `
                -FileName "Test" `
                -ErrorAction Stop } | Should -Throw
        }
    }
}