Tests/Unit/Private/Get-SIAPagination.Tests.ps1

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

Describe 'Get-SIAPagination' {
    It 'returns only the first page when -NoAutoPaging is set' {
        InModuleScope psSIA {
            $firstPage = [pscustomobject]@{ items = @(1); continuationToken = 'next-1' }

            $result = Get-SIAPagination -FirstPage $firstPage -NoAutoPaging -NextPageRequest {
                throw 'NextPageRequest should not be invoked when -NoAutoPaging is set'
            }

            $result.Count | Should -Be 1
            $result[0].continuationToken | Should -Be 'next-1'
        }
    }

    It 'follows continuation tokens until none remain' {
        InModuleScope psSIA {
            $page1 = [pscustomobject]@{ items = @(1); continuationToken = 'token-a' }
            $page2 = [pscustomobject]@{ items = @(2); continuationToken = 'token-b' }
            $page3 = [pscustomobject]@{ items = @(3); continuationToken = $null }
            $callLog = [System.Collections.Generic.List[string]]::new()

            $result = Get-SIAPagination -FirstPage $page1 -NextPageRequest {
                param($token)
                $callLog.Add($token)
                if ($token -eq 'token-a') { $page2 } else { $page3 }
            }

            $result.Count | Should -Be 3
            $callLog | Should -Be @('token-a', 'token-b')
        }
    }

    It 'uses a custom continuation property name' {
        InModuleScope psSIA {
            $page1 = [pscustomobject]@{ b64_last_evaluated_key = 'abc' }
            $page2 = [pscustomobject]@{ b64_last_evaluated_key = $null }

            $result = Get-SIAPagination -FirstPage $page1 -ContinuationProperty 'b64_last_evaluated_key' -NextPageRequest {
                param($token) $page2
            }

            $result.Count | Should -Be 2
        }
    }

    It 'stops instead of looping forever if a token repeats' {
        InModuleScope psSIA {
            $page = [pscustomobject]@{ continuationToken = 'stuck' }

            $result = Get-SIAPagination -FirstPage $page -NextPageRequest {
                param($token) [pscustomobject]@{ continuationToken = 'stuck' }
            }

            $result.Count | Should -Be 2
        }
    }

    It 'stops cleanly when no known continuation property is present' {
        InModuleScope psSIA {
            $page = [pscustomobject]@{ items = @(1) }

            $result = Get-SIAPagination -FirstPage $page -NextPageRequest {
                throw 'NextPageRequest should not be invoked when no continuation property is present'
            }

            $result.Count | Should -Be 1
        }
    }

    It 'stops cleanly if NextPageRequest returns nothing' {
        InModuleScope psSIA {
            $page = [pscustomobject]@{ continuationToken = 'token-a' }

            $result = Get-SIAPagination -FirstPage $page -NextPageRequest {
                param($token) $null
            }

            $result.Count | Should -Be 1
        }
    }
}