< Summary

Information
Class: Chronicis.Client.Services.Tree.TreeNodeIndex
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/Tree/TreeNodeIndex.cs
Line coverage
96%
Covered lines: 27
Uncovered lines: 1
Coverable lines: 28
Total lines: 111
Line coverage: 96.4%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_RootNodes()100%11100%
get_Nodes()100%210%
get_Count()100%11100%
TryGetNode(...)100%22100%
GetNode(...)50%22100%
ContainsNode(...)100%11100%
AddNode(...)100%11100%
AddRootNode(...)100%11100%
SetRootNodes(...)100%11100%
Clear()100%11100%
get_AllNodes()100%11100%
FindParentNode(...)100%44100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/Tree/TreeNodeIndex.cs

#LineLine coverage
 1using Chronicis.Client.Models;
 2
 3namespace Chronicis.Client.Services.Tree;
 4
 5/// <summary>
 6/// Shared container for tree node state.
 7/// Provides indexed access to all nodes in the tree and holds the root node collection.
 8/// This class is shared between TreeDataBuilder, TreeUiState, and TreeMutations.
 9/// </summary>
 10internal sealed class TreeNodeIndex
 11{
 11412    private readonly Dictionary<Guid, TreeNode> _nodes = new();
 11413    private List<TreeNode> _rootNodes = new();
 14
 15    /// <summary>
 16    /// Gets the root-level nodes of the tree.
 17    /// </summary>
 718    public IReadOnlyList<TreeNode> RootNodes => _rootNodes;
 19
 20    /// <summary>
 21    /// Gets all nodes indexed by their ID.
 22    /// </summary>
 023    public IReadOnlyDictionary<Guid, TreeNode> Nodes => _nodes;
 24
 25    /// <summary>
 26    /// Gets the count of all indexed nodes.
 27    /// </summary>
 428    public int Count => _nodes.Count;
 29
 30    /// <summary>
 31    /// Attempts to get a node by its ID.
 32    /// </summary>
 33    public bool TryGetNode(Guid id, out TreeNode? node)
 34    {
 8135        if (_nodes.TryGetValue(id, out var foundNode))
 36        {
 7237            node = foundNode;
 7238            return true;
 39        }
 940        node = null;
 941        return false;
 42    }
 43
 44    /// <summary>
 45    /// Gets a node by ID, or null if not found.
 46    /// </summary>
 47    public TreeNode? GetNode(Guid id)
 48    {
 149        return _nodes.TryGetValue(id, out var node) ? node : null;
 50    }
 51
 52    /// <summary>
 53    /// Checks if a node with the given ID exists.
 54    /// </summary>
 655    public bool ContainsNode(Guid id) => _nodes.ContainsKey(id);
 56
 57    /// <summary>
 58    /// Adds a node to the index.
 59    /// </summary>
 60    public void AddNode(TreeNode node)
 61    {
 6962        _nodes[node.Id] = node;
 6963    }
 64
 65    /// <summary>
 66    /// Adds a node to both the index and the root nodes collection.
 67    /// </summary>
 68    public void AddRootNode(TreeNode node)
 69    {
 470        _nodes[node.Id] = node;
 471        _rootNodes.Add(node);
 472    }
 73
 74    /// <summary>
 75    /// Sets the root nodes collection (used during tree building).
 76    /// </summary>
 77    public void SetRootNodes(List<TreeNode> rootNodes)
 78    {
 179        _rootNodes = rootNodes;
 180    }
 81
 82    /// <summary>
 83    /// Clears all nodes and root nodes.
 84    /// </summary>
 85    public void Clear()
 86    {
 187        _nodes.Clear();
 188        _rootNodes = new List<TreeNode>();
 189    }
 90
 91    /// <summary>
 92    /// Gets all nodes as an enumerable (for iteration).
 93    /// </summary>
 1794    public IEnumerable<TreeNode> AllNodes => _nodes.Values;
 95
 96    /// <summary>
 97    /// Finds the parent node that contains the given child in its Children collection.
 98    /// This searches through all nodes to find structural parents (for virtual groups, etc.).
 99    /// </summary>
 100    public TreeNode? FindParentNode(TreeNode child)
 101    {
 54102        foreach (var node in _nodes.Values)
 103        {
 17104            if (node.Children.Contains(child))
 105            {
 2106                return node;
 107            }
 108        }
 9109        return null;
 2110    }
 111}