Tests/Unit/Private/ConvertFrom-SCAResponse.Tests.ps1

Import-Module (Join-Path $PSScriptRoot '../../../psSCA.psd1') -Force

Describe 'ConvertFrom-SCAResponse' {
    InModuleScope psSCA {
        It 'parses a JSON object into a PSCustomObject' {
            $result = ConvertFrom-SCAResponse -InputObject '{"id":"1","name":"test"}'
            $result.id | Should -Be '1'
            $result.name | Should -Be 'test'
        }

        It 'stamps the requested PSTypeName onto the result' {
            $result = ConvertFrom-SCAResponse -InputObject '{"id":"1"}' -TypeName 'psSCA.Widget'
            $result.PSObject.TypeNames | Should -Contain 'psSCA.Widget'
        }

        It 'stamps the type name onto every item in a JSON array' {
            $result = @(ConvertFrom-SCAResponse -InputObject '[{"id":"1"},{"id":"2"}]' -TypeName 'psSCA.Widget')
            $result.Count | Should -Be 2
            foreach ($item in $result) {
                $item.PSObject.TypeNames | Should -Contain 'psSCA.Widget'
            }
        }

        It 'preserves properties the module does not explicitly know about' {
            $result = ConvertFrom-SCAResponse -InputObject '{"id":"1","futureField":"unknown-to-module"}'
            $result.futureField | Should -Be 'unknown-to-module'
        }

        It 'returns $null for an empty response body' {
            ConvertFrom-SCAResponse -InputObject '' | Should -BeNullOrEmpty
        }
    }
}