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

namespace NATS.Server.JetStream.Tests;

public class JetStreamStreamPolicyParityTests
{
    [Fact]
    public async Task Stream_rejects_oversize_message_and_prunes_by_max_age_and_per_subject_limits()
    {
        var streamManager = new StreamManager();
        var create = streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "P",
            Subjects = ["p.*"],
            MaxMsgSize = 8,
            MaxAgeMs = 20,
            MaxMsgsPer = 1,
        });
        create.Error.ShouldBeNull();

        var oversized = streamManager.Capture("p.a", "0123456789"u8.ToArray());
        oversized.ShouldNotBeNull();
        oversized!.ErrorCode.ShouldBe(10054);

        streamManager.Capture("p.a", "one"u8.ToArray())!.ErrorCode.ShouldBeNull();
        streamManager.Capture("p.a", "two"u8.ToArray())!.ErrorCode.ShouldBeNull();

        streamManager.TryGet("P", out var handle).ShouldBeTrue();
        var beforeAgePrune = await handle.Store.GetStateAsync(default);
        beforeAgePrune.Messages.ShouldBe((ulong)1);

        await Task.Delay(30);
        streamManager.Capture("p.b", "x"u8.ToArray())!.ErrorCode.ShouldBeNull();
        var afterAgePrune = await handle.Store.GetStateAsync(default);
        afterAgePrune.Messages.ShouldBe((ulong)1);
    }
}
