|
| 1 | +# Pester tests for Invoke-ListFunctionParameters |
| 2 | +# Validates the pregenerated cache path (Config/function-parameters.json), the guard |
| 3 | +# that skips commands outside the pregenerated set (deployed legacy behavior), the |
| 4 | +# live Get-Help fallback when no cache exists, and entrypoint filtering. |
| 5 | + |
| 6 | +BeforeAll { |
| 7 | + # Resolve by name under Modules/ so the test survives the function moving between modules. |
| 8 | + $RepoRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSCommandPath)) |
| 9 | + $FunctionPath = Get-ChildItem -Path (Join-Path $RepoRoot 'Modules') -Recurse -Filter 'Invoke-ListFunctionParameters.ps1' -File -ErrorAction SilentlyContinue | |
| 10 | + Select-Object -First 1 -ExpandProperty FullName |
| 11 | + if (-not $FunctionPath) { throw 'Could not locate Invoke-ListFunctionParameters.ps1 under Modules/' } |
| 12 | + |
| 13 | + class HttpResponseContext { |
| 14 | + [object]$StatusCode |
| 15 | + [object]$Body |
| 16 | + } |
| 17 | + # The Functions worker exposes [HttpStatusCode]; map it for standalone test runs. |
| 18 | + $Accelerators = [PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators') |
| 19 | + if (-not ('HttpStatusCode' -as [type])) { |
| 20 | + $Accelerators::Add('HttpStatusCode', [System.Net.HttpStatusCode]) |
| 21 | + } |
| 22 | + |
| 23 | + function New-ExoRequest { param($AvailableCmdlets, $tenantid, $NoAuthCheck, $Compliance) } |
| 24 | + |
| 25 | + # fake FunctionInfo, endpoint reads Name / Visibility / Parameters |
| 26 | + function New-FakeFunction { |
| 27 | + param($Name) |
| 28 | + [pscustomobject]@{ |
| 29 | + Name = $Name |
| 30 | + Visibility = 'Public' |
| 31 | + Parameters = @{ |
| 32 | + SomeParam = [pscustomobject]@{ |
| 33 | + ParameterType = [pscustomobject]@{ FullName = 'System.String' } |
| 34 | + Attributes = @([pscustomobject]@{ Mandatory = $true }) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + function New-CacheFile { |
| 41 | + param($Root, $Functions) |
| 42 | + $configDir = Join-Path $Root 'Config' |
| 43 | + $null = New-Item -ItemType Directory -Path $configDir -Force |
| 44 | + $Functions | ConvertTo-Json -Depth 8 | Set-Content -Path (Join-Path $configDir 'function-parameters.json') |
| 45 | + } |
| 46 | + |
| 47 | + . $FunctionPath |
| 48 | + |
| 49 | + # the function also emits $Results to the pipeline before returning the |
| 50 | + # response context, the worker keys on the HttpResponseContext object |
| 51 | + function Invoke-Endpoint { |
| 52 | + param($Request) |
| 53 | + Invoke-ListFunctionParameters -Request $Request -TriggerMetadata $null | Where-Object { $_ -is [HttpResponseContext] } | Select-Object -First 1 |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +Describe 'Invoke-ListFunctionParameters' { |
| 58 | + BeforeEach { |
| 59 | + $global:CIPPFunctionParameters = $null |
| 60 | + $script:savedRootPath = $env:CIPPRootPath |
| 61 | + |
| 62 | + Mock -CommandName Get-Help -MockWith { |
| 63 | + [pscustomobject]@{ |
| 64 | + Functionality = '' |
| 65 | + Synopsis = 'live synopsis' |
| 66 | + parameters = [pscustomobject]@{ |
| 67 | + parameter = @([pscustomobject]@{ name = 'SomeParam'; description = @([pscustomobject]@{ Text = 'live description' }) }) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + AfterEach { |
| 74 | + $global:CIPPFunctionParameters = $null |
| 75 | + $env:CIPPRootPath = $script:savedRootPath |
| 76 | + } |
| 77 | + |
| 78 | + It 'serves cached functions without calling Get-Help' { |
| 79 | + $env:CIPPRootPath = Join-Path $TestDrive 'cached' |
| 80 | + New-CacheFile -Root $env:CIPPRootPath -Functions @{ |
| 81 | + 'Get-CIPPFoo' = @{ |
| 82 | + Functionality = '' |
| 83 | + Synopsis = 'cached synopsis' |
| 84 | + Parameters = @(@{ Name = 'SomeParam'; Type = 'System.String'; Description = 'cached description'; Required = $true }) |
| 85 | + } |
| 86 | + } |
| 87 | + Mock -CommandName Get-Command -MockWith { @(New-FakeFunction -Name 'Get-CIPPFoo') } |
| 88 | + |
| 89 | + $request = [pscustomobject]@{ Query = [pscustomobject]@{ Module = 'CIPPCore' } } |
| 90 | + $response = Invoke-Endpoint -Request $request |
| 91 | + |
| 92 | + $response.StatusCode | Should -Be ([System.Net.HttpStatusCode]::OK) |
| 93 | + $response.Body | Should -HaveCount 1 |
| 94 | + $response.Body[0].Function | Should -Be 'Get-CIPPFoo' |
| 95 | + $response.Body[0].Synopsis | Should -Be 'cached synopsis' |
| 96 | + $response.Body[0].Parameters[0].Description | Should -Be 'cached description' |
| 97 | + Should -Invoke Get-Help -Times 0 -Exactly |
| 98 | + } |
| 99 | + |
| 100 | + It 'skips functions outside the pregenerated set without calling Get-Help' { |
| 101 | + $env:CIPPRootPath = Join-Path $TestDrive 'partial' |
| 102 | + New-CacheFile -Root $env:CIPPRootPath -Functions @{ |
| 103 | + 'Get-CIPPFoo' = @{ |
| 104 | + Functionality = '' |
| 105 | + Synopsis = 'cached synopsis' |
| 106 | + Parameters = @(@{ Name = 'SomeParam'; Type = 'System.String'; Description = 'cached description'; Required = $true }) |
| 107 | + } |
| 108 | + } |
| 109 | + # cache present -> uncached commands are guard-skipped, never Get-Help'd |
| 110 | + # (a bare Get-Command can return every command in the runspace) |
| 111 | + Mock -CommandName Get-Command -MockWith { |
| 112 | + @((New-FakeFunction -Name 'Get-CIPPFoo'), (New-FakeFunction -Name 'Get-SomeOtherModuleThing')) |
| 113 | + } |
| 114 | + |
| 115 | + $request = [pscustomobject]@{ Query = [pscustomobject]@{ Module = 'CIPPCore' } } |
| 116 | + $response = Invoke-Endpoint -Request $request |
| 117 | + |
| 118 | + $response.StatusCode | Should -Be ([System.Net.HttpStatusCode]::OK) |
| 119 | + $response.Body | Should -HaveCount 1 |
| 120 | + $response.Body[0].Function | Should -Be 'Get-CIPPFoo' |
| 121 | + Should -Invoke Get-Help -Times 0 -Exactly |
| 122 | + } |
| 123 | + |
| 124 | + It 'uses live Get-Help for everything when no cache file exists' { |
| 125 | + $env:CIPPRootPath = Join-Path $TestDrive 'empty' |
| 126 | + $null = New-Item -ItemType Directory -Path $env:CIPPRootPath -Force |
| 127 | + Mock -CommandName Get-Command -MockWith { |
| 128 | + @((New-FakeFunction -Name 'Get-CIPPFoo'), (New-FakeFunction -Name 'Get-CIPPBar')) |
| 129 | + } |
| 130 | + |
| 131 | + $request = [pscustomobject]@{ Query = [pscustomobject]@{ Module = 'CIPPCore' } } |
| 132 | + $response = Invoke-Endpoint -Request $request |
| 133 | + |
| 134 | + $response.StatusCode | Should -Be ([System.Net.HttpStatusCode]::OK) |
| 135 | + $response.Body | Should -HaveCount 2 |
| 136 | + Should -Invoke Get-Help -Times 2 -Exactly |
| 137 | + } |
| 138 | + |
| 139 | + It 'filters out entrypoint functions listed in the cache' { |
| 140 | + $env:CIPPRootPath = Join-Path $TestDrive 'entrypoints' |
| 141 | + New-CacheFile -Root $env:CIPPRootPath -Functions @{ |
| 142 | + 'Get-CIPPFoo' = @{ |
| 143 | + Functionality = '' |
| 144 | + Synopsis = 'cached synopsis' |
| 145 | + Parameters = @() |
| 146 | + } |
| 147 | + 'Invoke-CIPPBar' = @{ |
| 148 | + Functionality = 'Entrypoint,AnyTenant' |
| 149 | + Synopsis = 'an entrypoint' |
| 150 | + Parameters = @() |
| 151 | + } |
| 152 | + } |
| 153 | + Mock -CommandName Get-Command -MockWith { |
| 154 | + @((New-FakeFunction -Name 'Get-CIPPFoo'), (New-FakeFunction -Name 'Invoke-CIPPBar')) |
| 155 | + } |
| 156 | + |
| 157 | + $request = [pscustomobject]@{ Query = [pscustomobject]@{ Module = 'CIPPCore' } } |
| 158 | + $response = Invoke-Endpoint -Request $request |
| 159 | + |
| 160 | + $response.Body | Should -HaveCount 1 |
| 161 | + $response.Body[0].Function | Should -Be 'Get-CIPPFoo' |
| 162 | + } |
| 163 | +} |
0 commit comments