namespace NATS.Server.Core.Tests;

public class ClientFlagsTests
{
    [Fact]
    public void SetFlag_and_HasFlag_work()
    {
        var holder = new ClientFlagHolder();
        holder.HasFlag(ClientFlags.ConnectReceived).ShouldBeFalse();

        holder.SetFlag(ClientFlags.ConnectReceived);
        holder.HasFlag(ClientFlags.ConnectReceived).ShouldBeTrue();
    }

    [Fact]
    public void ClearFlag_removes_flag()
    {
        var holder = new ClientFlagHolder();
        holder.SetFlag(ClientFlags.ConnectReceived);
        holder.SetFlag(ClientFlags.IsSlowConsumer);

        holder.ClearFlag(ClientFlags.ConnectReceived);

        holder.HasFlag(ClientFlags.ConnectReceived).ShouldBeFalse();
        holder.HasFlag(ClientFlags.IsSlowConsumer).ShouldBeTrue();
    }

    [Fact]
    public void Multiple_flags_can_be_set_independently()
    {
        var holder = new ClientFlagHolder();
        holder.SetFlag(ClientFlags.ConnectReceived);
        holder.SetFlag(ClientFlags.WriteLoopStarted);
        holder.SetFlag(ClientFlags.FirstPongSent);

        holder.HasFlag(ClientFlags.ConnectReceived).ShouldBeTrue();
        holder.HasFlag(ClientFlags.WriteLoopStarted).ShouldBeTrue();
        holder.HasFlag(ClientFlags.FirstPongSent).ShouldBeTrue();
        holder.HasFlag(ClientFlags.IsSlowConsumer).ShouldBeFalse();
    }

    [Fact]
    public void SetFlag_is_thread_safe()
    {
        var holder = new ClientFlagHolder();
        var flags = Enum.GetValues<ClientFlags>();

        Parallel.ForEach(flags, flag => holder.SetFlag(flag));

        foreach (var flag in flags)
            holder.HasFlag(flag).ShouldBeTrue();
    }
}
