Private/Generator/Tests/ConvertTo-OpenApiGenAbout.Tests.ps1

BeforeAll {
    $env:TCS_CONFIG_ROOT = Join-Path -Path $TestDrive -ChildPath 'config'
    $env:TCS_SKIP_UPDATE_CHECK = '1'
    $env:TCS_TELEMETRY_OPTOUT = '1'
    $ModuleRoot = Split-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -Parent
    Import-Module -Name (Join-Path -Path $ModuleRoot -ChildPath 'tcs.openapi.psd1') -Force
    $RepoRoot = Split-Path -Path (Split-Path -Path $ModuleRoot -Parent) -Parent
    . (Join-Path -Path $RepoRoot -ChildPath 'tests/Helpers/New-TestOpenApiModel.ps1')
}

Describe 'ConvertTo-OpenApiGenAbout' {
    BeforeAll {
        function ConvertTo-TestAbout {
            param($Document, [string]$HelpUri)
            $functions = @(
                [pscustomobject]@{ Name = 'Set-ShopContext'; Summary = 'Set the connection used by the commands of this module.' },
                [pscustomobject]@{ Name = 'Get-ShopOrder'; Summary = 'Gets an **order** by {id}.' }
            )
            InModuleScope tcs.openapi -Parameters @{ Document = $Document; Functions = $functions; HelpUri = $HelpUri } {
                param($Document, $Functions, $HelpUri)
                $option = Resolve-OpenApiGenOption -ModuleName 'Shop.Api' -OutputPath '/out' -NounPrefix 'Shop' -GeneratorVersion '1.2.3' -HelpUri $HelpUri
                $template = (Get-OpenApiGenTemplate -Path (Join-Path -Path $script:TcsOpenApiModuleRoot -ChildPath 'Templates'))['About.help.txt']
                ConvertTo-OpenApiGenAbout -Option $option -Document $Document -Function $Functions -ConnectExample ' -ApiKey $key' -Template $template
            }
        }
    }

    It 'writes the sections of an about topic' {
        $text = ConvertTo-TestAbout -Document (New-TestDocument -Title 'Shop' -Version '2.0' -Description 'The <b>shop</b> API.')
        $text | Should -Match '(?m)^TOPIC\n about_Shop\.Api$'
        $text | Should -Match '(?m)^SHORT DESCRIPTION\n PowerShell commands for Shop \(API version 2\.0\), generated by tcs\.openapi\.$'
        $text | Should -Match '(?m)^LONG DESCRIPTION\n The shop API\.\n\n Every command calls one operation'
        $text | Should -Match '(?m)^ Set-ShopContext -ApiKey \$key$'
        $text | Should -Match '(?m)^COMMANDS\n Get-ShopOrder\n Gets an order by `\{id\}`\.\n Set-ShopContext\n'
        $text | Should -Match 'tcs\.openapi 1\.2\.3'
        $text | Should -Match '(?m)^SEE ALSO\n about_tcs\.openapi\n Set-ShopContext\n$'
    }

    It 'leaves out an empty long description' {
        $text = ConvertTo-TestAbout -Document (New-TestDocument -Title 'Shop')
        $text | Should -Match '(?m)^LONG DESCRIPTION\n Every command calls one operation'
    }

    It 'lists the online help and the document links in SEE ALSO' {
        $document = New-TestDocument -Title 'Shop' -ExternalDocsUrl 'https://docs.example.com' -ContactUrl 'https://example.com/support' -LicenseUrl 'not a url'
        $text = ConvertTo-TestAbout -Document $document -HelpUri 'https://help.example.com/shop/{0}'
        $text | Should -Match '(?m)^ https://help\.example\.com/shop/about_Shop\.Api\n https://docs\.example\.com\n https://example\.com/support\n$'
        $text | Should -Not -Match 'not a url'
    }

    It 'wraps long text at 80 characters' {
        $long = (1..40 | ForEach-Object -Process { 'word' }) -join ' '
        $text = ConvertTo-TestAbout -Document (New-TestDocument -Title 'Shop' -Description $long)
        foreach ($line in $text.Split("`n")) {
            $line.Length | Should -BeLessOrEqual 80
        }
    }
}