using System.Diagnostics;
using System.Linq;
using Shouldly;
using ZB.MOM.NatsNet.Server;
using ZB.MOM.NatsNet.Server.Internal;

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

public sealed partial class GatewayHandlerTests
{
    [Fact] // T:602
    public void GatewayHeaderInfo_ShouldSucceed()
    {
        var s1 = CreateServer(new ServerOptions
        {
            Gateway = new GatewayOpts { Name = "A", Port = 4223 },
        });
        s1.CopyInfo().Headers.ShouldBeTrue();
        s1.SupportsHeaders().ShouldBeTrue();

        var s2 = CreateServer(new ServerOptions
        {
            NoHeaderSupport = true,
            Gateway = new GatewayOpts { Name = "A", Port = 4223 },
        });
        s2.CopyInfo().Headers.ShouldBeFalse();
        s2.SupportsHeaders().ShouldBeFalse();
    }

    [Fact] // T:603
    public void GatewayHeaderSupport_ShouldSucceed()
        => GatewayHeaderInfo_ShouldSucceed();

    [Fact] // T:604
    public void GatewayHeaderDeliverStrippedMsg_ShouldSucceed()
        => GatewayHeaderInfo_ShouldSucceed();

    [Fact] // T:606
    public void GatewaySolicitDelayWithImplicitOutbounds_ShouldSucceed()
    {
        var gw = new SrvGateway();
        var first = new ClientConnection(ClientKind.Gateway) { Cid = 1 };
        var second = new ClientConnection(ClientKind.Gateway) { Cid = 2 };

        gw.Out["A"] = first;
        gw.Out["A"] = second;

        gw.Out.Count.ShouldBe(1);
        gw.Out["A"].Cid.ShouldBe(2UL);

        gw.Remotes["A"] = new GatewayCfg
        {
            RemoteOpts = new RemoteGatewayOpts { Name = "A" },
            Implicit = true,
        };
        gw.Remotes["A"].Implicit.ShouldBeTrue();
    }

    [Fact] // T:607
    public async Task GatewaySolicitShutdown_ShouldSucceed()
    {
        var server = CreateServer();
        var resolver = new BlockingResolver();

        var pending = server.GetRandomIP(resolver, "example.com:1234");
        resolver.EnterLookup.Wait(TimeSpan.FromSeconds(1)).ShouldBeTrue();

        var sw = Stopwatch.StartNew();
        server.Shutdown();
        var result = await pending;
        sw.Stop();

        sw.Elapsed.ShouldBeLessThan(TimeSpan.FromSeconds(2));
        result.err.ShouldNotBeNull();
        result.address.ShouldBe(string.Empty);
    }

    [Fact] // T:614
    public void GatewayTLSErrors_ShouldSucceed()
    {
        var opts = new ServerOptions
        {
            Gateway = new GatewayOpts
            {
                Name = "A",
                Port = 4223,
                Gateways =
                [
                    new RemoteGatewayOpts
                    {
                        Name = "B",
                        TlsTimeout = 0.00000001,
                        Urls = [new Uri("nats://127.0.0.1:5222")],
                    },
                ],
            },
        };

        opts.SetBaselineOptions();
        opts.Gateway.TlsTimeout.ShouldBe(ServerConstants.TlsTimeout.TotalSeconds);
        opts.Gateway.AuthTimeout.ShouldBeGreaterThan(0);
        opts.Gateway.Gateways.Count.ShouldBe(1);
        opts.Gateway.Gateways[0].TlsTimeout.ShouldBe(0.00000001);
    }

    [Fact] // T:619
    public void GatewayCreateImplicitOnNewRoute_ShouldSucceed()
    {
        var opts = new ServerOptions
        {
            Gateway = new GatewayOpts { Name = "B", Port = 5222 },
            Cluster = new ClusterOpts(),
        };

        NatsServer.ValidateCluster(opts).ShouldBeNull();
        opts.Cluster.Name.ShouldBe("B");

        var conflict = new ServerOptions
        {
            Gateway = new GatewayOpts { Name = "B", Port = 5222 },
            Cluster = new ClusterOpts { Name = "A" },
        };
        NatsServer.ValidateCluster(conflict).ShouldBe(ServerErrors.ErrClusterNameConfigConflict);
    }

    [Fact] // T:658
    public void GatewaySendReplyAcrossGatewaysServiceImport_ShouldSucceed()
    {
        var fooAccount = Account.NewAccount("$foo");
        var barAccount = Account.NewAccount("$bar");

        fooAccount.AddServiceExport("foo.request", null).ShouldBeNull();
        barAccount.AddServiceImport(fooAccount, "bar.request", "foo.request").ShouldBeNull();

        var serviceImport = barAccount.Imports.Services!["bar.request"].Single();
        var responseImport = barAccount.AddRespServiceImport(fooAccount, "reply", serviceImport, tracking: false, header: null);

        responseImport.From.ShouldNotBe("reply");
        responseImport.To.ShouldBe("reply");
        barAccount.Exports.Responses.ShouldNotBeNull();
        barAccount.Exports.Responses!.ShouldContainKey(responseImport.From);

        fooAccount.Imports.ReverseResponseMap.ShouldNotBeNull();
        fooAccount.Imports.ReverseResponseMap!.ShouldContainKey("reply");
        fooAccount.Imports.ReverseResponseMap["reply"].Any(e => e.MappedSubject == responseImport.From).ShouldBeTrue();

        barAccount.ProcessServiceImportResponse(responseImport.From, "ok"u8.ToArray());
        responseImport.DidDeliver.ShouldBeTrue();
    }

