#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' } <# FR-029: the interactive editor can delete an existing rule. Remove-PersonaConfigRule is the pure function the [D] command calls after the operator confirms against the rule's id, name, and priority - the confirmation prompt itself lives in Edit-PersonaEngineConfig.ps1 and is covered by EditorStructuralEdits.Tests.ps1, which drives the real interactive loop. #> BeforeAll { $repoRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent . (Join-Path $repoRoot 'tests/TestHelpers.ps1') foreach ($file in (Get-PersonaSourceFile -RepoRoot $repoRoot)) { . $file } function New-TestRule { param([string] $Id, [int] $Priority) [pscustomobject]@{ id = $Id name = "Rule $Id" description = 'A test rule.' enabled = $true priority = $Priority persona = 'Employee' match = [pscustomobject]@{ operator = 'all' conditions = @([pscustomobject]@{ type = 'property'; property = 'Department'; operator = 'isNotNull' }) } } } } Describe 'Remove-PersonaConfigRule (FR-029)' { It 'removes the named rule' { $rules = @( (New-TestRule -Id 'RULE-0010-GUEST' -Priority 10), (New-TestRule -Id 'RULE-0900-EMPLOYEE' -Priority 900) ) $result = Remove-PersonaConfigRule -Rules $rules -RuleId 'RULE-0010-GUEST' , $result | Should -BeOfType [array] $result.Count | Should -Be 1 $result[0].id | Should -Be 'RULE-0900-EMPLOYEE' } It 'does not mutate the input collection' { $rules = @( (New-TestRule -Id 'RULE-0010-GUEST' -Priority 10), (New-TestRule -Id 'RULE-0900-EMPLOYEE' -Priority 900) ) Remove-PersonaConfigRule -Rules $rules -RuleId 'RULE-0010-GUEST' | Out-Null $rules.Count | Should -Be 2 } It 'throws when the id does not exist, rather than silently doing nothing' { $rules = @((New-TestRule -Id 'RULE-0010-GUEST' -Priority 10)) { Remove-PersonaConfigRule -Rules $rules -RuleId 'RULE-9999-ABSENT' } | Should -Throw '*RULE-9999-ABSENT*' } It 'can remove the only remaining rule, leaving an empty collection' { $rules = @((New-TestRule -Id 'RULE-0010-GUEST' -Priority 10)) $result = Remove-PersonaConfigRule -Rules $rules -RuleId 'RULE-0010-GUEST' , $result | Should -BeOfType [array] $result.Count | Should -Be 0 } It 'a deletion that leaves zero enabled rules is caught by re-validation (PE-SEM-003)' { # Remove-PersonaConfigRule itself has no opinion on "zero enabled rules" - that # is VR-002's job. Exercised here to prove the editor's re-validation path # would in fact block this rather than silently accepting it. $document = New-TestConfigurationDocument ($document.rules | Where-Object { $_.id -eq 'RULE-0010-GUEST' }).enabled = $false $document.rules = Remove-PersonaConfigRule -Rules $document.rules -RuleId 'RULE-0900-EMPLOYEE' $path = Save-TestConfiguration -Document $document -Directory ([System.IO.Path]::GetTempPath()) try { $result = Test-PersonaConfiguration -Path $path $result.IsValid | Should -BeFalse @($result.Findings | Where-Object Code -EQ 'PE-SEM-003').Count | Should -BeGreaterThan 0 } finally { Remove-Item -LiteralPath $path -Force -ErrorAction SilentlyContinue } } }