BeforeAll { Import-Module "$PSScriptRoot/../src/Adapters.psm1" -Force } Describe 'Application configuration adapters' { It 'merges only declared INI keys and preserves other values' { $target = Join-Path $TestDrive 'nextcloud.cfg' $overlay = Join-Path $TestDrive 'overlay.ini' "[General]`nexisting=keep`nchunkSize=10`n`n[Account0]`nauthToken=secret" | Set-Content $target "[General]`nchunkSize=20`nnewValue=yes" | Set-Content $overlay $desired = Read-IniValues -Path $overlay $merged = Merge-IniFile -Target $target -Desired $desired $merged | Should -Match 'existing=keep' $merged | Should -Match 'chunkSize=20' $merged | Should -Match 'newValue=yes' $merged | Should -Match 'authToken=secret' } It 'treats a comment-only INI overlay as a no-op' { $source = Join-Path $TestDrive 'empty.ini' '# Add managed keys here.' | Set-Content $source $config = [pscustomobject]@{ name = 'safe'; mode = 'ini-overlay' } (Get-AdapterPlan -Config $config -Source $source -Target (Join-Path $TestDrive 'target.ini') -Record $null).action | Should -Be 'None' } It 'validates hosts entries and replaces only its named block' { $source = Join-Path $TestDrive 'hosts.entries' $target = Join-Path $TestDrive 'hosts' "127.0.0.1 before.example`n# ignored" | Set-Content $source "10.0.0.1 outside.example`n`n# BEGIN CONFIG CENTRAL: managed-hosts`n127.0.0.1 old.example`n# END CONFIG CENTRAL: managed-hosts" | Set-Content $target $lines = Get-HostsDeclaration -Path $source $content = Set-HostsBlockContent -Target $target -Name managed-hosts -Lines $lines $content | Should -Match '10.0.0.1 outside.example' $content | Should -Match '127.0.0.1 before.example' $content | Should -Not -Match 'old.example' } It 'rejects invalid hosts addresses' { $source = Join-Path $TestDrive 'bad-hosts.entries' 'not-an-address example.test' | Set-Content $source { Get-HostsDeclaration -Path $source } | Should -Throw '*Invalid hosts entry*' } It 'accepts constrained registry declarations and rejects escaping paths' { $valid = Join-Path $TestDrive 'registry.json' '{"schemaVersion":1,"values":[{"path":"Settings","name":"RememberFilePos","type":"DWord","value":1}]}' | Set-Content $valid (Read-RegistryDeclaration -Path $valid).values.Count | Should -Be 1 $invalid = Join-Path $TestDrive 'invalid-registry.json' '{"schemaVersion":1,"values":[{"path":"..\\Other","name":"Value","type":"String","value":"x"}]}' | Set-Content $invalid { Read-RegistryDeclaration -Path $invalid } | Should -Throw '*Unsafe registry subpath*' } } Describe 'Registry integer deployment' { It 'accepts signed exported DWORD and QWORD values without changing their bits' { InModuleScope Adapters { Mock New-Item {} Mock New-ItemProperty {} $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'Dword'; type = 'DWord'; value = -1 }, [pscustomobject]@{ path = ''; name = 'Qword'; type = 'QWord'; value = [long]::MinValue } ) } Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration Should -Invoke New-ItemProperty -Times 1 -ParameterFilter { $Name -eq 'Dword' -and $Value -eq -1 -and $Value -is [int32] } Should -Invoke New-ItemProperty -Times 1 -ParameterFilter { $Name -eq 'Qword' -and $Value -eq [long]::MinValue -and $Value -is [int64] } } } It 'deploys every checked-in MPC-HC value through mocked registry operations' { $declaration = Read-RegistryDeclaration -Path "$PSScriptRoot/../configs/mpc-hc/settings.json" InModuleScope Adapters -Parameters @{ Declaration = $declaration; Root = $TestDrive } { param($Declaration, $Root) Mock New-Item {} Mock New-ItemProperty {} { Set-RegistryDeclaration -Root $Root -Declaration $Declaration } | Should -Not -Throw Should -Invoke New-ItemProperty -Times $Declaration.values.Count -Exactly } } It 'rejects negative values outside the DWORD range' { InModuleScope Adapters { Mock New-Item {} Mock New-ItemProperty {} $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'Invalid'; type = 'DWord'; value = -2147483649 } ) } (Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration).failures.Count | Should -Be 1 Should -Invoke New-ItemProperty -Times 0 } } } Describe 'Registry key preservation' { It 'preserves existing values while applying multiple values to the same key' { InModuleScope Adapters { $script:stored = @{ Existing = 'keep' } Mock Test-Path { $true } Mock New-Item { throw 'Existing key must not be recreated' } Mock New-ItemProperty { $script:stored[$Name] = $Value } $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'First'; type = 'DWord'; value = 1 }, [pscustomobject]@{ path = ''; name = 'Second'; type = 'DWord'; value = 2 } ) } Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration $script:stored.Existing | Should -Be 'keep' $script:stored.First | Should -Be 1 $script:stored.Second | Should -Be 2 Should -Invoke New-Item -Times 0 } } It 'creates a missing key without forcing replacement' { InModuleScope Adapters { Mock Test-Path { $false } Mock New-Item {} Mock New-ItemProperty {} $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'First'; type = 'DWord'; value = 1 } ) } Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration Should -Invoke New-Item -Times 1 -ParameterFilter { -not $Force } } } } Describe 'MPC-HC running player protection' { It 'refuses registry writes when either player is running' { InModuleScope Adapters { Mock Get-Process { [pscustomobject]@{ ProcessName = 'mpc-hc64' } } Mock New-Item {} Mock New-ItemProperty {} $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'Volume'; type = 'DWord'; value = 80 } ) } { Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration } | Should -Throw '*Close both MPC-HC and MPC-HC x64*' Should -Invoke Get-Process -Times 1 -ParameterFilter { $Name -contains 'mpc-hc' -and $Name -contains 'mpc-hc64' } Should -Invoke New-Item -Times 0 Should -Invoke New-ItemProperty -Times 0 } } } Describe 'Missing MPC-HC settings guidance' { It 'collects key-creation errors without stopping the declaration' { InModuleScope Adapters { Mock Get-Process { $null } Mock Test-Path { $false } Mock New-Item { throw 'Access denied' } Mock New-ItemProperty {} $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'Volume'; type = 'DWord'; value = 80 } ) } $result = Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration $result.written | Should -Be 0 $result.failures[0].error | Should -Be 'Access denied' Should -Invoke New-ItemProperty -Times 0 } } } Describe 'Partial registry writes' { It 'continues after HWAcceleration fails and writes subsequent keys' { InModuleScope Adapters { Mock Get-Process { $null } Mock Test-Path { $true } Mock New-ItemProperty { if ($Name -eq 'HWAcceleration') { throw 'Unsupported value' } } $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'HWAcceleration'; type = 'DWord'; value = 1 }, [pscustomobject]@{ path = ''; name = 'Volume'; type = 'DWord'; value = 80 } ) } $result = Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration $result.written | Should -Be 1 $result.failures[0].name | Should -Be 'HWAcceleration' Should -Invoke New-ItemProperty -Times 1 -ParameterFilter { $Name -eq 'Volume' } } } } Describe 'Registry rollback' { It 'restores original types selectively and removes originally absent values' { InModuleScope Adapters { $backup = @{ values = @( @{ path = ''; name = 'Volume'; exists = $true; type = 'DWord'; value = -1 }, @{ path = ''; name = 'NewValue'; exists = $false; type = $null; value = $null }, @{ path = ''; name = 'Successful'; exists = $true; type = 'String'; value = 'old' } ) } Mock Get-RegistryBackup { @{ values = @(@{ exists = $true; type = 'String'; value = 'changed' }) } } Mock Set-RegistryDeclaration { @{ written = 1; failures = @() } } Mock Remove-ItemProperty {} Restore-RegistryDeclaration -Root TestRoot -Backup $backup -Only @($backup.values[0], $backup.values[1]) Should -Invoke Set-RegistryDeclaration -Times 1 -Exactly -ParameterFilter { $Declaration.values[0].name -eq 'Volume' -and $Declaration.values[0].type -eq 'DWord' -and $Declaration.values[0].value -eq -1 } Should -Invoke Remove-ItemProperty -Times 1 -Exactly -ParameterFilter { $Name -eq 'NewValue' } } } It 'avoids rewriting unchanged values after a denied write' { InModuleScope Adapters { $entry = @{ path = ''; name = 'HWAcceleration'; exists = $true; type = 'DWord'; value = 1 } Mock Get-RegistryBackup { @{ values = @(@{ exists = $true; type = 'DWord'; value = 1 }) } } Mock Set-RegistryDeclaration { throw 'Access denied' } Restore-RegistryDeclaration -Root TestRoot -Backup @{ values = @($entry) } Should -Invoke Set-RegistryDeclaration -Times 0 -Exactly } } It 'continues restoring remaining values when one rollback fails' { InModuleScope Adapters { $backup = @{ values = @( @{ path = ''; name = 'First'; exists = $true; type = 'String'; value = 'old' }, @{ path = ''; name = 'Second'; exists = $true; type = 'String'; value = 'old' } ) } Mock Get-RegistryBackup { @{ values = @(@{ exists = $false }) } } Mock Set-RegistryDeclaration { @{ written = 0; failures = @(@{ error = 'Access denied' }) } } { Restore-RegistryDeclaration -Root TestRoot -Backup $backup } | Should -Throw '*rollback incomplete*' Should -Invoke Set-RegistryDeclaration -Times 2 -Exactly } } It 'backs up unexpanded values with their original types and presence' { InModuleScope Adapters { Mock Test-Path { $true } Mock Get-Item { $key = [pscustomobject]@{} $key | Add-Member ScriptMethod GetValueNames { @('Existing') } $key | Add-Member ScriptMethod GetValueKind { param($name) 'ExpandString' } $key | Add-Member ScriptMethod GetValue { param($name, $default, $options) if ($options -ne [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) { throw 'Expanded value' } '%USERPROFILE%\original' } $key | Add-Member ScriptMethod Close {} $key } $backup = Get-RegistryBackup -Root TestRoot -Declaration @{ values = @( @{ path = ''; name = 'Existing' }, @{ path = ''; name = 'Missing' } ) } $backup.values[0].type | Should -Be 'ExpandString' $backup.values[0].value | Should -Be '%USERPROFILE%\original' $backup.values[1].exists | Should -BeFalse } } }