From 6bb8fa6914ac2c442b6304cd7b6440d14aaf40f7 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 12:15:52 -0500 Subject: [PATCH] =?UTF-8?q?test(grpc-data):=20A2a=20=E2=80=94=20condition?= =?UTF-8?q?=20codecs=20pin=2017/4/9=20with=20their=20real=20types=20(repla?= =?UTF-8?q?ces=20the=20X0=20tripwire)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/plans/evidence/grpc-data-ext/A2.md | 8 +- .../GrpcDataConditionCodecTests.cs | 109 ++++++++++++++++++ .../GrpcDataProtoReservationTests.cs | 14 +-- 3 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataConditionCodecTests.cs diff --git a/docs/plans/evidence/grpc-data-ext/A2.md b/docs/plans/evidence/grpc-data-ext/A2.md index 8b1c0ae58..4ac5ef35a 100644 --- a/docs/plans/evidence/grpc-data-ext/A2.md +++ b/docs/plans/evidence/grpc-data-ext/A2.md @@ -6,9 +6,11 @@ the `AddDataConnectionConditions` migration. Condition METHODS are stage A2b (a ## Progress log (resume from here) -- **Done:** `DataOperationSets.Understood` += `AlarmSubscribe` (X0 tolerance tests updated). -- **In progress:** contracts/proto/mapper; configuration slice (helper agent). -- **Next:** origin streaming in the session; host negotiation; tests; v1 gates; live smoke. +- **Done:** `Understood` += AlarmSubscribe (`a74a1aee`); config slice — migration/validator/AdminUI/F-P-U tests (`ac945563`, + `ee1556ac`, `6272c7cf`, helper agent); contract + codecs (`73142e52`); origin streaming + host negotiation + consumer decode + (`5b03c8a2`); Runtime R tests `GrpcDataConditionStreamTests` 12/12 ×5 (`14ab6be3`). +- **In progress:** Commons codec/reservation tests; Host wire tests (header-less never arm 9) + v1 gates. +- **Next:** manifest key A2; gates; live smoke; docs. ## Decisions diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataConditionCodecTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataConditionCodecTests.cs new file mode 100644 index 000000000..79d65c0a8 --- /dev/null +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataConditionCodecTests.cs @@ -0,0 +1,109 @@ +using Google.Protobuf; +using Google.Protobuf.Reflection; +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Commons.DataConnections; +using ZB.MOM.WW.OtOpcUa.Commons.Protos; +using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; +using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1; + +namespace ZB.MOM.WW.OtOpcUa.Commons.Tests.DataConnections; + +/// A2a condition codecs (plan §4.2, §5.5): the X0-reserved numbers now carry their planned fields. +[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "A2")] +public sealed class GrpcDataConditionCodecTests +{ + private static readonly string Key = new('k', ImportedConditionIds.ConditionKeyLength); + private static readonly DataView Placement = new(new DataAddress("origin", "https://zb.com/otopcua/uns", "string", "Area/Line/Eq/Temp"), + DataRealm.Uns, null, 0, "Area/Line/Eq/Temp"); + + [Fact] + public void The_reserved_numbers_now_carry_their_planned_fields_and_no_condition_rpc_exists_yet() + { + var caps = W.CapabilitiesResponse.Descriptor.FindFieldByNumber(17).ShouldNotBeNull(); + caps.Name.ShouldBe("conditions"); caps.FieldType.ShouldBe(FieldType.Message); caps.MessageType.Name.ShouldBe("ConditionCapabilities"); + var scope = W.SubscriptionItem.Descriptor.FindFieldByNumber(4).ShouldNotBeNull(); + scope.Name.ShouldBe("condition_scope"); scope.MessageType.Name.ShouldBe("ConditionScope"); + var arm = W.SubscriptionFrame.Descriptor.FindFieldByNumber(9).ShouldNotBeNull(); + arm.Name.ShouldBe("condition_states"); arm.MessageType.Name.ShouldBe("ConditionStates"); + arm.RealContainingOneof.ShouldNotBeNull().Name.ShouldBe("frame"); + // §4.2 numbers. + W.ConditionStates.HandleFieldNumber.ShouldBe(1); W.ConditionStates.IsSnapshotFieldNumber.ShouldBe(2); + W.ConditionStates.TotalFieldNumber.ShouldBe(3); W.ConditionStates.ItemsFieldNumber.ShouldBe(4); + W.ConditionStates.SnapshotCompleteFieldNumber.ShouldBe(5); + W.ConditionState.ConditionKeyFieldNumber.ShouldBe(1); W.ConditionState.StateKnownFieldNumber.ShouldBe(4); + W.ConditionState.InfoFieldNumber.ShouldBe(5); W.ConditionState.ActivationFieldNumber.ShouldBe(18); + W.ConditionState.ClientUserIdFieldNumber.ShouldBe(20); + W.ConditionInfo.PlacementsFieldNumber.ShouldBe(4); W.ConditionScope.IncludeInfoFieldNumber.ShouldBe(1); + // A2b adds CallConditionMethod; A2a adds no RPC. + W.DataConnectionReflection.Descriptor.Services.Single().Methods.Count.ShouldBe(7); + Should.NotThrow(DataConnectionProtoContract.Validate); + } + + [Fact] + public void A_condition_frame_round_trips_every_field() + { + var record = new DataConditionState(Key, 42, false, true, + new DataConditionInfo("nsu=http://opcfoundation.org/UA/;i=2915", "Temp", DataConditionSourceKind.Native, [Placement], 0), + true, false, true, true, 2, 700, "hot", 0x40000000u, DateTimeOffset.FromUnixTimeSeconds(1_800_000_000), 11163, + DateTimeOffset.FromUnixTimeSeconds(1_700_000_000), true, new DataActivationMarker("inc", 7), "note", "operator"); + var frame = new DataSubscriptionConditions("s", 3, 9, new DataItemHandle("s", 3, 5, 1), true, null, [record], true); + var wire = DataConditionProtoMapper.EncodeFrame(frame); + wire.FrameCase.ShouldBe(W.SubscriptionFrame.FrameOneofCase.ConditionStates); + var decoded = DataConditionProtoMapper.DecodeFrame(W.SubscriptionFrame.Parser.ParseFrom(wire.ToByteArray())); + decoded.Handle.ShouldBe(frame.Handle); decoded.SnapshotComplete.ShouldBeTrue(); decoded.Total.ShouldBeNull(); + var back = decoded.Items.ShouldHaveSingleItem(); + back.ShouldBe(record with { Info = back.Info, Activation = back.Activation }); + back.Info!.Placements.ShouldHaveSingleItem().ShouldBe(Placement); + back.Activation.ShouldBe(record.Activation); + } + + [Fact] + public void Begin_total_is_present_even_when_zero_and_absent_otherwise() + { + var begin = DataConditionProtoMapper.EncodeFrame(new("s", 1, 1, new("s", 1, 1, 1), true, 0, [], false)); + begin.ConditionStates.HasTotal.ShouldBeTrue(); + DataConditionProtoMapper.DecodeFrame(begin).Total.ShouldBe(0); + var chunk = DataConditionProtoMapper.EncodeFrame(new("s", 1, 2, new("s", 1, 1, 1), true, null, [], false)); + chunk.ConditionStates.HasTotal.ShouldBeFalse(); + } + + [Fact] + public void Malformed_frames_are_refused() + { + var good = DataConditionProtoMapper.EncodeFrame(new("s", 1, 1, new("s", 1, 1, 1), false, null, [], false)); + var foreign = good.Clone(); foreign.ConditionStates.Handle.SubscriptionId = "other"; + Should.Throw(() => DataConditionProtoMapper.DecodeFrame(foreign)); + var badKey = good.Clone(); badKey.ConditionStates.Items.Add(new W.ConditionState { ConditionKey = "short" }); + Should.Throw(() => DataConditionProtoMapper.DecodeFrame(badKey)); + var negative = good.Clone(); negative.ConditionStates.Total = -1; + Should.Throw(() => DataConditionProtoMapper.DecodeFrame(negative)); + } + + [Fact] + public void AlarmSubscribe_without_the_conditions_field_is_no_grant_and_the_field_decodes() + { + var caps = new DataCapabilities(1, 3, new ServingIdentity("n", "i", "Primary", 1, 1, 1), DataReadiness.Ready, + DataOperations.Subscribe | DataOperations.AlarmSubscribe, new HashSet { DataValueKind.Int32 }, + new(16, 16, 65536, 16, 4, 65536, TimeSpan.FromSeconds(5), TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(5)), false, 0, "c", + Conditions: new DataConditionCapabilities(true, 0, 1000)); + var wire = DataProtoMapper.EncodeCapabilities(caps); + var decoded = DataProtoMapper.DecodeCapabilities(wire); + decoded.Conditions.ShouldBe(caps.Conditions); + decoded.Operations.ShouldBe(DataOperations.Subscribe | DataOperations.AlarmSubscribe); + wire.Conditions = null; + DataProtoMapper.DecodeCapabilities(wire).Operations.ShouldBe(DataOperations.Subscribe); + } + + [Fact] + public void A_condition_scope_item_round_trips_and_a_plain_item_stays_v1_bytes() + { + var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, null); + var item = new DataSubscriptionItem("c", Placement, settings, new DataConditionScope(true)); + DataProtoMapper.DecodeSubscriptionItem(DataProtoMapper.EncodeSubscriptionItem(item)).ConditionScope.ShouldBe(item.ConditionScope); + var plain = DataProtoMapper.EncodeSubscriptionItem(item with { ConditionScope = null }); + plain.ConditionScope.ShouldBeNull(); + plain.ToByteArray().ShouldBe(new W.SubscriptionItem { ClientItemId = "c", View = DataProtoMapper.EncodeView(Placement), + Settings = DataProtoMapper.EncodeSettings(settings) }.ToByteArray()); + } +} diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataProtoReservationTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataProtoReservationTests.cs index 3e821b168..1c351cd71 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataProtoReservationTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/DataConnections/GrpcDataProtoReservationTests.cs @@ -82,17 +82,9 @@ public sealed class GrpcDataProtoReservationTests if (arm is not null) arm.RealContainingOneof.ShouldNotBeNull().Name.ShouldBe("frame"); } - // The X0 tripwire ("no field yet") was REPLACED by H2 for the numbers H2 took (16, 14 — pinned with their real types in - // GrpcDataHistoryCodecTests). The A2 numbers must still be unused; A2 replaces this in turn. - [Fact] - public void Numbers_reserved_for_A2_are_still_unused() - { - foreach (var (descriptor, number) in new (MessageDescriptor, int)[] - { - (CapabilitiesResponse.Descriptor, 17), (SubscriptionItem.Descriptor, 4), (SubscriptionFrame.Descriptor, 9), - }) - descriptor.FindFieldByNumber(number).ShouldBeNull($"{descriptor.Name} field {number} must not exist before A2"); - } + // The X0 tripwire ("no field yet") was REPLACED by H2 for 16/14 and by A2a for 17/4/9: those numbers are pinned with + // their real types in GrpcDataHistoryCodecTests and GrpcDataConditionCodecTests. The Reservations theory above still + // checks name/type/oneof for every reserved number. [Fact] public void Header_names_are_pinned_and_contract_validation_includes_the_reservations()