Tests/Remove-GitWorktree.Tests.ps1

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

Describe 'Remove-GitWorktree' {
    BeforeEach {
        $script:FakeWtPath = Join-Path $TestDrive 'feature\my-feature'
        $script:FakeWt = [PSCustomObject]@{
            Path       = $script:FakeWtPath
            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 git              { $global:LASTEXITCODE = 0 }                                   -ModuleName GittHelpers
        Mock Resolve-Path     { [PSCustomObject]@{ Path = $script:FakeWtPath } }             -ModuleName GittHelpers
    }

    It 'Throws when path is not a registered worktree' {
        Mock Get-WorktreeList { return @() } -ModuleName GittHelpers
        { Remove-GitWorktree -Path (Join-Path $TestDrive 'nonexistent') -Confirm:$false } | Should -Throw
    }

    It 'Throws when the worktree is dirty without -Force' {
        Mock git {
            $global:LASTEXITCODE = 0
            if ($args -contains 'status') { return 'M somefile.txt' }
        } -ModuleName GittHelpers

        { Remove-GitWorktree -Path $script:FakeWtPath -Confirm:$false } | Should -Throw
    }

    It 'Does not throw when -Force is used on dirty worktree' {
        Mock git {
            $global:LASTEXITCODE = 0
            if ($args -contains 'status') { return 'M somefile.txt' }
        } -ModuleName GittHelpers

        { Remove-GitWorktree -Path $script:FakeWtPath -Force -Confirm:$false } | Should -Not -Throw
    }

    It 'Refuses to remove the bare worktree entry' {
        $bareEntry = [PSCustomObject]@{ Path = $TestDrive; Branch = $null; IsBare = $true; IsDetached = $false; Head = 'abc' }
        Mock Get-WorktreeList { return @($bareEntry) }                    -ModuleName GittHelpers
        Mock Resolve-Path     { [PSCustomObject]@{ Path = $TestDrive } }  -ModuleName GittHelpers

        { Remove-GitWorktree -Path $TestDrive -Confirm:$false } | Should -Throw
    }
}