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') } } } 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 } } }