Tests/InvokeZEP.Tests.ps1

$modulePath = Join-Path $PSScriptRoot '..\InvokeZEP.psd1'
Import-Module $modulePath -Force

$config = Get-ZEPConfiguration
if (-not $config -or [string]::IsNullOrWhiteSpace($config.BaseUri)) {
    throw 'Get-ZEPConfiguration returned an invalid configuration object.'
}

$commands = @('Get-ZEPConfiguration', 'Set-ZEPConfiguration', 'Invoke-ZEPRest', 'Get-ZEPOffer', 'Get-ZEPOffers', 'Get-ZEPOfferItems', 'Search-ZEPOffers')
foreach ($commandName in $commands) {
    if (-not (Get-Command -Name $commandName -ErrorAction SilentlyContinue)) {
        throw "Command '$commandName' was not exported from the module."
    }
}

$cacheChanged = & (Get-Command -Name Test-ZEPOffersCacheNeedsRefresh -ErrorAction Stop) -CacheObject ([pscustomobject]@{ generatedAtUtc = (Get-Date).ToUniversalTime().ToString('o'); total = 10; items = @() }) -CurrentTotal 12
if ($cacheChanged -ne $true) {
    throw 'The cache refresh detection did not detect a changed total count.'
}

$cachePartial = & (Get-Command -Name Test-ZEPOffersCacheNeedsRefresh -ErrorAction Stop) -CacheObject ([pscustomobject]@{ generatedAtUtc = (Get-Date).ToUniversalTime().ToString('o'); total = 10; items = @(@{ id = 1 }, @{ id = 2 }) }) -CurrentTotal 10
if ($cachePartial -ne $true) {
    throw 'The cache refresh detection did not detect a partial cache.'
}

$cacheComplete = & (Get-Command -Name Test-ZEPOffersCacheNeedsRefresh -ErrorAction Stop) -CacheObject ([pscustomobject]@{ generatedAtUtc = (Get-Date).ToUniversalTime().ToString('o'); total = 10; items = @(@{ id = 1 }, @{ id = 2 }, @{ id = 3 }, @{ id = 4 }, @{ id = 5 }, @{ id = 6 }, @{ id = 7 }, @{ id = 8 }, @{ id = 9 }, @{ id = 10 }) }) -CurrentTotal 10
if ($cacheComplete -ne $false) {
    throw 'The cache refresh detection incorrectly flagged a complete cache as stale.'
}

$cacheExpired = & (Get-Command -Name Test-ZEPOffersCacheNeedsRefresh -ErrorAction Stop) -CacheObject ([pscustomobject]@{ generatedAtUtc = (Get-Date).ToUniversalTime().AddMinutes(-61).ToString('o'); total = 10; items = @(@{ id = 1 }, @{ id = 2 }, @{ id = 3 }, @{ id = 4 }, @{ id = 5 }, @{ id = 6 }, @{ id = 7 }, @{ id = 8 }, @{ id = 9 }, @{ id = 10 }) }) -CurrentTotal 10 -CacheTtlMinutes 60
if ($cacheExpired -ne $true) {
    throw 'The cache refresh detection did not detect an expired cache based on TTL.'
}

$tempCachePath = Join-Path ([System.IO.Path]::GetTempPath()) ('InvokeZEP-search-test-' + [guid]::NewGuid().ToString('N') + '.json')
try {
    $cacheFixture = [pscustomobject]@{
        version = 1
        generatedAtUtc = (Get-Date).ToUniversalTime().ToString('o')
        total = 3
        items = @(
            [pscustomobject]@{ id = 1; data = [pscustomobject]@{ id = 1; title = 'Test Angebot'; status = [pscustomobject]@{ name = 'Aktiv' } } },
            [pscustomobject]@{ id = 2; data = [pscustomobject]@{ id = 2; title = 'Anderes Angebot'; status = [pscustomobject]@{ name = 'Inaktiv' } } },
            [pscustomobject]@{ id = 3; data = [pscustomobject]@{ id = 3; title = 'Demo test case'; status = [pscustomobject]@{ name = 'Aktiv' } } }
        )
    }

    $cacheFixture | ConvertTo-Json -Depth 20 | Set-Content -LiteralPath $tempCachePath -Encoding UTF8

    $searchResults = & (Get-Command -Name Search-ZEPOffers -ErrorAction Stop) -PropertyName 'title' -SearchTerm 'test' -ReferenceSampleSize 1000 -MaxResults 5 -ValidateCache:$false -CachePath $tempCachePath
    if (@($searchResults).Count -ne 2) {
        throw 'Search-ZEPOffers did not return the expected amount of title matches.'
    }
}
finally {
    if (Test-Path -LiteralPath $tempCachePath) {
        Remove-Item -LiteralPath $tempCachePath -Force -ErrorAction SilentlyContinue
    }
}

Write-Host 'InvokeZEP module import and export validation passed.' -ForegroundColor Green