using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
///
/// Gitea #551 items 2/3: is accept-gate-first —
/// it asks for the verdict and only explains a
/// refusal.
///
/// Two properties, and they pull in opposite directions, which is why both are pinned here. The
/// gate must never be stricter than the mapper (a warning can be promoted to a blocking
/// deploy error by TagConfigValidationMode.Error, so a warning on a blob the driver serves
/// would refuse a working config); and it must never be silent on a blob the mapper refuses
/// (that tag deploys green and then reads BadNodeIdUnknown forever). The previous hand-list
/// of enum checks satisfied the first and failed the second for every cause not on the list —
/// an absent region, a bad address, stringByteOrder, and the two
/// type-dependent guards.
///
///
public sealed class ModbusInspectAcceptGateTests
{
private const string RawPath = "cell/modbus/dev1/T1";
private const string Clean = """{"region":"HoldingRegisters","address":1,"dataType":"Int16"}""";
/// Every blob below is one the mapper ACCEPTS, so the gate must say nothing about it.
public static TheoryData Accepted() =>
[
Clean,
"""{"region":"Coils","address":0,"dataType":"Bool"}""",
"""{"region":"HoldingRegisters","address":65535,"dataType":"String","stringLength":10,"stringByteOrder":"LowByteFirst"}""",
"""{"region":"HoldingRegisters","address":1,"dataType":"BitInRegister","bitIndex":15}""",
// The byte/ushort NARROWING is part of the mapper's rule: bitIndex 256 truncates to bit 0 and is
// ACCEPTED. A gate that described the un-narrowed value would refuse a tag the driver serves.
"""{"region":"HoldingRegisters","address":1,"dataType":"BitInRegister","bitIndex":256}""",
// A non-string enum token reads as ABSENT, not invalid — the mapper accepts it as the default.
"""{"region":"HoldingRegisters","address":1,"dataType":3}""",
];
/// Every blob below is one the mapper REJECTS, so the gate must produce at least one warning.
public static TheoryData Rejected() =>
[
"""{"address":1,"dataType":"Int16"}""", // no region
"""{"region":"HoldingRegisters","dataType":"Int16"}""", // no address
"""{"region":"HoldingRegisters","address":-1,"dataType":"Int16"}""", // address < 0
"""{"region":"HoldingRegisters","address":65536,"dataType":"Int16"}""", // address > ushort
"""{"region":"HoldingRegisters","address":"1","dataType":"Int16"}""", // address not a number
"""{"region":"HoldingRegisterz","address":1,"dataType":"Int16"}""", // typo'd region
"""{"region":"HoldingRegisters","address":1,"dataType":"Intt16"}""", // typo'd dataType
"""{"region":"HoldingRegisters","address":1,"dataType":"Int16","byteOrder":"Bigg"}""", // typo'd byteOrder
"""{"region":"HoldingRegisters","address":1,"dataType":"Int16","stringByteOrder":"Nope"}""", // typo'd stringByteOrder
"""{"region":"HoldingRegisters","address":1,"dataType":"String"}""", // String with no length
"""{"region":"HoldingRegisters","address":1,"dataType":"BitInRegister","bitIndex":16}""", // bit out of range
];
[Theory]
[MemberData(nameof(Accepted))]
public void A_config_the_mapper_accepts_draws_no_warning(string tagConfig)
{
ModbusTagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeTrue("fixture must be accepted");
ModbusTagDefinitionFactory.Inspect(tagConfig).ShouldBeEmpty();
}
[Theory]
[MemberData(nameof(Rejected))]
public void A_config_the_mapper_rejects_is_never_passed_over_in_silence(string tagConfig)
{
ModbusTagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeFalse("fixture must be rejected");
ModbusTagDefinitionFactory.Inspect(tagConfig).ShouldNotBeEmpty();
}
// ---- The specific causes the issue named, each with a message that points at the right key ----
[Fact]
public void An_absent_region_is_named()
=> ModbusTagDefinitionFactory.Inspect("""{"address":1,"dataType":"Int16"}""")
.ShouldContain(w => w.Contains("'region'"));
[Fact]
public void An_out_of_range_address_is_named()
=> ModbusTagDefinitionFactory.Inspect("""{"region":"Coils","address":70000,"dataType":"Int16"}""")
.ShouldContain(w => w.Contains("'address'"));
/// Gitea #551 item 3: read strictly by the mapper since R2-11, checked by nothing until now.
[Fact]
public void A_typo_in_stringByteOrder_is_named()
=> ModbusTagDefinitionFactory.Inspect(
"""{"region":"HoldingRegisters","address":1,"dataType":"Int16","stringByteOrder":"HighByteFrist"}""")
.ShouldContain(w => w.Contains("stringByteOrder") && w.Contains("HighByteFrist"));
[Fact]
public void A_String_tag_with_no_stringLength_is_named()
=> ModbusTagDefinitionFactory.Inspect("""{"region":"HoldingRegisters","address":1,"dataType":"String"}""")
.ShouldContain(w => w.Contains("stringLength"));
[Fact]
public void A_BitInRegister_tag_with_a_bit_over_15_is_named()
=> ModbusTagDefinitionFactory.Inspect(
"""{"region":"HoldingRegisters","address":1,"dataType":"BitInRegister","bitIndex":16}""")
.ShouldContain(w => w.Contains("bitIndex"));
// ---- Framing rules preserved from the pre-#551 shape ----
[Fact]
public void A_blank_reference_draws_nothing()
=> ModbusTagDefinitionFactory.Inspect(" ").ShouldBeEmpty();
///
/// The pre-existing framing convention, deliberately left alone by #551 and shared by all eleven
/// inspectors: a reference that does not START with { is not an equipment-tag TagConfig at
/// all, so it draws nothing — even though the mapper would refuse it. Widening this is a separate
/// decision about what a TagConfig IS, not about how a refusal is explained.
///
/// The non-object reference.
[Theory]
[InlineData("just a string")]
[InlineData("[1,2,3]")]
[InlineData("42")]
public void A_non_object_reference_draws_nothing_because_it_is_not_a_TagConfig(string reference)
{
ModbusTagDefinitionFactory.FromTagConfig(reference, RawPath, out _).ShouldBeFalse();
ModbusTagDefinitionFactory.Inspect(reference).ShouldBeEmpty();
}
[Fact]
public void Unparseable_json_names_the_json()
=> ModbusTagDefinitionFactory.Inspect("{oops").ShouldContain(w => w.Contains("not valid JSON"));
// ---- Gitea #556 item 1: leading whitespace no longer defeats the framing pre-filter ----
///
/// Padding a blob with leading whitespace. A driver is handed whatever authoring persisted, and
/// FromTagConfig passes it straight to JsonDocument.Parse, which skips leading
/// whitespace — so the MAPPER's verdict is identical padded or not, and the gate's must be too.
///
private const string Pad = " \t\r\n ";
///
/// Never stricter, under padding: a padded blob the mapper still accepts draws nothing. (This
/// direction already held before #556 — the pre-filter returned empty for everything padded — and
/// is kept so a future "just trim everywhere" change cannot start warning about a served tag.)
///
/// An accepted fixture, padded below.
[Theory]
[MemberData(nameof(Accepted))]
public void Leading_whitespace_leaves_an_accepted_config_silent(string tagConfig)
{
var padded = Pad + tagConfig;
ModbusTagDefinitionFactory.FromTagConfig(padded, RawPath, out _)
.ShouldBeTrue("the mapper's JsonDocument.Parse skips leading whitespace");
ModbusTagDefinitionFactory.Inspect(padded).ShouldBeEmpty();
}
///
/// Never silent, under padding — the #556 item 1 defect itself. The pre-filter tested
/// reference[0] != '{', so EVERY padded blob returned empty and a padded config the driver
/// refuses deployed clean. Asserting equality with the untrimmed inspection (rather than merely
/// "not empty") also pins that padding changes no message.
///
/// A rejected fixture, padded below.
[Theory]
[MemberData(nameof(Rejected))]
public void Leading_whitespace_draws_exactly_the_warnings_the_untrimmed_blob_draws(string tagConfig)
{
var padded = Pad + tagConfig;
ModbusTagDefinitionFactory.FromTagConfig(padded, RawPath, out _)
.ShouldBeFalse("the mapper's verdict is unchanged by padding");
ModbusTagDefinitionFactory.Inspect(padded).ShouldBe(ModbusTagDefinitionFactory.Inspect(tagConfig));
}
///
/// The other half of the same fix, and the one it would be easy to lose: trimming widens the gate
/// ONLY for JSON objects. A reference that is still not object-framed after the trim is not an
/// equipment-tag TagConfig at all — the pre-existing convention shared by all eleven inspectors —
/// so it keeps drawing nothing even though the mapper refuses it.
///
/// A padded, non-object reference.
[Theory]
[InlineData(" just a string")]
[InlineData(" [1,2,3]")]
[InlineData(" \t\r\n 42")]
[InlineData(" \"a\"")]
[InlineData(" ")]
public void A_non_object_reference_still_draws_nothing_after_trimming(string reference)
{
ModbusTagDefinitionFactory.FromTagConfig(reference, RawPath, out _).ShouldBeFalse();
ModbusTagDefinitionFactory.Inspect(reference).ShouldBeEmpty();
}
}