Tests/ggps.Tests.ps1

BeforeAll {
    $repoRoot = Split-Path -Parent $PSScriptRoot
    $manifestPath = Join-Path $repoRoot 'ggps.psd1'
    $scriptPath = Join-Path $repoRoot 'ggps.psm1'
}

Describe 'ggps Manifest Validation' {
    It 'Successfully validates ggps.psd1' {
        $manifest = Test-ModuleManifest -Path $manifestPath -ErrorAction Stop
        $manifest | Should -Not -BeNullOrEmpty
        $manifest.Name | Should -Be 'ggps'
        $manifest.Version.ToString() | Should -Match '^\d+\.\d+\.\d+$'
    }

    It 'Contains required metadata for PSGallery' {
        $data = Import-PowerShellDataFile -Path $manifestPath
        $data.RootModule | Should -Be 'ggps.psm1'
        $data.Author | Should -Not -BeNullOrEmpty
        $data.Description | Should -Not -BeNullOrEmpty
        $data.GUID | Should -Match '^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$'
        $data.FunctionsToExport | Should -Contain 'gg'
        $data.FunctionsToExport | Should -Not -Contain '*'
        $data.PrivateData.PSData.ProjectUri | Should -Not -BeNullOrEmpty
        $data.PrivateData.PSData.LicenseUri | Should -Not -BeNullOrEmpty
    }
}

Describe 'Code Quality & ScriptAnalyzer' {
    It 'Passes PSScriptAnalyzer with zero errors or warnings' {
        if (Get-Module -ListAvailable -Name PSScriptAnalyzer) {
            $results = Invoke-ScriptAnalyzer -Path $scriptPath -Severity Error, Warning
            $results | Should -BeNullOrEmpty
        }
    }
}

Describe 'Module Functionality' {
    It 'Exports gg function when imported' {
        $env:GGPS_SKIP_AUTO_RUN = '1'
        try {
            $mod = Import-Module -Name $manifestPath -PassThru -Force
            $mod.ExportedFunctions.Keys | Should -Contain 'gg'
            $cmd = Get-Command -Name gg -Module ggps
            $cmd.Parameters.Keys | Should -Contain 'MinDelayMs'
            $cmd.Parameters.Keys | Should -Contain 'MaxDelayMs'
            $cmd.Parameters.Keys | Should -Contain 'MovementThreshold'
        } finally {
            Remove-Module -Name ggps -ErrorAction SilentlyContinue
            $env:GGPS_SKIP_AUTO_RUN = $null
        }
    }
}