Tests/Install-WinslopFix.Tests.ps1

Describe 'Install-WinslopFix' {

    BeforeAll {
        $modulePath = Join-Path (Join-Path $PSScriptRoot '..') 'WinslopFix.psd1'
        Import-Module $modulePath -Force -ErrorAction Stop
    }

    AfterAll {
        Remove-Module WinslopFix -Force -ErrorAction SilentlyContinue
    }

    Context 'Pre-flight checks' {

        BeforeAll {
            Mock -ModuleName WinslopFix Assert-Administrator {}
            Mock -ModuleName WinslopFix Test-CopilotPlatform {
                [PSCustomObject]@{
                    IsCopilotPlatform         = $false
                    HasNPU                    = $false
                    HasAIFabricService        = $false
                    AIFabricServiceStatus     = 'NotFound'
                    WorkloadsSessionHostCount = 0
                    WindowsBuild              = '26100'
                    NPUDeviceName             = $null
                }
            }
            Mock -ModuleName WinslopFix Get-ScheduledTask { return $null }
            Mock -ModuleName WinslopFix New-Item {}
            Mock -ModuleName WinslopFix Test-Path { return $false }
            Mock -ModuleName WinslopFix Copy-Item {}
            Mock -ModuleName WinslopFix Register-ScheduledTask {}
            Mock -ModuleName WinslopFix Start-ScheduledTask {}
            Mock -ModuleName WinslopFix Write-WinslopFixLog {}
            Mock -ModuleName WinslopFix Unregister-ScheduledTask {}
            Mock -ModuleName WinslopFix Get-ItemProperty { return $null }
            Mock -ModuleName WinslopFix Remove-ItemProperty {}
            Mock -ModuleName WinslopFix Remove-Item {}
            Mock -ModuleName WinslopFix Remove-EventLog {}
            Mock -ModuleName WinslopFix New-EventLog {}
            Mock -ModuleName WinslopFix Split-Path { return 'C:\MockModuleRoot' }
        }

        It 'warns when machine is not a Copilot+ PC' {
            $result = Install-WinslopFix -InstallPath 'TestDrive:\WinslopFix' 3>&1
            $warnings = $result | Where-Object { $_ -is [System.Management.Automation.WarningRecord] }
            $warnings | Should -Not -BeNullOrEmpty
        }
    }

    Context 'Idempotency' {

        BeforeAll {
            Mock -ModuleName WinslopFix Assert-Administrator {}
            Mock -ModuleName WinslopFix Test-CopilotPlatform {
                [PSCustomObject]@{
                    IsCopilotPlatform = $true
                    HasNPU = $true
                    HasAIFabricService = $true
                    AIFabricServiceStatus = 'Running'
                    WorkloadsSessionHostCount = 3
                    WindowsBuild = '26100'
                    NPUDeviceName = 'Test NPU'
                }
            }
            Mock -ModuleName WinslopFix Get-ScheduledTask {
                [PSCustomObject]@{ TaskName = 'WinslopFix'; State = 'Running' }
            }
        }

        It 'warns and exits if already installed without -Force' {
            $result = Install-WinslopFix -InstallPath 'TestDrive:\WinslopFix' 3>&1
            $warnings = $result | Where-Object { $_ -is [System.Management.Automation.WarningRecord] }
            ($warnings | Where-Object { $_.Message -match 'already installed' }) | Should -Not -BeNullOrEmpty
        }
    }

    Context 'WhatIf support' {

        BeforeAll {
            Mock -ModuleName WinslopFix Assert-Administrator {}
            Mock -ModuleName WinslopFix Test-CopilotPlatform {
                [PSCustomObject]@{
                    IsCopilotPlatform = $true
                    HasNPU = $true
                    HasAIFabricService = $true
                    AIFabricServiceStatus = 'Running'
                    WorkloadsSessionHostCount = 3
                    WindowsBuild = '26100'
                    NPUDeviceName = 'Test NPU'
                }
            }
            Mock -ModuleName WinslopFix Get-ScheduledTask { return $null }
            Mock -ModuleName WinslopFix New-Item {}
            Mock -ModuleName WinslopFix Copy-Item {}
            Mock -ModuleName WinslopFix Register-ScheduledTask {}
            Mock -ModuleName WinslopFix Start-ScheduledTask {}
        }

        It 'does not create files or tasks with -WhatIf' {
            Install-WinslopFix -InstallPath 'TestDrive:\WinslopFix' -WhatIf

            Should -Not -Invoke -CommandName New-Item -ModuleName WinslopFix
            Should -Not -Invoke -CommandName Copy-Item -ModuleName WinslopFix
            Should -Not -Invoke -CommandName Register-ScheduledTask -ModuleName WinslopFix
        }
    }
}