# test_guards.ps1 -- check both guards on examples that MUST be blocked and MUST pass. # Run after every change of a guard: pwsh -NoProfile -File test_guards.ps1 # A guard that fails silently lets everything through; this test catches exactly that. # Put it next to the guards (\_shared\hooks\). ASCII-only source, like the guards. $here = Split-Path -Parent $MyInvocation.MyCommand.Path $fail = 0 function Test-Guard($guard, $tool, $toolInput, $expectDeny, $name) { $json = @{ tool_name = $tool; tool_input = $toolInput } | ConvertTo-Json -Compress -Depth 5 $out = $json | pwsh -NoProfile -ExecutionPolicy Bypass -File (Join-Path $here $guard) $denied = [string]$out -match '"permissionDecision":"deny"' if ($denied -eq $expectDeny) { Write-Host "ok $name" } else { Write-Host "FAIL $name (expected $(if ($expectDeny) {'deny'} else {'allow'}))"; $script:fail++ } } $g = 'edit_path_guard.ps1' Test-Guard $g 'Edit' @{ file_path = 'C:\work\_sessions\_journal.md' } $true 'edit of the journal is blocked' Test-Guard $g 'Write' @{ file_path = 'C:\work\site\deploy_log.md' } $true 'rewrite of a *_log.md is blocked' Test-Guard $g 'Edit' @{ file_path = 'C:\work\site\.env' } $true '.env is blocked' Test-Guard $g 'Edit' @{ file_path = 'C:\work\site\.env.local' } $true '.env.local is blocked' Test-Guard $g 'Write' @{ file_path = 'C:\Users\me\.ssh\id_ed25519' } $true 'private SSH key is blocked' Test-Guard $g 'Edit' @{ file_path = 'C:\work\site\.env.example' } $false '.env.example passes' Test-Guard $g 'Edit' @{ file_path = 'C:\work\site\HANDOFF\blog.md' } $false 'own handoff file passes' Test-Guard $g 'Write' @{ file_path = 'C:\Users\me\.ssh\id_ed25519.pub' } $false 'public key passes' $s = 'secret_in_doc_guard.ps1' Test-Guard $s 'Write' @{ file_path = 'C:\work\notes\server.md'; content = 'db password: Qw7rT9pL2x' } $true 'password value in a note is blocked' Test-Guard $s 'Edit' @{ file_path = 'C:\work\README.md'; new_string = 'api_key = sk4f8a9d2c7e1b' } $true 'api key value in a document is blocked' Test-Guard $s 'Write' @{ file_path = 'C:\work\notes\server.md'; content = 'db password: see .env' } $false 'pointer to .env passes' Test-Guard $s 'Write' @{ file_path = 'C:\work\notes\server.md'; content = 'token = ' } $false 'placeholder passes' Test-Guard $s 'Write' @{ file_path = 'C:\work\app\settings.py'; content = 'password = Qw7rT9pL2x' } $false 'code files are out of scope' if ($fail) { Write-Host "`n$fail check(s) failed"; exit 1 } else { Write-Host "`nall checks passed" }