Tests/Invoke-Gitt.Tests.ps1

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

Describe 'Invoke-Gitt dispatcher' {
    It 'Has a gitt alias' {
        Get-Alias gitt -ErrorAction SilentlyContinue | Should -Not -BeNullOrEmpty
    }

    It 'clone subcommand calls Invoke-GitCloneBare' {
        Mock Invoke-GitCloneBare { } -ModuleName GittHelpers
        Invoke-Gitt clone 'https://example.com/repo.git'
        Should -Invoke Invoke-GitCloneBare -Times 1 -ModuleName GittHelpers
    }

    It 'init subcommand calls Invoke-GitInit' {
        Mock Invoke-GitInit { } -ModuleName GittHelpers
        Invoke-Gitt init 'myrepo'
        Should -Invoke Invoke-GitInit -Times 1 -ModuleName GittHelpers
    }

    It 'worktree add subcommand calls New-GitWorktree' {
        Mock New-GitWorktree { } -ModuleName GittHelpers
        Invoke-Gitt worktree add -Feature 'x'
        Should -Invoke New-GitWorktree -Times 1 -ModuleName GittHelpers
    }

    It 'worktree remove subcommand calls Remove-GitWorktree' {
        Mock Remove-GitWorktree { } -ModuleName GittHelpers
        Invoke-Gitt worktree remove 'C:\some\path'
        Should -Invoke Remove-GitWorktree -Times 1 -ModuleName GittHelpers
    }

    It 'save subcommand calls Save-GitWorktree' {
        Mock Save-GitWorktree { } -ModuleName GittHelpers
        Invoke-Gitt save -Message 'hello'
        Should -Invoke Save-GitWorktree -Times 1 -ModuleName GittHelpers
    }

    It 'sync subcommand calls Sync-GitBranches' {
        Mock Sync-GitBranches { } -ModuleName GittHelpers
        Invoke-Gitt sync
        Should -Invoke Sync-GitBranches -Times 1 -ModuleName GittHelpers
    }

    It 'prune subcommand calls Remove-StaleGitBranches' {
        Mock Remove-StaleGitBranches { } -ModuleName GittHelpers
        Invoke-Gitt prune
        Should -Invoke Remove-StaleGitBranches -Times 1 -ModuleName GittHelpers
    }

    It 'Emits an error for unknown subcommands' {
        { Invoke-Gitt unknowncmd 2>&1 | Out-Null } | Should -Not -Throw
        Invoke-Gitt unknowncmd 2>&1 | Should -Match 'Unknown subcommand'
    }
    It 'Emits an error when worktree has no action' {
        Invoke-Gitt worktree 2>&1 | Should -Match 'Usage'
    }
}

Describe 'Invoke-Gitt ConvertTo-SafeBranchName' {
    It 'Preserves dots for version strings' {
        $result = & (Get-Module GittHelpers) { ConvertTo-SafeBranchName '1.2.3' }
        $result | Should -Be '1.2.3'
    }

    It 'Replaces spaces with hyphens' {
        $result = & (Get-Module GittHelpers) { ConvertTo-SafeBranchName 'my feature name' }
        $result | Should -Be 'my-feature-name'
    }

    It 'Strips leading and trailing hyphens' {
        $result = & (Get-Module GittHelpers) { ConvertTo-SafeBranchName ' --name-- ' }
        $result | Should -Be 'name'
    }

    It 'Throws on empty result' {
        { & (Get-Module GittHelpers) { ConvertTo-SafeBranchName '!!!---!!!' } } | Should -Throw
    }
}