using System.Net;
using System.Net.Sockets;
using NATS.Server.Mqtt;

namespace NATS.Server.Tests.Mqtt;

public class MqttQosAckRuntimeTests
{
    [Fact]
    public async Task Qos1_publish_receives_puback_and_redelivery_on_session_reconnect_when_unacked()
    {
        await using var listener = new MqttListener("127.0.0.1", 0);
        using var cts = new CancellationTokenSource();
        await listener.StartAsync(cts.Token);

        using var client = new TcpClient();
        await client.ConnectAsync(IPAddress.Loopback, listener.Port);
        var stream = client.GetStream();

        await MqttRuntimeWire.WriteLineAsync(stream, "CONNECT qos-client clean=false");
        (await MqttRuntimeWire.ReadLineAsync(stream, 1000)).ShouldBe("CONNACK");

        await MqttRuntimeWire.WriteLineAsync(stream, "PUBQ1 7 sensors.temp 42");
        (await MqttRuntimeWire.ReadLineAsync(stream, 1000)).ShouldBe("PUBACK 7");
    }
}
