Tests/Invoke-GitCloneBare.Tests.ps1

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

Describe 'Invoke-GitCloneBare' {
    BeforeEach {
        # Capture git calls
        $script:GitCalls = [System.Collections.Generic.List[string]]::new()
        Mock git {
            $script:GitCalls.Add($args -join ' ')
            if ($args -contains '--is-bare-repository') { return 'true' }
            if ($args -contains 'rev-parse' -and $args -contains '--git-common-dir') { return '.' }
            return 0
        } -ModuleName GittHelpers
    }

    It 'Calls git clone --bare with the supplied URL' {
        $tmpDir = Join-Path $TestDrive 'clone-test'
        New-Item -ItemType Directory -Path $tmpDir | Out-Null
        Mock git { $script:GitCalls.Add($args -join ' ') } -ModuleName GittHelpers

        { Invoke-GitCloneBare -Url 'https://example.com/repo.git' -Destination $tmpDir -Worktrees @() -WhatIf } | Should -Not -Throw
    }

    It 'Derives repo name from URL without .git suffix' {
        # The bare dir should be <name>.git
        $url      = 'https://example.com/myproject.git'
        $repoName = [System.IO.Path]::GetFileNameWithoutExtension($url)
        $repoName | Should -Be 'myproject'
    }

    It 'Skips safe.directory when -SkipSafeDirectory is set' {
        $tmpDir = Join-Path $TestDrive 'clone-safe'
        New-Item -ItemType Directory -Path $tmpDir | Out-Null

        Mock git { } -ModuleName GittHelpers

        Invoke-GitCloneBare 'https://example.com/r.git' -Destination $tmpDir -Worktrees @() -SkipSafeDirectory -WhatIf
        $script:GitCalls | Where-Object { $_ -match 'safe.directory' } | Should -BeNullOrEmpty
    }
}

Describe 'Invoke-GitCloneBare parameter validation' {
    It 'Throws when URL is missing' {
        { Invoke-GitCloneBare -Url '' } | Should -Throw
    }
}