Functions/Import-ExternalModule.Tests.ps1

describe "BitTitan.Runbooks.Modules/Import-ExternalModule" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Import-ExternalModule.ps1"

    # Declare external functions and mocks
    function Get-ChildItem {
        param ($Path, $ErrorAction)
    }
    function Install-Module {
        param ($Name, $RequiredVersion)
    }

    # Declare convenience variables
    $userModulesPath = "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules"

    context "when no version is not currently installed" {
        # Mock Get-ChildItem
        mock Get-ChildItem {}

        # Mock Install-Module
        mock Install-Module {}

        # Mock Import-Module
        mock Import-Module {}

        it "installs the required version of the module and imports it" {
            # Call the function
            Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.5 -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-ChildItem -Times 1 -Exactly -ParameterFilter {
                $Path -eq "$($userModulesPath)\MyExternalModule"
            }
            Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.5"
            }
            Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "$($userModulesPath)\MyExternalModule" -and $RequiredVersion -eq "1.2.3.5"
            }

            # Verify the output
            $errorVariable | Should BeNullOrEmpty
        }

        # Mock Get-ChildItem
        mock Get-ChildItem {}

        it "installs the module and imports it" {
            # Call the function
            Import-ExternalModule MyExternalModule -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-ChildItem -Times 1 -Exactly -ParameterFilter {
                $Path -eq "$($userModulesPath)\MyExternalModule"
            } -Scope it
            Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null
            } -Scope it
            Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "$($userModulesPath)\MyExternalModule" -and $RequiredVersion -eq $null
            } -Scope it

            # Verify the output
            $errorVariable | Should BeNullOrEmpty
        }
    }

    context "when the required version is not currently installed" {
        # Mock Get-ChildItem
        mock Get-ChildItem {
            return @{
                Name = "1.2.3.4"
            }
        }

        # Mock Install-Module
        mock Install-Module {}

        # Mock Import-Module
        mock Import-Module {}

        it "installs the required version of the module and imports it" {
            # Call the function
            Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.5 -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-ChildItem -Times 1 -Exactly -ParameterFilter {
                $Path -eq "$($userModulesPath)\MyExternalModule"
            }
            Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "MyExternalModule" -and $RequiredVersion -eq "1.2.3.5"
            }
            Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "$($userModulesPath)\MyExternalModule" -and $RequiredVersion -eq "1.2.3.5"
            }

            # Verify the output
            $errorVariable | Should BeNullOrEmpty
        }

        # Mock Get-ChildItem
        mock Get-ChildItem {}

        it "installs the module and imports it" {
            # Call the function
            Import-ExternalModule MyExternalModule -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-ChildItem -Times 1 -Exactly -ParameterFilter {
                $Path -eq "$($userModulesPath)\MyExternalModule"
            } -Scope it
            Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "MyExternalModule" -and $RequiredVersion -eq $null
            } -Scope it
            Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "$($userModulesPath)\MyExternalModule" -and $RequiredVersion -eq $null
            } -Scope it

            # Verify the output
            $errorVariable | Should BeNullOrEmpty
        }
    }

    context "when the required version is currently installed" {
        # Mock Get-ChildItem
        mock Get-ChildItem {
            return @{
                Name = "1.2.3.4"
            }
        }

        # Mock Install-Module
        mock Install-Module {}

        # Mock Import-Module
        mock Import-Module {}

        it "imports the required version of the module" {
            # Call the function
            Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.4 -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-ChildItem -Times 1 -Exactly -ParameterFilter {
                $Path -eq "$($userModulesPath)\MyExternalModule"
            } -Scope it
            Assert-MockCalled Install-Module -Times 0 -Exactly -Scope it
            Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "$($userModulesPath)\MyExternalModule" -and $RequiredVersion -eq "1.2.3.4"
            } -Scope it

            # Verify the output
            $errorVariable | Should BeNullOrEmpty
        }

        it "imports the module" {
            # Call the function
            Import-ExternalModule MyExternalModule -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-ChildItem -Times 1 -Exactly -ParameterFilter {
                $Path -eq "$($userModulesPath)\MyExternalModule"
            } -Scope it
            Assert-MockCalled Install-Module -Times 0 -Exactly -Scope it
            Assert-MockCalled Import-Module -Times 1 -Exactly -ParameterFilter {
                $Name -eq "$($userModulesPath)\MyExternalModule" -and $RequiredVersion -eq $null
            } -Scope it

            # Verify the output
            $errorVariable | Should BeNullOrEmpty
        }
    }

    # Declare the functions to throw an exception
    $functionsToThrowExceptions = @(
        "Get-ChildItem",
        "Install-Module",
        "Import-Module"
    )
    foreach ($function in $functionsToThrowExceptions) {
        context "when $($function) throws an exception" {
            # Mock Get-ChildItem
            mock Get-ChildItem {
                return @{
                    Name = "1.2.3.4"
                }
            }

            # Mock Install-Module
            mock Install-Module {}

            # Mock Import-Module
            mock Import-Module {}

            # Mock the function to throw an exception
            mock $function {
                throw "throws exception"
            }

            it "fails to install/import the module and outputs an error" {
                # Call the function
                Import-ExternalModule MyExternalModule -RequiredVersion 1.2.3.5 -ErrorAction SilentlyContinue -ErrorVariable errorVariable

                # Verify the output
                $errorVariable | Should Not BeNullOrEmpty
            }
        }
    }
}