fix(central-ui): InstanceTreeBuilder — orphaned instances surface at root; read-only Children; edge tests

Review follow-ups on f12ba2f9:
- An instance whose AreaId points at an area outside the supplied list was
  silently dropped; it now surfaces at the root, mirroring the orphaned-area
  rule, and stays filterable.
- InstanceTreeNode.Children is an IReadOnlyList over a private backing list,
  composed by the builder through an internal AddChild — the record no longer
  hands callers a mutable list.
- Extra tests: unknown-area instance (root + filterable), 3-level nesting,
  a ParentAreaId cycle that must neither loop nor throw, and a filter that
  matches instance names only (an area name match alone does not keep it).

Claude-Session: https://claude.ai/code/session_01NcMG6xXdsJbmD2wYAgkK29
This commit is contained in:
Joseph Doherty
2026-09-08 09:26:14 -04:00
parent f12ba2f9eb
commit 8e6b748029
3 changed files with 96 additions and 7 deletions
@@ -14,9 +14,9 @@ public static class InstanceTreeBuilder
/// instances beneath it, mirroring the Topology page's ordering: at every level the
/// child areas come first (sorted by name, ordinal-ignore-case), then that level's
/// instances (sorted by unique name, ordinal-ignore-case). Instances with no area sit
/// at the root after the root areas. An area whose parent id points at an area that is
/// not in <paramref name="areas"/> is treated as a root, so a stale row can never hide
/// a subtree.
/// at the root after the root areas. A row whose parent points at an area that is not in
/// <paramref name="areas"/> is treated as a root — both an orphaned area and an instance
/// with an unknown area id — so a stale row can never hide a subtree or an instance.
/// </summary>
/// <param name="areas">The site's areas (flat, linked by <see cref="Area.ParentAreaId"/>); null is treated as empty.</param>
/// <param name="instances">The site's instances; null is treated as empty.</param>
@@ -47,7 +47,7 @@ public static class InstanceTreeBuilder
var instancesByArea = instances
.Where(i => !hasFilter || i.UniqueName.Contains(needle!, StringComparison.OrdinalIgnoreCase))
.OrderBy(i => i.UniqueName, StringComparer.OrdinalIgnoreCase)
.ToLookup(i => i.AreaId);
.ToLookup(i => i.AreaId is int a && knownAreaIds.Contains(a) ? a : (int?)null);
return BuildLevel(parentId: null, areasByParent, instancesByArea, hasFilter, ancestors: []);
}
@@ -67,7 +67,11 @@ public static class InstanceTreeBuilder
if (!ancestors.Add(area.Id)) continue;
var node = new InstanceTreeNode($"a:{area.Id}", area.Name, area, Instance: null);
node.Children.AddRange(BuildLevel(area.Id, areasByParent, instancesByArea, hasFilter, ancestors));
foreach (var child in BuildLevel(area.Id, areasByParent, instancesByArea, hasFilter, ancestors))
{
node.AddChild(child);
}
ancestors.Remove(area.Id);
// With a filter an area survives only if something matched beneath it.
@@ -5,6 +5,7 @@ namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
/// <summary>
/// One row of the Instance Picker tree: an area branch or an instance leaf.
/// Keys are <c>a:{areaId}</c> for areas and <c>i:{instanceId}</c> for instances.
/// Children are populated by <see cref="InstanceTreeBuilder"/> and read-only to callers.
/// </summary>
/// <param name="Key">Stable tree key — <c>a:{areaId}</c> or <c>i:{instanceId}</c>.</param>
/// <param name="Label">Text rendered for this row.</param>
@@ -12,12 +13,18 @@ namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
/// <param name="Instance">The instance this node represents, or null for an area branch.</param>
public sealed record InstanceTreeNode(string Key, string Label, Area? Area, Instance? Instance)
{
private readonly List<InstanceTreeNode> _children = new();
/// <summary>Gets the child rows nested under this node (child areas first, then instances).</summary>
public List<InstanceTreeNode> Children { get; } = new();
public IReadOnlyList<InstanceTreeNode> Children => _children;
/// <summary>Gets a value indicating whether this node is a selectable instance leaf.</summary>
public bool IsInstance => Instance is not null;
/// <summary>Gets a value indicating whether this node has any child rows.</summary>
public bool HasChildren => Children.Count > 0;
public bool HasChildren => _children.Count > 0;
/// <summary>Appends a child row. Only the builder composes the tree.</summary>
/// <param name="child">The child row to append.</param>
internal void AddChild(InstanceTreeNode child) => _children.Add(child);
}