using System;
using StellaOps.Feedser.Models;
using Xunit;

namespace StellaOps.Feedser.Models.Tests;

public sealed class AdvisoryProvenanceTests
{
    [Fact]
    public void FieldMask_NormalizesAndDeduplicates()
    {
        var timestamp = DateTimeOffset.Parse("2025-01-01T00:00:00Z");
        var provenance = new AdvisoryProvenance(
            source: "nvd",
            kind: "map",
            value: "CVE-2025-0001",
            recordedAt: timestamp,
            fieldMask: new[] { " AffectedPackages[] ", "affectedpackages[]", "references[]" });

        Assert.Equal(timestamp, provenance.RecordedAt);
        Assert.Collection(
            provenance.FieldMask,
            mask => Assert.Equal("affectedpackages[]", mask),
            mask => Assert.Equal("references[]", mask));
        Assert.Null(provenance.DecisionReason);
    }

    [Fact]
    public void EmptyProvenance_ExposesEmptyFieldMask()
    {
        Assert.True(AdvisoryProvenance.Empty.FieldMask.IsEmpty);
        Assert.Null(AdvisoryProvenance.Empty.DecisionReason);
    }

    [Fact]
    public void DecisionReason_IsTrimmed()
    {
        var timestamp = DateTimeOffset.Parse("2025-03-01T00:00:00Z");
        var provenance = new AdvisoryProvenance(
            source: "merge",
            kind: "precedence",
            value: "summary",
            recordedAt: timestamp,
            fieldMask: new[] { ProvenanceFieldMasks.Advisory },
            decisionReason: "  freshness_override  ");

        Assert.Equal("freshness_override", provenance.DecisionReason);
    }
}
