using System.Text; using Akka.TestKit.Xunit; using Newtonsoft.Json.Linq; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.Interfaces; using ZB.MOM.WW.OtOpcUa.Commons.Types; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; /// /// H4 review item 7: the fleet-diagnostics reply crosses Akka remoting (FleetDiagnosticsClient asks the node's /// driver host) on the serializer Akka picks for it — round-trip the new members through THAT serializer, and decode an /// old-shape payload (a pre-H4 sender) to show mixed-version tolerance. /// [Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H4")] public sealed class GrpcDataDiagnosticsSerializationTests : TestKit { private static readonly string[] H4ConnectionMembers = ["NegotiatedFeatures", "HistoryVariants", "HistoryAggregateCount", "HistoryReadsGood", "HistoryReadsUncertain", "HistoryReadsBad", "OpenRemoteContinuations"]; private static NodeDiagnosticsSnapshot Snapshot() => new( // Arrays, as production builds them (CaptureDiagnostics .ToArray()s; RecordSelected builds string[]): a C# collection // expression typed IReadOnlyList is a compiler-synthesized type Newtonsoft cannot construct. NodeId.Parse("consumer-1:4053"), null, Array.Empty(), new DateTime(2026, 11, 24, 13, 0, 0, DateTimeKind.Utc), DataConnections: new[] { new DataConnectionDiagnosticsSnapshot("link", 3, "ready", "https://source-2:4057", null, 0, 0, 7, "artifact-7", new DateTime(2026, 11, 24, 13, 0, 0, DateTimeKind.Utc), DroppedUnknownOperations: DataOperations.None, MappingsWithDroppedOperations: 0, NegotiatedFeatures: DataFeatures.History, HistoryVariants: new[] { "raw", "processed", "attime" }, HistoryAggregateCount: 19, HistoryReadsGood: 34, HistoryReadsUncertain: 1, HistoryReadsBad: 3, OpenRemoteContinuations: 2), }, DroppedExportOperations: new Dictionary { ["export-a"] = (DataOperations)(1 << 20) }, OriginHistory: new OriginHistoryDiagnostics(true, 1, 2, 7, 0, 2, NoPeerTrafficYet: true)); private (Akka.Serialization.Serializer Serializer, string Manifest) SerializerFor(object message) { var serializer = Sys.Serialization.FindSerializerFor(message); return (serializer, Akka.Serialization.Serialization.ManifestFor(serializer, message)); } [Fact] public void A_snapshot_with_the_H4_members_round_trips_on_the_cluster_serializer() { var message = Snapshot(); var (serializer, manifest) = SerializerFor(message); var round = (NodeDiagnosticsSnapshot)Sys.Serialization.Deserialize(serializer.ToBinary(message), serializer.Identifier, manifest); round.OriginHistory.ShouldBe(message.OriginHistory); round.DroppedExportOperations!["export-a"].ShouldBe((DataOperations)(1 << 20)); var row = round.DataConnections.ShouldNotBeNull().ShouldHaveSingleItem(); row.NegotiatedFeatures.ShouldBe(DataFeatures.History); row.HistoryVariants.ShouldBe(["raw", "processed", "attime"]); (row.HistoryAggregateCount, row.HistoryReadsGood, row.HistoryReadsUncertain, row.HistoryReadsBad, row.OpenRemoteContinuations) .ShouldBe((19, 34L, 1L, 3L, 2)); } [Fact] public void An_old_shape_payload_without_the_H4_members_decodes_with_them_unavailable() { var message = Snapshot(); var (serializer, manifest) = SerializerFor(message); var json = JObject.Parse(Encoding.UTF8.GetString(serializer.ToBinary(message))); json.Remove("OriginHistory").ShouldBeTrue("the H4 node member is on the wire"); var removed = 0; foreach (var row in json.Descendants().OfType().Where(o => o.ContainsKey("ConnectionId")).ToArray()) foreach (var member in H4ConnectionMembers) if (row.Remove(member)) removed++; removed.ShouldBe(H4ConnectionMembers.Length, "every H4 connection member was on the wire and is now stripped"); var legacy = (NodeDiagnosticsSnapshot)Sys.Serialization.Deserialize(Encoding.UTF8.GetBytes(json.ToString(Newtonsoft.Json.Formatting.None)), serializer.Identifier, manifest); legacy.OriginHistory.ShouldBeNull("a pre-H4 node reports no origin evidence (rendered 'unavailable')"); // An origin record without the later NoPeerTrafficYet member decodes as live evidence, never "no peer traffic yet". var withOrigin = JObject.Parse(Encoding.UTF8.GetString(serializer.ToBinary(message))); ((JObject)withOrigin["OriginHistory"]!).Remove("NoPeerTrafficYet").ShouldBeTrue(); var partial = (NodeDiagnosticsSnapshot)Sys.Serialization.Deserialize( Encoding.UTF8.GetBytes(withOrigin.ToString(Newtonsoft.Json.Formatting.None)), serializer.Identifier, manifest); partial.OriginHistory.ShouldNotBeNull().NoPeerTrafficYet.ShouldBeFalse(); partial.OriginHistory.ReadsGood.ShouldBe(7); var row0 = legacy.DataConnections.ShouldNotBeNull().ShouldHaveSingleItem(); row0.ConnectionId.ShouldBe("link"); row0.NegotiatedFeatures.ShouldBeNull(); row0.HistoryVariants.ShouldBeNull(); row0.HistoryReadsGood.ShouldBeNull(); row0.OpenRemoteContinuations.ShouldBeNull(); row0.DroppedUnknownOperations.ShouldBe(DataOperations.None, "X0 members are untouched"); } }