using NuGet.Versioning;
using StellaOps.Feedser.Source.Common.Packages;

namespace StellaOps.Feedser.Source.Common.Tests;

public sealed class PackageCoordinateHelperTests
{
    [Fact]
    public void TryParsePackageUrl_ReturnsCanonicalForm()
    {
        var success = PackageCoordinateHelper.TryParsePackageUrl("pkg:npm/@scope/example@1.0.0?env=prod", out var coordinates);

        Assert.True(success);
        Assert.NotNull(coordinates);
        Assert.Equal("pkg:npm/@scope/example@1.0.0?env=prod", coordinates!.Canonical);
        Assert.Equal("npm", coordinates.Type);
        Assert.Equal("example", coordinates.Name);
        Assert.Equal("1.0.0", coordinates.Version);
        Assert.Equal("prod", coordinates.Qualifiers["env"]);
    }

    [Fact]
    public void TryParseSemVer_NormalizesVersion()
    {
        var success = PackageCoordinateHelper.TryParseSemVer("1.2.3+build", out var version, out var normalized);

        Assert.True(success);
        Assert.Equal(SemanticVersion.Parse("1.2.3"), version);
        Assert.Equal("1.2.3", normalized);
    }

    [Fact]
    public void TryParseSemVerRange_SupportsCaret()
    {
        var success = PackageCoordinateHelper.TryParseSemVerRange("^1.2.3", out var range);

        Assert.True(success);
        Assert.NotNull(range);
        Assert.True(range!.Satisfies(NuGetVersion.Parse("1.3.0")));
    }
}
