Files
personaEngine2/tests/Configuration/Semantic.Tests.ps1
T
dave cdc6bb33d3 Implement Stage A: rule engine, validation, audit, and safety gates
Completes 109 of 121 tasks. Every remaining task needs a tenant connection
(T055, T056, T101-T103) or an Azure Automation account (T115-T121).

  354 offline Pester tests      PASS
  Engine purity (Principle IV)  PASS
  Sanitization (SC-013)         PASS  (156 files)
  Graph module loaded in tests  none  (SC-008 holds)

What landed
  - Four-layer configuration validation with stable finding codes, covering
    every VR-002 and VR-003 condition, plus a 23-fixture invalid-config corpus
  - Run loop, audit records (NDJSON through a single sink), summaries,
    reconciliation, and exit codes 0-6
  - Persistence behind a single write-body builder whose result always has
    exactly one key
  - Invoke-PersonaEngine.ps1 and Edit-PersonaEngineConfig.ps1
  - Six docs, two pipelines, traceability matrix, V-5a and sanitization records

Three deviations from tasks.md, each recorded in its status block

  T033 is not in Resolve-UserPersona. evaluationErrorThreshold is run-level
  state and the rule engine is pure; a counter there would break Principle IV.
  It lives in New-PersonaRunCounter and is applied in the run loop.

  A new src/Engine/ layer holds Invoke-PersonaEngineRun. The entry script
  imports the manifest, which requires Microsoft.Graph.Authentication, so a
  loop living only inside it could not run on a machine without the Graph SDK
  and SC-004 could not be proven at all. The entry script is now a thin
  wrapper and what ships is what is tested.

  The invalid-config corpus is generated by a committed script, with the
  generated fixtures committed too, so a reviewer sees the fixture in the diff.

Defects found by running the code, not by reading it

  Group and role ID lists were double-wrapped: @(Get-PersonaGroupIdPage ...)
  around a comma-returned array collapsed every membership list into one
  bogus space-joined entry. That is a silent false non-match, exactly what
  FR-013 exists to prevent.

  A 403 whose status appears only in the exception message parsed as $null,
  which the retry policy treats as a transport error - five requests per
  account against a tenant already refusing. Status extraction now falls back
  to the message text, bounded to 400-599.

  The sanitization scan walked tracked files only, so it covered 34 of 156
  files and none of this phase's code. It now scans untracked non-ignored
  files too, and a negative control confirms it catches a planted leak.

  Test-Json reports one error per violating location, not first-failure-only
  as the V-5a draft claimed. Record and pin corrected.

Enforcement remains blocked on the V-4 security sign-off.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-20 21:48:19 -04:00

191 lines
7.7 KiB
PowerShell

