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

namespace NATS.Server.Tests;

public class JetStreamDedupeWindowParityTests
{
    [Fact]
    public async Task Dedupe_window_expires_entries_and_allows_republish_after_window_boundary()
    {
        var streamManager = new StreamManager();
        streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "D",
            Subjects = ["d.*"],
            DuplicateWindowMs = 25,
        }).Error.ShouldBeNull();

        var publisher = new JetStreamPublisher(streamManager);
        publisher.TryCaptureWithOptions("d.1", "one"u8.ToArray(), new PublishOptions { MsgId = "m-1" }, out var first).ShouldBeTrue();
        publisher.TryCaptureWithOptions("d.1", "dup"u8.ToArray(), new PublishOptions { MsgId = "m-1" }, out var second).ShouldBeTrue();
        second.Seq.ShouldBe(first.Seq);

        await Task.Delay(40);

        publisher.TryCaptureWithOptions("d.1", "after-window"u8.ToArray(), new PublishOptions { MsgId = "m-1" }, out var third).ShouldBeTrue();
        third.ErrorCode.ShouldBeNull();
        third.Seq.ShouldBeGreaterThan(first.Seq);
    }
}
