using StellaOps.Feedser.Source.Ghsa.Internal;
using StellaOps.Feedser.Storage.Mongo.Documents;

namespace StellaOps.Feedser.Source.Ghsa.Tests;

public sealed class GhsaMapperTests
{
    [Fact]
    public void Map_WhenCvssVectorMissing_UsesSeverityFallback()
    {
        var recordedAt = new DateTimeOffset(2025, 4, 10, 12, 0, 0, TimeSpan.Zero);
        var document = new DocumentRecord(
            Id: Guid.Parse("d7814678-3c3e-4e63-98c4-68e2f6d7ba6f"),
            SourceName: GhsaConnectorPlugin.SourceName,
            Uri: "https://github.com/advisories/GHSA-fallback-test",
            FetchedAt: recordedAt.AddHours(-2),
            Sha256: "sha256-ghsa-fallback-test",
            Status: "completed",
            ContentType: "application/json",
            Headers: null,
            Metadata: null,
            Etag: "\"etag-ghsa-fallback\"",
            LastModified: recordedAt.AddHours(-3),
            GridFsId: null);

        var dto = new GhsaRecordDto
        {
            GhsaId = "GHSA-fallback-test",
            Summary = "Severity-only GHSA advisory",
            Description = "GHSA record where GitHub omitted CVSS vector/score.",
            Severity = null,
            PublishedAt = recordedAt.AddDays(-3),
            UpdatedAt = recordedAt.AddDays(-1),
            Aliases = new[] { "GHSA-fallback-test" },
            References = Array.Empty<GhsaReferenceDto>(),
            Affected = Array.Empty<GhsaAffectedDto>(),
            Credits = Array.Empty<GhsaCreditDto>(),
            Cwes = Array.Empty<GhsaWeaknessDto>(),
            Cvss = new GhsaCvssDto
            {
                Severity = "CRITICAL",
                Score = null,
                VectorString = null,
            }
        };

        var advisory = GhsaMapper.Map(dto, document, recordedAt);

        Assert.Equal("critical", advisory.Severity);
        Assert.Equal("ghsa:severity/critical", advisory.CanonicalMetricId);
        Assert.True(advisory.CvssMetrics.IsEmpty);
    }
}
