|
| 1 | +/** |
| 2 | + * Unit tests for the innermost-enclosing-scope picker. Documents the |
| 3 | + * span-ASC-plus-containment-check invariant after a reviewer flagged |
| 4 | + * the inline copies of this logic as potentially buggy. Spoiler: not |
| 5 | + * a bug — but the test cases are useful regardless to lock the |
| 6 | + * invariant down. |
| 7 | + */ |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | +import { sortScopesBySpan, findEnclosingNode, type ScopeNode } from '../src/index-hooks/enclosing.js'; |
| 10 | + |
| 11 | +function pick(nodes: ScopeNode[], line: number): string | null { |
| 12 | + return findEnclosingNode(sortScopesBySpan(nodes), line); |
| 13 | +} |
| 14 | + |
| 15 | +describe('findEnclosingNode', () => { |
| 16 | + it('returns null when no scope contains the line', () => { |
| 17 | + expect(pick([ |
| 18 | + { id: 'f1', start: 1, end: 10 }, |
| 19 | + { id: 'f2', start: 20, end: 30 }, |
| 20 | + ], 15)).toBeNull(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns the only matching scope when scopes are adjacent', () => { |
| 24 | + const scopes: ScopeNode[] = [ |
| 25 | + { id: 'f1', start: 1, end: 10 }, |
| 26 | + { id: 'f2', start: 20, end: 30 }, |
| 27 | + ]; |
| 28 | + expect(pick(scopes, 5)).toBe('f1'); |
| 29 | + expect(pick(scopes, 25)).toBe('f2'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('picks the innermost scope on simple nesting (class > method)', () => { |
| 33 | + expect(pick([ |
| 34 | + { id: 'A', start: 10, end: 100 }, |
| 35 | + { id: 'm1', start: 20, end: 30 }, |
| 36 | + ], 25)).toBe('m1'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('picks the outer scope when only it contains the line', () => { |
| 40 | + expect(pick([ |
| 41 | + { id: 'A', start: 10, end: 100 }, |
| 42 | + { id: 'm1', start: 20, end: 30 }, |
| 43 | + ], 50)).toBe('A'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('picks the innermost scope on deep nesting (3 levels)', () => { |
| 47 | + expect(pick([ |
| 48 | + { id: 'outer', start: 1, end: 100 }, |
| 49 | + { id: 'middle', start: 10, end: 50 }, |
| 50 | + { id: 'inner', start: 20, end: 30 }, |
| 51 | + ], 25)).toBe('inner'); |
| 52 | + expect(pick([ |
| 53 | + { id: 'outer', start: 1, end: 100 }, |
| 54 | + { id: 'middle', start: 10, end: 50 }, |
| 55 | + { id: 'inner', start: 20, end: 30 }, |
| 56 | + ], 35)).toBe('middle'); |
| 57 | + expect(pick([ |
| 58 | + { id: 'outer', start: 1, end: 100 }, |
| 59 | + { id: 'middle', start: 10, end: 50 }, |
| 60 | + { id: 'inner', start: 20, end: 30 }, |
| 61 | + ], 75)).toBe('outer'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('REGRESSION: an unrelated tiny function does NOT win over the real enclosing scope', () => { |
| 65 | + // Reviewer's hypothesised bug case: a top-level function with a |
| 66 | + // smaller span than the enclosing class. Sort puts it first, but |
| 67 | + // the containment check filters it out — outer class wins |
| 68 | + // correctly. This test pins the invariant down. |
| 69 | + expect(pick([ |
| 70 | + { id: 'class_A', start: 1, end: 100 }, // span 99 |
| 71 | + { id: 'method_m', start: 20, end: 30 }, // span 10 (in A) |
| 72 | + { id: 'unrelated_f', start: 200, end: 205 }, // span 5 (not in A) |
| 73 | + ], 60)).toBe('class_A'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('handles ties on span length deterministically (first by stable sort)', () => { |
| 77 | + // When two scopes have identical spans and both contain the line, |
| 78 | + // either is acceptable as "innermost." In practice nested scopes |
| 79 | + // can't tie (parent strictly contains child by ≥1 line). But two |
| 80 | + // sibling adjacent scopes can tie — only one will contain the line. |
| 81 | + const result = pick([ |
| 82 | + { id: 'a', start: 1, end: 10 }, |
| 83 | + { id: 'b', start: 11, end: 20 }, |
| 84 | + ], 5); |
| 85 | + expect(result).toBe('a'); |
| 86 | + }); |
| 87 | +}); |
0 commit comments