Warning, /firebird/firebird-ng/src/lib-root-geometry/root-geo-navigation.spec.ts is written in an unsupported language. File is not indexed.
0001 import type { Mock } from "vitest";
0002 /**
0003 * @date Created on July 10, 2024
0004 * @author Dmitry Romanov
0005 *
0006 * @license This file is part of Firebird display, which is released under a license agreement
0007 * available in the LICENSE file located in the root directory of this project source tree. This
0008 * file is subject to that license and is intended to be used in accordance with it.
0009 *
0010 * @summary Unit tests for CERN ROOT geometry navigation functions
0011 */
0012
0013 import { walkGeoNodes, findGeoNodes } from './root-geo-navigation';
0014
0015 describe('walkGeoNodes', () => {
0016 let mockCallback: Mock;
0017 const rootNode = {
0018 fName: "Root",
0019 fVolume: {
0020 fNodes: {
0021 arr: [
0022 { fName: "Child1", fVolume: { fNodes: { arr: [{ fName: "GrandChild1", fVolume: { fNodes: { arr: [] } } }] } } },
0023 { fName: "Child2", fVolume: { fNodes: { arr: [] } } }
0024 ]
0025 }
0026 }
0027 };
0028
0029 beforeEach(() => {
0030 mockCallback = vi.fn().mockReturnValue(true);
0031 ;
0032 });
0033
0034 it('should not traverse beyond the specified max level', () => {
0035 walkGeoNodes(rootNode, mockCallback, 1);
0036 expect(vi.mocked(mockCallback).mock.calls.length).toEqual(3); // Root, Child1, Child2
0037 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "Root" }), 'Root', 0);
0038 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "Child1" }), 'Root/Child1', 1);
0039 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "Child2" }), 'Root/Child2', 1);
0040 });
0041
0042 it('should handle empty node volumes correctly', () => {
0043 const emptyNode = { fName: "Empty", fVolume: null };
0044 walkGeoNodes(emptyNode, mockCallback, 1);
0045 expect(vi.mocked(mockCallback).mock.calls.length).toEqual(1); // Only the empty node should invoke the callback
0046 expect(mockCallback).toHaveBeenCalledWith(emptyNode, 'Empty', 0);
0047 });
0048
0049 it('should invoke callback for each node up to the specified max level', () => {
0050 walkGeoNodes(rootNode, mockCallback, Infinity); // Using Infinity to check all levels
0051 expect(vi.mocked(mockCallback).mock.calls.length).toEqual(4); // Root, Child1, GrandChild1, Child2
0052 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "Root" }), 'Root', 0);
0053 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "Child1" }), 'Root/Child1', 1);
0054 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "GrandChild1" }), 'Root/Child1/GrandChild1', 2);
0055 expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ fName: "Child2" }), 'Root/Child2', 1);
0056 });
0057 });
0058
0059
0060 describe('findGeoNodes', () => {
0061 const rootNode = {
0062 fName: "Root",
0063 fVolume: {
0064 fNodes: {
0065 arr: [
0066 { fName: "Child1", fVolume: { fNodes: { arr: [{ fName: "GrandChild1", fVolume: { fNodes: { arr: [] } } }] } } },
0067 { fName: "Child2", fVolume: { fNodes: { arr: [] } } }
0068 ]
0069 }
0070 }
0071 };
0072
0073 it('should return only nodes matching the specified pattern', () => {
0074 const pattern = "*Child2*";
0075 const results = findGeoNodes(rootNode, pattern);
0076 expect(results.length).toBe(1);
0077 expect(results[0].fullPath).toContain('Root/Child2');
0078 });
0079
0080 it('should return an empty array if no nodes match the pattern', () => {
0081 const pattern = "*NotExist*";
0082 const results = findGeoNodes(rootNode, pattern);
0083 expect(results.length).toBe(0);
0084 });
0085
0086 it('should stop search if maxLevel is reached', () => {
0087 const pattern = "*Child1";
0088 const results = findGeoNodes(rootNode, pattern, 1);
0089 expect(results.length).toBe(1);
0090 });
0091
0092 it('should handle patterns that match deeply nested nodes', () => {
0093 const pattern = "*GrandChild*";
0094 const results = findGeoNodes(rootNode, pattern);
0095 expect(results.length).toBe(1);
0096 expect(results[0].fullPath).toContain('Root/Child1/GrandChild1');
0097 });
0098 });