From 73e8f01528f8be8200a5cbc3f668cb5c22a16583 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 16 Oct 2026 13:50:39 +0200 Subject: [PATCH] Add focused PowerShell behavior tests --- TASKS.md | 10 ++--- tests/Configs.Tests.ps1 | 60 +++++++++++++++++++++++++++++ tests/Manifest.Tests.ps1 | 33 ++++++++++++++++ tests/Packages.Tests.ps1 | 36 +++++++++++++++++ tests/Repository.Tests.ps1 | 42 ++++++++++++++++++++ tests/State.Tests.ps1 | 22 +++++++++++ tests/Windows.Integration.Tests.ps1 | 17 ++++++++ 7 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 tests/Configs.Tests.ps1 create mode 100644 tests/Manifest.Tests.ps1 create mode 100644 tests/Packages.Tests.ps1 create mode 100644 tests/Repository.Tests.ps1 create mode 100644 tests/State.Tests.ps1 create mode 100644 tests/Windows.Integration.Tests.ps1 diff --git a/TASKS.md b/TASKS.md index 2d1eaf4..73fd5ad 100644 --- a/TASKS.md +++ b/TASKS.md @@ -78,14 +78,14 @@ Complete the core CLI and manual workflow before optional automation or a TUI. ## 7. Validate and document the manual workflow -- [ ] Add focused Pester tests for manifest validation and profile resolution. -- [ ] Test config backup, conflict, restore, and idempotent rerun behavior using +- [x] Add focused Pester tests for manifest validation and profile resolution. +- [x] Test config backup, conflict, restore, and idempotent rerun behavior using temporary directories. -- [ ] Test repository snapshot selection, dirty checkout handling, and fetch failure. -- [ ] Test package result handling and partial failure with mocked WinGet calls. +- [x] Test repository snapshot selection, dirty checkout handling, and fetch failure. +- [x] Test package result handling and partial failure with mocked WinGet calls. - [ ] Test fresh bootstrap and a second no-change apply on disposable Windows. - [ ] Test elevation using both the same account and separate admin credentials. -- [ ] Test paths containing spaces/non-ASCII characters and interrupted runs. +- [x] Test paths containing spaces/non-ASCII characters and interrupted runs. - [ ] Test config-only updates and recovery after an installer requests a reboot. - [ ] Write README instructions for onboarding a machine, editing profiles, syncing, resolving conflicts, restoring configs, and finding logs. diff --git a/tests/Configs.Tests.ps1 b/tests/Configs.Tests.ps1 new file mode 100644 index 0000000..ed01134 --- /dev/null +++ b/tests/Configs.Tests.ps1 @@ -0,0 +1,60 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../src/State.psm1" -Force + Import-Module "$PSScriptRoot/../src/Configs.psm1" -Force +} + +Describe 'Configuration deployment' { + BeforeEach { + $script:repo = Join-Path $TestDrive 'repo' + $script:data = Join-Path $TestDrive 'data' + $script:destination = Join-Path $TestDrive '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 = $TestDrive; LOCALAPPDATA = $TestDrive; USERPROFILE = $TestDrive; PROGRAMDATA = $TestDrive } } + $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 $TestDrive '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 + } +} diff --git a/tests/Manifest.Tests.ps1 b/tests/Manifest.Tests.ps1 new file mode 100644 index 0000000..b285e8d --- /dev/null +++ b/tests/Manifest.Tests.ps1 @@ -0,0 +1,33 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../src/Manifest.psm1" -Force +} + +Describe 'Manifest resolution' { + It 'resolves the checked-in example in profile order' { + $result = Resolve-MachineManifest -RepositoryRoot "$PSScriptRoot/.." -Machine 'example-desktop' + $result.packages.Count | Should -Be 3 + $result.configs.Count | Should -Be 2 + $result.packages[0].id | Should -Be 'Git.Git' + } + + It 'lets later profiles replace and disable earlier items' { + $root = Join-Path $TestDrive 'repo' + New-Item -ItemType Directory "$root/profiles", "$root/machines" | Out-Null + '{"schemaVersion":1,"name":"one","packages":[{"id":"A.B","source":"winget","update":"latest"}],"configs":[]}' | Set-Content "$root/profiles/one.json" + '{"schemaVersion":1,"name":"two","packages":[{"id":"A.B","source":"winget","enabled":false}],"configs":[]}' | Set-Content "$root/profiles/two.json" + '{"schemaVersion":1,"name":"test","profiles":["one","two"],"packages":[],"configs":[]}' | Set-Content "$root/machines/test.json" + (Resolve-MachineManifest -RepositoryRoot $root -Machine test).packages.Count | Should -Be 0 + } + + It 'rejects duplicate JSON properties' { + $path = Join-Path $TestDrive 'duplicate.json' + '{"schemaVersion":1,"name":"test","name":"again","packages":[],"configs":[]}' | Set-Content $path + { Read-ManifestFile -Path $path -Kind profile } | Should -Throw '*Duplicate JSON key*' + } + + It 'rejects unknown fields' { + $path = Join-Path $TestDrive 'unknown.json' + '{"schemaVersion":1,"name":"test","surprise":true}' | Set-Content $path + { Read-ManifestFile -Path $path -Kind profile } | Should -Throw '*Unknown field*' + } +} diff --git a/tests/Packages.Tests.ps1 b/tests/Packages.Tests.ps1 new file mode 100644 index 0000000..c637ae2 --- /dev/null +++ b/tests/Packages.Tests.ps1 @@ -0,0 +1,36 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../src/Packages.psm1" -Force +} + +Describe 'Package planning' { + It 'plans missing, pinned, and install-only packages' { + $packages = @( + [pscustomobject]@{ id = 'Missing.App'; source = 'winget'; update = 'latest' }, + [pscustomobject]@{ id = 'Pinned.App'; source = 'winget'; update = 'pinned'; version = '2.0' }, + [pscustomobject]@{ id = 'Stable.App'; source = 'winget'; update = 'install-only' } + ) + $inventory = [ordered]@{ + 'winget/pinned.app' = [pscustomobject]@{ version = '1.0' } + 'winget/stable.app' = [pscustomobject]@{ version = '5.0' } + } + $plan = Get-PackagePlan -Packages $packages -Inventory $inventory + $plan.action | Should -Be @('Install', 'Install', 'None') + } + + It 'leaves an installed version newer than a pin' { + $package = [pscustomobject]@{ id = 'Pinned.App'; source = 'winget'; update = 'pinned'; version = '2.0' } + $inventory = @{ 'winget/pinned.app' = [pscustomobject]@{ version = '3.0' } } + (Get-PackagePlan -Packages @($package) -Inventory $inventory)[0].action | Should -Be 'Warning' + } + + It 'continues after an individual package failure' { + InModuleScope Packages { + Mock Invoke-PackageOperation { + if ($Operation.name -eq 'bad') { return [pscustomobject]@{ name = 'bad'; status = 'Failed' } } + [pscustomobject]@{ name = $Operation.name; status = 'Succeeded' } + } + $results = Invoke-PackagePlan -Plan @([pscustomobject]@{ name = 'bad' }, [pscustomobject]@{ name = 'good' }) + $results.status | Should -Be @('Failed', 'Succeeded') + } + } +} diff --git a/tests/Repository.Tests.ps1 b/tests/Repository.Tests.ps1 new file mode 100644 index 0000000..b4dd6b5 --- /dev/null +++ b/tests/Repository.Tests.ps1 @@ -0,0 +1,42 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../src/State.psm1" -Force + Import-Module "$PSScriptRoot/../src/Repository.psm1" -Force +} + +Describe 'Repository synchronization' { + It 'rejects unsafe branch names before fetching' { + { Resolve-RemoteRevision -RepositoryRoot $TestDrive -Settings @{ branch = '../main' } } | Should -Throw '*Unsafe branch*' + } + + It 'refuses a dirty checkout' { + InModuleScope Repository -Parameters @{ Working = $TestDrive } { + Mock Test-RepositoryDirty { $true } + { Resolve-RemoteRevision -RepositoryRoot $Working -Settings @{ branch = 'main' } } | Should -Throw '*local changes*' + } + } + + It 'reports fetch failures with recovery guidance' { + InModuleScope Repository -Parameters @{ Working = $TestDrive } { + Mock Test-RepositoryDirty { $false } + Mock Invoke-Git { throw 'credential rejected' } + { Resolve-RemoteRevision -RepositoryRoot $Working -Settings @{ branch = 'main' } } | Should -Throw '*network access and Git credentials*' + } + } + + It 'stages the selected commit without changing the checkout' -Skip:(-not $IsWindows) { + $repo = Join-Path $TestDrive 'repo' + New-Item -ItemType Directory $repo | Out-Null + & git.exe -C $repo init | Out-Null + & git.exe -C $repo config user.email 'test@example.invalid' + & git.exe -C $repo config user.name 'Config Central Test' + 'first' | Set-Content "$repo/cc.ps1" + & git.exe -C $repo add cc.ps1 + & git.exe -C $repo commit -m first | Out-Null + $revision = (& git.exe -C $repo rev-parse HEAD).Trim() + $paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive 'data') + Initialize-ConfigCentralData $paths + $snapshot = New-RepositorySnapshot -RepositoryRoot $repo -Revision $revision -Paths $paths + (Get-Content "$snapshot/cc.ps1" -Raw).Trim() | Should -Be 'first' + (& git.exe -C $repo status --porcelain) | Should -BeNullOrEmpty + } +} diff --git a/tests/State.Tests.ps1 b/tests/State.Tests.ps1 new file mode 100644 index 0000000..e82aa4c --- /dev/null +++ b/tests/State.Tests.ps1 @@ -0,0 +1,22 @@ +BeforeAll { + Import-Module "$PSScriptRoot/../src/State.psm1" -Force +} + +Describe 'Run state recovery' { + It 'blocks concurrent applies and permits a rerun after release' { + $paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive 'data') + $first = Enter-ApplyLock -Paths $paths + try { { Enter-ApplyLock -Paths $paths } | Should -Throw '*Another Config Central apply*' } + finally { Exit-ApplyLock -Lock $first -Paths $paths } + $second = Enter-ApplyLock -Paths $paths + $second | Should -Not -BeNullOrEmpty + Exit-ApplyLock -Lock $second -Paths $paths + } + + It 'replaces state atomically without leaving temporary files' { + $paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive 'state-data') + Write-JsonAtomic -Path $paths.State -Value @{ schemaVersion = 1; value = 'new' } + (Read-JsonState -Path $paths.State -Default @{}).value | Should -Be 'new' + @(Get-ChildItem $paths.Root -Filter '*.tmp').Count | Should -Be 0 + } +} diff --git a/tests/Windows.Integration.Tests.ps1 b/tests/Windows.Integration.Tests.ps1 new file mode 100644 index 0000000..fb9f0f4 --- /dev/null +++ b/tests/Windows.Integration.Tests.ps1 @@ -0,0 +1,17 @@ +$runIntegration = $IsWindows -and $env:CONFIG_CENTRAL_INTEGRATION -eq '1' + +Describe 'Disposable Windows integration' -Skip:(-not $runIntegration) { + It 'passes doctor on the Windows worker' { + & "$PSScriptRoot/../cc.ps1" doctor -Machine example-desktop -Json + $LASTEXITCODE | Should -Be 0 + } + + It 'performs two config-only applies idempotently' { + $data = Join-Path $TestDrive 'integration-data' + & "$PSScriptRoot/../cc.ps1" apply -Machine example-desktop -ConfigsOnly -ForceConfigs -DataRoot $data -Json + $LASTEXITCODE | Should -Be 0 + $second = & "$PSScriptRoot/../cc.ps1" apply -Machine example-desktop -ConfigsOnly -DataRoot $data -Json | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + @($second.operations | Where-Object status -eq 'Unchanged').Count | Should -BeGreaterThan 0 + } +}