config-central/tests/Packages.Tests.ps1

197 lines
9.6 KiB
PowerShell

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 'plans explicit package absence idempotently' {
$package = [pscustomobject]@{ id = 'Cimbali.pympress'; source = 'winget'; state = 'absent'; update = 'latest' }
(Get-PackagePlan -Packages @($package) -Inventory @{})[0].action | Should -Be 'None'
$inventory = @{ 'winget/cimbali.pympress' = [pscustomobject]@{ version = '1.8.3' } }
$operation = (Get-PackagePlan -Packages @($package) -Inventory $inventory)[0]
$operation.action | Should -Be 'Uninstall'
$operation.reason | Should -Match '1\.8\.3'
}
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')
}
}
}
Describe 'WinGet package removal' {
It 'uninstalls by exact ID and treats a missing package as unchanged' {
InModuleScope Packages {
Mock Invoke-WingetCapture {
[pscustomobject]@{ exitCode = -1978335212; unsignedExitCode = 2316632084; output = 'No installed package found' }
}
$operation = [pscustomobject]@{
action = 'Uninstall'; name = 'Cimbali.pympress'
package = [pscustomobject]@{ id = 'Cimbali.pympress'; source = 'winget'; state = 'absent' }
}
$result = Invoke-PackageOperation -Operation $operation
$result.status | Should -Be 'Unchanged'
$result.message | Should -Be 'Already absent'
Should -Invoke Invoke-WingetCapture -Times 1 -Exactly -ParameterFilter {
$Arguments[0] -eq 'uninstall' -and $Arguments -contains '--id' -and
$Arguments -contains 'Cimbali.pympress' -and $Arguments -contains '--exact' -and
$Arguments -contains '--silent' -and $Arguments -notcontains '--accept-package-agreements'
}
}
}
It 'preserves uninstall failure diagnostics' {
InModuleScope Packages {
Mock Invoke-WingetCapture { [pscustomobject]@{ exitCode = 1; unsignedExitCode = 1; output = 'Uninstall failed' } }
$operation = [pscustomobject]@{
action = 'Uninstall'; name = 'Cimbali.pympress'
package = [pscustomobject]@{ id = 'Cimbali.pympress'; source = 'winget'; state = 'absent' }
}
$result = Invoke-PackageOperation -Operation $operation
$result.status | Should -Be 'Failed'
$result.message | Should -Be 'Uninstall failed'
}
}
}
Describe 'Release lookup failures' {
It 'returns a structured failure without invoking either package provider' {
InModuleScope Packages {
Mock Invoke-WingetCapture { throw 'WinGet must not run' }
Mock Invoke-PortablePackageOperation { throw 'Portable install must not run' }
$operation = [pscustomobject]@{ action = 'Failed'; name = 'broken'; reason = 'Release API unavailable'; package = [pscustomobject]@{ provider = 'portable' } }
$result = Invoke-PackageOperation $operation
$result.status | Should -Be 'Failed'
$result.message | Should -Be 'Release API unavailable'
Should -Invoke Invoke-WingetCapture -Times 0
Should -Invoke Invoke-PortablePackageOperation -Times 0
}
}
}
Describe 'WinGet signed process exit codes' {
It 'preserves the HRESULT bits and diagnostics for a negative exit code' {
InModuleScope Packages {
Mock Start-Process {
'WinGet diagnostic' | Set-Content -LiteralPath $RedirectStandardError
[pscustomobject]@{ ExitCode = -1978335189 }
}
$result = Invoke-WingetCapture -Arguments @('export')
$result.exitCode | Should -Be -1978335189
$result.unsignedExitCode | Should -Be 2316632107
$result.output | Should -Match 'WinGet diagnostic'
{ $result | ConvertTo-Json } | Should -Not -Throw
}
}
It 'recognizes a signed no-applicable-upgrade result' {
InModuleScope Packages {
Mock Start-Process { [pscustomobject]@{ ExitCode = -1978335189 } }
$operation = [pscustomobject]@{
action = 'Upgrade'; name = 'Test.App'
package = [pscustomobject]@{ id = 'Test.App'; source = 'winget' }
}
(Invoke-PackageOperation -Operation $operation).status | Should -Be 'Unchanged'
}
}
It 'preserves ordinary reboot exit codes' {
InModuleScope Packages {
Mock Start-Process { [pscustomobject]@{ ExitCode = 3010 } }
$result = Invoke-WingetCapture -Arguments @('install')
$result.unsignedExitCode | Should -Be 3010
}
}
}
Describe 'Missing installation during upgrade' {
It 'installs once with the original scope and package constraints' {
InModuleScope Packages {
Mock Invoke-WingetCapture {
if ($Arguments[0] -eq 'upgrade') {
return [pscustomobject]@{ exitCode = -1978335212; unsignedExitCode = 2316632084; output = 'Not installed' }
}
[pscustomobject]@{ exitCode = 0; unsignedExitCode = 0; output = 'Installed' }
}
$operation = [pscustomobject]@{
action = 'Upgrade'; name = 'Helix.Helix'
package = [pscustomobject]@{ id = 'Helix.Helix'; source = 'winget'; scope = 'machine' }
}
$result = Invoke-PackageOperation -Operation $operation
$result.status | Should -Be 'Succeeded'
$result.message | Should -Match 'Installed after upgrade'
Should -Invoke Invoke-WingetCapture -Times 2 -Exactly
Should -Invoke Invoke-WingetCapture -Times 1 -Exactly -ParameterFilter {
$Arguments[0] -eq 'install' -and $Arguments -contains 'Helix.Helix' -and
$Arguments -contains '--scope' -and $Arguments -contains 'machine' -and
$Arguments -contains '--source' -and $Arguments -contains 'winget'
}
}
}
It 'reports a failed fallback without retrying again' {
InModuleScope Packages {
Mock Invoke-WingetCapture {
[pscustomobject]@{ exitCode = -1978335212; unsignedExitCode = 2316632084; output = 'Still missing' }
}
$operation = [pscustomobject]@{ action = 'Upgrade'; name = 'Test.App'; package = [pscustomobject]@{ id = 'Test.App'; source = 'winget' } }
$result = Invoke-PackageOperation -Operation $operation
$result.status | Should -Be 'Failed'
$result.message | Should -Be 'Still missing'
Should -Invoke Invoke-WingetCapture -Times 2 -Exactly
}
}
It 'does not retry unrelated upgrade failures as installs' {
InModuleScope Packages {
Mock Invoke-WingetCapture { [pscustomobject]@{ exitCode = 1; unsignedExitCode = 1; output = 'Other failure' } }
$operation = [pscustomobject]@{ action = 'Upgrade'; name = 'Test.App'; package = [pscustomobject]@{ id = 'Test.App'; source = 'winget' } }
(Invoke-PackageOperation -Operation $operation).status | Should -Be 'Failed'
Should -Invoke Invoke-WingetCapture -Times 1 -Exactly
}
}
}
Describe 'PowerShell runtime protection' {
It 'warns even when PowerShell is missing from WinGet inventory' {
$package = [pscustomobject]@{ id = 'Microsoft.PowerShell'; source = 'winget'; update = 'latest' }
(Get-PackagePlan -Packages @($package) -Inventory @{})[0].action | Should -Be 'Warning'
}
It 'blocks stale install and upgrade plans in the worker' {
InModuleScope Packages {
Mock Invoke-WingetCapture { throw 'The running runtime must not be replaced' }
foreach ($id in @('Microsoft.PowerShell', 'Microsoft.PowerShell.Preview')) {
foreach ($action in @('Install', 'Upgrade')) {
$operation = [pscustomobject]@{ name = $id; action = $action; package = [pscustomobject]@{ id = $id; source = 'winget' } }
(Invoke-PackageOperation $operation).status | Should -Be 'Warning'
}
}
Should -Invoke Invoke-WingetCapture -Times 0
}
}
}