|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { Edge } from '../common/extraction/graph'; |
| 5 | +import { ImportKind } from '../common/util/import-kinds'; |
| 6 | +import { GraphReporter } from './graph-reporter'; |
| 7 | + |
| 8 | +const graph: Edge[] = [ |
| 9 | + { |
| 10 | + source: 'src/app/controller.ts', |
| 11 | + target: 'src/domain/service.ts', |
| 12 | + external: false, |
| 13 | + importKinds: [ImportKind.NAMED], |
| 14 | + }, |
| 15 | + { |
| 16 | + source: 'src/app/controller.ts', |
| 17 | + target: 'src/domain/service.ts', |
| 18 | + external: false, |
| 19 | + importKinds: [ImportKind.TYPE], |
| 20 | + }, |
| 21 | + { |
| 22 | + source: 'src/domain/service.ts', |
| 23 | + target: 'src/infra/repository.ts', |
| 24 | + external: false, |
| 25 | + importKinds: [ImportKind.DEFAULT], |
| 26 | + }, |
| 27 | + { |
| 28 | + source: 'src/app/controller.ts', |
| 29 | + target: 'src/infra/repository.ts', |
| 30 | + external: false, |
| 31 | + importKinds: [ImportKind.DEFAULT], |
| 32 | + }, |
| 33 | + { |
| 34 | + source: 'src/infra/repository.ts', |
| 35 | + target: 'src/infra/repository.ts', |
| 36 | + external: false, |
| 37 | + importKinds: [], |
| 38 | + }, |
| 39 | + { |
| 40 | + source: 'src/app/controller.ts', |
| 41 | + target: 'node_modules/react/index.d.ts', |
| 42 | + external: true, |
| 43 | + importKinds: [ImportKind.DEFAULT], |
| 44 | + }, |
| 45 | +]; |
| 46 | + |
| 47 | +describe('GraphReporter', () => { |
| 48 | + it('aggregates duplicate edges and excludes self/external edges by default', () => { |
| 49 | + const snapshot = GraphReporter.createSnapshot(graph); |
| 50 | + |
| 51 | + expect(snapshot.summary.nodeCount).toBe(3); |
| 52 | + expect(snapshot.summary.edgeCount).toBe(3); |
| 53 | + expect(snapshot.summary.rawEdgeCount).toBe(4); |
| 54 | + expect(snapshot.summary.externalEdgeCount).toBe(0); |
| 55 | + expect(snapshot.edges).toContainEqual({ |
| 56 | + source: 'src/app/controller.ts', |
| 57 | + target: 'src/domain/service.ts', |
| 58 | + count: 2, |
| 59 | + external: false, |
| 60 | + importKinds: ['named', 'type'], |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('can include external dependencies when requested', () => { |
| 65 | + const snapshot = GraphReporter.createSnapshot(graph, { |
| 66 | + includeExternalDependencies: true, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(snapshot.summary.externalEdgeCount).toBe(1); |
| 70 | + expect(snapshot.edges).toContainEqual({ |
| 71 | + source: 'src/app/controller.ts', |
| 72 | + target: 'node_modules/react/index.d.ts', |
| 73 | + count: 1, |
| 74 | + external: true, |
| 75 | + importKinds: ['default'], |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('focuses on matching nodes and configurable neighbor depth', () => { |
| 80 | + const focused = GraphReporter.createSnapshot(graph, { |
| 81 | + focus: 'src/domain/**', |
| 82 | + focusDepth: 0, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(focused.nodes.map((node) => node.label)).toEqual([ |
| 86 | + 'src/domain/service.ts', |
| 87 | + ]); |
| 88 | + expect(focused.edges).toEqual([]); |
| 89 | + |
| 90 | + const withNeighbors = GraphReporter.createSnapshot(graph, { |
| 91 | + focus: 'src/domain/**', |
| 92 | + focusDepth: 1, |
| 93 | + }); |
| 94 | + |
| 95 | + expect(withNeighbors.nodes.map((node) => node.label)).toEqual([ |
| 96 | + 'src/app/controller.ts', |
| 97 | + 'src/domain/service.ts', |
| 98 | + 'src/infra/repository.ts', |
| 99 | + ]); |
| 100 | + expect(withNeighbors.summary.edgeCount).toBe(3); |
| 101 | + }); |
| 102 | + |
| 103 | + it('filters to reachable dependencies and dependents', () => { |
| 104 | + const reachable = GraphReporter.createSnapshot(graph, { |
| 105 | + reachableFrom: 'src/domain/**', |
| 106 | + }); |
| 107 | + |
| 108 | + expect(reachable.nodes.map((node) => node.label)).toEqual([ |
| 109 | + 'src/domain/service.ts', |
| 110 | + 'src/infra/repository.ts', |
| 111 | + ]); |
| 112 | + expect(reachable.edges).toHaveLength(1); |
| 113 | + |
| 114 | + const dependents = GraphReporter.createSnapshot(graph, { |
| 115 | + dependentsOf: 'src/infra/**', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(dependents.nodes.map((node) => node.label)).toEqual([ |
| 119 | + 'src/app/controller.ts', |
| 120 | + 'src/domain/service.ts', |
| 121 | + 'src/infra/repository.ts', |
| 122 | + ]); |
| 123 | + expect(dependents.summary.edgeCount).toBe(3); |
| 124 | + }); |
| 125 | + |
| 126 | + it('collapses nodes to folder depth before aggregating edges', () => { |
| 127 | + const snapshot = GraphReporter.createSnapshot(graph, { |
| 128 | + collapse: { |
| 129 | + type: 'folder-depth', |
| 130 | + depth: 2, |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + expect(snapshot.nodes.map((node) => node.label)).toEqual([ |
| 135 | + 'src/app', |
| 136 | + 'src/domain', |
| 137 | + 'src/infra', |
| 138 | + ]); |
| 139 | + expect(snapshot.edges).toContainEqual({ |
| 140 | + source: 'src/app', |
| 141 | + target: 'src/domain', |
| 142 | + count: 2, |
| 143 | + external: false, |
| 144 | + importKinds: ['named', 'type'], |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('renders supported text formats', () => { |
| 149 | + const mermaid = GraphReporter.toMermaid(graph); |
| 150 | + const dot = GraphReporter.toDOT(graph); |
| 151 | + const d2 = GraphReporter.toD2(graph); |
| 152 | + const csv = GraphReporter.toCSV(graph); |
| 153 | + const json = GraphReporter.toJSON(graph); |
| 154 | + const html = GraphReporter.toHTML(graph, { title: 'Architecture Report' }); |
| 155 | + |
| 156 | + expect(mermaid).toContain('flowchart LR'); |
| 157 | + expect(mermaid).toContain('|2|'); |
| 158 | + expect(dot).toContain('digraph dependencies'); |
| 159 | + expect(dot).toContain('"src/app/controller.ts" -> "src/domain/service.ts"'); |
| 160 | + expect(d2).toContain('"src/app/controller.ts" -> "src/domain/service.ts"'); |
| 161 | + expect(csv.split('\n')[0]).toBe('source,target,count,external,importKinds'); |
| 162 | + expect(JSON.parse(json).summary.edgeCount).toBe(3); |
| 163 | + expect(html).toContain('<title>Architecture Report</title>'); |
| 164 | + expect(html).toContain('Generated by ArchUnitTS graph reporting'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('writes reports to disk', async () => { |
| 168 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'archunit-graph-')); |
| 169 | + const outputPath = path.join(dir, 'dependencies.mmd'); |
| 170 | + |
| 171 | + await GraphReporter.exportAsMermaid(graph, outputPath); |
| 172 | + |
| 173 | + expect(fs.readFileSync(outputPath, 'utf8')).toContain('flowchart LR'); |
| 174 | + }); |
| 175 | +}); |
0 commit comments