microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/tests/agents/activation-harness/Test-AdrCreationActivation.Tests.ps1
176lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | # Test-AdrCreationActivation.Tests.ps1 |
| 6 | # |
| 7 | # Activation-harness regression suite for @adr-creation. Exercises the four |
| 8 | # canonical activation scenarios (CleanWorkspace, SteadyState, GovernEntry, |
| 9 | # AdoptTemplate) via Get-AgentActivationFingerprint and asserts: |
| 10 | # * Per-scenario fingerprint hash matches the committed baseline (drift gate) |
| 11 | # * CleanWorkspace cold-start byte budget < 44,000 bytes (PD-04=A) |
| 12 | # * Lifecycle Dispatch load-set composition (always-attach vs on-demand) |
| 13 | # * pester runner emits logs/pester-summary.json + logs/pester-failures.json |
| 14 | |
| 15 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', |
| 16 | Justification = 'Consumed by Pester -ForEach blocks at discovery time')] |
| 17 | param() |
| 18 | |
| 19 | BeforeDiscovery { |
| 20 | $ScenarioCases = @( |
| 21 | @{ ScenarioName = 'CleanWorkspace' } |
| 22 | @{ ScenarioName = 'SteadyState' } |
| 23 | @{ ScenarioName = 'GovernEntry' } |
| 24 | @{ ScenarioName = 'AdoptTemplate' } |
| 25 | ) |
| 26 | } |
| 27 | |
| 28 | BeforeAll { |
| 29 | $script:RepoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '../../../..')).Path |
| 30 | $modulePath = Join-Path $script:RepoRoot 'scripts/agents/activation-harness/Get-AgentActivationFingerprint.psm1' |
| 31 | Import-Module -Name $modulePath -Force |
| 32 | |
| 33 | $script:AgentRelPath = '.github/agents/project-planning/adr-creation.agent.md' |
| 34 | $script:AgentPath = Join-Path $script:RepoRoot $script:AgentRelPath |
| 35 | |
| 36 | $baselinePath = Join-Path $script:RepoRoot 'scripts/agents/activation-harness/baseline.json' |
| 37 | $script:Baseline = Get-Content -LiteralPath $baselinePath -Raw -Encoding UTF8 | |
| 38 | ConvertFrom-Json -AsHashtable |
| 39 | |
| 40 | $script:ColdStartBudget = 44000 |
| 41 | |
| 42 | $script:Fingerprints = @{} |
| 43 | foreach ($name in @('CleanWorkspace', 'SteadyState', 'GovernEntry', 'AdoptTemplate')) { |
| 44 | $script:Fingerprints[$name] = Get-AgentActivationFingerprint ` |
| 45 | -AgentPath $script:AgentPath ` |
| 46 | -ScenarioName $name ` |
| 47 | -RepoRoot $script:RepoRoot |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | AfterAll { |
| 52 | Remove-Module -Name 'Get-AgentActivationFingerprint' -Force -ErrorAction SilentlyContinue |
| 53 | } |
| 54 | |
| 55 | Describe '@adr-creation activation harness module contract' -Tag 'Unit' { |
| 56 | It 'exports Get-AgentActivationFingerprint' { |
| 57 | Get-Command -Name Get-AgentActivationFingerprint -ErrorAction Stop | Should -Not -BeNullOrEmpty |
| 58 | } |
| 59 | |
| 60 | It 'returns a hashtable with required keys for scenario <ScenarioName>' -ForEach $ScenarioCases { |
| 61 | $fp = $script:Fingerprints[$ScenarioName] |
| 62 | $fp.Keys | Should -Contain 'ScenarioName' |
| 63 | $fp.Keys | Should -Contain 'AgentBytes' |
| 64 | $fp.Keys | Should -Contain 'ColdStartBytes' |
| 65 | $fp.Keys | Should -Contain 'LoadedFiles' |
| 66 | $fp.Keys | Should -Contain 'Hash' |
| 67 | $fp.ScenarioName | Should -Be $ScenarioName |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Describe '@adr-creation activation fingerprint matches baseline' -Tag 'Unit' { |
| 72 | It 'scenario <ScenarioName> hash matches baseline.json' -ForEach $ScenarioCases { |
| 73 | $current = $script:Fingerprints[$ScenarioName] |
| 74 | $expected = $script:Baseline[$ScenarioName] |
| 75 | $expected | Should -Not -BeNullOrEmpty -Because "baseline.json must contain an entry for $ScenarioName" |
| 76 | $current.Hash | Should -Be $expected.Hash -Because @" |
| 77 | Activation load-set drift detected for scenario '$ScenarioName'. |
| 78 | Expected hash : $($expected.Hash) |
| 79 | Actual hash : $($current.Hash) |
| 80 | Expected files: $($expected.LoadedFiles | ForEach-Object { "$($_.Path) ($($_.Bytes))" } | Sort-Object | Join-String -Separator '; ') |
| 81 | Actual files : $($current.LoadedFiles | ForEach-Object { "$($_.Path) ($($_.Bytes))" } | Sort-Object | Join-String -Separator '; ') |
| 82 | If the change is intentional, recapture baseline.json by running 'npm run test:activation:baseline' (see scripts/agents/activation-harness/README.md). |
| 83 | "@ |
| 84 | } |
| 85 | |
| 86 | It 'scenario <ScenarioName> cold-start byte total matches baseline' -ForEach $ScenarioCases { |
| 87 | $current = $script:Fingerprints[$ScenarioName] |
| 88 | $expected = $script:Baseline[$ScenarioName] |
| 89 | $current.ColdStartBytes | Should -Be $expected.ColdStartBytes |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | Describe '@adr-creation cold-start byte budget' -Tag 'Unit' { |
| 94 | It 'CleanWorkspace ColdStartBytes is below the PD-04 budget' { |
| 95 | $current = $script:Fingerprints['CleanWorkspace'] |
| 96 | $current.ColdStartBytes | Should -BeLessThan $script:ColdStartBudget -Because @" |
| 97 | Cold-start byte budget violation (PD-04=A). |
| 98 | Target : less than $($script:ColdStartBudget) bytes |
| 99 | Actual : $($current.ColdStartBytes) bytes |
| 100 | Loaded : $($current.LoadedFiles | ForEach-Object { "$($_.Path) ($($_.Bytes))" } | Join-String -Separator '; ') |
| 101 | "@ |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Describe '@adr-creation lifecycle dispatch load-set composition' -Tag 'Unit' { |
| 106 | BeforeAll { |
| 107 | $script:CleanLoaded = $script:Fingerprints['CleanWorkspace'].LoadedFiles | ForEach-Object { $_.Path } |
| 108 | $script:SteadyLoaded = $script:Fingerprints['SteadyState'].LoadedFiles | ForEach-Object { $_.Path } |
| 109 | $script:GovernLoaded = $script:Fingerprints['GovernEntry'].LoadedFiles | ForEach-Object { $_.Path } |
| 110 | $script:AdoptLoaded = $script:Fingerprints['AdoptTemplate'].LoadedFiles | ForEach-Object { $_.Path } |
| 111 | } |
| 112 | |
| 113 | It 'CleanWorkspace includes the agent file itself' { |
| 114 | $script:CleanLoaded | Should -Contain '.github/agents/project-planning/adr-creation.agent.md' |
| 115 | } |
| 116 | |
| 117 | It 'CleanWorkspace includes adr-identity.instructions.md (always-on identity)' { |
| 118 | $script:CleanLoaded | Should -Contain '.github/instructions/project-planning/adr-identity.instructions.md' |
| 119 | } |
| 120 | |
| 121 | It 'CleanWorkspace includes shared disclaimer-language.instructions.md' { |
| 122 | $script:CleanLoaded | Should -Contain '.github/instructions/shared/disclaimer-language.instructions.md' |
| 123 | } |
| 124 | |
| 125 | It 'CleanWorkspace excludes adr-handoff.instructions.md (Govern on-demand)' { |
| 126 | $script:CleanLoaded | Should -Not -Contain '.github/instructions/project-planning/adr-handoff.instructions.md' |
| 127 | } |
| 128 | |
| 129 | It 'CleanWorkspace excludes adr-byo-template.instructions.md (adopt-template on-demand)' { |
| 130 | $script:CleanLoaded | Should -Not -Contain '.github/instructions/project-planning/adr-byo-template.instructions.md' |
| 131 | } |
| 132 | |
| 133 | It 'SteadyState is a strict superset of CleanWorkspace' { |
| 134 | foreach ($file in $script:CleanLoaded) { |
| 135 | $script:SteadyLoaded | Should -Contain $file |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | It 'GovernEntry includes adr-handoff.instructions.md (Govern Table A trigger)' { |
| 140 | $script:GovernLoaded | Should -Contain '.github/instructions/project-planning/adr-handoff.instructions.md' |
| 141 | } |
| 142 | |
| 143 | It 'AdoptTemplate includes adr-byo-template.instructions.md (Table B trigger)' { |
| 144 | $script:AdoptLoaded | Should -Contain '.github/instructions/project-planning/adr-byo-template.instructions.md' |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | Describe '@adr-creation activation scenarios produce distinct fingerprints' -Tag 'Unit' { |
| 149 | It 'GovernEntry hash differs from SteadyState (Govern Table A on-demand load)' { |
| 150 | $script:Fingerprints['GovernEntry'].Hash | Should -Not -Be $script:Fingerprints['SteadyState'].Hash |
| 151 | } |
| 152 | |
| 153 | It 'AdoptTemplate hash differs from SteadyState (Table B on-demand load)' { |
| 154 | $script:Fingerprints['AdoptTemplate'].Hash | Should -Not -Be $script:Fingerprints['SteadyState'].Hash |
| 155 | } |
| 156 | |
| 157 | It 'AdoptTemplate hash differs from GovernEntry (distinct dispatch tables)' { |
| 158 | $script:Fingerprints['AdoptTemplate'].Hash | Should -Not -Be $script:Fingerprints['GovernEntry'].Hash |
| 159 | } |
| 160 | |
| 161 | It 'CleanWorkspace hash differs from SteadyState (cold-start vs steady)' { |
| 162 | $script:Fingerprints['CleanWorkspace'].Hash | Should -Not -Be $script:Fingerprints['SteadyState'].Hash |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | Describe '@adr-creation activation harness emits Pester runner artifacts' -Tag 'Unit' { |
| 167 | It 'logs/pester-summary.json exists after this suite is invoked via Invoke-PesterTests.ps1' { |
| 168 | $summaryPath = Join-Path $script:RepoRoot 'logs/pester-summary.json' |
| 169 | Test-Path -LiteralPath $summaryPath | Should -BeTrue -Because 'Invoke-PesterTests.ps1 writes this file before discovery completes' |
| 170 | } |
| 171 | |
| 172 | It 'logs/pester-failures.json exists after this suite is invoked via Invoke-PesterTests.ps1' { |
| 173 | $failuresPath = Join-Path $script:RepoRoot 'logs/pester-failures.json' |
| 174 | Test-Path -LiteralPath $failuresPath | Should -BeTrue |
| 175 | } |
| 176 | } |
| 177 | |