using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
///
/// Gitea #610 — pins 's Galaxy row against the REAL write encoder
/// (). The OPC UA server hands the driver the CLR array type of the node's
/// DataType (the SDK refuses a mistyped array before the write handler runs), so for every
/// Tag.DataType the server can materialise, the node is advertised writable exactly when the encoder
/// accepts that array type.
///
[Trait("Category", "Unit")]
public sealed class GalaxyArrayWriteSupportParityTests
{
// Tag.DataType name → the CLR array the OPC UA stack delivers for a 1-D write of that type.
private static readonly Dictionary ClrArrays = new()
{
["Boolean"] = new[] { true, false },
["SByte"] = new sbyte[] { 1, 2 },
["Byte"] = new byte[] { 1, 2 },
["Int16"] = new short[] { 1, 2 },
["UInt16"] = new ushort[] { 1, 2 },
["Int32"] = new[] { 1, 2 },
["UInt32"] = new uint[] { 1, 2 },
["Int64"] = new long[] { 1, 2 },
["UInt64"] = new ulong[] { 1, 2 },
["Float"] = new[] { 1f, 2f },
["Double"] = new[] { 1d, 2d },
["String"] = new[] { "a", "b" },
["DateTime"] = new[] { DateTime.UtcNow, DateTime.UtcNow },
};
public static TheoryData DataTypes() => new(ClrArrays.Keys);
[Theory]
[MemberData(nameof(DataTypes))]
public void Table_agrees_with_MxValueEncoder(string dataType)
{
bool encoderAccepts;
try
{
MxValueEncoder.Encode(ClrArrays[dataType]);
encoderAccepts = true;
}
catch (ArgumentException)
{
encoderAccepts = false;
}
// UInt32 / UInt64 are a deliberate exclusion: CLR array covariance lets uint[] / ulong[] match the encoder's
// int[] / long[] arms, so it "accepts" them by REINTERPRETING the bits (uint.MaxValue → -1) — unlike the
// scalar arms, which range-check. A Galaxy attribute is never unsigned (DataTypeMap), so the table refuses
// rather than advertise a write that silently corrupts large values.
var expected = encoderAccepts && dataType is not ("UInt32" or "UInt64");
if (dataType is "UInt32" or "UInt64") encoderAccepts.ShouldBeTrue("the covariance hazard this exclusion documents");
DriverArrayWriteSupport.Supports(DriverTypeNames.Galaxy, dataType, "{\"FullName\":\"Obj.Attr\"}")
.ShouldBe(expected, dataType);
}
}