Tests/Save-GitWorktree.Tests.ps1

BeforeAll {
    Import-Module "$PSScriptRoot\..\GittHelpers.psd1" -Force
}

Describe 'Save-GitWorktree' {
    BeforeEach {
        $script:WtPath = $TestDrive
        $script:FakeWt = [PSCustomObject]@{
            Path       = $script:WtPath
            Branch     = 'feature/my-feature'
            IsBare     = $false
            IsDetached = $false
            Head       = 'abc1234'
        }
        Mock Get-GitRoot      { return $TestDrive }           -ModuleName GittHelpers
        Mock Get-WorktreeList { return @($script:FakeWt) }   -ModuleName GittHelpers
        Mock Resolve-Path     { [PSCustomObject]@{ Path = $script:WtPath } } -ModuleName GittHelpers
        Mock git              { $global:LASTEXITCODE = 0 }   -ModuleName GittHelpers
        Mock Set-Location     { }                            -ModuleName GittHelpers
    }

    It 'Requires -Message' {
        { Save-GitWorktree -Message '' } | Should -Throw
    }

    It 'Throws when path is not a worktree' {
        Mock Get-WorktreeList { return @() } -ModuleName GittHelpers
        { Save-GitWorktree -Message 'test' -Path $script:WtPath } | Should -Throw
    }

    It 'Throws when worktree is detached HEAD' {
        $script:FakeWt.Branch = $null
        Mock Get-WorktreeList { return @($script:FakeWt) } -ModuleName GittHelpers
        { Save-GitWorktree -Message 'test' -Path $script:WtPath } | Should -Throw
    }

    It 'Does not push when -NoPush is specified' {
        $script:PushCalled = $false
        Mock git {
            $global:LASTEXITCODE = 0
            if ($args -contains 'push')  { $script:PushCalled = $true }
            if ($args -contains 'diff')  { return 'file.txt' }
        } -ModuleName GittHelpers

        Save-GitWorktree -Message 'test' -NoPush -NoPrune -Path $script:WtPath -Confirm:$false
        $script:PushCalled | Should -BeFalse
    }
}