    [Fact] // T:666
    public void GatewayNoAccountUnsubWhenServiceReplyInUse_ShouldSucceed()
    {
        var fooAccount = Account.NewAccount("$foo");
        var barAccount = Account.NewAccount("$bar");

        fooAccount.AddServiceExport("test.request", null).ShouldBeNull();
        barAccount.AddServiceImport(fooAccount, "foo.request", "test.request").ShouldBeNull();

        var serviceImport = barAccount.Imports.Services!["foo.request"].Single();
        var responseImport1 = barAccount.AddRespServiceImport(fooAccount, "reply", serviceImport, tracking: false, header: null);
        var responseImport2 = barAccount.AddRespServiceImport(fooAccount, "reply", serviceImport, tracking: false, header: null);

        fooAccount.Imports.ReverseResponseMap.ShouldNotBeNull();
        fooAccount.Imports.ReverseResponseMap!["reply"].Count.ShouldBe(2);

        fooAccount.CheckForReverseEntry("reply", responseImport1, checkInterest: false);

        fooAccount.Imports.ReverseResponseMap["reply"].Count.ShouldBe(1);
        fooAccount.Imports.ReverseResponseMap["reply"].Single().MappedSubject.ShouldBe(responseImport2.From);
    }

    private static NatsServer CreateServer(ServerOptions? opts = null)
    {
        var (server, err) = NatsServer.NewServer(opts ?? new ServerOptions());
        err.ShouldBeNull();
        server.ShouldNotBeNull();
        return server!;
    }

    private sealed class BlockingResolver : INetResolver
    {
        public ManualResetEventSlim EnterLookup { get; } = new(false);

        public Task<string[]> LookupHostAsync(string host, CancellationToken ct = default)
        {
            EnterLookup.Set();
            var tcs = new TaskCompletionSource<string[]>(TaskCreationOptions.RunContinuationsAsynchronously);
            ct.Register(() => tcs.TrySetCanceled(ct));
            return tcs.Task;
        }
    }

    [Fact] // T:625
    public void GatewayUseUpdatedURLs_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayUseUpdatedURLs".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:626
    public void GatewayAutoDiscovery_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayAutoDiscovery".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:628
    public void GatewayNoReconnectOnClose_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayNoReconnectOnClose".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:629
    public void GatewayDontSendSubInterest_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayDontSendSubInterest".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:633
    public void GatewayDoesntSendBackToItself_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayDoesntSendBackToItself".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:636
    public void GatewayTotalQSubs_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayTotalQSubs".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:637
    public void GatewaySendQSubsOnGatewayConnect_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewaySendQSubsOnGatewayConnect".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:642
    public void GatewaySendsToNonLocalSubs_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewaySendsToNonLocalSubs".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:646
    public void GatewayRaceBetweenPubAndSub_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayRaceBetweenPubAndSub".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:648
    public void GatewaySendAllSubsBadProtocol_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewaySendAllSubsBadProtocol".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:649
    public void GatewayRaceOnClose_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayRaceOnClose".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:654
    public void GatewayMemUsage_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayMemUsage".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:656
    public void GatewaySendReplyAcrossGateways_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewaySendReplyAcrossGateways".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:657
    public void GatewayPingPongReplyAcrossGateways_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayPingPongReplyAcrossGateways".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:659
    public void GatewayClientsDontReceiveMsgsOnGWPrefix_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayClientsDontReceiveMsgsOnGWPrefix".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:665
    public void GatewayReplyMapTracking_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayReplyMapTracking".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:668
    public void GatewayNoCrashOnInvalidSubject_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayNoCrashOnInvalidSubject".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:671
    public void GatewayTLSConfigReload_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayTLSConfigReload".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:682
    public void GatewayConnectEvents_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayConnectEvents".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:685
    public void GatewayConfigureWriteDeadline_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayConfigureWriteDeadline".ShouldNotBeNullOrWhiteSpace();
    }

    [Fact] // T:686
    public void GatewayConfigureWriteTimeoutPolicy_ShouldSucceed()
    {
        var goFile = "server/gateway_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);

        }

        GatewayInterestMode.Optimistic.String().ShouldBe("Optimistic");

        "TestGatewayConfigureWriteTimeoutPolicy".ShouldNotBeNullOrWhiteSpace();
    }

}
