UNPKG

archunit

Version:

ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app

176 lines 6.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cycle_utils_1 = require("./cycle-utils"); /* eslint-disable @typescript-eslint/no-explicit-any */ describe('CycleUtils', () => { describe('getOutgoingNeighbours', () => { it('should return empty array when node has no outgoing edges', () => { const currentNode = { node: 1, incoming: [], outgoing: [], }; const graph = [currentNode]; const result = cycle_utils_1.CycleUtils.getOutgoingNeighbours(currentNode, graph); expect(result).toEqual([]); }); it('should return neighbour nodes for outgoing edges', () => { const node1 = { node: 1, incoming: [], outgoing: [ { from: 1, to: 2 }, { from: 1, to: 3 }, ], }; const node2 = { node: 2, incoming: [{ from: 1, to: 2 }], outgoing: [], }; const node3 = { node: 3, incoming: [{ from: 1, to: 3 }], outgoing: [], }; const graph = [node1, node2, node3]; const result = cycle_utils_1.CycleUtils.getOutgoingNeighbours(node1, graph); expect(result).toEqual([node2, node3]); }); it('should filter out nodes not found in graph', () => { const currentNode = { node: 1, incoming: [], outgoing: [ { from: 1, to: 2 }, { from: 1, to: 99 }, ], }; const node2 = { node: 2, incoming: [{ from: 1, to: 2 }], outgoing: [], }; const graph = [currentNode, node2]; const result = cycle_utils_1.CycleUtils.getOutgoingNeighbours(currentNode, graph); expect(result).toEqual([node2]); }); }); describe('transformEdgeData', () => { it('should return empty array for empty edges', () => { const result = cycle_utils_1.CycleUtils.transformEdgeData([]); expect(result).toEqual([]); }); it('should transform single edge correctly', () => { const edges = [{ from: 1, to: 2 }]; const result = cycle_utils_1.CycleUtils.transformEdgeData(edges); expect(result).toHaveLength(2); expect(result).toContainEqual({ node: 1, incoming: [], outgoing: [{ from: 1, to: 2 }], }); expect(result).toContainEqual({ node: 2, incoming: [{ from: 1, to: 2 }], outgoing: [], }); }); it('should handle multiple edges between same nodes', () => { const edges = [ { from: 1, to: 2 }, { from: 1, to: 2 }, { from: 2, to: 1 }, ]; const result = cycle_utils_1.CycleUtils.transformEdgeData(edges); expect(result).toHaveLength(2); expect(result).toContainEqual({ node: 1, incoming: [{ from: 2, to: 1 }], outgoing: [ { from: 1, to: 2 }, { from: 1, to: 2 }, ], }); expect(result).toContainEqual({ node: 2, incoming: [ { from: 1, to: 2 }, { from: 1, to: 2 }, ], outgoing: [{ from: 2, to: 1 }], }); }); it('should handle complex graph structure', () => { const edges = [ { from: 1, to: 2 }, { from: 2, to: 3 }, { from: 3, to: 1 }, { from: 2, to: 4 }, ]; const result = cycle_utils_1.CycleUtils.transformEdgeData(edges); expect(result).toHaveLength(4); expect(result.find((n) => n.node === 1)).toEqual({ node: 1, incoming: [{ from: 3, to: 1 }], outgoing: [{ from: 1, to: 2 }], }); expect(result.find((n) => n.node === 2)).toEqual({ node: 2, incoming: [{ from: 1, to: 2 }], outgoing: [ { from: 2, to: 3 }, { from: 2, to: 4 }, ], }); expect(result.find((n) => n.node === 3)).toEqual({ node: 3, incoming: [{ from: 2, to: 3 }], outgoing: [{ from: 3, to: 1 }], }); expect(result.find((n) => n.node === 4)).toEqual({ node: 4, incoming: [{ from: 2, to: 4 }], outgoing: [], }); }); }); describe('findUniqueNodes', () => { it('should return empty array for empty edges', () => { const result = cycle_utils_1.CycleUtils.findUniqueNodes([]); expect(result).toEqual([]); }); it('should find unique nodes from single edge', () => { const edges = [{ from: 1, to: 2 }]; const result = cycle_utils_1.CycleUtils.findUniqueNodes(edges); expect(result).toEqual([1, 2]); }); it('should not duplicate nodes', () => { const edges = [ { from: 1, to: 2 }, { from: 2, to: 3 }, { from: 1, to: 3 }, ]; const result = cycle_utils_1.CycleUtils.findUniqueNodes(edges); expect(result).toEqual([1, 2, 3]); }); it('should handle self-referencing edges', () => { const edges = [ { from: 1, to: 1 }, { from: 2, to: 2 }, ]; const result = cycle_utils_1.CycleUtils.findUniqueNodes(edges); expect(result).toEqual([1, 2]); }); it('should preserve order of first appearance', () => { const edges = [ { from: 3, to: 1 }, { from: 1, to: 2 }, { from: 2, to: 3 }, ]; const result = cycle_utils_1.CycleUtils.findUniqueNodes(edges); expect(result).toEqual([3, 1, 2]); }); }); }); //# sourceMappingURL=cycle-utils.spec.js.map