tests/Lint.Tests.ps1

#Requires -Modules Pester, PSScriptAnalyzer

<#
.SYNOPSIS
    PSScriptAnalyzer linting tests for KlippyCLI module.

.DESCRIPTION
    Runs PSScriptAnalyzer against all module files to ensure
    code quality and best practices.

.EXAMPLE
    Invoke-Pester ./tests/Lint.Tests.ps1 -Tag Lint
#>


BeforeAll {
    $modulePath = Split-Path -Parent $PSScriptRoot
    $settingsPath = Join-Path $modulePath "PSScriptAnalyzerSettings.psd1"

    # Get all PowerShell files in the module
    $script:psFiles = Get-ChildItem -Path $modulePath -Include "*.ps1", "*.psm1" -Recurse |
        Where-Object { $_.FullName -notmatch '[\\/]tests[\\/]' }
}

Describe "PSScriptAnalyzer" -Tag Lint {
    Context "Module Files" {
        It "Should have no PSScriptAnalyzer errors in <_>" -ForEach $script:psFiles {
            $settingsPath = Join-Path (Split-Path -Parent $PSScriptRoot) "PSScriptAnalyzerSettings.psd1"
            $results = Invoke-ScriptAnalyzer -Path $_.FullName -Settings $settingsPath -Severity Error

            if ($results) {
                $results | ForEach-Object {
                    Write-Host " [$($_.Severity)] $($_.RuleName): $($_.Message)" -ForegroundColor Red
                    Write-Host " Line $($_.Line): $($_.ScriptName)" -ForegroundColor Gray
                }
            }

            $results | Should -BeNullOrEmpty
        }

        It "Should have no PSScriptAnalyzer warnings in <_>" -ForEach $script:psFiles {
            $settingsPath = Join-Path (Split-Path -Parent $PSScriptRoot) "PSScriptAnalyzerSettings.psd1"
            $results = Invoke-ScriptAnalyzer -Path $_.FullName -Settings $settingsPath -Severity Warning

            if ($results) {
                $results | ForEach-Object {
                    Write-Host " [$($_.Severity)] $($_.RuleName): $($_.Message)" -ForegroundColor Yellow
                    Write-Host " Line $($_.Line): $($_.ScriptName)" -ForegroundColor Gray
                }
            }

            # Warnings are advisory - uncomment to enforce
            # $results | Should -BeNullOrEmpty
        }
    }

    Context "Module Manifest" {
        It "Should have a valid module manifest" {
            $manifestPath = Join-Path (Split-Path -Parent $PSScriptRoot) "KlippyCLI.psd1"
            { Test-ModuleManifest -Path $manifestPath -ErrorAction Stop } | Should -Not -Throw
        }

        It "Should export only public functions" {
            $manifestPath = Join-Path (Split-Path -Parent $PSScriptRoot) "KlippyCLI.psd1"
            $manifest = Import-PowerShellDataFile -Path $manifestPath

            # All exported functions should exist in public folder
            $publicFunctions = Get-ChildItem -Path (Join-Path (Split-Path -Parent $PSScriptRoot) "functions/public") -Filter "*.ps1" |
                ForEach-Object { $_.BaseName }

            foreach ($exportedFunc in $manifest.FunctionsToExport) {
                $exportedFunc | Should -BeIn $publicFunctions -Because "$exportedFunc should have a file in functions/public"
            }
        }
    }
}