#Requires -Version 5.1 Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' Import-Module (Join-Path $PSScriptRoot '../src/Wind.Core.psm1') -Force $script:passed = 0 function Assert-True { param([bool]$Condition,[string]$Message) if (-not $Condition) { throw $Message } } function Assert-Throws { param([scriptblock]$Action) $threw=$false; try { & $Action | Out-Null } catch { $threw=$true }; Assert-True $threw 'Expected an exception.' } function Run-Test { param([string]$Name,[scriptblock]$Action) & $Action; $script:passed++; Write-Output "PASS $Name" } function New-Harness { $state = [pscustomobject]@{ Values=@{}; Writes=0; FailName=''; FailAfterWrite=$false; ReadFail=$false; Saved=$null; Saves=0; FailSaveAt=0 } $store = @{ Read = { param($policy) if ($state.ReadFail) { throw 'Injected read error' } $hive=if($policy.PSObject.Properties['Hive']){[string]$policy.Hive}else{'LocalMachine'} $key=$hive+'|'+$policy.Path+'|'+$policy.Name if ($state.Values.ContainsKey($key)) { $v=$state.Values[$key] return [pscustomobject]@{ Exists=$true; Kind=$v.Kind; Value=$v.Value } } [pscustomobject]@{ Exists=$false; Kind=$null; Value=$null } }.GetNewClosure() Write = { param($policy,$value) $state.Writes++ $fail=$state.FailName -eq $policy.Name if ($fail -and -not $state.FailAfterWrite) { $state.FailName=''; throw 'Injected write error' } $hive=if($policy.PSObject.Properties['Hive']){[string]$policy.Hive}else{'LocalMachine'} $key=$hive+'|'+$policy.Path+'|'+$policy.Name if ($value.Exists) { $state.Values[$key]=[pscustomobject]@{ Kind=$value.Kind; Value=$value.Value } } else { $state.Values.Remove($key) } if ($fail) { $state.FailName=''; throw 'Injected error after write' } }.GetNewClosure() } $save = { param($journal) $state.Saves++ if ($state.Saves -eq $state.FailSaveAt) { throw 'Injected journal error' } $state.Saved=$journal | ConvertTo-Json -Depth 12 | ConvertFrom-Json }.GetNewClosure() [pscustomobject]@{ State=$state; Store=$store; Save=$save; Context=[pscustomobject]@{ Edition='Professional'; Build=26200; Revision=1 } } } function New-Plan { param($Harness,[string[]]$Ids=@('advertising-id','clipboard-sync')) @(Get-WindPlan -Context $Harness.Context -Store $Harness.Store -Id $Ids) } function Apply-Test { param($Harness,$Plan) Invoke-WindTransaction -Plan $Plan -Context $Harness.Context -Store $Harness.Store -Save $Harness.Save -InstallationId 'test-installation' } function Restore-Test { param($Harness,$Journal) Restore-WindTransaction -Journal $Journal -Store $Harness.Store -Save $Harness.Save -InstallationId 'test-installation' } Run-Test 'Home is unsupported even if a matching policy value exists' { $h=New-Harness; $h.Context.Edition='Core' $plan=New-Plan $h Assert-True (@($plan | Where-Object Status -NE 'Unsupported').Count -eq 0) 'Home policies must not be called enforced.' Assert-Throws { Apply-Test $h $plan } Assert-True ($h.State.Writes -eq 0 -and $h.State.Saves -eq 0) 'Unsupported apply touched state.' } Run-Test 'Unknown build is audit-only' { $h=New-Harness; $h.Context.Build=99999 Assert-True ((New-Plan $h)[0].Status -eq 'Unsupported') 'Unknown build accepted.' } Run-Test 'Edition-aware diagnostic level' { $h=New-Harness Assert-True ((New-Plan $h @('diagnostics'))[0].Desired -eq 1) 'Pro should retain required diagnostics.' $h.Context.Edition='Enterprise' Assert-True ((New-Plan $h @('diagnostics'))[0].Desired -eq 0) 'Enterprise should use supported minimum.' } Run-Test 'User-scoped and edition-scoped policies are planned honestly' { $h=New-Harness Assert-True ((New-Plan $h @('tailored-experiences'))[0].Desired -eq 1) 'Current-user tailored-experience policy missing.' Assert-True (@(New-Plan $h @('windows-spotlight')).Count -eq 0) 'Pro must not claim the Enterprise-only Spotlight policy.' $h.Context.Edition='Enterprise' Assert-True ((New-Plan $h @('windows-spotlight'))[0].Desired -eq 1) 'Enterprise Spotlight policy was not selected.' } Run-Test 'Update choices are explicit and preserve update services' { $h=New-Harness $keep=@(Get-WindPlan -Context $h.Context -Store $h.Store) Assert-True (@($keep | Where-Object Id -Like 'updates-*').Count -eq 0) 'Default modified update policy.' $notify=@(Get-WindPlan -Context $h.Context -Store $h.Store -Updates Notify) Assert-True (($notify | Where-Object Id -EQ 'updates-options').Desired -eq 2) 'Notify value wrong.' $manual=@(Get-WindPlan -Context $h.Context -Store $h.Store -Updates Manual) Assert-True (($manual | Where-Object Id -EQ 'updates-auto').Desired -eq 1) 'Manual value wrong.' } Run-Test 'Unknown ID fails instead of silently ignoring a typo' { $h=New-Harness; Assert-Throws { New-Plan $h @('not-a-policy') } } Run-Test 'Non-DWORD pre-existing value is never overwritten' { $h=New-Harness; $p=Get-WindCatalog | Where-Object Id -EQ 'advertising-id' $h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name]=[pscustomobject]@{ Kind='String'; Value='custom' } Assert-Throws { Apply-Test $h (New-Plan $h) } Assert-True ($h.State.Writes -eq 0) 'Conflicting type was overwritten.' } Run-Test 'Microphone per-app overrides block misleading default-deny claim' { $h=New-Harness; $p=Get-WindCatalog | Where-Object Id -EQ 'microphone-apps' $h.State.Values['LocalMachine|'+$p.Path+'|LetAppsAccessMicrophone_ForceAllowTheseApps']=[pscustomobject]@{ Kind='MultiString'; Value=@('example.app') } Assert-True ((New-Plan $h @('microphone-apps'))[0].Status -eq 'Conflict') 'Exception was missed.' } Run-Test 'Unreadable registry is a read error, not missing or configured' { $h=New-Harness; $h.State.ReadFail=$true Assert-True ((New-Plan $h)[0].Status -eq 'ReadError') 'Read error hidden.' } Run-Test 'Apply and restore preserve absence and arbitrary original DWORD' { $h=New-Harness; $p=Get-WindCatalog | Where-Object Id -EQ 'advertising-id' $h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name]=[pscustomobject]@{ Kind='DWord'; Value=7 } $j=Apply-Test $h (New-Plan $h) Assert-True ($j.Status -eq 'Applied' -and $h.State.Values.Count -eq 2) 'Apply incomplete.' $r=Restore-Test $h ($j | ConvertTo-Json -Depth 12 | ConvertFrom-Json) Assert-True ($r.Status -eq 'Restored' -and $h.State.Values.Count -eq 1) 'Restore did not preserve absence.' Assert-True ($h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name].Value -eq 7) 'Original value lost.' } Run-Test 'Reapplying configured policies makes zero registry writes' { $h=New-Harness; Apply-Test $h (New-Plan $h) | Out-Null; $count=$h.State.Writes $j=Apply-Test $h (New-Plan $h) Assert-True ($h.State.Writes -eq $count -and @($j.Entries).Count -eq 0) 'Reapply mutated configured values.' } Run-Test 'Write-ahead journal failure prevents all policy writes' { $h=New-Harness; $h.State.FailSaveAt=1 Assert-Throws { Apply-Test $h (New-Plan $h) } Assert-True ($h.State.Writes -eq 0) 'Write occurred without durable journal.' } Run-Test 'Mid-apply failure rolls back successful earlier writes' { $h=New-Harness; $h.State.FailName='AllowCrossDeviceClipboard' Assert-Throws { Apply-Test $h (New-Plan $h) } Assert-True ($h.State.Values.Count -eq 0 -and $h.State.Saved.Status -eq 'Restored') 'Rollback incomplete.' } Run-Test 'Failure after mutation also rolls back the current setting' { $h=New-Harness; $h.State.FailName='AllowCrossDeviceClipboard'; $h.State.FailAfterWrite=$true Assert-Throws { Apply-Test $h (New-Plan $h) } Assert-True ($h.State.Values.Count -eq 0) 'Current setting was not recovered.' } Run-Test 'External edits cause restore conflict and are preserved' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h) $p=Get-WindCatalog | Where-Object Id -EQ 'advertising-id' $h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name].Value=9 $r=Restore-Test $h $j Assert-True ($r.Status -eq 'RestoreIncomplete') 'Conflict not reported.' Assert-True ($h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name].Value -eq 9) 'External edit overwritten.' } Run-Test 'Crash with Writing journal status remains recoverable' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h); $j.Entries[0].Status='Writing' Restore-Test $h $j | Out-Null Assert-True ($h.State.Values.Count -eq 0) 'Writing status not recovered.' } Run-Test 'Pending entries never restore changes made by another program' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h); $j.Entries[0].Status='Pending' Restore-Test $h $j | Out-Null Assert-True ($h.State.Values.Count -eq 1) 'Never-started entry was modified.' } Run-Test 'Repeated restore does not undo subsequent configuration' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h); Restore-Test $h $j | Out-Null Apply-Test $h (New-Plan $h) | Out-Null; $writes=$h.State.Writes Restore-Test $h $j | Out-Null Assert-True ($h.State.Writes -eq $writes -and $h.State.Values.Count -eq 2) 'Repeated restore overwrote later apply.' } Run-Test 'Journal rejects unknown entries before any write' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h); $j.Entries[1].Id='untrusted-path'; $writes=$h.State.Writes Assert-Throws { Restore-Test $h $j } Assert-True ($writes -eq $h.State.Writes) 'Partial restore before validation.' } Run-Test 'Journal rejects wrong installation and invalid target' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h) $j.InstallationId='other'; Assert-Throws { Restore-Test $h $j } $j.InstallationId='test-installation'; $j.Entries[0].After.Value=999 Assert-Throws { Restore-Test $h $j } } Run-Test 'State change between planning and apply is not overwritten' { $h=New-Harness; $plan=New-Plan $h; $p=Get-WindCatalog | Where-Object Id -EQ 'advertising-id' $h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name]=[pscustomobject]@{ Kind='DWord'; Value=88 } Assert-Throws { Apply-Test $h $plan } Assert-True ($h.State.Writes -eq 0 -and $h.State.Values['LocalMachine|'+$p.Path+'|'+$p.Name].Value -eq 88) 'Stale plan wrote registry.' } Run-Test 'Journal failure after a successful write triggers recovery' { $h=New-Harness; $h.State.FailSaveAt=3 Assert-Throws { Apply-Test $h (New-Plan $h) } Assert-True ($h.State.Values.Count -eq 0 -and $h.State.Saved.Status -eq 'Restored') 'Post-write journal failure was not recovered.' } Run-Test 'Duplicate journal entry is rejected before restore' { $h=New-Harness; $j=Apply-Test $h (New-Plan $h); $j.Entries[1].Id=$j.Entries[0].Id; $writes=$h.State.Writes Assert-Throws { Restore-Test $h $j } Assert-True ($writes -eq $h.State.Writes) 'Duplicate journal triggered writes.' } Run-Test 'Durable journal creation and replacement round-trip' { $testRoot=[IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../reports')) [IO.Directory]::CreateDirectory($testRoot) | Out-Null $path=Join-Path $testRoot ([guid]::NewGuid().ToString('N')+'.journal.json') try { $h=New-Harness; $j=Apply-Test $h (New-Plan $h) Save-WindJournal -Journal $j -Path $path $loaded=Get-Content -LiteralPath $path -Raw | ConvertFrom-Json Assert-WindJournal $loaded 'test-installation' $j.Status='AppliedAgain'; Save-WindJournal -Journal $j -Path $path Assert-True ((Get-Content -LiteralPath $path -Raw | ConvertFrom-Json).Status -eq 'AppliedAgain') 'Atomic replacement failed.' } finally { # Exact single file generated in the workspace; never a recursive cleanup. if ([IO.File]::Exists($path)) { [IO.File]::Delete($path) } } } Run-Test 'Switching Strict to Standard releases strict-only restrictions' { $h=New-Harness $strict=@(Get-WindPlan -Context $h.Context -Store $h.Store -Profile Strict) Apply-Test $h $strict | Out-Null $standard=@(Get-WindPlan -Context $h.Context -Store $h.Store -Profile Standard) Assert-True (($standard | Where-Object Id -eq 'clipboard-history').Desired -eq 1) 'Standard left history disabled.' Assert-True (($standard | Where-Object Id -eq 'delivery-optimization').Desired -eq 0) 'Standard retained strict DO mode.' Apply-Test $h $standard | Out-Null Assert-True (@(Get-WindPlan -Context $h.Context -Store $h.Store -Profile Standard | Where-Object Status -ne 'Configured').Count -eq 0) 'Mode change incomplete.' } Write-Output "$script:passed tests passed. No Windows policy settings were modified."