using libplctag;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests;
[Trait("Category", "Unit")]
public sealed class AbCipStatusMapperTests
{
/// Verifies that CIP general status codes are mapped to OPC UA status codes.
/// The raw CIP general status byte.
/// The expected OPC UA status code.
[Theory]
[InlineData((byte)0x00, AbCipStatusMapper.Good)]
[InlineData((byte)0x04, AbCipStatusMapper.BadNodeIdUnknown)]
[InlineData((byte)0x05, AbCipStatusMapper.BadNodeIdUnknown)]
[InlineData((byte)0x06, AbCipStatusMapper.GoodMoreData)]
[InlineData((byte)0x08, AbCipStatusMapper.BadNotSupported)]
[InlineData((byte)0x0A, AbCipStatusMapper.BadOutOfRange)]
[InlineData((byte)0x13, AbCipStatusMapper.BadOutOfRange)]
[InlineData((byte)0x0B, AbCipStatusMapper.Good)]
[InlineData((byte)0x0E, AbCipStatusMapper.BadNotWritable)]
[InlineData((byte)0x10, AbCipStatusMapper.BadDeviceFailure)]
[InlineData((byte)0x16, AbCipStatusMapper.BadNodeIdUnknown)]
[InlineData((byte)0xFF, AbCipStatusMapper.BadInternalError)]
public void MapCipGeneralStatus_maps_known_codes(byte status, uint expected)
{
AbCipStatusMapper.MapCipGeneralStatus(status).ShouldBe(expected);
}
// Driver.AbCip-002 — the integers here are the underlying values of the libplctag.NET
// Status enum (what (int)Tag.GetStatus() actually returns), NOT raw native PLCTAG_ERR_*
// constants. The libplctag.NET wrapper renumbers the native codes into a contiguous enum.
/// Verifies that libplctag Status enum values are mapped to OPC UA status codes.
/// The libplctag Status enum value to map.
/// The expected OPC UA status code.
[Theory]
[InlineData(Status.Ok, AbCipStatusMapper.Good)]
[InlineData(Status.Pending, AbCipStatusMapper.GoodMoreData)]
[InlineData(Status.ErrorTimeout, AbCipStatusMapper.BadTimeout)]
[InlineData(Status.ErrorNotFound, AbCipStatusMapper.BadNodeIdUnknown)]
[InlineData(Status.ErrorNotAllowed, AbCipStatusMapper.BadNotWritable)]
[InlineData(Status.ErrorOutOfBounds, AbCipStatusMapper.BadOutOfRange)]
[InlineData(Status.ErrorBadConnection, AbCipStatusMapper.BadCommunicationError)]
[InlineData(Status.ErrorBadGateway, AbCipStatusMapper.BadCommunicationError)]
[InlineData(Status.ErrorUnsupported, AbCipStatusMapper.BadNotSupported)]
[InlineData(Status.ErrorNoMem, AbCipStatusMapper.BadCommunicationError)] // unmapped → generic comms
public void MapLibplctagStatus_maps_real_enum_members(Status status, uint expected)
{
AbCipStatusMapper.MapLibplctagStatus(status).ShouldBe(expected);
// The int overload must agree — it is the seam IAbCipTagRuntime.GetStatus() drives.
AbCipStatusMapper.MapLibplctagStatus((int)status).ShouldBe(expected);
}
/// Verifies that timeout is distinguished from generic communication error.
[Fact]
public void MapLibplctagStatus_distinguishes_timeout_from_generic_comms_error()
{
// Regression for Driver.AbCip-002: a real timeout used to fall through to
// BadCommunicationError because the code matched the wrong integer (-5).
AbCipStatusMapper.MapLibplctagStatus((int)Status.ErrorTimeout)
.ShouldBe(AbCipStatusMapper.BadTimeout);
AbCipStatusMapper.MapLibplctagStatus((int)Status.ErrorNotFound)
.ShouldBe(AbCipStatusMapper.BadNodeIdUnknown);
AbCipStatusMapper.MapLibplctagStatus((int)Status.ErrorNotFound)
.ShouldNotBe(AbCipStatusMapper.BadCommunicationError);
}
}