using System.Linq;
using StellaOps.Feedser.Normalization.Identifiers;

namespace StellaOps.Feedser.Normalization.Tests;

public sealed class PackageUrlNormalizerTests
{
    [Fact]
    public void TryNormalizePackageUrl_LowersTypeAndNamespace()
    {
        var input = "pkg:NPM/Acme/Widget@1.0.0?Arch=X86_64";

        var success = IdentifierNormalizer.TryNormalizePackageUrl(input, out var normalized, out var parsed);

        Assert.True(success);
        Assert.Equal("pkg:npm/acme/widget@1.0.0?arch=X86_64", normalized);
        Assert.NotNull(parsed);
        Assert.Equal("npm", parsed!.Type);
        Assert.Equal(new[] { "acme" }, parsed.NamespaceSegments.ToArray());
        Assert.Equal("widget", parsed.Name);
    }

    [Fact]
    public void TryNormalizePackageUrl_OrdersQualifiers()
    {
        var input = "pkg:deb/debian/openssl?distro=x%2Fy&arch=amd64";

        var success = IdentifierNormalizer.TryNormalizePackageUrl(input, out var normalized, out _);

        Assert.True(success);
        Assert.Equal("pkg:deb/debian/openssl?arch=amd64&distro=x%2Fy", normalized);
    }

    [Fact]
    public void TryNormalizePackageUrl_TrimsWhitespace()
    {
        var input = "  pkg:pypi/Example/Package  ";

        var success = IdentifierNormalizer.TryNormalizePackageUrl(input, out var normalized, out _);

        Assert.True(success);
        Assert.Equal("pkg:pypi/example/package", normalized);
    }
}
