using NATS.Server.Protocol;

namespace NATS.Server.Tests;

public class NatsHeaderParserTests
{
    [Fact]
    public void Parse_status_line_only()
    {
        var input = "NATS/1.0 503\r\n\r\n"u8;
        var result = NatsHeaderParser.Parse(input);
        result.Status.ShouldBe(503);
        result.Description.ShouldBeEmpty();
        result.Headers.ShouldBeEmpty();
    }

    [Fact]
    public void Parse_status_with_description()
    {
        var input = "NATS/1.0 503 No Responders\r\n\r\n"u8;
        var result = NatsHeaderParser.Parse(input);
        result.Status.ShouldBe(503);
        result.Description.ShouldBe("No Responders");
    }

    [Fact]
    public void Parse_headers_with_values()
    {
        var input = "NATS/1.0\r\nFoo: bar\r\nBaz: qux\r\n\r\n"u8;
        var result = NatsHeaderParser.Parse(input);
        result.Status.ShouldBe(0);
        result.Headers["Foo"].ShouldBe(["bar"]);
        result.Headers["Baz"].ShouldBe(["qux"]);
    }

    [Fact]
    public void Parse_multi_value_header()
    {
        var input = "NATS/1.0\r\nX-Tag: a\r\nX-Tag: b\r\n\r\n"u8;
        var result = NatsHeaderParser.Parse(input);
        result.Headers["X-Tag"].ShouldBe(["a", "b"]);
    }

    [Fact]
    public void Parse_invalid_returns_defaults()
    {
        var input = "GARBAGE\r\n\r\n"u8;
        var result = NatsHeaderParser.Parse(input);
        result.Status.ShouldBe(-1);
    }
}
