BeforeAll { $script:installerPath = "$PSScriptRoot/../install.ps1" $tokens = $null $errors = $null $script:installerAst = [System.Management.Automation.Language.Parser]::ParseFile($installerPath, [ref]$tokens, [ref]$errors) if ($errors.Count -gt 0) { throw "install.ps1 does not parse: $($errors[0].Message)" } $functionAst = $installerAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Select-ConfigCentralMachine' }, $true) . ([scriptblock]::Create($functionAst.Extent.Text)) } Describe 'One-stop installer machine selection' { BeforeEach { $script:Branch = 'main' $script:previousComputerName = $env:COMPUTERNAME $env:COMPUTERNAME = 'OFFICE-PC' } AfterEach { $env:COMPUTERNAME = $script:previousComputerName } It 'uses an explicit machine selection' { Select-ConfigCentralMachine -Requested living-room -Saved office-pc -Available @('living-room', 'office-pc') | Should -Be 'living-room' } It 'reuses the saved machine on subsequent runs' { Select-ConfigCentralMachine -Requested '' -Saved living-room -Available @('living-room', 'office-pc') | Should -Be 'living-room' } It 'matches the Windows computer name before prompting' { Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('living-room', 'office-pc') | Should -Be 'office-pc' } It 'automatically selects the only available manifest' { Select-ConfigCentralMachine -Requested '' -Saved '' -Available @('example-desktop') | Should -Be 'example-desktop' } It 'allows unset environment defaults through parameter binding' { $previousMachine = $env:CONFIG_CENTRAL_MACHINE $previousBranch = $env:CONFIG_CENTRAL_BRANCH try { $env:CONFIG_CENTRAL_MACHINE = $null $env:CONFIG_CENTRAL_BRANCH = $null $parameterProbe = [scriptblock]::Create( $installerAst.ParamBlock.Extent.Text + "`n@{ Machine = `$Machine; Branch = `$Branch }" ) $bound = & $parameterProbe $bound.Machine | Should -BeNullOrEmpty $bound.Branch | Should -BeNullOrEmpty } finally { $env:CONFIG_CENTRAL_MACHINE = $previousMachine $env:CONFIG_CENTRAL_BRANCH = $previousBranch } } }