BeforeAll { $messageTokens = $null; $messageErrors = $null $messageAst = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../bootstrap.ps1", [ref]$messageTokens, [ref]$messageErrors) $messageFn = $messageAst.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Write-ConfigCentralMessage' }, $true) . ([scriptblock]::Create($messageFn.Extent.Text)) $script:bootstrapPath = "$PSScriptRoot/../bootstrap.ps1" $tokens = $null $errors = $null $script:bootstrapAst = [System.Management.Automation.Language.Parser]::ParseFile($bootstrapPath, [ref]$tokens, [ref]$errors) if ($errors.Count -gt 0) { throw "bootstrap.ps1 does not parse: $($errors[0].Message)" } $functionAst = $bootstrapAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Find-GitPath' }, $true) . ([scriptblock]::Create($functionAst.Extent.Text)) } Describe 'Bootstrap invocation' { It 'rejects in-memory execution because elevation requires a script file' { $source = Get-Content $bootstrapPath -Raw $bootstrap = [scriptblock]::Create($source) $previousLocalAppData = $env:LOCALAPPDATA try { $env:LOCALAPPDATA = $TestDrive & $bootstrap -Repository 'https://example.invalid/config-central.git' -Machine test $LASTEXITCODE | Should -Be 2 } finally { $env:LOCALAPPDATA = $previousLocalAppData } } It 'finds a fresh machine-scope Git install before PATH refreshes' { $previousProgramFiles = $env:ProgramFiles try { Mock Get-Command { $null } -ParameterFilter { $Name -eq 'git.exe' } $env:ProgramFiles = $TestDrive $expected = Join-Path $TestDrive 'Git\cmd\git.exe' New-Item -ItemType File -Path $expected -Force | Out-Null Find-GitPath | Should -Be $expected } finally { $env:ProgramFiles = $previousProgramFiles } } } Describe 'Bootstrap PowerShell prerequisites' { BeforeAll { foreach ($name in @('Find-PowerShell7Path', 'Invoke-BootstrapWingetInstall', 'Assert-BootstrapPowerShell')) { $fn = $bootstrapAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $name }, $true) . ([scriptblock]::Create($fn.Extent.Text)) } function winget.exe { $global:LASTEXITCODE = 0 } function Test-Runtime { $global:LASTEXITCODE = 0; 'ConfigCentral.PowerShell7.Ready' } } It 'requests the MSI only for PowerShell' { Mock winget.exe { $global:LASTEXITCODE = 0 } Invoke-BootstrapWingetInstall -Id Microsoft.PowerShell Should -Invoke winget.exe -Times 1 -Exactly -ParameterFilter { $args -contains '--installer-type' -and $args -contains 'wix' -and $args -contains 'machine' } Invoke-BootstrapWingetInstall -Id Git.Git Should -Invoke winget.exe -Times 1 -Exactly -ParameterFilter { $args -contains 'Git.Git' -and $args -notcontains '--installer-type' } } It 'prefers the standard installation over a dotnet shim on PATH' { $previousProgramFiles = $env:ProgramFiles try { $env:ProgramFiles = $TestDrive $expected = Join-Path $TestDrive 'PowerShell/7/pwsh.exe' New-Item -ItemType File -Path $expected -Force | Out-Null Mock Get-Command { [pscustomobject]@{ Source = 'broken-dotnet-shim' } } -ParameterFilter { $Name -eq 'pwsh.exe' } Find-PowerShell7Path | Should -Be $expected } finally { $env:ProgramFiles = $previousProgramFiles } } It 'accepts a working runtime' { { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Not -Throw } It 'retains startup diagnostics and recommends repair' { Mock Test-Runtime { $global:LASTEXITCODE = 1; 'You must install or update .NET' } { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*install or update .NET*Repair*' } It 'rejects a shim that exits successfully without starting PowerShell' { Mock Test-Runtime { $global:LASTEXITCODE = 0 } { Assert-BootstrapPowerShell -Path Test-Runtime } | Should -Throw '*could not start*' } } Describe 'Bootstrap first-apply feedback' { BeforeAll { $fn = $bootstrapAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Invoke-BootstrapApply' }, $true) . ([scriptblock]::Create($fn.Extent.Text)) } It 'shows the active machine and preserves child output, diagnostics, and exit code' { $powerShell = (Get-Process -Id $PID).Path Mock Write-ConfigCentralMessage $originalOutput = [Console]::Out $originalError = [Console]::Error $capturedOutput = [IO.StringWriter]::new() $capturedError = [IO.StringWriter]::new() try { [Console]::SetOut($capturedOutput) [Console]::SetError($capturedError) $code = Invoke-BootstrapApply -PowerShell $powerShell -Arguments @('-NoProfile', '-Command', 'Write-Output child-output; [Console]::Error.WriteLine(123); exit 7') -Machine 'test-machine' } finally { [Console]::SetOut($originalOutput) [Console]::SetError($originalError) } $code | Should -Be 7 $capturedOutput.ToString() | Should -Match 'child-output' $capturedError.ToString() | Should -Match '123' Should -Invoke Write-ConfigCentralMessage -Times 1 -Exactly -ParameterFilter { $Message -match "Applying software and configs for 'test-machine'" } } }