tests/command.tests.ps1


BeforeAll {
    . "$PSScriptRoot\..\modules\command.ps1"
    $env:PWSHRUN_RUNNER = "test"

    function Command-Equals {
        Param(
            [PwshRunCommand]$cmdA,
            [PwshRunCommand]$cmdB
        )
        $cmdB.Cmd | Should -Be $cmdA.Cmd
        $cmdB.IsBlock | Should -Be $cmdA.IsBlock
        $cmdB.Arguments | Should -Be $cmdA.Arguments
        $cmdB.WorkDir | Should -Be $cmdA.WorkDir
        $cmdB.Runner | Should -Be $cmdA.Runner
    }
}

Describe -name "Testing command abstraction" {

    Context "Basic Command" {
        BeforeEach {
            $command = New-PwshRunCommand "git"
        }

        It "basic command is created correctly" {
            $command.Cmd | Should -Be "git"
            $command.IsBlock | Should -Be $false
            $command.Runner | Should -Be "test"
        }

        It "basic command is serialized correctly" {
            $cmd = $command.Serialize() | ConvertFrom-Json -AsHashtable
            $cmd.Cmd | Should -Be "git"
            $cmd.IsBlock | Should -Be $false
            $cmd.Runner | Should -Be "test"
        }

        It "basic command is deserialized correctly" {
            $cmd = [PwshRunCommand]::Deserialize($command.Serialize())
            Command-Equals $command $cmd
        }
    }

    Context "Advanced Command" {

        BeforeEach {
            $command = New-PwshRunCommand "git" -Arguments @("log", "--graph", "--all", "--decorate") -WorkDir $env:TEMP
        }

        It "advanced command is created correctly" {
            $command.Cmd | Should -Be "git"
            $command.Arguments | Should -Be @("log", "--graph", "--all", "--decorate")
            $command.WorkDir | Should -Be $env:TEMP
            $command.IsBlock | Should -Be $false
            $command.Runner | Should -Be "test"
        }

        It "advanced command is serialized correctly" {
            $cmd = $command.Serialize() | ConvertFrom-Json -AsHashtable
            $cmd.Cmd | Should -Be "git"
            $cmd.Arguments | Should -Be @("log", "--graph", "--all", "--decorate")
            $cmd.WorkDir | Should -Be $env:TEMP
            $cmd.IsBlock | Should -Be $false
            $cmd.Runner | Should -Be "test"
        }

        It "advanced command is deserialized correctly" {
            $cmd = [PwshRunCommand]::Deserialize($command.Serialize())
            Command-Equals $command $cmd
        }

    }

    Context "Block Command" {

        BeforeEach {
            $command = New-PwshRunCommand { param([string]$value) return "Value: $value" } -Arguments @("FooBar")
        }

        It "should return the correct string" {
            $result = $command.StartProcess()
            $result | Should -Be "Value: FooBar"
        }

        It "should still return the correct string after serialize/deserialize" {
            
        }

    }
}