namespace NATS.Server.Core.Tests;

// Go reference: server/client.go (write timeout handling, per-kind policies)

public class WriteTimeoutTests
{
    [Fact]
    public void WriteTimeoutPolicy_defaults_by_kind()
    {
        // Go reference: server/client.go — CLIENT closes, others use TCP flush
        NatsClient.GetWriteTimeoutPolicy(ClientKind.Client).ShouldBe(NatsClient.WriteTimeoutPolicy.Close);
        NatsClient.GetWriteTimeoutPolicy(ClientKind.Router).ShouldBe(NatsClient.WriteTimeoutPolicy.TcpFlush);
        NatsClient.GetWriteTimeoutPolicy(ClientKind.Gateway).ShouldBe(NatsClient.WriteTimeoutPolicy.TcpFlush);
        NatsClient.GetWriteTimeoutPolicy(ClientKind.Leaf).ShouldBe(NatsClient.WriteTimeoutPolicy.TcpFlush);
    }

    [Fact]
    public void PartialFlushResult_tracks_bytes()
    {
        // Go reference: server/client.go — partial write tracking
        var result = new NatsClient.FlushResult(BytesAttempted: 1024, BytesWritten: 512);
        result.IsPartial.ShouldBeTrue();
        result.BytesRemaining.ShouldBe(512L);
    }

    [Fact]
    public void PartialFlushResult_complete_is_not_partial()
    {
        // Go reference: server/client.go — complete write
        var result = new NatsClient.FlushResult(BytesAttempted: 1024, BytesWritten: 1024);
        result.IsPartial.ShouldBeFalse();
        result.BytesRemaining.ShouldBe(0L);
    }

    [Fact]
    public void WriteTimeoutPolicy_system_kind_defaults_to_close()
    {
        // Go reference: server/client.go — system/internal kinds default to Close
        NatsClient.GetWriteTimeoutPolicy(ClientKind.System).ShouldBe(NatsClient.WriteTimeoutPolicy.Close);
        NatsClient.GetWriteTimeoutPolicy(ClientKind.JetStream).ShouldBe(NatsClient.WriteTimeoutPolicy.Close);
        NatsClient.GetWriteTimeoutPolicy(ClientKind.Account).ShouldBe(NatsClient.WriteTimeoutPolicy.Close);
    }

    [Fact]
    public void FlushResult_zero_bytes_is_partial_when_attempted_nonzero()
    {
        // Edge case: nothing written but something attempted
        var result = new NatsClient.FlushResult(BytesAttempted: 100, BytesWritten: 0);
        result.IsPartial.ShouldBeTrue();
        result.BytesRemaining.ShouldBe(100L);
    }

    [Fact]
    public void FlushResult_zero_zero_is_not_partial()
    {
        // Edge case: nothing attempted, nothing written
        var result = new NatsClient.FlushResult(BytesAttempted: 0, BytesWritten: 0);
        result.IsPartial.ShouldBeFalse();
        result.BytesRemaining.ShouldBe(0L);
    }
}
