#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' } BeforeAll { $repoRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent foreach ($f in @( 'src/Normalization/New-PersonaMembershipRecord.ps1' 'src/Normalization/New-PersonaUserRecord.ps1' 'src/RuleEngine/Test-PersonaCondition.ps1' 'src/RuleEngine/Test-PersonaConditionGroup.ps1' )) { . (Join-Path $repoRoot $f) } $script:user = New-PersonaUserRecord ` -AccountObjectId '00000000-0000-0000-0000-000000000101' ` -UserPrincipalName 'svc-billing@example.invalid' ` -UserType 'Member' ` -Properties @{ Department = 'Finance'; JobTitle = 'Analyst' } function Cond { param([string] $Property, [string] $Operator = 'equals', [string] $Value) [pscustomobject]@{ type = 'property'; property = $Property; operator = $Operator; value = $Value } } function New-ConditionGroup { param([string] $Operator, [object[]] $Conditions) [pscustomobject]@{ operator = $Operator; conditions = $Conditions } } # Builds exactly $Depth nested groups around a leaf condition that is always # true. Leaf conditions do not add a level: the engine counts group nesting, # so -Depth 3 yields group(group(group(leaf))) and evaluates at depths 1..3. function New-NestedGroup { param([int] $Depth) $node = Cond -Property 'Department' -Value 'Finance' for ($i = 0; $i -lt $Depth; $i++) { $node = New-ConditionGroup -Operator 'all' -Conditions @($node) } $node } } Describe 'Logical composition (RE-003)' { Context 'all' { It 'is true when every condition is true' { Test-PersonaConditionGroup -Group (New-ConditionGroup 'all' @((Cond 'Department' -Value 'Finance'), (Cond 'JobTitle' -Value 'Analyst'))) -UserRecord $user | Should -Be 'True' } It 'is false when any condition is false' { Test-PersonaConditionGroup -Group (New-ConditionGroup 'all' @((Cond 'Department' -Value 'Finance'), (Cond 'JobTitle' -Value 'Manager'))) -UserRecord $user | Should -Be 'False' } } Context 'any' { It 'is true when at least one condition is true' { Test-PersonaConditionGroup -Group (New-ConditionGroup 'any' @((Cond 'Department' -Value 'Legal'), (Cond 'JobTitle' -Value 'Analyst'))) -UserRecord $user | Should -Be 'True' } It 'is false when every condition is false' { Test-PersonaConditionGroup -Group (New-ConditionGroup 'any' @((Cond 'Department' -Value 'Legal'), (Cond 'JobTitle' -Value 'Manager'))) -UserRecord $user | Should -Be 'False' } } Context 'nesting' { It 'resolves an any group nested inside an all group' { $inner = New-ConditionGroup 'any' @((Cond 'JobTitle' -Value 'Manager'), (Cond 'JobTitle' -Value 'Analyst')) $outer = New-ConditionGroup 'all' @((Cond 'Department' -Value 'Finance'), $inner) Test-PersonaConditionGroup -Group $outer -UserRecord $user | Should -Be 'True' } It 'resolves an all group nested inside an any group' { $inner = New-ConditionGroup 'all' @((Cond 'Department' -Value 'Legal'), (Cond 'JobTitle' -Value 'Analyst')) $outer = New-ConditionGroup 'any' @((Cond 'Department' -Value 'Finance'), $inner) Test-PersonaConditionGroup -Group $outer -UserRecord $user | Should -Be 'True' } } Context 'empty and malformed groups' { It 'returns Unknown for an empty conditions collection' { Test-PersonaConditionGroup -Group (New-ConditionGroup 'all' @()) -UserRecord $user | Should -Be 'Unknown' } It 'returns Unknown for an unrecognized group operator' { Test-PersonaConditionGroup -Group (New-ConditionGroup 'either' @((Cond 'Department' -Value 'Finance'))) -UserRecord $user | Should -Be 'Unknown' } } } Describe 'Depth limits (RE-004)' { It 'evaluates a tree exactly at the configured depth' { Test-PersonaConditionGroup -Group (New-NestedGroup -Depth 5) -UserRecord $user -MaxDepth 5 | Should -Be 'True' } It 'returns Unknown beyond the configured depth rather than truncating' { # Silent truncation would evaluate a rule the author did not write. Test-PersonaConditionGroup -Group (New-NestedGroup -Depth 7) -UserRecord $user -MaxDepth 5 | Should -Be 'Unknown' } It 'honours a lowered depth limit' { Test-PersonaConditionGroup -Group (New-NestedGroup -Depth 3) -UserRecord $user -MaxDepth 2 | Should -Be 'Unknown' } It 'rejects a MaxDepth above the hard ceiling of 10' { { Test-PersonaConditionGroup -Group (New-NestedGroup -Depth 2) -UserRecord $user -MaxDepth 11 } | Should -Throw } It 'rejects a MaxDepth below the minimum of 1' { { Test-PersonaConditionGroup -Group (New-NestedGroup -Depth 2) -UserRecord $user -MaxDepth 0 } | Should -Throw } }