206 lines
13 KiB
PowerShell
206 lines
13 KiB
PowerShell
BeforeAll {
|
|
Import-Module "$PSScriptRoot/../src/State.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/Repository.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/WindowsSettings.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/Adapters.psm1" -Force
|
|
Import-Module "$PSScriptRoot/../src/Configs.psm1" -Force
|
|
}
|
|
|
|
Describe 'Windows setting declarations' {
|
|
It 'accepts seconds including never and rejects malformed or additional settings' {
|
|
$path = Join-Path $TestDrive power.json
|
|
foreach ($json in @('{"schemaVersion":1,"batteryDisplayTimeoutSeconds":0}', '{"schemaVersion":1,"batteryDisplayTimeoutSeconds":90}')) {
|
|
$json | Set-Content $path
|
|
{ Read-PowerDeclaration $path } | Should -Not -Throw
|
|
}
|
|
foreach ($json in @('{"schemaVersion":1,"batteryDisplayTimeoutSeconds":-1}', '{"schemaVersion":1,"batteryDisplayTimeoutSeconds":"0"}', '{"schemaVersion":1,"batteryDisplayTimeoutSeconds":0.5}', '{"schemaVersion":1,"batteryDisplayTimeoutSeconds":0,"sleep":0}')) {
|
|
$json | Set-Content $path
|
|
{ Read-PowerDeclaration $path } | Should -Throw
|
|
}
|
|
}
|
|
|
|
It 'expands No Sounds into current event assignments without changing other schemes' {
|
|
Mock Get-SoundDeclaration {
|
|
[pscustomobject]@{ schemaVersion = 1; values = @(
|
|
[pscustomobject]@{ path = ''; name = ''; type = 'String'; value = '.Default' },
|
|
[pscustomobject]@{ path = 'Apps\.Default\MailBeep\.Current'; name = ''; type = 'ExpandString'; value = '%WINDIR%\Media\notify.wav' }
|
|
) }
|
|
} -ModuleName WindowsSettings
|
|
$value = Read-SoundDeclaration "$PSScriptRoot/../configs/windows/sounds.json"
|
|
$value.values[0].value | Should -Be '.None'
|
|
$value.values[1].value | Should -BeExactly ''
|
|
$value.values[1].type | Should -Be 'ExpandString'
|
|
}
|
|
|
|
It 'accepts captured assignments but rejects arbitrary registry paths' {
|
|
$path = Join-Path $TestDrive sounds.json
|
|
foreach ($subpath in @('Apps\.Default\MailBeep\.Current', 'Apps\..\MailBeep\.Current', 'Software\Other')) {
|
|
@{ schemaVersion = 1; values = @(@{ path = $subpath; name = ''; type = 'String'; value = '' }) } | ConvertTo-Json -Depth 5 | Set-Content $path
|
|
if ($subpath -eq 'Apps\.Default\MailBeep\.Current') { { Read-SoundDeclaration $path } | Should -Not -Throw }
|
|
else { { Read-SoundDeclaration $path } | Should -Throw }
|
|
}
|
|
}
|
|
}
|
|
|
|
Describe 'Windows setting planning and capture' {
|
|
BeforeEach {
|
|
$script:repo = Join-Path $TestDrive ([guid]::NewGuid().ToString('N'))
|
|
New-Item -ItemType Directory "$repo/configs/windows" -Force | Out-Null
|
|
Copy-Item "$PSScriptRoot/../configs/windows/power.json" "$repo/configs/windows/power.json"
|
|
Copy-Item "$PSScriptRoot/../configs/windows/sounds.json" "$repo/configs/windows/sounds.json"
|
|
$script:powerConfig = [pscustomobject]@{ name = 'windows-power'; mode = 'windows-power'; target = 'active-power-scheme'; source = 'configs/windows/power.json' }
|
|
$script:soundConfig = [pscustomobject]@{ name = 'windows-sounds'; mode = 'windows-sounds'; target = 'HKCU:\AppEvents\Schemes'; source = 'configs/windows/sounds.json' }
|
|
}
|
|
|
|
It 'plans power changes without writes and detects local drift and matching state' {
|
|
Mock Get-PowerSettings { @{ scheme = 'plan-a'; batteryDisplayTimeoutSeconds = 120 } } -ModuleName Adapters
|
|
$first = Get-AdapterPlan $powerConfig "$repo/configs/windows/power.json" 'active-power-scheme' $null
|
|
$first.action | Should -Be 'Conflict'
|
|
$first.requiresAdmin | Should -BeFalse
|
|
$record = @{ deployedHash = $first.currentHash }
|
|
(Get-AdapterPlan $powerConfig "$repo/configs/windows/power.json" 'active-power-scheme' $record).action | Should -Be 'Update'
|
|
Mock Get-PowerSettings { @{ scheme = 'plan-a'; batteryDisplayTimeoutSeconds = 0 } } -ModuleName Adapters
|
|
(Get-AdapterPlan $powerConfig "$repo/configs/windows/power.json" 'active-power-scheme' @{ deployedHash = $first.desiredHash }).action | Should -Be 'None'
|
|
(Get-AdapterPlan $powerConfig "$repo/configs/windows/power.json" 'active-power-scheme' $record).action | Should -Be 'Conflict'
|
|
}
|
|
|
|
It 'treats unchanged sound values as current and preserves types in comparison' {
|
|
Mock Read-SoundDeclaration { @{ values = @([pscustomobject]@{ path = ''; name = ''; type = 'String'; value = '.None' }) } } -ModuleName Adapters
|
|
Mock Get-RegistryBackup { @{ values = @([pscustomobject]@{ path = ''; name = ''; type = 'String'; value = '.None'; exists = $true }) } } -ModuleName Adapters
|
|
$first = Get-AdapterPlan $soundConfig "$repo/configs/windows/sounds.json" $soundConfig.target $null
|
|
$first.action | Should -Be 'Adopt'
|
|
(Get-AdapterPlan $soundConfig "$repo/configs/windows/sounds.json" $soundConfig.target @{ deployedHash = $first.currentHash }).action | Should -Be 'None'
|
|
}
|
|
|
|
It 'captures portable silence and power seconds at repository-relative paths without a plan GUID' {
|
|
Mock Get-RepositoryRevision { 'abc' } -ModuleName Configs
|
|
Mock Get-ConfigLocations { @(
|
|
[pscustomobject]@{ name = 'windows-sounds'; targetExists = $true },
|
|
[pscustomobject]@{ name = 'windows-power'; targetExists = $true }
|
|
) } -ModuleName Configs
|
|
Mock Get-SoundDeclaration { @{ schemaVersion = 1; values = @(@{ path = ''; name = ''; type = 'String'; value = '.None' }, @{ path = 'Apps\.Default\MailBeep\.Current'; name = ''; type = 'String'; value = '' }) } } -ModuleName Configs
|
|
Mock Get-PowerSettings { @{ scheme = 'local-only'; batteryDisplayTimeoutSeconds = 90 } } -ModuleName Configs
|
|
$capture = Export-ConfigCapture -Configs @($soundConfig, $powerConfig) -RepositoryRoot $repo -Settings @{} -Machine test -OutputPath "$repo/capture"
|
|
$capture.operations.status | Should -Not -Contain 'Failed'
|
|
$sounds = Get-Content "$repo/capture/configs/windows/sounds.json" -Raw | ConvertFrom-Json
|
|
$sounds.disabled | Should -BeTrue
|
|
$power = Read-PowerDeclaration "$repo/capture/configs/windows/power.json"
|
|
$power.batteryDisplayTimeoutSeconds | Should -Be 90
|
|
$power.Keys | Should -Not -Contain 'scheme'
|
|
}
|
|
}
|
|
|
|
Describe 'Windows setting backup and rollback' {
|
|
BeforeEach {
|
|
$script:paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive ([guid]::NewGuid().ToString('N')))
|
|
Initialize-ConfigCentralData $paths
|
|
$script:state = @{ configs = @{} }
|
|
$script:operation = [pscustomobject]@{ name = 'windows-power'; action = 'Update'; mode = 'windows-power'; target = 'active-power-scheme'; sourceHash = 'new'; desired = @{ batteryDisplayTimeoutSeconds = 0 } }
|
|
Mock Get-PowerSettings { @{ scheme = 'original-plan'; batteryDisplayTimeoutSeconds = 120 } } -ModuleName Configs
|
|
Mock Set-PowerSettings {} -ModuleName Configs
|
|
}
|
|
|
|
It 'backs up the original plan before applying its battery timeout' {
|
|
(Invoke-ConfigOperation $operation $paths $state).status | Should -Be 'Succeeded'
|
|
$backup = Get-Content $state.configs['windows-power'].lastBackup -Raw | ConvertFrom-Json
|
|
$backup.scheme | Should -Be 'original-plan'
|
|
$backup.batteryDisplayTimeoutSeconds | Should -Be 120
|
|
Should -Invoke Set-PowerSettings -ModuleName Configs -Times 1 -Exactly -ParameterFilter { $Settings.scheme -eq 'original-plan' -and $Settings.batteryDisplayTimeoutSeconds -eq 0 }
|
|
}
|
|
|
|
It 'restores the original timeout after a failed write' {
|
|
Mock Set-PowerSettings { if ($Settings.batteryDisplayTimeoutSeconds -eq 0) { throw 'Policy denied' } } -ModuleName Configs
|
|
$result = Invoke-ConfigOperation $operation $paths $state
|
|
$result.status | Should -Be 'Failed'
|
|
$result.message | Should -Match 'Policy denied; previous power setting restored'
|
|
Should -Invoke Set-PowerSettings -ModuleName Configs -Times 1 -Exactly -ParameterFilter { $Settings.batteryDisplayTimeoutSeconds -eq 120 -and $Settings.scheme -eq 'original-plan' }
|
|
$state.configs.ContainsKey('windows-power') | Should -BeFalse
|
|
}
|
|
|
|
It 'reports failed rollback with its backup location' {
|
|
Mock Set-PowerSettings { throw 'Policy denied' } -ModuleName Configs
|
|
$result = Invoke-ConfigOperation $operation $paths $state
|
|
$result.message | Should -Match 'rollback incomplete. Backup:'
|
|
$result.status | Should -Be 'Failed'
|
|
}
|
|
|
|
It 'does not write if backup persistence fails' {
|
|
Mock Write-JsonAtomic { throw 'Disk full' } -ModuleName Configs
|
|
(Invoke-ConfigOperation $operation $paths $state).status | Should -Be 'Failed'
|
|
Should -Invoke Set-PowerSettings -ModuleName Configs -Times 0 -Exactly
|
|
}
|
|
|
|
It 'rolls back all sound assignments on any failed sound write' {
|
|
$operation.mode = 'windows-sounds'; $operation.name = 'windows-sounds'; $operation.target = 'HKCU:\AppEvents\Schemes'
|
|
Mock Get-RegistryBackup { @{ values = @() } } -ModuleName Configs
|
|
Mock Set-RegistryDeclaration { @{ written = 2; failures = @(@{ path = 'Apps\.Default\MailBeep\.Current'; name = ''; error = 'Denied' }) } } -ModuleName Configs
|
|
Mock Restore-RegistryDeclaration {} -ModuleName Configs
|
|
(Invoke-ConfigOperation $operation $paths $state).status | Should -Be 'Failed'
|
|
Should -Invoke Restore-RegistryDeclaration -ModuleName Configs -Times 1 -Exactly -ParameterFilter { $null -eq $Only }
|
|
}
|
|
}
|
|
|
|
Describe 'Sound registry edge cases' {
|
|
It 'includes events without current keys or default values as silent assignments' {
|
|
InModuleScope WindowsSettings {
|
|
Mock Get-ChildItem {
|
|
param($LiteralPath)
|
|
if ($LiteralPath -like '*\Apps') { [pscustomobject]@{ PSChildName = '.Default'; PSPath = 'app' } }
|
|
else { @([pscustomobject]@{ PSChildName = 'Missing' }, [pscustomobject]@{ PSChildName = 'Empty' }) }
|
|
}
|
|
Mock Get-Item {
|
|
param($LiteralPath)
|
|
if ($LiteralPath -like '*\Missing\.Current') { return $null }
|
|
$key = [pscustomobject]@{}
|
|
$key | Add-Member ScriptMethod GetValue { param($name) '.None' }
|
|
$key | Add-Member ScriptMethod GetValueNames { @() }
|
|
$key | Add-Member ScriptMethod Close {}
|
|
$key
|
|
}
|
|
$value = Get-SoundDeclaration
|
|
$value.values.Count | Should -Be 3
|
|
$value.values[1].value | Should -BeExactly ''
|
|
$value.values[2].value | Should -BeExactly ''
|
|
}
|
|
}
|
|
|
|
It 'writes default registry values through a writable handle and preserves empty strings' {
|
|
InModuleScope Adapters {
|
|
$script:assigned = $null
|
|
Mock Test-Path { $true }
|
|
Mock Get-Item {
|
|
$key = [pscustomobject]@{}
|
|
$key | Add-Member ScriptMethod OpenSubKey { param($path, $writable)
|
|
if (-not $writable) { throw 'Read-only handle' }
|
|
$this
|
|
}
|
|
$key | Add-Member ScriptMethod SetValue { param($name, $value, $type)
|
|
$script:assigned = @{ name = $name; value = $value; type = $type }
|
|
}
|
|
$key | Add-Member ScriptMethod Close {}
|
|
$key
|
|
}
|
|
$result = Set-RegistryDeclaration -Root 'HKCU:\AppEvents\Schemes' -Declaration @{ values = @(@{ path = ''; name = ''; type = 'String'; value = '' }) }
|
|
$result.written | Should -Be 1
|
|
$result.failures.Count | Should -Be 0
|
|
$script:assigned.name | Should -BeExactly ''
|
|
$script:assigned.value | Should -BeExactly ''
|
|
}
|
|
}
|
|
|
|
It 'round trips a custom sound assignment without applying it during capture' {
|
|
Mock Get-RepositoryRevision { 'abc' } -ModuleName Configs
|
|
Mock Get-ConfigLocations { [pscustomobject]@{ name = 'windows-sounds'; targetExists = $true } } -ModuleName Configs
|
|
Mock Get-SoundDeclaration { [pscustomobject]@{ schemaVersion = 1; values = @(
|
|
[pscustomobject]@{ path = ''; name = ''; type = 'String'; value = '.Default' },
|
|
[pscustomobject]@{ path = 'Apps\.Default\MailBeep\.Current'; name = ''; type = 'ExpandString'; value = '%WINDIR%\Media\notify.wav' }
|
|
) } } -ModuleName Configs
|
|
Mock Set-RegistryDeclaration { throw 'Capture must not write' } -ModuleName Configs
|
|
$config = [pscustomobject]@{ name = 'windows-sounds'; mode = 'windows-sounds'; source = 'configs/windows/sounds.json' }
|
|
$output = Join-Path $TestDrive custom
|
|
$result = Export-ConfigCapture -Configs @($config) -RepositoryRoot $TestDrive -Settings @{} -Machine test -OutputPath $output
|
|
(Read-SoundDeclaration "$output/configs/windows/sounds.json").values[1].value | Should -Be '%WINDIR%\Media\notify.wav'
|
|
Should -Invoke Set-RegistryDeclaration -ModuleName Configs -Times 0 -Exactly
|
|
}
|
|
}
|