using StellaOps.Feedser.Models;
using Xunit;

namespace StellaOps.Feedser.Models.Tests;

public sealed class NevraPrimitiveExtensionsTests
{
    [Fact]
    public void ToNormalizedVersionRule_ProducesRangeWhenBoundsAvailable()
    {
        var primitive = new NevraPrimitive(
            Introduced: new NevraComponent("openssl", 1, "1.1.1k", "4", "x86_64"),
            Fixed: new NevraComponent("openssl", 1, "1.1.1n", "5", "x86_64"),
            LastAffected: null);

        var rule = primitive.ToNormalizedVersionRule("rhel-8");

        Assert.NotNull(rule);
        Assert.Equal(NormalizedVersionSchemes.Nevra, rule!.Scheme);
        Assert.Equal(NormalizedVersionRuleTypes.Range, rule.Type);
        Assert.Equal("openssl-1:1.1.1k-4.x86_64", rule.Min);
        Assert.True(rule.MinInclusive);
        Assert.Equal("openssl-1:1.1.1n-5.x86_64", rule.Max);
        Assert.False(rule.MaxInclusive);
        Assert.Equal("rhel-8", rule.Notes);
    }

    [Fact]
    public void ToNormalizedVersionRule_UsesLastAffectedAsInclusiveUpperBound()
    {
        var primitive = new NevraPrimitive(
            Introduced: null,
            Fixed: null,
            LastAffected: new NevraComponent("kernel", 0, "5.15.0", "1024.18", "x86_64"));

        var rule = primitive.ToNormalizedVersionRule();

        Assert.NotNull(rule);
        Assert.Equal(NormalizedVersionSchemes.Nevra, rule!.Scheme);
        Assert.Equal(NormalizedVersionRuleTypes.LessThanOrEqual, rule.Type);
        Assert.Equal("kernel-5.15.0-1024.18.x86_64", rule.Max);
        Assert.True(rule.MaxInclusive);
    }
}
