From 98aea3c3033ea03848a4eaeb610c35f002559fa3 Mon Sep 17 00:00:00 2001 From: Marco Wende Date: Wed, 16 Sep 2026 12:23:09 +0200 Subject: [PATCH] =?UTF-8?q?Tests:=20Pester-Tests=20f=C3=BCr=20PolicyIO-Pay?= =?UTF-8?q?load-Transformationen=20(#2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deckt die Array-Normalisierung (Repair-SettingsCatalogArrays) inkl. choiceSettingCollectionValue sowie Remove-PolicyNullProps ab — genau die Bug-Klasse (PS-5.1-JSON-Roundtrip skalarisiert Ein-Element-Arrays), die uns mehrfach getroffen hat. 9 Tests, klassische Pester-3/4-Syntax. Ausführen: Invoke-Pester .\tests Co-Authored-By: Claude Opus 4.8 --- tests/PolicyIO.Transform.Tests.ps1 | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/PolicyIO.Transform.Tests.ps1 diff --git a/tests/PolicyIO.Transform.Tests.ps1 b/tests/PolicyIO.Transform.Tests.ps1 new file mode 100644 index 0000000..1854482 --- /dev/null +++ b/tests/PolicyIO.Transform.Tests.ps1 @@ -0,0 +1,89 @@ +# Pester-Tests fuer die Payload-Transformationen in PolicyIO.ps1. +# Diese reinen Funktionen sind genau die Bug-Klasse, die uns mehrfach getroffen +# hat: der JSON-Roundtrip unter Windows PowerShell 5.1 entpackt Ein-Element-Arrays +# zu Einzelobjekten -> Graph antwortet mit 400 "... does not match schema". +# +# Ausfuehren: Invoke-Pester .\tests +# (klassische Pester-3/4-Syntax, passend zur mit Windows gelieferten Version.) + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +. (Join-Path $here '..\src\PolicyIO.ps1') + +Describe 'Repair-SettingsCatalogArrays' { + + It 'macht aus einem skalaren choiceSettingCollectionValue ein Array' { + # Genau der Fall aus dem gefixten Defender-Antivirus-Import. + $node = @{ + settingInstance = @{ + '@odata.type' = '#microsoft.graph.deviceManagementConfigurationChoiceSettingCollectionInstance' + choiceSettingCollectionValue = @{ value = 'x'; children = @() } + } + } + $fixed = Repair-SettingsCatalogArrays $node + $ccv = $fixed.settingInstance.choiceSettingCollectionValue + ($ccv -is [System.Collections.IList]) | Should Be $true + @($ccv).Count | Should Be 1 + } + + It 'erzwingt auch group- und simpleSettingCollectionValue als Array' { + $node = @{ + settingInstance = @{ + groupSettingCollectionValue = @{ children = @() } + simpleSettingCollectionValue = @{ value = 1 } + } + } + $fixed = Repair-SettingsCatalogArrays $node + ($fixed.settingInstance.groupSettingCollectionValue -is [System.Collections.IList]) | Should Be $true + ($fixed.settingInstance.simpleSettingCollectionValue -is [System.Collections.IList]) | Should Be $true + } + + It 'macht aus einem einzelnen settings-Objekt ein Array' { + $node = @{ settings = @{ id = '0'; settingInstance = @{} } } + $fixed = Repair-SettingsCatalogArrays $node + ($fixed.settings -is [System.Collections.IList]) | Should Be $true + @($fixed.settings).Count | Should Be 1 + } + + It 'ersetzt null-Collections durch ein leeres Array (nicht {})' { + $node = @{ settingInstance = @{ children = $null } } + $fixed = Repair-SettingsCatalogArrays $node + ($fixed.settingInstance.children -is [System.Collections.IList]) | Should Be $true + @($fixed.settingInstance.children).Count | Should Be 0 + } + + It 'ist idempotent: ein bereits korrektes Array bleibt ein Array gleicher Laenge' { + $node = @{ choiceSettingCollectionValue = @( @{ value = 'a' }, @{ value = 'b' } ) } + $fixed = Repair-SettingsCatalogArrays $node + ($fixed.choiceSettingCollectionValue -is [System.Collections.IList]) | Should Be $true + @($fixed.choiceSettingCollectionValue).Count | Should Be 2 + } + + It 'filtert leere String-Elemente aus einer Collection heraus' { + $node = @{ children = @('', @{ value = 'x' }, ' ') } + $fixed = Repair-SettingsCatalogArrays $node + @($fixed.children).Count | Should Be 1 + } + + It 'setzt einen zu String stringifizierten *TemplateReference auf null' { + $node = @{ settingInstance = @{ settingInstanceTemplateReference = 'System.Collections.Hashtable' } } + $fixed = Repair-SettingsCatalogArrays $node + $fixed.settingInstance.settingInstanceTemplateReference | Should Be $null + } +} + +Describe 'Remove-PolicyNullProps' { + + It 'entfernt Properties mit null-Wert' { + $node = @{ keep = 'a'; drop = $null } + $fixed = Remove-PolicyNullProps $node + $fixed.ContainsKey('keep') | Should Be $true + $fixed.ContainsKey('drop') | Should Be $false + } + + It 'behaelt Array-Properties als Array' { + $node = @{ values = @( @{ a = 1 } ) } + $fixed = Remove-PolicyNullProps $node + ($fixed.values -is [System.Collections.IList]) | Should Be $true + @($fixed.values).Count | Should Be 1 + } +}