functions/RemoteGistMap.Integration.Tests.ps1

# <copyright file="RemoteGistMap.Integration.Tests.ps1" company="Endjin Limited">
# Copyright (c) Endjin Limited. All rights reserved.
# </copyright>

Describe "Remote GistMap Integration" -Tag 'Integration' {

    BeforeAll {
        Import-Module powershell-yaml -Force
        Get-Module endjin-gists | Remove-Module
        Import-Module (Join-Path $PSScriptRoot '..' 'endjin-gists.psd1') -Force

        $TestGistMapRef = 'main'
        $TestGistMapRepo = 'endjin/endjin-gists'
        $TestGistMapRepoPath = 'module/test-gist-map.yml'
        $TestGitSource = @{
            url  = "https://github.com/$TestGistMapRepo.git"
            ref  = $TestGistMapRef
            path = $TestGistMapRepoPath
        }
    }

    Context "_Get-RemoteGistMap via vendir" {

        BeforeAll {
            $script:yamlResult = & (Get-Module 'endjin-gists') {
                param($GitSource)
                _Get-RemoteGistMap -GitSource $GitSource
            } $TestGitSource
        }

        It "Should return non-null YAML string" {
            $script:yamlResult | Should -Not -BeNullOrEmpty
        }

        It "Should return parseable YAML with expected groups" {
            $parsed = $script:yamlResult | ConvertFrom-Yaml
            $parsed.Keys | Should -Contain 'test-group-1'
            $parsed.Keys | Should -Contain 'test-group-2'
        }

        It "Should have correct entry counts per group" {
            $parsed = $script:yamlResult | ConvertFrom-Yaml
            $parsed['test-group-1'].Count | Should -Be 2
            $parsed['test-group-2'].Count | Should -Be 3
        }

        It "Should clean up temp directory after fetch" {
            $tempDirs = Get-ChildItem ([System.IO.Path]::GetTempPath()) -Directory -Filter 'endjin-gists-remote-*' -ErrorAction SilentlyContinue
            $tempDirs | Should -BeNullOrEmpty
        }
    }

    Context "_Get-GistMapData end-to-end" {

        It "Should succeed via vendir git fetch" {
            $result = & (Get-Module 'endjin-gists') {
                param($GitSource)
                _Get-GistMapData -GistMapGitSource $GitSource -NoCache
            } $TestGitSource

            $result | Should -Not -BeNullOrEmpty
            $result.Keys | Should -Contain 'test-group-1'
            $result.Keys | Should -Contain 'test-group-2'
        }

        It "Should fall back to vendir when HTTP URL is broken" {
            $brokenUrl = "https://raw.githubusercontent.com/$TestGistMapRepo/$TestGistMapRef/nonexistent/path.yml"
            $result = & (Get-Module 'endjin-gists') {
                param($Url, $GitSource)
                _Get-GistMapData -GistMapUrl $Url -GistMapGitSource $GitSource -NoCache
            } $brokenUrl $TestGitSource

            $result | Should -Not -BeNullOrEmpty
            $result.Keys | Should -Contain 'test-group-1'
            $result.Keys | Should -Contain 'test-group-2'
        }
    }

    Context "Get-EndjinGists with -GistMapRepo" {

        It "Should return 5 gists with -PassThru" {
            $result = Get-EndjinGists -GistMapRepo $TestGistMapRepo `
                                      -GistMapRef $TestGistMapRef `
                                      -GistMapRepoPath $TestGistMapRepoPath `
                                      -PassThru

            $result | Should -HaveCount 5
            $result | ForEach-Object {
                $_.PSObject.Properties.Name | Should -Contain 'Group'
                $_.PSObject.Properties.Name | Should -Contain 'Name'
            }
        }

        It "Should filter by group" {
            $result = Get-EndjinGists -GistMapRepo $TestGistMapRepo `
                                      -GistMapRef $TestGistMapRef `
                                      -GistMapRepoPath $TestGistMapRepoPath `
                                      -Group 'test-group-1' `
                                      -PassThru

            $result | Should -HaveCount 2
            $result | ForEach-Object { $_.Group | Should -Be 'test-group-1' }
        }

        It "Should filter by group and name" {
            $result = Get-EndjinGists -GistMapRepo $TestGistMapRepo `
                                      -GistMapRef $TestGistMapRef `
                                      -GistMapRepoPath $TestGistMapRepoPath `
                                      -Group 'test-group-1' `
                                      -Name 'gist1' `
                                      -PassThru

            $result | Should -HaveCount 1
            $result[0].Name | Should -Be 'gist1'
            $result[0].Description | Should -Be 'A test gist to demonstrate remote gist-map'
        }
    }
}