config-central/tests/Adapters.Tests.ps1

141 lines
6.9 KiB
PowerShell

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 } | Should -Throw
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
}
}
}