Tests/Enter-GitWorktree.Tests.ps1

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

Describe 'Enter-GitWorktree' {
    BeforeEach {
        $script:WtPath   = Join-Path $TestDrive 'feature\login'
        $script:FakeWt   = [PSCustomObject]@{
            Path       = $script:WtPath
            Branch     = 'feature/you/login'
            IsBare     = $false
            IsDetached = $false
            Head       = 'abc1234'
        }
        $script:BareWt = [PSCustomObject]@{
            Path       = $TestDrive
            Branch     = $null
            IsBare     = $true
            IsDetached = $false
            Head       = 'abc1234'
        }
        Mock Get-GitRoot      { return $TestDrive }                          -ModuleName GittHelpers
        Mock Get-WorktreeList { return @($script:BareWt, $script:FakeWt) }  -ModuleName GittHelpers
        Mock Set-Location     { }                                            -ModuleName GittHelpers
        Mock Open-WorktreeTerminal { }                                       -ModuleName GittHelpers
    }

    It 'Navigates to worktree by exact path' {
        Enter-GitWorktree -Worktree $script:WtPath
        Should -Invoke Set-Location -ModuleName GittHelpers -Times 1 `
            -ParameterFilter { $Path -eq $script:WtPath }
    }

    It 'Navigates to worktree by branch name' {
        Enter-GitWorktree -Worktree 'feature/you/login'
        Should -Invoke Set-Location -ModuleName GittHelpers -Times 1 `
            -ParameterFilter { $Path -eq $script:WtPath }
    }

    It 'Throws when no matching worktree is found' {
        { Enter-GitWorktree -Worktree 'nonexistent' } | Should -Throw
    }

    It 'Does not navigate the bare worktree entry' {
        Mock Get-WorktreeList {
            return @([PSCustomObject]@{ Path = $TestDrive; Branch = $null; IsBare = $true; IsDetached = $false; Head = 'x' })
        } -ModuleName GittHelpers

        { Enter-GitWorktree -Worktree $TestDrive } | Should -Throw
    }

    It 'Opens a new terminal window when -NewWindow is set' {
        Enter-GitWorktree -Worktree 'feature/you/login' -NewWindow
        Should -Invoke Open-WorktreeTerminal -ModuleName GittHelpers -Times 1 `
            -ParameterFilter { $WorktreePath -eq $script:WtPath }
        Should -Invoke Set-Location -ModuleName GittHelpers -Times 0
    }

    It 'Does not open a new window when -NewWindow is not set' {
        Enter-GitWorktree -Worktree 'feature/you/login'
        Should -Invoke Open-WorktreeTerminal -ModuleName GittHelpers -Times 0
        Should -Invoke Set-Location -ModuleName GittHelpers -Times 1
    }
}

Describe 'Invoke-Gitt goto dispatch' {
    BeforeEach {
        Mock Enter-GitWorktree { } -ModuleName GittHelpers
    }

    It 'goto subcommand calls Enter-GitWorktree' {
        Invoke-Gitt goto 'main'
        Should -Invoke Enter-GitWorktree -Times 1 -ModuleName GittHelpers
    }

    It 'goto passes -NewWindow flag through' {
        Invoke-Gitt goto 'main' -NewWindow
        Should -Invoke Enter-GitWorktree -Times 1 -ModuleName GittHelpers
    }
}