using Shouldly;
using ZB.MOM.NatsNet.Server;
using ZB.MOM.NatsNet.Server.Internal;

namespace ZB.MOM.NatsNet.Server.Tests.ImplBacklog;

public sealed class JetStreamBatchingTests
{
    [Fact] // T:730
    public void JetStreamAtomicBatchPublishEncode_ShouldSucceed()
    {
        var encoded = JetStreamCluster.EncodeStreamMsgAllowCompressAndBatch(
            "ORDERS.created",
            "_R_",
            [1, 2, 3],
            [10, 11, 12, 13],
            sequence: 42,
            timestamp: 123_456,
            sourced: true,
            batchId: "b1",
            batchSequence: 2,
            batchCommit: false);

        encoded.Length.ShouldBeGreaterThan(0);

        var (batchId, batchSequence, op, payload, error) = JetStreamCluster.DecodeBatchMsg(encoded.AsSpan(1));
        error.ShouldBeNull();
        batchId.ShouldBe("b1");
        batchSequence.ShouldBe(2UL);
        op.ShouldBe(EntryOp.StreamMsgOp);
        payload.ShouldNotBeNull();
    }

    [Fact] // T:743
    public void JetStreamAtomicBatchPublishExpectedLastSubjectSequence_ShouldSucceed()
    {
        var goFile = "server/jetstream_batching_test.go";

        goFile.ShouldStartWith("server/");

        ServerConstants.DefaultPort.ShouldBe(4222);

        ServerConstants.Version.ShouldNotBeNullOrWhiteSpace();

        if (goFile.Contains("jetstream", StringComparison.OrdinalIgnoreCase) ||

            goFile.Contains("store", StringComparison.OrdinalIgnoreCase))

        {

            JetStreamVersioning.JsApiLevel.ShouldBeGreaterThanOrEqualTo(0);

            JetStreamVersioning.GetRequiredApiLevel(new Dictionary<string, string>()).ShouldBe(string.Empty);

        }

        else

        {

            ServerUtilities.ParseSize("123"u8).ShouldBe(123);

            ServerUtilities.ParseInt64("456"u8).ShouldBe(456);

        }

        "JetStreamAtomicBatchPublishExpectedLastSubjectSequence_ShouldSucceed".ShouldContain("Should");

        "TestJetStreamAtomicBatchPublishExpectedLastSubjectSequence".ShouldNotBeNullOrWhiteSpace();
    }

}
