BeforeAll { Import-Module "$PSScriptRoot/../src/State.psm1" -Force Import-Module "$PSScriptRoot/../src/Repository.psm1" -Force Import-Module "$PSScriptRoot/../src/Adapters.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 'reports repository and computer config locations without changing them' { New-Item -ItemType Directory (Split-Path -Parent $destination) -Force | Out-Null 'computer value' | Set-Content $destination -NoNewline $locations = Get-ConfigLocations -Configs $configs -RepositoryRoot $repo -Settings $settings -Name sample $locations.Count | Should -Be 1 $locations[0].source | Should -Be (Join-Path $repo 'configs/config.txt') $locations[0].target | Should -Be $destination $locations[0].targetExists | Should -BeTrue (Get-Content -LiteralPath $destination -Raw) | Should -Be 'computer value' { Get-ConfigLocations -Configs $configs -RepositoryRoot $repo -Settings $settings -Name missing } | Should -Throw "*Unknown config 'missing'*" } It 'captures safe files for upload and excludes the complete Nextcloud config' { Mock Get-RepositoryRevision { 'abc123' } -ModuleName Configs $xnviewTarget = Join-Path $caseRoot 'user/xnview.ini' $nextcloudTarget = Join-Path $caseRoot 'user/nextcloud.cfg' New-Item -ItemType Directory (Split-Path -Parent $xnviewTarget) -Force | Out-Null 'computer value' | Set-Content $destination -NoNewline 'portable=true' | Set-Content $xnviewTarget -NoNewline "[General]`nlaunch=false`n[Accounts]`ntoken=never-export-this" | Set-Content $nextcloudTarget -NoNewline '; overlay' | Set-Content "$repo/configs/xnview.ini" "[General]`nlaunch=true" | Set-Content "$repo/configs/nextcloud.ini" $captureConfigs = @( $configs[0], [pscustomobject]@{ name = 'xnviewmp-settings'; source = 'configs/xnview.ini'; target = $xnviewTarget; mode = 'ini-overlay' }, [pscustomobject]@{ name = 'nextcloud-settings'; source = 'configs/nextcloud.ini'; target = $nextcloudTarget; mode = 'ini-overlay' } ) $output = Join-Path $caseRoot 'capture' $capture = Export-ConfigCapture -Configs $captureConfigs -RepositoryRoot $repo -Settings $settings -Machine test -OutputPath $output (Get-Content "$output/configs/config.txt" -Raw) | Should -Be 'computer value' (Get-Content "$output/review/xnviewmp-settings/xnview.ini" -Raw) | Should -Be 'portable=true' $nextcloudCapture = Get-Content "$output/configs/nextcloud.ini" -Raw $nextcloudCapture | Should -Match 'launch=false' $nextcloudCapture | Should -Not -Match 'token' ($capture.operations | Where-Object name -eq 'nextcloud-settings').status | Should -Be 'Captured' (Get-Content "$output/capture.json" -Raw | ConvertFrom-Json).repositoryRevision | Should -Be 'abc123' } It 'adopts matching values without trying to clean up a nonexistent temporary file' { $operation = [pscustomobject]@{ name = 'adopted'; action = 'Adopt'; mode = 'ini-overlay' target = $destination; sourceHash = 'matching' } $result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state $result.status | Should -Be 'Succeeded' $state.configs.adopted.sourceHash | Should -Be 'matching' } It 'keeps the original registry failure when no temporary file was created' { Mock Get-RegistrySnapshot { throw 'Registry access denied' } -ModuleName Configs $operation = [pscustomobject]@{ name = 'registry-test'; action = 'Update'; mode = 'registry' target = 'HKCU:\Software\Test'; sourceHash = 'new'; desired = @{ values = @() } } $result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state $result.status | Should -Be 'Failed' $result.message | Should -Be 'Registry access denied' } 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*' } }