using Shouldly;
using ZB.MOM.NatsNet.Server;

namespace ZB.MOM.NatsNet.Server.Tests.ImplBacklog;

public sealed partial class ConcurrencyTests1
{
    [Fact]
    public void NoRaceJetStreamDeleteStreamManyConsumers_ShouldSucceed()
    {
        var cluster = new JetStreamCluster();
        var account = "A";
        var stream = "S";

        for (var i = 0; i < 250; i++)
        {
            var assignment = new ConsumerAssignment { Name = $"C{i}", Stream = stream };
            cluster.TrackInflightConsumerProposal(account, stream, assignment, deleted: false);
        }

        for (var i = 0; i < 250; i++)
            cluster.RemoveInflightConsumerProposal(account, stream, $"C{i}");

        cluster.InflightConsumers.ContainsKey(account).ShouldBeFalse();
    }

    [Fact]
    public void NoRaceJetStreamAPIConsumerListPaging_ShouldSucceed()
    {
        var cluster = new JetStreamCluster();
        var account = "A";
        var stream = "S";

        for (var i = 0; i < 120; i++)
        {
            cluster.TrackInflightConsumerProposal(
                account,
                stream,
                new ConsumerAssignment { Name = $"C{i}", Stream = stream },
                deleted: false);
        }

        var page1 = cluster.InflightConsumers[account][stream].Keys.OrderBy(static k => k).Take(50).ToArray();
        var page2 = cluster.InflightConsumers[account][stream].Keys.OrderBy(static k => k).Skip(50).Take(50).ToArray();

        page1.Length.ShouldBe(50);
        page2.Length.ShouldBe(50);
        page1.Intersect(page2).ShouldBeEmpty();
    }
}
