config-central/tests/Core.Tests.ps1

81 lines
4.8 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') }
}
}
}