Functions/Find-StringInMSPCompleteTaskInputs.Tests.ps1

describe "BitTitan.Runbooks.MSPComplete/Find-StringInMSPCompleteTaskInputs" -Tag "module", "unit" {

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

    # Declare external functions and mocks
    function Get-BT_RunbookEnvironment {}
    function Get-BT_TaskInstance {}

    context "when running on the local machine" {
        # Declare mocks
        mock Get-BT_RunbookEnvironment {
            return @{
                IsRunningOnLocalMachine = $true
            }
        }
        mock Get-BT_TaskInstance {}

        it "returns false immediately" {
            # Call the function
            $output = Find-StringInMSPCompleteTaskInputs -SearchString "*"

            # Verify the mocks
            Assert-MockCalled Get-BT_TaskInstance -Times 0 -Exactly -Scope it

            # Verify the output
            $output | Should Be $false
        }
    }

    context "when running on the platform" {
        # Declare mocks
        mock Get-BT_RunbookEnvironment {
            return @{
                IsRunningOnLocalMachine = $false
            }
        }
        mock Get-BT_TaskInstance {
            return @{
                InputVariables = @(
                    @{
                        TaskDataName = "InputVariable"
                    }
                )
            }
        }

        it "returns true if the search string has a case-sensitive match to the inputs" {
            # Declare the inputs
            $Global:InputVariable = "SearchString"

            # Call the function
            $output = Find-StringInMSPCompleteTaskInputs -SearchString "SearchString" -CaseSensitive

            # Verify the output
            $output | Should Be $true
        }

        it "returns true if the search string has a case-insensitive match to the inputs" {
            # Declare the inputs
            $Global:InputVariable = "SearchString"

            # Call the function
            $output = Find-StringInMSPCompleteTaskInputs -SearchString "searchString" -CaseSensitive:$false

            # Verify the output
            $output | Should Be $true
        }

        it "returns false if the search string does not have a case-sensitive match to the inputs" {
            # Declare the inputs
            $Global:InputVariable = "SearchString"

            # Call the function
            $output = Find-StringInMSPCompleteTaskInputs -SearchString "searchString" -CaseSensitive

            # Verify the output
            $output | Should Be $false
        }

        it "returns false if the search string does not have a case-insensitive match to the inputs" {
            # Declare the inputs
            $Global:InputVariable = "SearchString"

            # Call the function
            $output = Find-StringInMSPCompleteTaskInputs -SearchString "searchForString" -CaseSensitive

            # Verify the output
            $output | Should Be $false
        }

        # Declare mocks
        mock Get-BT_TaskInstance {
            throw "up"
        }

        it "returns false if an exception occurs" {
            # Declare the inputs
            $Global:InputVariable = "SearchString"

            # Call the function
            $output = Find-StringInMSPCompleteTaskInputs -SearchString "SearchString" -CaseSensitive

            # Verify the output
            $output | Should Be $false
        }
    }
}