using StellaOps.Feedser.Normalization.Distro;

namespace StellaOps.Feedser.Normalization.Tests;

public sealed class NevraParserTests
{
    [Fact]
    public void ToCanonicalString_RoundTripsTrimmedInput()
    {
        var parsed = Nevra.Parse("  kernel-0:4.18.0-80.el8.x86_64  ");

        Assert.Equal("kernel-0:4.18.0-80.el8.x86_64", parsed.Original);
        Assert.Equal("kernel-0:4.18.0-80.el8.x86_64", parsed.ToCanonicalString());
    }

    [Fact]
    public void ToCanonicalString_ReconstructsKnownArchitecture()
    {
        var parsed = Nevra.Parse("bash-5.2.15-3.el9_4.arm64");

        Assert.Equal("bash-5.2.15-3.el9_4.arm64", parsed.ToCanonicalString());
    }

    [Fact]
    public void ToCanonicalString_HandlesMissingArchitecture()
    {
        var parsed = Nevra.Parse("openssl-libs-1:1.1.1k-7.el8");

        Assert.Equal("openssl-libs-1:1.1.1k-7.el8", parsed.ToCanonicalString());
    }

    [Fact]
    public void TryParse_ReturnsTrueForExplicitZeroEpoch()
    {
        var success = Nevra.TryParse("glibc-0:2.36-8.el9.x86_64", out var nevra);

        Assert.True(success);
        Assert.NotNull(nevra);
        Assert.True(nevra!.HasExplicitEpoch);
        Assert.Equal(0, nevra.Epoch);
        Assert.Equal("glibc-0:2.36-8.el9.x86_64", nevra.ToCanonicalString());
    }

    [Fact]
    public void TryParse_IgnoresUnknownArchitectureSuffix()
    {
        var success = Nevra.TryParse("package-1.0-1.el9.weirdarch", out var nevra);

        Assert.True(success);
        Assert.NotNull(nevra);
        Assert.Null(nevra!.Architecture);
        Assert.Equal("package-1.0-1.el9.weirdarch", nevra.Original);
        Assert.Equal("package-1.0-1.el9.weirdarch", nevra.ToCanonicalString());
    }

    [Fact]
    public void TryParse_ReturnsFalseForMalformedNevra()
    {
        var success = Nevra.TryParse("bad-format", out var nevra);

        Assert.False(success);
        Assert.Null(nevra);
    }
}
