functions/common/Test-DynamicsNavModuleCommandAvailability.ps1

function Test-DynamicsNavModuleCommandAvailability {
    <#
    .SYNOPSIS
    Tests whether the expected NAV/BC PowerShell commands are available for a given module type.
 
    .DESCRIPTION
    Checks if the key PowerShell commands provided by the specified Dynamics NAV module (NavModelTools or NavAdminTool) are available in the current session. Returns a hashtable containing available and missing commands.
 
    .PARAMETER PlatformVersion
    The platform version of Dynamics NAV (e.g., "100", "210").
 
    .PARAMETER ModuleType
    Specifies which module to test: either "NavModelTools" or "NavAdminTool".
 
    .OUTPUTS
    Hashtable. Contains ModuleType, PlatformVersion, AvailableCommands, and MissingCommands.
 
    .EXAMPLE
    Test-DynamicsNavModuleCommandAvailability -PlatformVersion "100" -ModuleType "NavModelTools"
 
    Returns a list of available and missing NavModelTools commands for platform version 100.
 
    .NOTES
    - Useful for verifying successful module loading.
    - Commands are tested using Get-Command with silent error handling.
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$PlatformVersion,

        [Parameter(Mandatory = $true)]
        [ValidateSet("NavModelTools", "NavAdminTool")]
        [string]$ModuleType,

        [Parameter(Mandatory = $false)]
        [string]$CommandName
    )

    $expectedCommands = switch ($ModuleType) {
        "NavModelTools" {
            @(
                "Compare-NAVApplicationObject",
                "Export-NAVApplicationObjectLanguage",
                "Get-NAVApplicationObjectProperty",
                "Import-NAVApplicationObjectLanguage",
                "Join-NAVApplicationObjectFile",
                "Join-NAVApplicationObjectLanguageFile",
                "Merge-NAVApplicationObject",
                "New-NAVCrmTable",
                "Remove-NAVApplicationObjectLanguage",
                "Set-NAVApplicationObjectProperty",
                "Split-NAVApplicationObjectFile",
                "Split-NAVApplicationObjectLanguageFile"
            )
        }
        "NavAdminTool" {
            @(
                "Get-NAVServerInstance",
                "Get-NAVServerConfiguration",
                "Set-NAVServerConfiguration",
                "Restart-NAVServerInstance",
                "New-NAVServerUser",
                "Invoke-NAVCodeunit",
                "Get-NAVAppInfo",
                "Get-NAVAppRuntimePackage",
                "Get-NAVAppTableModification",
                "Get-NAVAppTenant",
                "Install-NAVApp",
                "Publish-NAVApp",
                "Repair-NAVApp",
                "Start-NAVAppDataUpgrade",
                "Sync-NAVApp",
                "Uninstall-NAVApp",
                "Unpublish-NAVApp"
            )
        }
    }

    if (-not [string]::IsNullOrWhiteSpace($CommandName)) {
        $commandsToTest = @($CommandName)
    } else {
        $commandsToTest = $expectedCommands
    }

    try {
        $return = @{
            ModuleType = $ModuleType
            PlatformVersion = $PlatformVersion
            AvailableCommands = @()
            MissingCommands = @()
        }

        foreach ($command in $commandsToTest) {
            if (-not (Get-Command -Name $command -ErrorAction SilentlyContinue)) {
                Write-Warning "Command '$command' is not available."
                $return.MissingCommands += $command
            } else {
                Write-Verbose "Command '$command' is available."
                $return.AvailableCommands += $command
            }
        }
    } catch {
        Write-Warning "Error during test: $_"
    }
}