using Akka.Configuration;
using Shouldly;
using Xunit;

namespace ZB.MOM.WW.OtOpcUa.Cluster.Tests;

public sealed class HoconLoaderTests
{
    /// <summary>Verifies that LoadBaseConfig returns a non-empty string.</summary>
    [Fact]
    public void LoadBaseConfig_returns_nonempty_string()
    {
        var hocon = HoconLoader.LoadBaseConfig();
        hocon.ShouldNotBeNullOrWhiteSpace();
    }

    /// <summary>Verifies that base config parses to cluster provider.</summary>
    [Fact]
    public void Base_config_parses_to_cluster_provider()
    {
        var cfg = ConfigurationFactory.ParseString(HoconLoader.LoadBaseConfig());
        cfg.GetString("akka.actor.provider").ShouldBe("cluster");
    }

    /// <summary>Verifies that split-brain resolver is set to keep-oldest.</summary>
    [Fact]
    public void Split_brain_resolver_is_keep_oldest()
    {
        var cfg = ConfigurationFactory.ParseString(HoconLoader.LoadBaseConfig());
        cfg.GetString("akka.cluster.split-brain-resolver.active-strategy").ShouldBe("keep-oldest");
    }

    /// <summary>Verifies that stable-after is configured to 15 seconds.</summary>
    [Fact]
    public void Stable_after_is_15_seconds()
    {
        var cfg = ConfigurationFactory.ParseString(HoconLoader.LoadBaseConfig());
        cfg.GetTimeSpan("akka.cluster.split-brain-resolver.stable-after")
            .ShouldBe(TimeSpan.FromSeconds(15));
    }

    /// <summary>Verifies that failure detector threshold is set to 10.</summary>
    [Fact]
    public void Failure_detector_threshold_is_10()
    {
        var cfg = ConfigurationFactory.ParseString(HoconLoader.LoadBaseConfig());
        cfg.GetDouble("akka.cluster.failure-detector.threshold").ShouldBe(10.0);
    }

    /// <summary>Verifies that OPC UA synchronized dispatcher is configured as pinned.</summary>
    [Fact]
    public void Opcua_synchronized_dispatcher_is_pinned()
    {
        var cfg = ConfigurationFactory.ParseString(HoconLoader.LoadBaseConfig());
        cfg.GetString("opcua-synchronized-dispatcher.type").ShouldBe("PinnedDispatcher");
    }
}
