using System.Collections.Concurrent; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests; public sealed class DeferredAddressSpaceSinkTests { /// Verifies that the default inner is a null sink so calls before SetSink are safe. [Fact] public void Default_inner_is_null_sink_so_calls_before_SetSink_are_safe() { var deferred = new DeferredAddressSpaceSink(); // No throw, no observable side effect. deferred.WriteValue("x", 1, OpcUaQuality.Good, DateTime.UtcNow); deferred.WriteAlarmCondition("a", Snapshot(active: true), DateTime.UtcNow); deferred.RebuildAddressSpace(); } /// Verifies that calls after SetSink are forwarded to the inner sink. [Fact] public void Calls_after_SetSink_are_forwarded_to_the_inner() { var deferred = new DeferredAddressSpaceSink(); var inner = new RecordingSink(); deferred.SetSink(inner); deferred.WriteValue("x", 42, OpcUaQuality.Good, DateTime.UtcNow); deferred.WriteAlarmCondition("a-1", Snapshot(active: true), DateTime.UtcNow); deferred.RebuildAddressSpace(); inner.Calls.ShouldBe(new[] { "WV:x", "WA:a-1", "RB" }); } /// Verifies that setting sink to null reverts to null sink. [Fact] public void SetSink_to_null_reverts_to_null_sink() { var deferred = new DeferredAddressSpaceSink(); var inner = new RecordingSink(); deferred.SetSink(inner); deferred.WriteValue("x", 1, OpcUaQuality.Good, DateTime.UtcNow); inner.Calls.Count.ShouldBe(1); deferred.SetSink(null); deferred.WriteValue("y", 2, OpcUaQuality.Good, DateTime.UtcNow); // dropped inner.Calls.Count.ShouldBe(1); } /// Verifies that sink can be swapped between implementations. [Fact] public void SetSink_can_swap_between_implementations() { var deferred = new DeferredAddressSpaceSink(); var first = new RecordingSink(); var second = new RecordingSink(); deferred.SetSink(first); deferred.WriteValue("a", 1, OpcUaQuality.Good, DateTime.UtcNow); deferred.SetSink(second); deferred.WriteValue("b", 2, OpcUaQuality.Good, DateTime.UtcNow); first.Calls.Single().ShouldBe("WV:a"); second.Calls.Single().ShouldBe("WV:b"); } /// Verifies that forwards the /// historianTagname argument to the inner sink unchanged. [Fact] public void EnsureVariable_forwards_historianTagname_to_inner_sink() { var deferred = new DeferredAddressSpaceSink(); var inner = new RecordingSink(); deferred.SetSink(inner); deferred.EnsureVariable("v-1", null, "MyVar", "Float", writable: false, historianTagname: "MyTag.PV"); var call = inner.HistorianCalls.ShouldHaveSingleItem(); call.NodeId.ShouldBe("v-1"); call.HistorianTagname.ShouldBe("MyTag.PV"); } /// F10b regression: the deferred wrapper MUST forward the surgical capability to a /// surgical inner sink — otherwise AddressSpaceApplier (which injects THIS wrapper on every /// driver-role host, not the inner SdkAddressSpaceSink) never sees the capability and the /// in-place tag-attribute optimization is inert in production (it silently falls back to rebuild). [Fact] public void UpdateTagAttributes_forwards_to_a_surgical_inner_sink() { var deferred = new DeferredAddressSpaceSink(); var inner = new SurgicalRecordingSink { Result = true }; deferred.SetSink(inner); ((ISurgicalAddressSpaceSink)deferred).UpdateTagAttributes("v-1", writable: true, historianTagname: "MyTag.PV") .ShouldBeTrue(); var call = inner.SurgicalCalls.ShouldHaveSingleItem(); call.NodeId.ShouldBe("v-1"); call.Writable.ShouldBeTrue(); call.Historian.ShouldBe("MyTag.PV"); } /// The surgical forward returns the inner's own result (false ⇒ node missing) so the caller /// falls back to a full rebuild. [Fact] public void UpdateTagAttributes_returns_inner_result_when_node_missing() { var deferred = new DeferredAddressSpaceSink(); deferred.SetSink(new SurgicalRecordingSink { Result = false }); ((ISurgicalAddressSpaceSink)deferred).UpdateTagAttributes("v-1", writable: false, historianTagname: null) .ShouldBeFalse(); } /// When the inner sink does NOT implement (e.g. the /// null sink before the real one is swapped in, or any non-surgical sink) the wrapper returns false /// so the caller rebuilds. [Fact] public void UpdateTagAttributes_returns_false_when_inner_is_not_surgical() { var deferred = new DeferredAddressSpaceSink(); // default inner = null sink (not surgical) ((ISurgicalAddressSpaceSink)deferred).UpdateTagAttributes("v-1", writable: true, historianTagname: null) .ShouldBeFalse(); deferred.SetSink(new RecordingSink()); // a non-surgical inner ((ISurgicalAddressSpaceSink)deferred).UpdateTagAttributes("v-1", writable: true, historianTagname: null) .ShouldBeFalse(); } /// Builds a minimal for the forwarding tests (the /// inner sink only records the node id, so the exact state values don't matter here). private static AlarmConditionSnapshot Snapshot(bool active = false) => new(active, Acknowledged: true, Confirmed: true, Enabled: true, Shelving: AlarmShelvingKind.Unshelved, Severity: 500, Message: "test"); private sealed class RecordingSink : IOpcUaAddressSpaceSink { /// Gets the queue of recorded calls. public ConcurrentQueue CallQueue { get; } = new(); /// Gets the list of recorded calls. public List Calls => CallQueue.ToList(); /// Gets the queue of (NodeId, HistorianTagname) pairs captured per /// EnsureVariable call, in call order. public ConcurrentQueue<(string NodeId, string? HistorianTagname)> HistorianQueue { get; } = new(); /// Gets the list of (NodeId, HistorianTagname) pairs captured per EnsureVariable call. public List<(string NodeId, string? HistorianTagname)> HistorianCalls => HistorianQueue.ToList(); /// public void WriteValue(string nodeId, object? value, OpcUaQuality quality, DateTime sourceTimestampUtc) => CallQueue.Enqueue($"WV:{nodeId}"); /// public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc) => CallQueue.Enqueue($"WA:{alarmNodeId}"); /// public void MaterialiseAlarmCondition(string alarmNodeId, string equipmentNodeId, string displayName, string alarmType, int severity, bool isNative = false) => CallQueue.Enqueue($"MA:{alarmNodeId}"); /// public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName) => CallQueue.Enqueue($"EF:{folderNodeId}"); /// public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType, bool writable, string? historianTagname = null, bool isArray = false, uint? arrayLength = null) { CallQueue.Enqueue($"EV:{variableNodeId}"); HistorianQueue.Enqueue((variableNodeId, historianTagname)); } /// public void RebuildAddressSpace() => CallQueue.Enqueue("RB"); } private sealed class SurgicalRecordingSink : IOpcUaAddressSpaceSink, ISurgicalAddressSpaceSink { /// Gets or sets the value returns. public bool Result { get; set; } = true; /// Gets the recorded surgical calls. public List<(string NodeId, bool Writable, string? Historian)> SurgicalCalls { get; } = new(); /// public bool UpdateTagAttributes(string variableNodeId, bool writable, string? historianTagname) { SurgicalCalls.Add((variableNodeId, writable, historianTagname)); return Result; } /// public void WriteValue(string nodeId, object? value, OpcUaQuality quality, DateTime sourceTimestampUtc) { } /// public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc) { } /// public void MaterialiseAlarmCondition(string alarmNodeId, string equipmentNodeId, string displayName, string alarmType, int severity, bool isNative = false) { } /// public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName) { } /// public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType, bool writable, string? historianTagname = null, bool isArray = false, uint? arrayLength = null) { } /// public void RebuildAddressSpace() { } } }