using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Tests;
///
/// Gitea #551 item 2: is accept-gate-first — it asks
/// for the verdict and only explains a refusal. See
/// ModbusInspectAcceptGateTests's remarks for why both directions (never stricter, never silent)
/// are pinned.
///
/// S7 carries the fleet's one unique rule: the reader REQUIRES the dataType key to be
/// present. A blob with only an address therefore deploys green and reads
/// BadNodeIdUnknown forever — and neither this gate nor the AdminUI model said a word about
/// it until #551.
///
///
public sealed class S7InspectAcceptGateTests
{
private const string RawPath = "cell/s7/dev1/T1";
/// Blobs the mapper ACCEPTS — the gate must say nothing about any of them.
public static TheoryData Accepted() =>
[
"""{"address":"DB1.DBW0","dataType":"Int16"}""",
"""{"address":"M0.0","dataType":"Bool"}""",
"""{"address":"DB1.DBB0","dataType":"String","stringLength":254}""",
// stringLength is irrelevant to a non-String tag, so an out-of-range remnant is ACCEPTED.
"""{"address":"DB1.DBW0","dataType":"Int16","stringLength":9999}""",
// A non-string dataType token satisfies the PRESENCE rule and then reads as absent ⇒ Int16.
"""{"address":"DB1.DBW0","dataType":7}""",
];
/// Blobs the mapper REJECTS — the gate must produce at least one warning for each.
public static TheoryData Rejected() =>
[
"""{"dataType":"Int16"}""", // no address
"""{"address":"","dataType":"Int16"}""", // blank address
"""{"address":123,"dataType":"Int16"}""", // address not a string
"""{"address":"DB1.DBW0"}""", // no dataType key at all (#551)
"""{"address":"DB1.DBW0","dataType":"Intt16"}""", // typo'd dataType
"""{"address":"DB1.DBB0","dataType":"String","stringLength":255}""", // over the 254 S7 limit
"""{"address":"DB1.DBB0","dataType":"String","stringLength":-1}""", // negative length
];
[Theory]
[MemberData(nameof(Accepted))]
public void A_config_the_mapper_accepts_draws_no_warning(string tagConfig)
{
S7TagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeTrue("fixture must be accepted");
S7TagDefinitionFactory.Inspect(tagConfig).ShouldBeEmpty();
}
[Theory]
[MemberData(nameof(Rejected))]
public void A_config_the_mapper_rejects_is_never_passed_over_in_silence(string tagConfig)
{
S7TagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeFalse("fixture must be rejected");
S7TagDefinitionFactory.Inspect(tagConfig).ShouldNotBeEmpty();
}
/// The headline case: present-and-typo'd was reported; ABSENT was not (Gitea #551 item 2).
[Fact]
public void An_absent_dataType_is_named()
=> S7TagDefinitionFactory.Inspect("""{"address":"DB1.DBW0"}""")
.ShouldContain(w => w.Contains("no 'dataType'"));
[Fact]
public void An_absent_address_is_named()
=> S7TagDefinitionFactory.Inspect("""{"dataType":"Int16"}""")
.ShouldContain(w => w.Contains("'address'"));
[Fact]
public void An_out_of_range_string_length_is_named()
=> S7TagDefinitionFactory.Inspect("""{"address":"DB1.DBB0","dataType":"String","stringLength":255}""")
.ShouldContain(w => w.Contains("stringLength"));
// ---- DeclaresDataType is the shared authority the AdminUI model asks (one rule, two doors) ----
[Theory]
[InlineData("""{"dataType":"Int16"}""", true)]
[InlineData("""{"dataType":7}""", true)] // presence is judged on the KEY, not the value
[InlineData("""{"dataType":null}""", true)]
[InlineData("""{"address":"DB1.DBW0"}""", false)]
public void DeclaresDataType_judges_the_key_not_the_value(string tagConfig, bool expected)
{
using var doc = System.Text.Json.JsonDocument.Parse(tagConfig);
S7TagDefinitionFactory.DeclaresDataType(doc.RootElement).ShouldBe(expected);
}
[Fact]
public void A_blank_reference_draws_nothing()
=> S7TagDefinitionFactory.Inspect(" ").ShouldBeEmpty();
[Fact]
public void Unparseable_json_names_the_json()
=> S7TagDefinitionFactory.Inspect("{oops").ShouldContain(w => w.Contains("not valid JSON"));
}