tests/Parse-WingetShowOutput.Tests.ps1

Describe "Parse-WingetShowOutput" {
    BeforeAll {
        # Parse-WingetShowOutput is internal, so test it via InModuleScope.
        # IMPORTANT: InModuleScope runs the scriptblock in the MODULE's session
        # state, not the test's -- so $output from the It block is NOT visible
        # unless passed explicitly via -Parameters (consumed by a matching param()).
        Import-Module "$PSScriptRoot/../WingetBatch.psd1" -Force
    }

    Context "Standard Output Parsing" {
        It "Parses standard fields correctly" {
            $output = @"
Found MongoDB Shell [MongoDB.Shell]
Version: 2.3.2
Publisher: MongoDB, Inc.
Publisher Url: https://www.mongodb.com/
Author: MongoDB, Inc.
Moniker: mongosh
Description: The MongoDB Shell description.
Homepage: https://www.mongodb.com/try/download/shell
License: SSPL
License Url: https://www.mongodb.com/licensing/server-side-public-license
Privacy Url: https://www.mongodb.com/legal/privacy-policy
Copyright: Copyright (c) MongoDB, Inc.
Copyright Url: https://www.mongodb.com/legal/copyright
Tags: mongodb, shell, cli
Installer:
  Installer Type: wix
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "MongoDB.Shell"
            }

            $result.Id | Should -Be "MongoDB.Shell"
            $result.Version | Should -Be "2.3.2"
            $result.Publisher | Should -Be "MongoDB, Inc."
            $result.PublisherUrl | Should -Be "https://www.mongodb.com/"
            $result.Author | Should -Be "MongoDB, Inc."
            $result.Moniker | Should -Be "mongosh"
            $result.Description | Should -Be "The MongoDB Shell description."
            $result.Homepage | Should -Be "https://www.mongodb.com/try/download/shell"
            $result.License | Should -Be "SSPL"
            $result.LicenseUrl | Should -Be "https://www.mongodb.com/licensing/server-side-public-license"
            $result.PrivacyUrl | Should -Be "https://www.mongodb.com/legal/privacy-policy"
            $result.Copyright | Should -Be "Copyright (c) MongoDB, Inc."
            $result.CopyrightUrl | Should -Be "https://www.mongodb.com/legal/copyright"
            $result.Tags | Should -Be "mongodb", "shell", "cli"
            $result.Installer | Should -Be "wix"
        }

        It "Parses GitHub Publisher URL correctly" {
            $output = @"
Publisher Url: https://github.com/microsoft/winget-cli
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "Test"
            }

            $result.PublisherGitHub | Should -Be "https://github.com/microsoft/winget-cli"
        }
    }

    Context "Edge Cases" {
        It "Handles extra whitespace around keys and values" {
            $output = @"
  Version: 1.0.0
   Publisher: Test Pub
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "Test"
            }

            $result.Version | Should -Be "1.0.0"
            $result.Publisher | Should -Be "Test Pub"
        }

        It "Handles empty values gracefully" {
            $output = @"
Version:
Publisher:
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "Test"
            }

            $result.Version | Should -BeNullOrEmpty
            $result.Publisher | Should -BeNullOrEmpty
        }

        It "Handles lines without colons (ignores them)" {
            $output = @"
Just some text
Another line
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "Test"
            }

            $result.Version | Should -BeNullOrEmpty
        }

        It "Handles keys with spaces correctly" {
            $output = @"
Release Notes Url: https://example.com/notes
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "Test"
            }

            $result.ReleaseNotesUrl | Should -Be "https://example.com/notes"
        }

        It "Handles colons in values correctly" {
            $output = @"
Description: This is a description: with a colon
"@

            $result = InModuleScope WingetBatch -Parameters @{ output = $output } {
                param($output)
                Parse-WingetShowOutput -Output $output -PackageId "Test"
            }

            $result.Description | Should -Be "This is a description: with a colon"
        }
    }
}