From 3be309190808ff9c48f49dd8ebe8e62a48a3f396 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Fri, 4 Dec 2026 14:32:11 -0500 Subject: [PATCH] feat(adminui): #1090 local-parent tree picker + clear realm-scoped roots on realm change --- .../Clusters/ClusterDataConnections.razor | 49 +++++---------- .../Pages/DataConnectionRootPickerTests.cs | 62 +++++++++++++++++++ 2 files changed, 76 insertions(+), 35 deletions(-) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/Clusters/ClusterDataConnections.razor b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/Clusters/ClusterDataConnections.razor index 6f14d2e01..2278ba55b 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/Clusters/ClusterDataConnections.razor +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Components/Pages/Clusters/ClusterDataConnections.razor @@ -115,7 +115,7 @@ else

Export draft

- +
Or type the root as Kind|EntityId (e.g. RawTag|tag-id).
@Operations(e.AllowedOperations, v => e.AllowedOperations = v) @@ -132,8 +132,8 @@ else

Mapping draft

- - + +
The picker browses the configured target cluster. For a separately managed cluster, enter its root ID below.
@@ -148,8 +148,8 @@ else
@Actions(m, m.RemoteNamespaceMappingRowId != Guid.Empty)
} - "Choose local parent", _ => "Choose remote root" })" + Hint="@(_picker switch { RootPicker.Export => "Pick the node this export publishes. The tree is scoped to this cluster.", RootPicker.Parent => "Pick the local node the remote data is placed under. The tree is scoped to this cluster and shows containers only.", _ => "Pick the remote node to mirror. The tree is scoped to the connection's target cluster." })" Realm="@PickerRealm" RawRoot="@PickerRaw" UnsRoot="@PickerUns" CurrentKind="@PickerKind" CurrentId="@PickerId" OnPicked="PickRoot" OnCancel="() => _picker = RootPicker.None" /> @@ -201,36 +201,14 @@ else private static void Toggle(List ids, string id, bool on) { if (on && !ids.Contains(id)) ids.Add(id); else if (!on) ids.Remove(id); } private static void TargetChanged(DataConnection c) { if (c.TargetMode == DataTargetMode.Cluster) c.ExpectedNodeId = null; else c.EligibleTargetNodeIds.Clear(); } private static void SetRealm(RemoteNamespaceMapping m) { m.RemoteRealm = m.Realm; m.RemoteNamespaceUri = m.Realm == DataRealm.Raw ? V3NodeIds.RawNamespaceUri : V3NodeIds.UnsNamespaceUri; } - private static void SetParent(RemoteNamespaceMapping m, string? value) { var p = (value ?? "").Split('|', 2); m.LocalParentKind = p[0]; m.LocalParentId = p.Length > 1 ? p[1] : ""; } - private IEnumerable<(string Value, string Label)> Parents(DataRealm realm) - { - yield return ($"Cluster|{ClusterId}", "Cluster root"); - if (realm == DataRealm.Raw) - { - foreach (var x in _snapshot!.RawFolders.Where(x => Same(x.ClusterId, ClusterId))) yield return ($"RawFolder|{x.RawFolderId}", $"Folder: {x.Name} ({x.RawFolderId})"); - foreach (var x in _snapshot.DriverInstances.Where(x => Same(x.ClusterId, ClusterId))) yield return ($"RawDriver|{x.DriverInstanceId}", $"Driver: {x.Name} ({x.DriverInstanceId})"); - var drivers = _snapshot.DriverInstances.Where(x => Same(x.ClusterId, ClusterId)).Select(x => x.DriverInstanceId).ToHashSet(StringComparer.OrdinalIgnoreCase); - var devices = _snapshot.Devices.Where(x => drivers.Contains(x.DriverInstanceId)).ToList(); - foreach (var x in devices) yield return ($"RawDevice|{x.DeviceId}", $"Device: {x.Name} ({x.DeviceId})"); - var ids = devices.Select(x => x.DeviceId).ToHashSet(StringComparer.OrdinalIgnoreCase); - foreach (var x in _snapshot.TagGroups.Where(x => ids.Contains(x.DeviceId))) yield return ($"RawTagGroup|{x.TagGroupId}", $"Tag group: {x.Name} ({x.TagGroupId})"); - } - else if (realm == DataRealm.Uns) - { - var areas = _snapshot!.UnsAreas.Where(x => Same(x.ClusterId, ClusterId)).ToList(); - foreach (var x in areas) yield return ($"UnsArea|{x.UnsAreaId}", $"Area: {x.Name} ({x.UnsAreaId})"); - var areaIds = areas.Select(x => x.UnsAreaId).ToHashSet(StringComparer.OrdinalIgnoreCase); - var lines = _snapshot.UnsLines.Where(x => areaIds.Contains(x.UnsAreaId)).ToList(); - foreach (var x in lines) yield return ($"UnsLine|{x.UnsLineId}", $"Line: {x.Name} ({x.UnsLineId})"); - var lineIds = lines.Select(x => x.UnsLineId).ToHashSet(StringComparer.OrdinalIgnoreCase); - foreach (var x in _snapshot.Equipment.Where(x => lineIds.Contains(x.UnsLineId))) yield return ($"Equipment|{x.EquipmentId}", $"Equipment: {x.Name} ({x.EquipmentId})"); - } - } - private enum RootPicker { None, Export, Remote } + // #1090: a realm change invalidates every realm-scoped id, so none may survive it. + private static void ClearExportRoot(DataExport e) { e.RootKind = ""; e.RootEntityId = ""; } + private static void MappingRealmChanged(RemoteNamespaceMapping m) { SetRealm(m); m.LocalParentKind = ""; m.LocalParentId = ""; m.RemoteRootEntityId = ""; } + private enum RootPicker { None, Export, Remote, Parent } private RootPicker _picker; private string? RemoteTargetCluster(RemoteNamespaceMapping m) => Connections.FirstOrDefault(x => Same(x.DataConnectionId, m.DataConnectionId))?.ExpectedClusterId; private DataRealm PickerRealm => _picker == RootPicker.Export ? _export?.Realm ?? DataRealm.Raw : _mapping?.Value.Realm ?? DataRealm.Raw; - private string? PickerCluster => _picker == RootPicker.Export ? ClusterId : _mapping is { } m ? RemoteTargetCluster(m.Value) : null; + private string? PickerCluster => _picker is RootPicker.Export or RootPicker.Parent ? ClusterId : _mapping is { } m ? RemoteTargetCluster(m.Value) : null; // The trees are built once per open, from the snapshot, for the one cluster in scope. private RawNode? PickerRaw; private UnsPickNode? PickerUns; @@ -240,14 +218,15 @@ else PickerRaw = null; PickerUns = null; var cluster = PickerCluster; if (string.IsNullOrEmpty(cluster)) return; - if (PickerRealm == DataRealm.Raw) PickerRaw = DataRootTrees.BuildRaw(_snapshot!, cluster); + if (PickerRealm == DataRealm.Raw) PickerRaw = DataRootTrees.BuildRaw(_snapshot!, cluster, includeTags: which != RootPicker.Parent); else PickerUns = DataRootTrees.BuildUns(_snapshot!, cluster, equipmentLeaves: which == RootPicker.Export); } - private string? PickerKind => _picker == RootPicker.Export ? _export?.RootKind : null; - private string? PickerId => _picker == RootPicker.Export ? _export?.RootEntityId : _mapping?.Value.RemoteRootEntityId; + private string? PickerKind => _picker == RootPicker.Export ? _export?.RootKind : _picker == RootPicker.Parent ? _mapping?.Value.LocalParentKind : null; + private string? PickerId => _picker switch { RootPicker.Export => _export?.RootEntityId, RootPicker.Parent => _mapping?.Value.LocalParentId, _ => _mapping?.Value.RemoteRootEntityId }; private void PickRoot((string Kind, string Id) picked) { if (_picker == RootPicker.Export && _export is not null) { _export.RootKind = picked.Kind; _export.RootEntityId = picked.Id; } + else if (_picker == RootPicker.Parent && _mapping is not null) { _mapping.Value.LocalParentKind = picked.Kind; _mapping.Value.LocalParentId = picked.Id; } else if (_picker == RootPicker.Remote && _mapping is not null) _mapping.Value.RemoteRootEntityId = picked.Id; _picker = RootPicker.None; } diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/DataConnectionRootPickerTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/DataConnectionRootPickerTests.cs index 1d42578e1..33bb60d4c 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/DataConnectionRootPickerTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Pages/DataConnectionRootPickerTests.cs @@ -138,6 +138,68 @@ public sealed class DataConnectionRootPickerTests : BunitContext cut.WaitForAssertion(() => cut.Find("#remote-root").GetAttribute("value").ShouldBe("AREA-OTHER")); } + [Fact] + public void LocalParent_UnsTreePicker_IsLocalClusterScoped_AndStoresKindAndId() + { + var cut = RenderPage(); + cut.WaitForElement("button:contains('New mapping')").Click(); + cut.WaitForElement("#mapping-parent").GetAttribute("value").ShouldBe("No parent chosen"); + cut.Find("#mapping-realm").Change("Uns"); + cut.Find("#mapping-parent-browse").Click(); + cut.WaitForAssertion(() => cut.Find("[role=dialog]")); + cut.WaitForAssertion(() => cut.FindAll(".uns-root-picker input[type=radio]").Count.ShouldBeGreaterThan(0)); + // The parent is a LOCAL node: this page's cluster, never the connection's target cluster. + cut.Find("[role=dialog]").InnerHtml.ShouldContain("Packaging"); + cut.Find("[role=dialog]").InnerHtml.ShouldNotContain("Foreign area"); + cut.Find("input[title='UnsArea AREA-1']").Change(true); + cut.Find("#root-picker-confirm").Click(); + cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("UnsArea AREA-1")); + cut.FindAll("[role=dialog]").Count.ShouldBe(0); + } + + [Fact] + public void LocalParent_RawTreePicker_PicksAContainer() + { + var cut = RenderPage(); + cut.WaitForElement("button:contains('New mapping')").Click(); + cut.WaitForElement("#mapping-parent-browse").Click(); + cut.WaitForAssertion(() => cut.FindAll("[role=dialog] input[type=radio]").Count.ShouldBeGreaterThan(0)); + cut.Find("button[aria-label='Expand PLCs']").Click(); + cut.WaitForElement("button[aria-label='Expand modbus-1']").Click(); + cut.WaitForElement("input[title='Device dev:" + RawTreeTestDb.FolderedDeviceId + "']").Change(true); + cut.Find("#root-picker-confirm").Click(); + cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("RawDevice " + RawTreeTestDb.FolderedDeviceId)); + } + + [Fact] + public void ChangingTheExportRealm_ClearsTheStaleRoot() + { + var cut = RenderPage(); + cut.WaitForElement("button:contains('New export')").Click(); + cut.WaitForElement("#export-root-id").Change("RawTag|tag-1"); + cut.Find("#export-root").GetAttribute("value").ShouldBe("RawTag tag-1"); + cut.Find("#export-realm").Change("Uns"); + cut.WaitForAssertion(() => cut.Find("#export-root").GetAttribute("value").ShouldBe("No root chosen")); + } + + [Fact] + public void ChangingTheMappingRealm_ClearsTheParentAndRemoteRoot() + { + var cut = RenderPage(); + cut.WaitForElement("button:contains('New mapping')").Click(); + cut.WaitForElement("#mapping-realm").Change("Uns"); + cut.Find("#mapping-parent-browse").Click(); + cut.WaitForAssertion(() => cut.FindAll(".uns-root-picker input[type=radio]").Count.ShouldBeGreaterThan(0)); + cut.Find("input[title='UnsArea AREA-1']").Change(true); + cut.Find("#root-picker-confirm").Click(); + cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("UnsArea AREA-1")); + cut.Find("#remote-root-id").Change("AREA-OTHER"); + cut.Find("#remote-root").GetAttribute("value").ShouldBe("AREA-OTHER"); + cut.Find("#mapping-realm").Change("Raw"); + cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("No parent chosen")); + cut.Find("#remote-root").GetAttribute("value").ShouldBe("No root chosen"); + } + [Fact] public void Cancel_LeavesTheFieldUnchanged() {