using Microsoft.Extensions.Logging.Abstractions;
using StellaOps.Feedser.Storage.Mongo.MergeEvents;

namespace StellaOps.Feedser.Storage.Mongo.Tests;

[Collection("mongo-fixture")]
public sealed class MergeEventStoreTests : IClassFixture<MongoIntegrationFixture>
{
    private readonly MongoIntegrationFixture _fixture;

    public MergeEventStoreTests(MongoIntegrationFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public async Task AppendAndReadMergeEvents()
    {
        var store = new MergeEventStore(_fixture.Database, NullLogger<MergeEventStore>.Instance);
        var record = new MergeEventRecord(
            Guid.NewGuid(),
            "ADV-1",
            new byte[] { 0x01 },
            new byte[] { 0x02 },
            DateTimeOffset.UtcNow,
            new List<Guid> { Guid.NewGuid() },
            Array.Empty<MergeFieldDecision>());

        await store.AppendAsync(record, CancellationToken.None);

        var recent = await store.GetRecentAsync("ADV-1", 10, CancellationToken.None);
        Assert.Single(recent);
        Assert.Equal(record.AfterHash, recent[0].AfterHash);
    }
}
