using Shouldly;
using Xunit;

namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;

/// <summary>
///     Confirms the driver's default transport-factory closure — and, by extension, the
///     probe's transport construction — routes through <see cref="ModbusTransportFactory"/>
///     instead of hardcoding <see cref="ModbusTcpTransport"/>, so an <c>RtuOverTcp</c>-authored
///     config actually gets RTU framing rather than silently running as TCP/MBAP.
/// </summary>
[Trait("Category", "Unit")]
public sealed class ModbusTransportWiringTests
{
    private static IModbusTransport BuildDefaultTransport(ModbusDriverOptions opts)
    {
        using var driver = new ModbusDriver(opts, "wire-test");
        var factory = (Func<ModbusDriverOptions, IModbusTransport>)typeof(ModbusDriver)
            .GetField("_transportFactory", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!
            .GetValue(driver)!;
        return factory(opts);
    }

    [Fact]
    public void Default_closure_builds_rtu_transport_for_RtuOverTcp()
        => BuildDefaultTransport(new ModbusDriverOptions { Transport = ModbusTransportMode.RtuOverTcp })
            .ShouldBeOfType<ModbusRtuOverTcpTransport>();

    [Fact]
    public void Default_closure_builds_tcp_transport_for_Tcp()
        => BuildDefaultTransport(new ModbusDriverOptions())
            .ShouldBeOfType<ModbusTcpTransport>();
}
