Skip to content

Commit 0f3d17a

Browse files
thymikeeclaude
andcommitted
test(depgraph): pin computeLevels on a diamond and on a value cycle
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 136eb63 commit 0f3d17a

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎scripts/depgraph/model.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
buildGraph,
1313
collapseEdges,
1414
collectCycles,
15+
computeLevels,
1516
markTransitivelyReachableEdges,
1617
typeInversionsByPair,
1718
} from './model.ts';
@@ -270,6 +271,47 @@ test('buildGraph still reports a value cycle and leaves edges unmarked', () => {
270271
);
271272
});
272273

274+
test('computeLevels takes the longest value path to a sink and ignores type edges', () => {
275+
const files = sources({
276+
'src/core/a.ts': [
277+
"import { b } from './b.ts';",
278+
"import { c } from './c.ts';",
279+
"import { d } from './d.ts';",
280+
"import type { E } from './e.ts';",
281+
'export const a = [b, c, d];',
282+
].join('\n'),
283+
'src/core/b.ts': "import { d } from './d.ts';\nexport const b = d;",
284+
'src/core/c.ts': "import { d } from './d.ts';\nexport const c = d;",
285+
'src/core/d.ts': 'export const d = 1;',
286+
'src/core/e.ts': "import type { A } from './a.ts';\nexport type E = A;",
287+
'src/core/lone.ts': 'export const lone = 1;',
288+
});
289+
const graph = buildGraph(files, resolveImportEdges(files));
290+
291+
assert.deepEqual(Object.fromEntries(computeLevels(graph.nodes, graph.edges)), {
292+
'src/core/a.ts': 2,
293+
'src/core/b.ts': 1,
294+
'src/core/c.ts': 1,
295+
'src/core/d.ts': 0,
296+
'src/core/e.ts': 0,
297+
'src/core/lone.ts': 0,
298+
});
299+
});
300+
301+
test('computeLevels stays finite on a value cycle, where the closing edge adds no height', () => {
302+
const files = sources({
303+
...Object.fromEntries(valueCycleFixture()),
304+
'src/core/z.ts': "import { a } from './a.ts';\nexport const z = a;",
305+
});
306+
const graph = buildGraph(files, resolveImportEdges(files));
307+
308+
assert.deepEqual(Object.fromEntries(computeLevels(graph.nodes, graph.edges)), {
309+
'src/core/a.ts': 1,
310+
'src/core/b.ts': 0,
311+
'src/core/z.ts': 2,
312+
});
313+
});
314+
273315
test('a type-only shortcut is never flagged against a value path', () => {
274316
const edges = collapseEdges(
275317
resolveImportEdges(

0 commit comments

Comments
 (0)