<# .SYNOPSIS Unit tests for Privacy module .DESCRIPTION Pester v5 tests for the Privacy module functionality. Tests return values, DryRun behavior, mode selection, and compliance. .NOTES Author: NexusOne23 Version: 2.2.2 Requires: Pester 5.0+ #> BeforeAll { # Import the module being tested $modulePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Privacy.psm1" if (Test-Path $modulePath) { Import-Module $modulePath -Force } else { throw "Module not found: $modulePath" } # Import Core modules for testing $coreModules = @("Logger.ps1", "Config.ps1", "Validator.ps1", "Rollback.ps1") $corePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Core" foreach ($module in $coreModules) { $moduleFile = Join-Path $corePath $module if (Test-Path $moduleFile) { . $moduleFile } } # Import Utils $utilsModules = @("Registry.ps1", "Service.ps1") $utilsPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Utils" foreach ($module in $utilsModules) { $moduleFile = Join-Path $utilsPath $module if (Test-Path $moduleFile) { . $moduleFile } } # Initialize logging (silent for tests) if (Get-Command Initialize-Logger -ErrorAction SilentlyContinue) { Initialize-Logger -EnableConsole $false } # Initialize config if (Get-Command Initialize-Config -ErrorAction SilentlyContinue) { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "config.json" Initialize-Config -ConfigPath $configPath } # Initialize backup system if (Get-Command Initialize-BackupSystem -ErrorAction SilentlyContinue) { Initialize-BackupSystem } } Describe "Privacy Module" { Context "Module Structure" { It "Should export Invoke-PrivacyHardening function" { $command = Get-Command -Name Invoke-PrivacyHardening -ErrorAction SilentlyContinue $command | Should -Not -BeNullOrEmpty } It "Should export Test-PrivacyCompliance function" { $command = Get-Command -Name Test-PrivacyCompliance -ErrorAction SilentlyContinue $command | Should -Not -BeNullOrEmpty } It "Should have CmdletBinding attribute" { $command = Get-Command -Name Invoke-PrivacyHardening $command.CmdletBinding | Should -Be $true } } Context "Function Parameters" { It "Should have Mode parameter" { $command = Get-Command -Name Invoke-PrivacyHardening $command.Parameters.ContainsKey('Mode') | Should -Be $true } It "Mode parameter should accept specific values" { $command = Get-Command -Name Invoke-PrivacyHardening $validateSet = $command.Parameters['Mode'].Attributes | Where-Object { $_ -is [System.Management.Automation.ValidateSetAttribute] } $validateSet.ValidValues | Should -Contain 'MSRecommended' $validateSet.ValidValues | Should -Contain 'Strict' $validateSet.ValidValues | Should -Contain 'Paranoid' } It "Should have DryRun parameter" { $command = Get-Command -Name Invoke-PrivacyHardening $command.Parameters.ContainsKey('DryRun') | Should -Be $true } It "Should have RemoveBloatware parameter" { $command = Get-Command -Name Invoke-PrivacyHardening $command.Parameters.ContainsKey('RemoveBloatware') | Should -Be $true } } Context "Privacy Mode Configurations" { It "Should load MSRecommended config from JSON" { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Privacy-MSRecommended.json" $configPath | Should -Exist } It "Should load Strict config from JSON" { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Privacy-Strict.json" $configPath | Should -Exist } It "Should load Paranoid config from JSON" { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Privacy-Paranoid.json" $configPath | Should -Exist } It "MSRecommended config should be valid JSON" { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Privacy-MSRecommended.json" { Get-Content $configPath -Raw | ConvertFrom-Json } | Should -Not -Throw } It "MSRecommended config should be valid" { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Privacy-MSRecommended.json" $config = Get-Content $configPath -Raw | ConvertFrom-Json $config | Should -Not -BeNullOrEmpty } It "Strict config should be valid" { $configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Privacy-Strict.json" $config = Get-Content $configPath -Raw | ConvertFrom-Json $config | Should -Not -BeNullOrEmpty } } Context "Bloatware Configuration" { It "Should load Bloatware config from JSON" { $bloatwarePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Bloatware.json" $bloatwarePath | Should -Exist } It "Bloatware config should be valid JSON" { $bloatwarePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Bloatware.json" { Get-Content $bloatwarePath -Raw | ConvertFrom-Json } | Should -Not -Throw } It "Bloatware config should have apps" { $bloatwarePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\Privacy\Config\Bloatware.json" $config = Get-Content $bloatwarePath -Raw | ConvertFrom-Json $config | Should -Not -BeNullOrEmpty } } Context "Function Execution - DryRun Mode" -Skip:$true { # These tests require admin rights and Core modules - skipped on CI It "Should execute without errors in DryRun mode" -Tag 'Interactive' { { Invoke-PrivacyHardening -Mode 'MSRecommended' -DryRun } | Should -Not -Throw } } Context "Return Object Structure" -Skip:$true { # Skipped - requires proper environment } Context "Compliance Testing" -Skip:$true { # Skipped - Test-PrivacyCompliance requires different parameters } } AfterAll { # Clean up Remove-Module Privacy -Force -ErrorAction SilentlyContinue }