Tests/Unit/Private/ConvertTo-SIARequestBody.Tests.ps1

BeforeAll {
    $moduleRoot = (Resolve-Path (Join-Path $PSScriptRoot '../../..')).Path
    Import-Module (Join-Path $moduleRoot 'psSIA.psd1') -Force
}

Describe 'ConvertTo-SIARequestBody' {
    It 'serializes a hashtable to JSON by default' {
        InModuleScope psSIA {
            $json = ConvertTo-SIARequestBody -InputObject @{ name = 'contoso' }
            ($json | ConvertFrom-Json).name | Should -Be 'contoso'
        }
    }

    It 'serializes nested objects to JSON' {
        InModuleScope psSIA {
            $json = ConvertTo-SIARequestBody -InputObject @{ target = @{ id = 'abc'; tags = @('a', 'b') } }
            $parsed = $json | ConvertFrom-Json
            $parsed.target.id | Should -Be 'abc'
            $parsed.target.tags.Count | Should -Be 2
        }
    }

    It 'form-encodes when -AsForm is specified' {
        InModuleScope psSIA {
            $form = ConvertTo-SIARequestBody -AsForm -InputObject @{
                grant_type = 'client_credentials'
                client_id  = 'svc account'
            }
            $form | Should -Match 'grant_type=client_credentials'
            $form | Should -Match 'client_id=svc%20account'
        }
    }

    It 'joins multiple form fields with an ampersand' {
        InModuleScope psSIA {
            $form = ConvertTo-SIARequestBody -AsForm -InputObject @{ a = '1'; b = '2' }
            ($form -split '&').Count | Should -Be 2
        }
    }
}