using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests;
///
/// Gitea #551: 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.
///
public sealed class AbLegacyInspectAcceptGateTests
{
private const string RawPath = "cell/ablegacy/dev1/T1";
/// Blobs the mapper ACCEPTS — the gate must say nothing about any of them.
public static TheoryData Accepted() =>
[
"""{"address":"N7:0","dataType":"Int"}""",
"""{"address":"B3:1/2"}""",
"""{"address":"F8:5","dataType":"Float","isArray":true,"arrayLength":10}""",
// An over-long array is CLAMPED by the mapper, not rejected — so the gate must stay silent, even
// though the AdminUI model refuses it (the un-clamped length also sets OPC UA ArrayDimensions).
"""{"address":"F8:5","dataType":"Float","isArray":true,"arrayLength":9999}""",
];
/// Blobs the mapper REJECTS — the gate must produce at least one warning for each.
public static TheoryData Rejected() =>
[
"""{"dataType":"Int"}""", // no address
"""{"address":"","dataType":"Int"}""", // blank address
"""{"address":7,"dataType":"Int"}""", // address not a string
"""{"address":"N7:0","dataType":"Intt"}""", // typo'd dataType
];
[Theory]
[MemberData(nameof(Accepted))]
public void A_config_the_mapper_accepts_draws_no_warning(string tagConfig)
{
AbLegacyTagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeTrue("fixture must be accepted");
AbLegacyTagDefinitionFactory.Inspect(tagConfig).ShouldBeEmpty();
}
[Theory]
[MemberData(nameof(Rejected))]
public void A_config_the_mapper_rejects_is_never_passed_over_in_silence(string tagConfig)
{
AbLegacyTagDefinitionFactory.FromTagConfig(tagConfig, RawPath, out _).ShouldBeFalse("fixture must be rejected");
AbLegacyTagDefinitionFactory.Inspect(tagConfig).ShouldNotBeEmpty();
}
[Fact]
public void An_absent_address_is_named()
=> AbLegacyTagDefinitionFactory.Inspect("""{"dataType":"Int"}""")
.ShouldContain(w => w.Contains("'address'"));
[Fact]
public void A_blank_reference_draws_nothing()
=> AbLegacyTagDefinitionFactory.Inspect(" ").ShouldBeEmpty();
[Fact]
public void Unparseable_json_names_the_json()
=> AbLegacyTagDefinitionFactory.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;
AbLegacyTagDefinitionFactory.FromTagConfig(padded, RawPath, out _)
.ShouldBeTrue("the mapper's JsonDocument.Parse skips leading whitespace");
AbLegacyTagDefinitionFactory.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;
AbLegacyTagDefinitionFactory.FromTagConfig(padded, RawPath, out _)
.ShouldBeFalse("the mapper's verdict is unchanged by padding");
AbLegacyTagDefinitionFactory.Inspect(padded).ShouldBe(AbLegacyTagDefinitionFactory.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)
{
AbLegacyTagDefinitionFactory.FromTagConfig(reference, RawPath, out _).ShouldBeFalse();
AbLegacyTagDefinitionFactory.Inspect(reference).ShouldBeEmpty();
}
}