using NATS.Server.JetStream;
using NATS.Server.JetStream.Models;

namespace NATS.Server.Tests;

public class JetStreamConsumerFlowControlParityTests
{
    [Fact]
    public async Task Push_consumer_emits_flow_control_frames_when_enabled()
    {
        var streams = new StreamManager();
        streams.CreateOrUpdate(new StreamConfig
        {
            Name = "ORDERS",
            Subjects = ["orders.*"],
        });

        var consumers = new ConsumerManager();
        consumers.CreateOrUpdate("ORDERS", new ConsumerConfig
        {
            DurableName = "PUSH",
            Push = true,
            AckPolicy = AckPolicy.Explicit,
            FlowControl = true,
            RateLimitBps = 1024,
        });

        var ack = streams.Capture("orders.created", "x"u8.ToArray());
        streams.TryGet("ORDERS", out var stream).ShouldBeTrue();
        var message = await stream.Store.LoadAsync(ack!.Seq, default);
        message.ShouldNotBeNull();
        consumers.OnPublished("ORDERS", message!);

        var first = consumers.ReadPushFrame("ORDERS", "PUSH");
        var second = consumers.ReadPushFrame("ORDERS", "PUSH");
        first.ShouldNotBeNull();
        second.ShouldNotBeNull();
        first!.IsData.ShouldBeTrue();
        second!.IsFlowControl.ShouldBeTrue();
    }
}
