scripts/modules/prerequisites/git/test-git.ps1

# strangeloop Setup - Git Testing Module
# Version: 1.0.0


param(
    [switch]$Detailed
)

# Import shared modules
$SharedPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent | Join-Path -ChildPath "shared"
Import-Module "$SharedPath\write-functions.ps1" -Force -DisableNameChecking
Import-Module "$SharedPath\test-functions.ps1" -Force -DisableNameChecking

function Test-Git {
    param(
        [switch]$Detailed
    )
    
    Write-Step "Testing Git installation..."
    
    if (-not (Test-Command "git")) {
        Write-Error "Git command 'git' not found"
        return $false
    }
    
    $version = Get-ToolVersion "git"
    if (-not $version) {
        Write-Error "Could not determine Git version"
        return $false
    }
    
    Write-Success "Git $version is properly installed"
    
    # Test Git functionality
    try {
        $versionOutput = git --version 2>$null
        if ($versionOutput) {
            Write-Success "Git functionality test passed"
            
            if ($Detailed) {
                # Test Git configuration
                $userName = git config --global user.name 2>$null
                $userEmail = git config --global user.email 2>$null
                
                if ($userName -and $userEmail) {
                    Write-Info "Git is configured with user: $userName <$userEmail>"
                } else {
                    Write-Warning "Git is not fully configured (missing user.name or user.email)"
                }
                
                # Test Git repository operations
                $tempDir = Join-Path $env:TEMP "GitTest-$(Get-Random)"
                New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
                if (Test-Path $tempDir) {
                    try {
                        Push-Location $tempDir
                        
                        # Initialize a test repository
                        git init --quiet 2>$null
                        if ($LASTEXITCODE -eq 0) {
                            Write-Success "Git repository initialization test passed"
                        } else {
                            Write-Warning "Git repository initialization test failed"
                        }
                        
                        # Test basic Git operations
                        "test" | Out-File -FilePath "test.txt"
                        git add test.txt 2>$null
                        git commit -m "Test commit" --quiet 2>$null
                        if ($LASTEXITCODE -eq 0) {
                            Write-Success "Git commit test passed"
                        } else {
                            Write-Warning "Git commit test failed"
                        }
                        
                    } catch {
                        Write-Warning "Git operations test failed: $($_.Exception.Message)"
                    } finally {
                        Pop-Location
                        Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
                    }
                }
            }
            
            return $true
        } else {
            Write-Warning "Git functionality test failed"
            return $false
        }
    } catch {
        Write-Warning "Git functionality test failed: $($_.Exception.Message)"
        return $false
    }
}

# Main execution
if ($MyInvocation.InvocationName -ne '.') {
    $result = Test-Git -Detailed:$Detailed
    
    if ($result) {
        Write-Success "Git testing completed successfully"
        exit 0
    } else {
        Write-Error "Git testing failed"
        exit 1
    }
}

# Export functions for module usage
Export-ModuleMember -Function @(
    'Test-Git'
)