tests/Network.Tests.ps1

$moduleRoot = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $moduleRoot 'Octa.psd1') -Force
. (Join-Path $moduleRoot 'private\ActionHelpers.ps1')
Get-ChildItem -Path (Join-Path $moduleRoot 'categories') -Filter '*.ps1' | ForEach-Object { . $_.FullName }

Describe 'Get-OctaNetworkActions' {
    It 'never throws regardless of adapter count/vendor' {
        { Get-OctaNetworkActions } | Should Not Throw
    }

    It 'every returned action targets an HKLM:\ path in the standard drive-letter form' {
        $actions = Get-OctaNetworkActions
        foreach ($a in $actions) {
            $a.TargetIdentifier | Should Match '^HKLM:\\'
        }
    }

    It 'emits no Nagle action for an interface with no configured IP address' {
        $interfacesKey = 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces'
        $unconfigured = @(Get-ChildItem -Path $interfacesKey -ErrorAction SilentlyContinue | Where-Object {
                $p = Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue
                -not (($p.IPAddress -and $p.IPAddress -ne '0.0.0.0') -or ($p.DhcpIPAddress -and $p.DhcpIPAddress -ne '0.0.0.0'))
            })
        $actions = Get-OctaNetworkActions
        foreach ($iface in $unconfigured) {
            $actions | Where-Object { $_.TargetIdentifier -match [regex]::Escape($iface.PSChildName) } | Should BeNullOrEmpty
        }
    }

    It 'emits no Energy Efficient Ethernet action when no adapter exposes a matching advanced property' {
        $anyMatch = $false
        foreach ($adapter in @(Get-NetAdapter -Physical -ErrorAction SilentlyContinue)) {
            $props = @(Get-NetAdapterAdvancedProperty -Name $adapter.Name -AllProperties -ErrorAction SilentlyContinue |
                Where-Object { $_.DisplayName -match 'Energy.*Efficient|Green Ethernet' -or $_.RegistryKeyword -match 'EEE' })
            if ($props.Count -gt 0) { $anyMatch = $true }
        }
        if (-not $anyMatch) {
            $actions = @(Get-OctaNetworkActions)
            ($actions | Where-Object { $_.TargetIdentifier -notmatch 'Tcpip' }).Count | Should Be 0
        }
    }
}