Private/UI/Test-ModuleUpdate.ps1

# Copyright (c) 2026 Sandy Zeng. All rights reserved.
# Source-available. All rights reserved. See LICENSE file.

<#
    Test-ModuleUpdate.ps1 — Checks PowerShell Gallery for a newer version of IntuneDiff.
 
    Author: Sandy Zeng
    Project: IntuneDiff
 
    Version History:
    1.0.0 Initial release.
    1.0.1 Added module update check and update banner in main window.
    1.0.2 Use Get-Module instead of Import-PowerShellDataFile for reliable version detection.
    1.0.4 Version bump to validate end-to-end update notification.
#>


function Test-ModuleUpdate {
    <#
    .SYNOPSIS
        Checks PowerShell Gallery for a newer version of IntuneDiff.
        Returns a PSCustomObject with UpdateAvailable, CurrentVersion, LatestVersion, or $null on failure.
 
    #>

    [CmdletBinding()]
    param()

    try {
        $mod = Get-Module -Name IntuneDiff | Select-Object -First 1
        if (-not $mod) { return $null }
        $currentVersion = $mod.Version.ToString()
        $galleryUrl = 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id=%27IntuneDiff%27&$orderby=Version%20desc&$top=1'
        $response = Invoke-RestMethod -Uri $galleryUrl -TimeoutSec 5 -ErrorAction Stop
        $latestVersion = ($response | Select-Object -First 1).properties.Version
        if (-not $latestVersion) { return $null }

        $current = [version]$currentVersion
        $latest  = [version]$latestVersion

        [pscustomobject]@{
            UpdateAvailable = $latest -gt $current
            CurrentVersion  = $currentVersion
            LatestVersion   = $latestVersion
        }
    } catch {
        Write-IDLog "Update check failed: $($_.Exception.Message)"
        return $null
    }
}