using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.Types;

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

/// <summary>
///     Pins <see cref="DeviceConfigIntent"/> device-host extraction + normalization semantics (ported
///     from the historical composer statics; R2-11): null on blank/non-object/non-string/whitespace,
///     trim+lower normalization, and idempotence.
/// </summary>
public sealed class DeviceConfigIntentTests
{
    [Theory]
    [InlineData("{\"HostAddress\":\"10.0.0.5:8193\"}", "10.0.0.5:8193")]
    [InlineData("{\"HostAddress\":\"  HOST:8193  \"}", "host:8193")]       // trim + lower
    [InlineData("{\"HostAddress\":\"HOST:8193\",\"other\":1}", "host:8193")]
    // null-yielding cases
    [InlineData(null, null)]
    [InlineData("", null)]
    [InlineData("   ", null)]
    [InlineData("not json {", null)]
    [InlineData("[1,2]", null)]                                            // non-object root
    [InlineData("{\"HostAddress\":123}", null)]                           // non-string
    [InlineData("{\"HostAddress\":\"   \"}", null)]                       // whitespace value
    [InlineData("{\"other\":\"x\"}", null)]                              // absent
    public void TryExtractHost(string? cfg, string? expected)
    {
        DeviceConfigIntent.TryExtractHost(cfg).ShouldBe(expected);
    }

    [Theory]
    [InlineData("  HOST:8193  ", "host:8193")]
    [InlineData("host:8193", "host:8193")]
    public void NormalizeHost_trims_and_lowercases(string raw, string expected)
    {
        DeviceConfigIntent.NormalizeHost(raw).ShouldBe(expected);
    }

    [Fact]
    public void NormalizeHost_is_idempotent()
    {
        var once = DeviceConfigIntent.NormalizeHost("  HOST:8193 ");
        DeviceConfigIntent.NormalizeHost(once).ShouldBe(once);
    }
}
