using Shouldly; using Xunit; namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests; /// /// Gitea #551 item 3: 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. /// /// AbCip's uncovered surface was the declared-UDT members array: the mapper rejects the WHOLE /// tag for one member with no name or a typo'd dataType, but neither this gate nor the /// AdminUI editor (which emits only tagPath/dataType/writable and carries /// members through its preserved key bag) ever looked. /// /// public sealed class AbCipInspectAcceptGateTests { private const string RawPath = "cell/abcip/dev1/T1"; /// Blobs the mapper ACCEPTS — the gate must say nothing about any of them. public static TheoryData Accepted() => [ """{"tagPath":"Program:Main.Speed","dataType":"DInt"}""", """{"tagPath":"Speed"}""", """{"tagPath":"Motor","dataType":"Structure","members":[{"name":"Run","dataType":"Bool"}]}""", // members present but not an ARRAY is ignored by the mapper, so it must be ignored here too. """{"tagPath":"Speed","members":{"name":"Run"}}""", """{"tagPath":"Speed","members":[]}""", // A member's writable / writeIdempotent / array keys are all optional. """{"tagPath":"Motor","members":[{"name":"Run"}]}""", ]; /// Blobs the mapper REJECTS — the gate must produce at least one warning for each. public static TheoryData Rejected() => [ """{"dataType":"DInt"}""", // no tagPath """{"tagPath":"","dataType":"DInt"}""", // blank tagPath """{"tagPath":123}""", // tagPath not a string """{"tagPath":"Speed","dataType":"DIntt"}""", // typo'd top-level dataType """{"tagPath":"Motor","members":[{"dataType":"Bool"}]}""", // member with no name """{"tagPath":"Motor","members":[{"name":" ","dataType":"Bool"}]}""", // member with a blank name """{"tagPath":"Motor","members":[{"name":"Run","dataType":"Booll"}]}""", // typo'd member dataType """{"tagPath":"Motor","members":["Run"]}""", // member not an object ]; [Theory] [MemberData(nameof(Accepted))] public void A_config_the_mapper_accepts_draws_no_warning(string tagConfig) { AbCipTagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeTrue("fixture must be accepted"); AbCipTagDefinitionFactory.Inspect(tagConfig).ShouldBeEmpty(); } [Theory] [MemberData(nameof(Rejected))] public void A_config_the_mapper_rejects_is_never_passed_over_in_silence(string tagConfig) { AbCipTagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeFalse("fixture must be rejected"); AbCipTagDefinitionFactory.Inspect(tagConfig).ShouldNotBeEmpty(); } [Fact] public void An_absent_tagPath_is_named() => AbCipTagDefinitionFactory.Inspect("""{"dataType":"DInt"}""") .ShouldContain(w => w.Contains("'tagPath'")); [Fact] public void A_typo_in_a_member_dataType_is_named_with_its_index() => AbCipTagDefinitionFactory.Inspect( """{"tagPath":"Motor","members":[{"name":"Run","dataType":"Bool"},{"name":"Speed","dataType":"Reall"}]}""") .ShouldContain(w => w.Contains("members[1]") && w.Contains("Reall")); [Fact] public void A_member_with_no_name_is_named_with_its_index() => AbCipTagDefinitionFactory.Inspect("""{"tagPath":"Motor","members":[{"dataType":"Bool"}]}""") .ShouldContain(w => w.Contains("members[0]") && w.Contains("name")); // ---- DescribeInvalidMember is the shared authority the AdminUI model asks (one rule, two doors) ---- [Theory] [InlineData("""{"tagPath":"Motor","members":[{"name":"Run","dataType":"Bool"}]}""", false)] [InlineData("""{"tagPath":"Motor"}""", false)] [InlineData("""{"tagPath":"Motor","members":[]}""", false)] [InlineData("""{"tagPath":"Motor","members":{"name":"Run"}}""", false)] [InlineData("not json at all", false)] [InlineData("""{"tagPath":"Motor","members":[{"name":"Run","dataType":"Booll"}]}""", true)] [InlineData("""{"tagPath":"Motor","members":[{"dataType":"Bool"}]}""", true)] public void DescribeInvalidMember_over_a_string_matches_the_mapper(string tagConfig, bool expectWarning) => (AbCipTagDefinitionFactory.DescribeInvalidMember(tagConfig) is not null).ShouldBe(expectWarning); [Fact] public void A_blank_reference_draws_nothing() => AbCipTagDefinitionFactory.Inspect(" ").ShouldBeEmpty(); [Fact] public void Unparseable_json_names_the_json() => AbCipTagDefinitionFactory.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; AbCipTagDefinitionFactory.FromTagConfig(padded, RawPath, out _) .ShouldBeTrue("the mapper's JsonDocument.Parse skips leading whitespace"); AbCipTagDefinitionFactory.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; AbCipTagDefinitionFactory.FromTagConfig(padded, RawPath, out _) .ShouldBeFalse("the mapper's verdict is unchanged by padding"); AbCipTagDefinitionFactory.Inspect(padded).ShouldBe(AbCipTagDefinitionFactory.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) { AbCipTagDefinitionFactory.FromTagConfig(reference, RawPath, out _).ShouldBeFalse(); AbCipTagDefinitionFactory.Inspect(reference).ShouldBeEmpty(); } }