BeforeAll { Import-Module "$PSScriptRoot/../src/State.psm1" -Force Import-Module "$PSScriptRoot/../src/Configs.psm1" -Force } Describe 'Configuration deployment' { BeforeEach { $caseRoot = Join-Path $TestDrive ([guid]::NewGuid().ToString('N')) $script:repo = Join-Path $caseRoot 'repo' $script:data = Join-Path $caseRoot 'data' $script:destination = Join-Path $caseRoot 'user/config.txt' New-Item -ItemType Directory "$repo/configs" -Force | Out-Null 'version one' | Set-Content "$repo/configs/config.txt" -NoNewline $script:settings = @{ userPaths = @{ APPDATA = $caseRoot; LOCALAPPDATA = $caseRoot; USERPROFILE = $caseRoot; PROGRAMDATA = $caseRoot } } $script:state = @{ schemaVersion = 1; configs = @{} } $script:paths = Get-ConfigCentralPaths -DataRoot $data Initialize-ConfigCentralData $paths $script:configs = @([pscustomobject]@{ name = 'sample'; source = 'configs/config.txt'; target = $destination; mode = 'copy' }) } It 'deploys and becomes idempotent' { $plan = Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings $plan[0].action | Should -Be 'Create' (Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded' (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0].action | Should -Be 'None' } It 'detects local edits and backs them up before force' { $first = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0] [void](Invoke-ConfigOperation -Operation $first -Paths $paths -State $state) 'local edit' | Set-Content $destination -NoNewline $conflict = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0] $conflict.action | Should -Be 'Conflict' (Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state).status | Should -Be 'Conflict' (Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state -Force).status | Should -Be 'Succeeded' (Get-ChildItem "$data/backups/sample/*.bak").Count | Should -Be 1 } It 'restores the newest backup and preserves the replaced file' { $first = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0] [void](Invoke-ConfigOperation -Operation $first -Paths $paths -State $state) 'local edit' | Set-Content $destination -NoNewline $conflict = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0] [void](Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state -Force) (Restore-ConfigBackup -Name sample -Paths $paths -State $state).status | Should -Be 'Restored' (Get-Content -LiteralPath $destination -Raw) | Should -Be 'local edit' (Get-ChildItem "$data/backups/sample/*.bak").Count | Should -Be 2 } It 'rejects sources outside configs' { 'secret' | Set-Content "$repo/outside.txt" { Resolve-ConfigSource -RepositoryRoot $repo -Source 'outside.txt' } | Should -Throw '*escapes configs*' } It 'handles spaces and non-ASCII target names' { $configs[0].target = Join-Path (Split-Path -Parent $repo) 'Données utilisateur/a file.txt' $plan = Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings (Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded' Test-Path -LiteralPath $configs[0].target | Should -BeTrue } It 'refuses unsafe automatic restoration of registry and hosts backups' { $registryState = @{ configs = @{ 'mpc-hc-settings' = @{ mode = 'registry'; target = 'HKCU:\Software\MPC-HC\MPC-HC' } } } { Restore-ConfigBackup -Name mpc-hc-settings -Paths $paths -State $registryState } | Should -Throw '*not supported for registry*' $hostsState = @{ configs = @{ 'windows-hosts' = @{ mode = 'hosts-block'; target = 'C:\Windows\System32\drivers\etc\hosts' } } } { Restore-ConfigBackup -Name windows-hosts -Paths $paths -State $hostsState } | Should -Throw '*not supported for hosts*' } }