#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }
<#
SC-009: every VR-002 condition produces a finding with a code, a severity, and a
location.
Layer 3 is exercised directly rather than through Test-PersonaConfiguration. Most
of these defects are also caught by the JSON Schema, so a full-pipeline test would
stop at layer 2 and never reach the code under test - it would be asserting that
the schema works, which LayerOrdering.Tests.ps1 already does.
The overlap is deliberate (see the note in Test-PersonaConfigurationSemantic):
layer 2 can be bypassed with -SchemaPath, and V-5a showed an unparseable schema
passes silently on this build. Anything that can misclassify a privileged account
is checked twice.
#>
BeforeAll {
$repoRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
. (Join-Path $repoRoot 'tests/TestHelpers.ps1')
foreach ($file in (Get-PersonaSourceFile -RepoRoot $repoRoot)) { . $file }
$script:corpus = Join-Path $repoRoot 'tests/TestData/InvalidConfigs'
function Get-Finding {
param([string] $Fixture, [string] $Code)
$document = Get-Content -LiteralPath (Join-Path $script:corpus "$Fixture.json") -Raw | ConvertFrom-Json -Depth 32
@(Test-PersonaConfigurationSemantic -Document $document | Where-Object Code -EQ $Code)
}
}
Describe 'Semantic validation, VR-002 conditions (SC-009)' {
It 'detects duplicate rule IDs' {
$findings = Get-Finding -Fixture 'PE-SEM-001-duplicate-rule-id' -Code 'PE-SEM-001'
$findings.Count | Should -BeGreaterThan 0
$findings[0].Severity | Should -Be 'Error'
$findings[0].Location | Should -Match 'RULE-0010-GUEST'
}
It 'detects duplicate priorities among enabled rules' {
$findings = Get-Finding -Fixture 'PE-SEM-002-duplicate-priority' -Code 'PE-SEM-002'
$findings.Count | Should -BeGreaterThan 0
$findings[0].Severity | Should -Be 'Error'
$findings[0].Description | Should -Match 'RULE-0010-GUEST'
$findings[0].Description | Should -Match 'RULE-0900-EMPLOYEE'
}
It 'detects a configuration with no enabled rules' {
$findings = Get-Finding -Fixture 'PE-SEM-003-no-enabled-rules' -Code 'PE-SEM-003'
$findings.Count | Should -Be 1
$findings[0].Severity | Should -Be 'Error'
}
It 'detects a blank target attribute' {
$findings = Get-Finding -Fixture 'PE-SEM-004-blank-target-attribute' -Code 'PE-SEM-004'
$findings.Count | Should -Be 1
$findings[0].Location | Should -Be 'engine.targetAttribute'
}
It 'detects a target attribute absent from the approved list' {
$findings = Get-Finding -Fixture 'PE-SEM-005-target-not-approved' -Code 'PE-SEM-005'
$findings.Count | Should -Be 1
$findings[0].Severity | Should -Be 'Error'
}
It 'detects a reference to a disabled data source' {
$findings = Get-Finding -Fixture 'PE-SEM-006-unavailable-data-source' -Code 'PE-SEM-006'
$findings.Count | Should -BeGreaterThan 0
$findings[0].Severity | Should -Be 'Error'
$findings[0].Description | Should -Match 'EvaluationError'
}
It 'detects memberOf with no group Object IDs' {
$findings = Get-Finding -Fixture 'PE-SEM-007-memberof-without-groups' -Code 'PE-SEM-007'
$findings.Count | Should -BeGreaterThan 0
$findings[0].Severity | Should -Be 'Error'
}
It 'detects in without a values array' {
$findings = Get-Finding -Fixture 'PE-SEM-008-in-without-values' -Code 'PE-SEM-008'
$findings.Count | Should -Be 1
$findings[0].Severity | Should -Be 'Error'
}
It 'detects isNotNull carrying a comparison value' {
# The value is silently ignored at evaluation time, so the rule does not do
# what the author plainly intended it to do.
$findings = Get-Finding -Fixture 'PE-SEM-009-isnull-with-value' -Code 'PE-SEM-009'
$findings.Count | Should -Be 1
$findings[0].Severity | Should -Be 'Error'
}
It 'detects a persona absent from the declared catalogue' {
$findings = Get-Finding -Fixture 'PE-SEM-010-undeclared-persona' -Code 'PE-SEM-010'
$findings.Count | Should -Be 1
$findings[0].Description | Should -Match 'Undeclared-Persona'
}
It 'detects Unclassified used as an ordinary rule persona' {
$findings = Get-Finding -Fixture 'PE-SEM-011-unclassified-as-persona' -Code 'PE-SEM-011'
$findings.Count | Should -Be 1
$findings[0].Severity | Should -Be 'Error'
}
It 'detects nesting deeper than the configured maximum' {
$findings = Get-Finding -Fixture 'PE-SEM-012-depth-over-configured-maximum' -Code 'PE-SEM-012'
$findings.Count | Should -BeGreaterThan 0
$findings[0].Severity | Should -Be 'Error'
$findings[0].Location | Should -Match 'conditions'
}
It 'detects a configured maximum above the hard ceiling of 10' {
$findings = Get-Finding -Fixture 'PE-SEM-013-depth-over-hard-ceiling' -Code 'PE-SEM-013'
$findings.Count | Should -Be 1
$findings[0].Location | Should -Be 'engine.maxConditionDepth'
}
It 'warns when a condition mode differs from an explicitly pinned global mode' {
# A Warning, not an Error: the three facets are retrieved independently, so
# the condition is answered correctly. The cost is an extra call per account,
# which is worth surfacing but not worth blocking.
$findings = Get-Finding -Fixture 'PE-SEM-014-mode-not-enabled-globally' -Code 'PE-SEM-014'
$findings.Count | Should -BeGreaterThan 0
$findings[0].Severity | Should -Be 'Warning'
}
It 'detects an unsupported property name' {
$findings = Get-Finding -Fixture 'PE-SEM-015-unsupported-property' -Code 'PE-SEM-015'
$findings.Count | Should -Be 1
$findings[0].Description | Should -Match 'employeeHireDate'
}
It 'detects an invalid regular expression' {
$findings = Get-Finding -Fixture 'PE-SEM-016-invalid-regex' -Code 'PE-SEM-016'
$findings.Count | Should -Be 1
$findings[0].Severity | Should -Be 'Error'
}
}
Describe 'Semantic findings carry everything VR-004 requires' {
It 'gives every finding a severity, code, location, description, resolution, and layer' {
$fixtures = Get-ChildItem -Path $script:corpus -Filter 'PE-SEM-*.json'
$fixtures.Count | Should -BeGreaterThan 0
foreach ($fixture in $fixtures) {
$document = Get-Content -LiteralPath $fixture.FullName -Raw | ConvertFrom-Json -Depth 32
foreach ($finding in (Test-PersonaConfigurationSemantic -Document $document)) {
$finding.Severity | Should -BeIn @('Error', 'Warning', 'Information')
$finding.Code | Should -Match '^PE-SEM-\d{3}$'
$finding.Location | Should -Not -BeNullOrEmpty
$finding.Description | Should -Not -BeNullOrEmpty
$finding.SuggestedResolution | Should -Not -BeNullOrEmpty
$finding.Layer | Should -Be 'Semantic'
}
}
}
It 'produces no findings for the valid baseline' {
# Without this, a validator that flagged everything would pass every test above.
$document = Get-Content -LiteralPath (Join-Path $script:corpus 'baseline-deployed.json') -Raw | ConvertFrom-Json -Depth 32
@(Test-PersonaConfigurationSemantic -Document $document) | Should -BeNullOrEmpty
}
It 'produces no findings for the shipped example configuration' {
$document = Get-Content -LiteralPath (Join-Path $repoRoot 'config/persona-engine.example.json') -Raw | ConvertFrom-Json -Depth 32
@(Test-PersonaConfigurationSemantic -Document $document) | Should -BeNullOrEmpty
}
}