|
| 1 | +import { performance } from 'perf_hooks'; |
1 | 2 | import * as depGraphLib from '../../src'; |
2 | 3 | import * as helpers from '../helpers'; |
3 | 4 |
|
| 5 | +function buildDiamondChain(layers: number): depGraphLib.DepGraph { |
| 6 | + const builder = new depGraphLib.DepGraphBuilder( |
| 7 | + { name: 'npm' }, |
| 8 | + { name: 'root', version: '1.0.0' }, |
| 9 | + ); |
| 10 | + const rootNodeId = 'root-node'; |
| 11 | + |
| 12 | + let prevNodeId = rootNodeId; |
| 13 | + for (let i = 0; i < layers; i++) { |
| 14 | + const leftId = `left-${i}`; |
| 15 | + const rightId = `right-${i}`; |
| 16 | + const joinId = `join-${i}`; |
| 17 | + |
| 18 | + builder.addPkgNode({ name: leftId, version: '1.0.0' }, leftId); |
| 19 | + builder.addPkgNode({ name: rightId, version: '1.0.0' }, rightId); |
| 20 | + builder.addPkgNode({ name: joinId, version: '1.0.0' }, joinId); |
| 21 | + |
| 22 | + builder.connectDep(prevNodeId, leftId); |
| 23 | + builder.connectDep(prevNodeId, rightId); |
| 24 | + builder.connectDep(leftId, joinId); |
| 25 | + builder.connectDep(rightId, joinId); |
| 26 | + |
| 27 | + prevNodeId = joinId; |
| 28 | + } |
| 29 | + |
| 30 | + return builder.build(); |
| 31 | +} |
| 32 | + |
4 | 33 | describe('countPathsToRoot', () => { |
5 | 34 | describe('basic', () => { |
6 | 35 | const depGraphData = helpers.loadFixture('plain-dep-graph.json'); |
@@ -122,4 +151,49 @@ describe('countPathsToRoot', () => { |
122 | 151 | ); |
123 | 152 | }); |
124 | 153 | }); |
| 154 | + |
| 155 | + describe('diamond chain (exponential paths)', () => { |
| 156 | + it('computes correct count for a small diamond chain', () => { |
| 157 | + const graph = buildDiamondChain(5); |
| 158 | + const leafPkg = { name: 'join-4', version: '1.0.0' }; |
| 159 | + expect(graph.countPathsToRoot(leafPkg)).toBe(Math.pow(2, 5)); |
| 160 | + }); |
| 161 | + |
| 162 | + it('handles 50-layer diamond chain (2^50 paths) efficiently', () => { |
| 163 | + const layers = 50; |
| 164 | + const graph = buildDiamondChain(layers); |
| 165 | + const leafPkg = { name: `join-${layers - 1}`, version: '1.0.0' }; |
| 166 | + |
| 167 | + const start = performance.now(); |
| 168 | + const count = graph.countPathsToRoot(leafPkg); |
| 169 | + const elapsed = performance.now() - start; |
| 170 | + |
| 171 | + expect(count).toBe(Math.pow(2, layers)); |
| 172 | + expect(elapsed).toBeLessThan(5000); |
| 173 | + }); |
| 174 | + |
| 175 | + it('respects limit on exponential-path graph', () => { |
| 176 | + const graph = buildDiamondChain(50); |
| 177 | + const leafPkg = { name: 'join-49', version: '1.0.0' }; |
| 178 | + expect(graph.countPathsToRoot(leafPkg, { limit: 100 })).toBe(100); |
| 179 | + }); |
| 180 | + |
| 181 | + it('counts scale linearly with layers, not exponentially', () => { |
| 182 | + const small = buildDiamondChain(100); |
| 183 | + const large = buildDiamondChain(400); |
| 184 | + |
| 185 | + const startSmall = performance.now(); |
| 186 | + small.countPathsToRoot({ name: 'join-99', version: '1.0.0' }); |
| 187 | + const timeSmall = performance.now() - startSmall || 1; |
| 188 | + |
| 189 | + const startLarge = performance.now(); |
| 190 | + large.countPathsToRoot({ name: 'join-399', version: '1.0.0' }); |
| 191 | + const timeLarge = performance.now() - startLarge || 1; |
| 192 | + |
| 193 | + // With DP, 4x the layers should take ~4x the time (linear). |
| 194 | + // With backtracking, it would be 2^300 times slower (never finish). |
| 195 | + // Use a generous bound to avoid flakiness. |
| 196 | + expect(timeLarge).toBeLessThan(timeSmall * 50); |
| 197 | + }); |
| 198 | + }); |
125 | 199 | }); |
0 commit comments