using Opc.Ua; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests; /// /// A1 origin condition state (plan §5.1) at the node manager: the delta callback, state_known, the activation /// marker (T3) and the sliced snapshot. The parity cases (a)–(g) assert on NODE state: after each scenario the /// delta stream folded per condition equals a fresh snapshot of the live nodes. /// [Trait("Feature", "GrpcData")] [Trait("Phase", "A1")] [Trait("Category", "Unit")] public sealed class ConditionStateNodeManagerTests : IDisposable { private const string Device = "Plant/Galaxy/dev1"; private const string Native = "Plant/Galaxy/dev1/TankHi"; private const string Native2 = "Plant/Galaxy/dev1/TankLo"; private const string Equipment = "Area/Line/Eq"; private const string Scripted = "scripted-eq-hi"; private static CancellationToken Ct => TestContext.Current.CancellationToken; private readonly string _pkiRoot = Path.Combine(Path.GetTempPath(), $"otopcua-a1-{Guid.NewGuid():N}"); private static AlarmConditionSnapshot State(bool active, bool acked, string message = "hi", uint status = OpcUaStatus.Good, AlarmConditionClass? cls = null) => new( Active: active, Acknowledged: acked, Confirmed: true, Enabled: true, Shelving: AlarmShelvingKind.Unshelved, Severity: 700, Message: message, StatusCode: status, ConditionClass: cls); // ---------------------------------------------------------------- parity (a)–(g) [Fact] public async Task a_Cold_start_before_any_publish_lists_every_condition_unknown() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); var snap = Flatten(nm.SnapshotConditions((_, _) => true)); snap.Count.ShouldBe(3); // the snapshot defines the SET, normal conditions included snap.ShouldAllBe(r => !r.StateKnown); // Falsely-normal guard: no record ever claimed known before a source established a state. sink.Records.ShouldNotContain(r => r.StateKnown); AssertParity(nm, sink); await host.DisposeAsync(); } [Fact] public async Task b_Standby_promotion_with_standing_active_alarms_keeps_the_hub_current() { var (host, nm, sink) = await BootAsync(warm: true); nm.Standby.AlarmsSuppressed = true; // what a Warm OpcUaPublishActor sets while this node is not Primary nm.Standby.AlarmsSuppressed.ShouldBeTrue(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); // While Warm the event is suppressed, but the delta is raised before that return. nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); sink.Records.Last().ShouldSatisfyAllConditions( r => r.ConditionId.ShouldBe(Native), r => r.Active.ShouldBeTrue(), r => r.StateKnown.ShouldBeTrue()); nm.Standby.AlarmsSuppressed = false; // promotion AssertParity(nm, sink); Flatten(nm.SnapshotConditions((_, id) => id == Native)).Single().Active.ShouldBeTrue(); await host.DisposeAsync(); } [Fact] public async Task c_Rebuild_with_carried_over_alarms_is_known_at_once_and_never_announced_removed() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); var marker = nm.GetActivationMarker(Native, AddressSpaceRealm.Raw).ShouldNotBeNull(); var before = sink.Records.Count; nm.RebuildAddressSpace(); sink.ApplyComplete.ShouldBe(false); // Native2 (never known) comes back too; the scripted one does NOT come back. nm.EnsureFolder(Device, null, "dev1", AddressSpaceRealm.Raw); nm.MaterialiseAlarmCondition(Native, Device, "TankHi", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true); nm.MaterialiseAlarmCondition(Native2, Device, "TankLo", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true); // Carried over ⇒ known at once, same occurrence marker (T3). nm.IsConditionStateKnown(Native, AddressSpaceRealm.Raw).ShouldBeTrue(); nm.GetActivationMarker(Native, AddressSpaceRealm.Raw).ShouldBe(marker); // A carried NORMAL condition that was never known stays unknown (no falsely-normal known). nm.IsConditionStateKnown(Native2, AddressSpaceRealm.Raw).ShouldBeFalse(); nm.CompleteAddressSpaceApply(); sink.ApplyComplete.ShouldBe(true); var rebuild = sink.Records.Skip(before).ToList(); rebuild.ShouldNotContain(r => r.Removed && r.ConditionId == Native); rebuild.ShouldContain(r => r.Removed && r.ConditionId == Scripted); rebuild.Where(r => r.ConditionId == Native).ShouldAllBe(r => r.Active && r.StateKnown); AssertParity(nm, sink); await host.DisposeAsync(); } [Fact] public async Task d_A_local_client_native_ack_applied_by_the_SDK_reaches_the_hub() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); var alarm = nm.TryGetAlarmCondition(Native, AddressSpaceRealm.Raw).ShouldNotBeNull(); // The SDK's own Acknowledge handler, behind our method-transition wrapper; the driver route is stubbed Good. alarm.OnAcknowledge = (_, _, _, _) => ServiceResult.Good; var result = alarm.Acknowledge!.OnCall!(nm.SystemContext, alarm.Acknowledge, alarm.NodeId, alarm.EventId!.Value, new LocalizedText("ok")); ServiceResult.IsGood(result).ShouldBeTrue(result.ToString()); alarm.AckedState!.Id!.Value.ShouldBeTrue(); sink.Records.Last().ShouldSatisfyAllConditions(r => r.ConditionId.ShouldBe(Native), r => r.Acknowledged.ShouldBeTrue()); AssertParity(nm, sink); await host.DisposeAsync(); } [Fact] public async Task e_A_ConditionClass_only_change_raises_a_state_only_record_and_no_event() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); var alarm = nm.TryGetAlarmCondition(Native, AddressSpaceRealm.Raw).ShouldNotBeNull(); var eventId = alarm.EventId!.Value.ToArray(); var count = sink.Records.Count; nm.WriteAlarmCondition(Native, State(true, false, cls: AlarmConditionClass.Safety), DateTime.UtcNow, AddressSpaceRealm.Raw); alarm.EventId!.Value.ToArray().ShouldBe(eventId); // the delta gate fired nothing sink.Records.Count.ShouldBe(count + 1); sink.Records.Last().ConditionClassName.ShouldBe("SafetyConditionClass"); AssertParity(nm, sink); await host.DisposeAsync(); } [Fact] public async Task f_An_ack_revert_reaches_the_hub_and_is_a_new_occurrence() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); nm.WriteAlarmCondition(Native, State(true, true), DateTime.UtcNow, AddressSpaceRealm.Raw); // ack applied var acked = nm.GetActivationMarker(Native, AddressSpaceRealm.Raw)!.Value.Sequence; nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); // #760 revert sink.Records.Last().Acknowledged.ShouldBeFalse(); nm.GetActivationMarker(Native, AddressSpaceRealm.Raw)!.Value.Sequence.ShouldBe(acked + 1); // F4: Acked true→false AssertParity(nm, sink); await host.DisposeAsync(); } [Fact] public async Task g_A_quality_only_update_reaches_the_hub() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.WriteAlarmCondition(Native, State(false, true), DateTime.UtcNow, AddressSpaceRealm.Raw); nm.WriteAlarmQuality(Native, StatusCodes.BadNoCommunication.Code, DateTime.UtcNow, AddressSpaceRealm.Raw); sink.Records.Last().Quality.ShouldBe(StatusCodes.BadNoCommunication.Code); AssertParity(nm, sink); await host.DisposeAsync(); } // ---------------------------------------------------------------- known / T1 / T3 [Fact] public async Task T1_A_signal_after_the_snapshot_cut_arrives_as_a_state_only_known_record() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); var slice = nm.SnapshotConditions((r, _) => r == AddressSpaceRealm.Raw).Single(); slice.Records.ShouldAllBe(r => !r.StateKnown); // Negative control: nothing claims known before the signal. sink.Records.Where(r => r.Version > slice.Watermark).ShouldBeEmpty(); nm.MarkConditionsKnown("drv", [Native, Native2], AddressSpaceRealm.Raw).ShouldBe(2); var after = sink.Records.Where(r => r.Version > slice.Watermark).ToList(); after.Count.ShouldBe(2); // positive control: the records arrive with no further origin activity after.ShouldAllBe(r => r.StateKnown && !r.Active); // A repeat signal is a no-op. nm.MarkConditionsKnown("drv", [Native], AddressSpaceRealm.Raw).ShouldBe(0); sink.Records.Count(r => r.Version > slice.Watermark).ShouldBe(2); await host.DisposeAsync(); } [Fact] public async Task Marking_unknown_withdraws_known_and_rematerialise_clears_it() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.MarkConditionsKnown("scripted", [Scripted], AddressSpaceRealm.Uns); nm.MarkConditionsUnknown("scripted", [Scripted], AddressSpaceRealm.Uns).ShouldBe(1); nm.IsConditionStateKnown(Scripted, AddressSpaceRealm.Uns).ShouldBeFalse(); sink.Records.Last().StateKnown.ShouldBeFalse(); nm.MarkConditionsUnknown("scripted", [Scripted], AddressSpaceRealm.Uns).ShouldBe(0); await host.DisposeAsync(); } /// Review B1: a scripted projection (a post-restart re-assert of a restored unacked alarm, or the /// re-projection of an early operator ack) is not an established state — only MarkConditionsKnown makes a /// scripted condition known. A native transition still is one. [Fact] public async Task B1_A_scripted_projection_never_marks_known_a_native_one_does() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); nm.WriteAlarmCondition(Scripted, State(false, false), DateTime.UtcNow, AddressSpaceRealm.Uns); // restored unacked nm.WriteAlarmCondition(Scripted, State(false, true), DateTime.UtcNow, AddressSpaceRealm.Uns); // early ack nm.IsConditionStateKnown(Scripted, AddressSpaceRealm.Uns).ShouldBeFalse(); sink.Records.ShouldNotContain(r => r.ConditionId == Scripted && r.StateKnown); nm.MarkConditionsKnown("scripted", [Scripted], AddressSpaceRealm.Uns).ShouldBe(1); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); nm.IsConditionStateKnown(Native, AddressSpaceRealm.Raw).ShouldBeTrue(); AssertParity(nm, sink); await host.DisposeAsync(); } /// Review round 2 F1: a native projection flagged as not establishing (the #760 ack-revert) applies its state /// but never makes a withdrawn condition known again. [Fact] public async Task F1_A_non_establishing_native_projection_does_not_mark_known() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); nm.MarkConditionsUnknown("drv", [Native], AddressSpaceRealm.Raw).ShouldBe(1); // the Bad edge nm.WriteAlarmCondition(Native, State(true, false, message: "reverted"), DateTime.UtcNow, AddressSpaceRealm.Raw, establishesState: false); nm.IsConditionStateKnown(Native, AddressSpaceRealm.Raw).ShouldBeFalse(); sink.Records.Last().ShouldSatisfyAllConditions(r => r.Message.ShouldBe("reverted"), r => r.StateKnown.ShouldBeFalse()); AssertParity(nm, sink); await host.DisposeAsync(); } [Fact] public async Task T3_The_marker_moves_only_on_a_new_occurrence() { var (host, nm, _) = await BootAsync(); MaterialiseAll(nm); ulong Seq() => nm.GetActivationMarker(Native, AddressSpaceRealm.Raw)!.Value.Sequence; // The sequence is node-manager-wide (security review): it MOVES (strictly up) on a new occurrence, by any amount. var initial = Seq(); nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); var raised = Seq(); raised.ShouldBeGreaterThan(initial); nm.WriteAlarmCondition(Native, State(true, false, message: "changed"), DateTime.UtcNow, AddressSpaceRealm.Raw); nm.WriteAlarmCondition(Native, State(true, false, message: "changed"), DateTime.UtcNow, AddressSpaceRealm.Raw); Seq().ShouldBe(raised); // message-only change and same-state re-projection nm.WriteAlarmCondition(Native, State(false, true), DateTime.UtcNow, AddressSpaceRealm.Raw); Seq().ShouldBe(raised); nm.WriteAlarmCondition(Native, State(false, false), DateTime.UtcNow, AddressSpaceRealm.Raw); // InactiveUnacknowledged Seq().ShouldBeGreaterThan(raised); await host.DisposeAsync(); } [Fact] public async Task A_scripted_condition_keeps_its_marker_across_a_rebuild_reassert() { var (host, nm, _) = await BootAsync(); MaterialiseAll(nm); nm.WriteAlarmCondition(Scripted, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Uns); var marker = nm.GetActivationMarker(Scripted, AddressSpaceRealm.Uns); nm.RebuildAddressSpace(); MaterialiseAll(nm); nm.IsConditionStateKnown(Scripted, AddressSpaceRealm.Uns).ShouldBeFalse(); // scripted: re-established by its engine nm.WriteAlarmCondition(Scripted, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Uns); // the re-assert nm.CompleteAddressSpaceApply(); nm.GetActivationMarker(Scripted, AddressSpaceRealm.Uns).ShouldBe(marker); await host.DisposeAsync(); } // ---------------------------------------------------------------- snapshot structure [Fact] public async Task A_snapshot_never_copies_more_than_MaxConditions_under_one_Lock_hold() { var (host, nm, sink) = await BootAsync(); nm.EnsureFolder(Device, null, "dev1", AddressSpaceRealm.Raw); for (var i = 0; i < 25; i++) nm.MaterialiseAlarmCondition($"{Device}/A{i:00}", Device, $"A{i:00}", "OffNormalAlarm", 500, AddressSpaceRealm.Raw, isNative: true); var holds = new List(); nm.OnConditionSnapshotHold = n => { Monitor.IsEntered(nm.Lock).ShouldBeFalse(); // reported after the hold is released holds.Add(n); // A delta raised BETWEEN holds: into the second slice (A15) and into the first, already-cut one (A00). if (holds.Count == 1) { nm.WriteAlarmCondition($"{Device}/A15", State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); nm.WriteAlarmCondition($"{Device}/A00", State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); } }; var slices = nm.SnapshotConditions((_, _) => true, maxConditionsPerHold: 10); holds.ShouldBe([10, 10, 5]); slices.Count.ShouldBe(3); // Atomic cut: A15's change is inside slice 2 (≤ its watermark); A00's is after slice 1's watermark. slices[1].Records.Single(r => r.ConditionId.EndsWith("A15")).Active.ShouldBeTrue(); slices[0].Records.Single(r => r.ConditionId.EndsWith("A00")).Active.ShouldBeFalse(); sink.Records.Single(r => r.ConditionId.EndsWith("A00") && r.Active).Version.ShouldBeGreaterThan(slices[0].Watermark); sink.Records.Single(r => r.ConditionId.EndsWith("A15") && r.Active).Version.ShouldBeLessThanOrEqualTo(slices[1].Watermark); await host.DisposeAsync(); } /// Review S3a: a condition materialised and activated between the key enumeration and slice 1's hold is in /// no slice, and its records are after the snapshot's START cursor — the slice watermark alone would hide them. [Fact] public async Task S3a_A_condition_materialised_after_the_enumeration_is_covered_by_deltas_after_the_start_cursor() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); const string Late = Device + "/Late"; nm.OnConditionSnapshotKeysEnumerated = () => { nm.MaterialiseAlarmCondition(Late, Device, "Late", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true); nm.WriteAlarmCondition(Late, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); }; var slices = nm.SnapshotConditions((_, _) => true); slices.SelectMany(x => x.Records).ShouldNotContain(r => r.ConditionId == Late); slices.ShouldAllBe(x => x.StartCursor == slices[0].StartCursor); var late = sink.Records.Where(r => r.ConditionId == Late).ToList(); late.ShouldNotBeEmpty(); late.ShouldAllBe(r => r.Version > slices[0].StartCursor); late.ShouldAllBe(r => r.Version <= slices[0].Watermark); // why the start cursor is needed await host.DisposeAsync(); } /// Review S3b: readiness is cut with the snapshot — a rebuild that straddles a multi-slice snapshot (even one /// that completes before the last hold) makes it incomplete; one inside a single apply generation is complete. [Fact] public async Task S3b_A_rebuild_straddling_a_multi_slice_snapshot_makes_it_incomplete() { var (host, nm, _) = await BootAsync(); MaterialiseAll(nm); ConditionSnapshotSlice.IsCompleteSnapshot(nm.SnapshotConditions((_, _) => true, 1)).ShouldBeFalse(); // not applied yet nm.CompleteAddressSpaceApply(); ConditionSnapshotSlice.IsCompleteSnapshot(nm.SnapshotConditions((_, _) => true, 1)).ShouldBeTrue(); var holds = 0; nm.OnConditionSnapshotHold = _ => { if (++holds != 1) return; nm.RebuildAddressSpace(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); }; var slices = nm.SnapshotConditions((_, _) => true, maxConditionsPerHold: 1); slices.Count.ShouldBe(3); slices.ShouldAllBe(x => x.ApplyComplete); // each hold alone looks ready … ConditionSnapshotSlice.IsCompleteSnapshot(slices).ShouldBeFalse(); // … but the cut spans two generations await host.DisposeAsync(); } /// Review S3c: a caller's per-hold maximum is clamped to . [Fact] public async Task S3c_MaxConditions_per_hold_is_clamped() { var (host, nm, _) = await BootAsync(); nm.EnsureFolder(Device, null, "dev1", AddressSpaceRealm.Raw); for (var i = 0; i <= OtOpcUaNodeManager.MaxConditionsPerHoldLimit; i++) nm.MaterialiseAlarmCondition($"{Device}/C{i:00000}", Device, $"C{i:00000}", "OffNormalAlarm", 500, AddressSpaceRealm.Raw, isNative: true); var holds = new List(); nm.OnConditionSnapshotHold = holds.Add; nm.SnapshotConditions((_, _) => true, maxConditionsPerHold: int.MaxValue); holds.ShouldBe([OtOpcUaNodeManager.MaxConditionsPerHoldLimit, 1]); await host.DisposeAsync(); } /// Review S4: a failed/degraded rebuild apply neither emits removal records for the conditions it did not /// re-materialise nor flips readiness; the next successful apply completes it. [Fact] public async Task S4_A_failed_rebuild_apply_emits_no_removals_and_keeps_readiness_withheld() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); nm.CompleteAddressSpaceApply(); nm.RebuildAddressSpace(); nm.EnsureFolder(Device, null, "dev1", AddressSpaceRealm.Raw); nm.MaterialiseAlarmCondition(Native, Device, "TankHi", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true); var before = sink.Records.Count; nm.CompleteAddressSpaceApply(succeeded: false); // the pass threw before re-creating Native2 and the scripted one sink.Records.Skip(before).ShouldNotContain(r => r.Removed); sink.ApplyComplete.ShouldBe(false); nm.IsConditionApplyComplete.ShouldBeFalse(); nm.CompleteAddressSpaceApply(succeeded: true); // a later successful apply (that also dropped them) sink.Records.Skip(before).Where(r => r.Removed).Select(r => r.ConditionId).ShouldBe([Native2, Scripted], ignoreOrder: true); sink.ApplyComplete.ShouldBe(true); await host.DisposeAsync(); } /// Review nit: a timed shelve's absolute expiry rides the record (A2 projects the remaining time). [Fact] public async Task A_timed_shelve_record_carries_its_UnshelveTime() { var (host, nm, sink) = await BootAsync(); MaterialiseAll(nm); var at = DateTime.UtcNow.AddMinutes(5); nm.WriteAlarmCondition(Scripted, State(true, false) with { Shelving = AlarmShelvingKind.Timed, UnshelveAtUtc = at }, DateTime.UtcNow, AddressSpaceRealm.Uns); sink.Records.Last().ShouldSatisfyAllConditions( r => r.Shelving.ShouldBe(AlarmShelvingKind.Timed), r => r.UnshelveTimeUtc.ShouldBe(at)); nm.WriteAlarmCondition(Scripted, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Uns); sink.Records.Last().UnshelveTimeUtc.ShouldBeNull(); AssertParity(nm, sink); await host.DisposeAsync(); } // ---------------------------------------------------------------- helpers private static void MaterialiseAll(OtOpcUaNodeManager nm) { nm.EnsureFolder(Device, null, "dev1", AddressSpaceRealm.Raw); nm.MaterialiseAlarmCondition(Native, Device, "TankHi", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true); nm.MaterialiseAlarmCondition(Native2, Device, "TankLo", "OffNormalAlarm", 700, AddressSpaceRealm.Raw, isNative: true); nm.EnsureFolder(Equipment, null, "Eq", AddressSpaceRealm.Uns); nm.MaterialiseAlarmCondition(Scripted, Equipment, "Hi", "AlarmCondition", 500, AddressSpaceRealm.Uns); } private static List Flatten(IReadOnlyList slices) => slices.SelectMany(s => s.Records).ToList(); /// Hub snapshot == node state: the delta stream folded per condition equals a fresh snapshot of the nodes. internal static void AssertParity(OtOpcUaNodeManager nm, RecordingConditionSink sink) { var fold = new Dictionary<(AddressSpaceRealm, string), ConditionStateRecord>(); foreach (var r in sink.Records) { if (r.Removed) fold.Remove((r.Realm, r.ConditionId)); else fold[(r.Realm, r.ConditionId)] = r; } var nodes = nm.SnapshotConditions((_, _) => true).SelectMany(s => s.Records).ToList(); fold.Count.ShouldBe(nodes.Count); foreach (var node in nodes) { fold.TryGetValue((node.Realm, node.ConditionId), out var folded).ShouldBeTrue(node.ConditionId); (folded! with { Version = 0 }).ShouldBe(node with { Version = 0 }, node.ConditionId); } } private async Task<(OpcUaApplicationHost Host, OtOpcUaNodeManager Nm, RecordingConditionSink Sink)> BootAsync(bool warm = false) { var applicationUri = $"urn:OtOpcUa.A1Test:{Guid.NewGuid():N}"; var started = await OpcUaHostStart.StartAsync( port => new OpcUaApplicationHost( new OpcUaApplicationHostOptions { ApplicationName = "OtOpcUa.A1Test", ApplicationUri = applicationUri, OpcUaPort = port, PublicHostname = "localhost", PkiStoreRoot = _pkiRoot, Redundancy = new OpcUaRedundancyOptions { Mode = warm ? "Warm" : "Unset" }, PeerApplicationUris = warm ? ["urn:peer"] : [], }, Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance), () => new OtOpcUaSdkServer(), Ct); var nm = started.Server.NodeManager!; var sink = new RecordingConditionSink(); nm.ConditionStateSink = sink; return (started.Host, nm, sink); } /// Cleans up the PKI root directory. public void Dispose() { if (Directory.Exists(_pkiRoot)) { try { Directory.Delete(_pkiRoot, recursive: true); } catch { /* best-effort cleanup */ } } } } /// An in-memory that keeps every record with its assigned version. internal sealed class RecordingConditionSink : IConditionStateSink { private long _version; /// Every appended record, in order, with its version. public List Records { get; } = new(); /// The last apply state the node manager reported. public bool? ApplyComplete { get; private set; } /// public long Watermark => _version; /// public long Append(ConditionStateRecord record) { var v = ++_version; Records.Add(record with { Version = v }); return v; } /// public void OnApplyStateChanged(bool complete) => ApplyComplete = complete; }