Functions/Invoke-ScriptBlock.Tests.ps1

describe "BitTitan.Runbooks.Common/Invoke-ScriptBlock" -Tag "module", "unit" {

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

    it "does not output any errors when invoking a script block with no output when no output is expected" {
        # Call the function
        $result = Invoke-ScriptBlock "generate no output" { }

        # Verify the output
        $result.Output | Should Be $null
        $result.ErrorMessage | Should Be $null
    }

    it "generates an error when invoking a script block that throws an exception when no output is expected" {
        # Call the function
        $result = Invoke-ScriptBlock "generate no output" { throw "exceptionMessage" } -ErrorAction SilentlyContinue

        # Verify the output
        $result.Output | Should Be $null
        $result.ErrorMessage | Should BeLike "Exception during 'generate no output'.*exceptionMessage*"
    }

    it "does not output any errors when invoking a script block with output when output is expected" {
        # Call the function
        $result = Invoke-ScriptBlock "retrieve test" { "test" } -ShouldHaveOutput

        # Verify the output
        $result.Output | Should Be "test"
        $result.ErrorMessage | Should Be $null
    }

    it "generates an error when invoking a script block which throws an exception when output is expected" {
        # Call the function
        $result = Invoke-ScriptBlock "retrieve test" { throw "exceptionMessage" } -ShouldHaveOutput -ErrorAction SilentlyContinue

        # Verify the output
        $result.Output | Should Be $null
        $result.ErrorMessage | Should BeLike "Exception during 'retrieve test'.*exceptionMessage*"
    }

    it "generates an error when invoking a script block with no output when output is expected" {
        # Call the function
        $result = Invoke-ScriptBlock "retrieve test" { } -ShouldHaveOutput -ErrorAction SilentlyContinue

        # Verify the output
        $result.Output | Should Be $null
        $result.ErrorMessage | Should Be "Failed to 'retrieve test' - no output when an output is expected."
    }

    it "does not output any errors when invoking a script block with the expected output" {
        # Call the function
        $result = Invoke-ScriptBlock "retrieve test" { "test" } -ExpectedOutput "test"

        # Verify the output
        $result.Output | Should Be "test"
        $result.ErrorMessage | Should Be $null
    }

    it "generates an error when invoking a script block which throws an exception instead of the expected output" {
        # Call the function
        $result = Invoke-ScriptBlock "retrieve test" { throw "exceptionMessage" } -ExpectedOutput "test" -ErrorAction SilentlyContinue

        # Verify the output
        $result.Output | Should Be $null
        $result.ErrorMessage | Should BeLike "Exception during 'retrieve test'.*exceptionMessage*"
    }

    it "generates an error when invoking a script block with output which is not the expected output" {
        # Call the function
        $result = Invoke-ScriptBlock "retrieve test" { "testing" } -ExpectedOutput "test" -ErrorAction SilentlyContinue

        # Verify the output
        $result.Output | Should Be "testing"
        $result.ErrorMessage | Should Be "Failed to 'retrieve test' - the output does not match the expected output."
    }
}