Tests/WinPath-Clean.Tests.ps1

#Requires -Modules Pester

BeforeAll {
    $ModulePath = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
    Import-Module "$ModulePath\WinPath-Clean.psd1" -Force
}

Describe 'Get-PathAnalysis' {
    Context 'Basic functionality' {
        It 'Should return PATH entries' {
            $result = Get-PathAnalysis -Target User
            $result | Should -Not -BeNullOrEmpty
        }

        It 'Should have required properties' {
            $result = Get-PathAnalysis -Target User | Select-Object -First 1
            $result.PSObject.Properties.Name | Should -Contain 'Status'
            $result.PSObject.Properties.Name | Should -Contain 'Path'
            $result.PSObject.Properties.Name | Should -Contain 'Scope'
            $result.PSObject.Properties.Name | Should -Contain 'Exists'
            $result.PSObject.Properties.Name | Should -Contain 'Issue'
        }

        It 'Should filter by target User' {
            $result = Get-PathAnalysis -Target User
            $result | ForEach-Object { $_.Scope | Should -Be 'User' }
        }

        It 'Should filter by target Machine' {
            $result = Get-PathAnalysis -Target Machine
            $result | ForEach-Object { $_.Scope | Should -Be 'Machine' }
        }

        It 'Should return both scopes with Both target' {
            $result = Get-PathAnalysis -Target Both
            ($result | Select-Object -ExpandProperty Scope -Unique).Count | Should -BeGreaterOrEqual 1
        }
    }

    Context 'Issue detection' {
        It 'Should mark existing paths with checkmark' {
            $result = Get-PathAnalysis -Target Both |
            Where-Object { $_.Exists -eq $true } |
            Select-Object -First 1
            
            if ($result) {
                $result.Status | Should -Be '✓'
            }
        }

        It 'Should detect non-existent paths' {
            $result = Get-PathAnalysis -Target Both |
            Where-Object { $_.Exists -eq $false } |
            Select-Object -First 1
            
            if ($result) {
                $result.Status | Should -BeIn @('✗', '⚠')
            }
        }
    }

    Context 'IssuesOnly switch' {
        It 'Should return only issues when IssuesOnly is specified' {
            $result = Get-PathAnalysis -Target Both -IssuesOnly
            
            if ($result) {
                $result | ForEach-Object {
                    $_.Status | Should -Not -Be '✓'
                }
            }
        }
    }
}

Describe 'Backup-Path' {
    Context 'Backup creation' {
        It 'Should create a backup file' {
            $result = Backup-Path -Target User
            $result.BackupFile | Should -Exist
        }

        It 'Should return backup metadata' {
            $result = Backup-Path -Target User
            $result.Timestamp | Should -Not -BeNullOrEmpty
            $result.Target | Should -Be 'User'
        }

        It 'Should create valid JSON' {
            $result = Backup-Path -Target Both
            $content = Get-Content $result.BackupFile | ConvertFrom-Json
            $content.Paths | Should -Not -BeNullOrEmpty
        }

        AfterAll {
            # Cleanup test backups (keep only last 5)
            $backups = Get-ChildItem "$env:USERPROFILE\.config\winpath-clean\backups" -Filter '*.json' |
            Sort-Object LastWriteTime -Descending |
            Select-Object -Skip 5
            $backups | Remove-Item -Force -ErrorAction SilentlyContinue
        }
    }
}

Describe 'Get-PathBackups' {
    It 'Should list available backups' {
        # Create a backup first to ensure there's at least one
        Backup-Path -Target User | Out-Null
        
        $result = Get-PathBackups
        $result | Should -Not -BeNullOrEmpty
        $result[0].FileName | Should -Match '\.json$'
    }
}

Describe 'Module Structure' {
    It 'Should export expected functions' {
        $module = Get-Module WinPath-Clean
        $module.ExportedFunctions.Keys | Should -Contain 'Get-PathAnalysis'
        $module.ExportedFunctions.Keys | Should -Contain 'Set-CleanPath'
        $module.ExportedFunctions.Keys | Should -Contain 'Invoke-WinPathClean'
        $module.ExportedFunctions.Keys | Should -Contain 'Backup-Path'
        $module.ExportedFunctions.Keys | Should -Contain 'Restore-Path'
        $module.ExportedFunctions.Keys | Should -Contain 'Export-PathReport'
    }

    It 'Should export expected aliases' {
        $module = Get-Module WinPath-Clean
        $module.ExportedAliases.Keys | Should -Contain 'wpc'
        $module.ExportedAliases.Keys | Should -Contain 'pathclean'
    }
}