using System.Text.Json;
using NATS.Server.Protocol;

namespace NATS.Server.Tests;

public class AuthProtocolTests
{
    [Fact]
    public void ClientOptions_deserializes_auth_fields()
    {
        var json = """{"user":"alice","pass":"secret","auth_token":"mytoken","nkey":"UABC","sig":"base64sig"}""";
        var opts = JsonSerializer.Deserialize<ClientOptions>(json);

        opts.ShouldNotBeNull();
        opts.Username.ShouldBe("alice");
        opts.Password.ShouldBe("secret");
        opts.Token.ShouldBe("mytoken");
        opts.Nkey.ShouldBe("UABC");
        opts.Sig.ShouldBe("base64sig");
    }

    [Fact]
    public void ServerInfo_serializes_auth_required_and_nonce()
    {
        var info = new ServerInfo
        {
            ServerId = "test",
            ServerName = "test",
            Version = "0.1.0",
            Host = "127.0.0.1",
            Port = 4222,
            AuthRequired = true,
            Nonce = "abc123",
        };

        var json = JsonSerializer.Serialize(info);
        json.ShouldContain("\"auth_required\":true");
        json.ShouldContain("\"nonce\":\"abc123\"");
    }

    [Fact]
    public void ServerInfo_omits_nonce_when_null()
    {
        var info = new ServerInfo
        {
            ServerId = "test",
            ServerName = "test",
            Version = "0.1.0",
            Host = "127.0.0.1",
            Port = 4222,
        };

        var json = JsonSerializer.Serialize(info);
        json.ShouldNotContain("nonce");
    }
}
