using System.Collections.Generic;
using StellaOps.Vexer.Core;
using Xunit;

namespace StellaOps.Vexer.Core.Tests;

public sealed class VexQuerySignatureTests
{
    [Fact]
    public void FromFilters_SortsAlphabetically()
    {
        var filters = new[]
        {
            new KeyValuePair<string, string>("provider", "redhat"),
            new KeyValuePair<string, string>("vulnId", "CVE-2025-0001"),
            new KeyValuePair<string, string>("provider", "cisco"),
        };

        var signature = VexQuerySignature.FromFilters(filters);

        Assert.Equal("provider=cisco&provider=redhat&vulnId=CVE-2025-0001", signature.Value);
    }

    [Fact]
    public void FromQuery_NormalizesFiltersAndSort()
    {
        var query = VexQuery.Create(
            filters: new[]
            {
                new VexQueryFilter(" provider ", " redhat "),
                new VexQueryFilter("vulnId", "CVE-2025-0002"),
            },
            sort: new[]
            {
                new VexQuerySort("published", true),
                new VexQuerySort("severity", false),
            },
            limit: 200,
            offset: 10,
            view: "consensus");

        var signature = VexQuerySignature.FromQuery(query);

        Assert.Equal(
            "provider=redhat&vulnId=CVE-2025-0002&sort=-published&sort=+severity&limit=200&offset=10&view=consensus",
            signature.Value);
    }

    [Fact]
    public void ComputeHash_ReturnsStableSha256()
    {
        var signature = new VexQuerySignature("provider=redhat&vulnId=CVE-2025-0003");

        var address = signature.ComputeHash();

        Assert.Equal("sha256", address.Algorithm);
        Assert.Equal("44c9881aaa79050ae943eaaf78afa697b1a4d3e38b03e20db332f2bd1e5b1029", address.Digest);
    }
}
