using System;
using System.Linq;
using StellaOps.Feedser.Source.Vndr.Chromium;
using StellaOps.Feedser.Source.Vndr.Chromium.Internal;
using Xunit;

namespace StellaOps.Feedser.Source.Vndr.Chromium.Tests;

public sealed class ChromiumMapperTests
{
    [Fact]
    public void Map_DeduplicatesReferencesAndOrdersDeterministically()
    {
        var published = new DateTimeOffset(2024, 9, 12, 14, 0, 0, TimeSpan.Zero);
        var metadata = new ChromiumDocumentMetadata(
            "post-123",
            "Stable Channel Update",
            new Uri("https://chromium.example/stable-update.html"),
            published,
            null,
            "Security fixes");

        var dto = ChromiumDto.From(
            metadata,
            new[] { "CVE-2024-0001" },
            new[] { "windows" },
            new[] { new ChromiumVersionInfo("windows", "stable", "128.0.6613.88") },
            new[]
            {
                new ChromiumReference("https://chromium.example/ref1", "advisory", "Ref 1"),
                new ChromiumReference("https://chromium.example/ref1", "advisory", "Ref 1 duplicate"),
                new ChromiumReference("https://chromium.example/ref2", "patch", "Ref 2"),
            });

        var (advisory, _) = ChromiumMapper.Map(dto, VndrChromiumConnectorPlugin.SourceName, published);

        var referenceUrls = advisory.References.Select(r => r.Url).ToArray();
        Assert.Equal(
            new[]
            {
                "https://chromium.example/ref1",
                "https://chromium.example/ref2",
                "https://chromium.example/stable-update.html",
            },
            referenceUrls);
    }
}
