config-central/tests/Core.Tests.ps1

119 lines
7.3 KiB
PowerShell

BeforeAll {
foreach ($module in @('State', 'Manifest', 'Repository', 'Packages', 'Configs', 'Elevation', 'Scheduling', 'Core')) {
Import-Module "$PSScriptRoot/../src/$module.psm1" -Force
}
}
Describe 'Core workflow policy' {
It 'does not inventory or change packages in config-only mode' {
InModuleScope Core -Parameters @{ Working = $TestDrive } {
Mock Read-JsonState { @{ configs = @{} } }
Mock Resolve-MachineManifest { [pscustomobject]@{ packages = @([pscustomobject]@{ id = 'Never.Run' }); configs = @() } }
Mock Get-WingetInventory { throw 'WinGet must not run' }
Mock Get-ConfigPlan { @() }
$paths = [pscustomobject]@{ Settings = "$Working/settings.json"; State = "$Working/state.json" }
$plan = Get-OperationPlan -Machine test -RepositoryRoot $Working -Paths $paths -ConfigsOnly
$plan.packages.Count | Should -Be 0
Should -Invoke Get-WingetInventory -Times 0
}
}
It 'does not fail the run for a partial registry warning' {
InModuleScope Core -Parameters @{ Working = $TestDrive } {
Mock Invoke-ConfigOperation { [pscustomobject]@{ name = 'mpc-hc-settings'; status = 'Warning'; message = 'Could not write registry key(s): HWAcceleration' } }
$paths = Get-ConfigCentralPaths -DataRoot (Join-Path $Working 'warning-data')
$operation = [pscustomobject]@{ name = 'mpc-hc-settings'; requiresPackage = $null; requiresAdmin = $false; action = 'Update' }
$plan = [pscustomobject]@{ packages = @(); configs = @($operation); state = @{ schemaVersion = 1; configs = @{} } }
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc
$result.succeeded | Should -BeTrue
$result.exitCode | Should -Be 0
$result.operations[0].status | Should -Be 'Warning'
}
}
It 'returns the stable reboot-required exit code after successful work' {
InModuleScope Core -Parameters @{ Working = $TestDrive } {
Mock Invoke-PackageOperation { [pscustomobject]@{ name = 'Reboot.App'; status = 'Succeeded'; rebootRequired = $true; message = 'Installed; reboot required' } }
$paths = Get-ConfigCentralPaths -DataRoot (Join-Path $Working 'reboot-data')
$operation = [pscustomobject]@{
kind = 'package'; name = 'Reboot.App'; action = 'Install'; reason = 'Missing'
package = [pscustomobject]@{ id = 'Reboot.App'; source = 'winget'; update = 'latest' }
}
$plan = [pscustomobject]@{ packages = @($operation); configs = @(); state = @{ schemaVersion = 1; configs = @{} } }
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc
$result.succeeded | Should -BeTrue
$result.exitCode | Should -Be 6
}
}
}
Describe 'Result status colors' {
It 'colors statuses without coloring item names and messages' {
InModuleScope Core {
Mock Test-ResultColor { $true }
Mock Write-Host {}
$result = [pscustomobject]@{ command = 'apply'; revision = ''; warnings = @(); operations = @(
[pscustomobject]@{ name = 'one'; status = 'Failed'; message = 'Problem' },
[pscustomobject]@{ name = 'two'; status = 'Succeeded'; message = 'Done' },
[pscustomobject]@{ name = 'three'; status = 'Conflict'; message = 'Local edits' },
[pscustomobject]@{ name = 'four'; status = 'Unchanged'; message = 'Current' }
) }
Write-ConfigCentralResult $result
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Red' -and $NoNewline }
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Green' -and $NoNewline }
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'Yellow' -and $NoNewline }
Should -Invoke Write-Host -Times 1 -ParameterFilter { $ForegroundColor -eq 'DarkGray' -and $NoNewline }
}
}
It 'writes complete plain rows when color is disabled' {
InModuleScope Core {
Mock Test-ResultColor { $false }
Mock Write-Host {}
$result = [pscustomobject]@{ command = 'apply'; revision = ''; warnings = @(); operations = @(
[pscustomobject]@{ name = 'one'; status = 'Failed'; message = 'Problem' }
) }
Write-ConfigCentralResult $result
Should -Invoke Write-Host -Times 1 -ParameterFilter { $Object -match '^Failed\s+one\s+Problem$' -and -not $NoNewline }
Should -Invoke Write-Host -Times 0 -ParameterFilter { $PSBoundParameters.ContainsKey('ForegroundColor') }
}
}
}
Describe 'Portable workflow integration' {
It 'plans portable-only machines without WinGet and performs no writes' {
InModuleScope Core -Parameters @{ Working = $TestDrive } {
Mock Read-JsonState { @{ configs = @{} } }
Mock Resolve-MachineManifest { [pscustomobject]@{ packages = @(
[pscustomobject]@{ id = 'presenter'; source = 'gitea'; provider = 'portable'; version = '1.0'; update = 'pinned'; sha256 = ('a' * 64); executable = 'app.exe' }
); configs = @() } }
Mock Get-WingetInventory { throw 'WinGet must not run' }
Mock Get-ConfigPlan { @() }
$paths = Get-ConfigCentralPaths -DataRoot "$Working/portable-plan"
$plan = Get-OperationPlan -Machine test -RepositoryRoot $Working -Paths $paths
$plan.packages[0].action | Should -Be 'Install'
Test-Path $paths.Root | Should -BeFalse
Should -Invoke Get-WingetInventory -Times 0
}
}
It 'continues independent packages and skips configs after a portable failure' {
InModuleScope Core -Parameters @{ Working = $TestDrive } {
Mock Invoke-PackageOperation {
if ($Operation.name -eq 'presenter') { return [pscustomobject]@{ name = 'presenter'; status = 'Failed'; message = 'Checksum mismatch'; rebootRequired = $false } }
[pscustomobject]@{ name = 'other'; status = 'Succeeded'; message = 'Installed'; rebootRequired = $false }
}
Mock Invoke-ConfigOperation { throw 'Dependent config must not run' }
$paths = Get-ConfigCentralPaths -DataRoot "$Working/portable-failure"
$plan = [pscustomobject]@{ state = @{ configs = @{} }; packages = @(
[pscustomobject]@{ name = 'presenter'; package = [pscustomobject]@{ provider = 'portable'; scope = 'user' } },
[pscustomobject]@{ name = 'other'; package = [pscustomobject]@{ provider = 'winget'; scope = 'user' } }
); configs = @([pscustomobject]@{ name = 'dependent'; requiresPackage = 'presenter' }) }
$result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc
$result.exitCode | Should -Be 5
$result.operations.status | Should -Be @('Failed', 'Succeeded', 'Skipped')
$json = $result | ConvertTo-Json -Depth 16 | ConvertFrom-Json
$json.operations[0].message | Should -Be 'Checksum mismatch'
(Read-JsonState $paths.State @{}).lastRun.revision | Should -Be 'abc'
}
}
}