using Mbproxy.Diagnostics;
using Shouldly;
using Xunit;

namespace Mbproxy.Tests.Diagnostics;

/// <summary>
/// Unit tests for <see cref="EventLogMessage.TruncateToLimit"/> — the 32 KB Windows
/// Event Log truncation rule. The helper is pure and OS-agnostic, so these run on
/// every platform (the Windows-only <see cref="EventLogBridge"/> sink itself is not
/// exercised here).
/// </summary>
[Trait("Category", "Unit")]
public sealed class EventLogMessageTests
{
    [Fact]
    public void TruncateToLimit_ShortMessage_ReturnedUnchanged()
    {
        const string msg = "mbproxy backend connect failed";
        EventLogMessage.TruncateToLimit(msg).ShouldBeSameAs(msg);
    }

    [Fact]
    public void TruncateToLimit_MessageAtTheLimit_NotTruncated()
    {
        // MaxBytes / 2 chars = exactly MaxBytes at the 2-bytes-per-char upper bound.
        var atLimit = new string('y', EventLogMessage.MaxBytes / 2);
        EventLogMessage.TruncateToLimit(atLimit).ShouldBe(atLimit);
    }

    [Fact]
    public void TruncateToLimit_OversizeMessage_TruncatedWithinLimit_AndEndsWithEllipsis()
    {
        var huge = new string('x', EventLogMessage.MaxBytes); // well over the limit
        var result = EventLogMessage.TruncateToLimit(huge);

        (result.Length * 2).ShouldBeLessThanOrEqualTo(EventLogMessage.MaxBytes);
        result.ShouldEndWith("...");
        result.Length.ShouldBeLessThan(huge.Length);
    }
}
