Tests/Sync-GitBranches.Tests.ps1

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

Describe 'Sync-GitBranches' {
    BeforeEach {
        Mock Get-GitRoot      { return $TestDrive } -ModuleName GittHelpers
        Mock Get-WorktreeList { return @() }        -ModuleName GittHelpers
        Mock git              { $global:LASTEXITCODE = 0 } -ModuleName GittHelpers
    }

    It 'Does not throw in a clean repo with no branches' {
        Mock git {
            $global:LASTEXITCODE = 0
            if ($args -contains 'branch') { return @() }
        } -ModuleName GittHelpers

        { Sync-GitBranches -Confirm:$false } | Should -Not -Throw
    }

    It 'Warns and skips diverged branches' {
        Mock git {
            $global:LASTEXITCODE = 0
            if ($args -contains 'merge-base')  { return 'aaa' }
            if ($args -contains 'rev-parse' -and ($args -join ' ') -match 'origin') { return 'bbb' }
            if ($args -contains 'rev-parse')   { return 'ccc' }
            if ($args -contains 'branch' -and ($args -join ' ') -match 'format') {
                return 'my-branch origin/my-branch'
            }
        } -ModuleName GittHelpers

        { Sync-GitBranches -Confirm:$false -WarningAction SilentlyContinue } | Should -Not -Throw
    }

    It 'Skips already up-to-date branches' {
        $sha = 'abc123'
        Mock git {
            $global:LASTEXITCODE = 0
            if ($args -contains 'merge-base') { return $sha }
            if ($args -contains 'rev-parse')  { return $sha }
            if ($args -contains 'branch' -and ($args -join ' ') -match 'format') {
                return "main origin/main"
            }
        } -ModuleName GittHelpers

        { Sync-GitBranches -Confirm:$false } | Should -Not -Throw
    }
}