|
| 1 | +/** |
| 2 | + * this-dispatch scope: same-file fallback must not emit false-positive edges |
| 3 | + * to methods in unrelated classes. |
| 4 | + * |
| 5 | + * Fixtures: |
| 6 | + * - shapes.ts — three unrelated classes (Shape, Calculator, Formatter) all |
| 7 | + * defining area(). this.area() inside Shape.describe must resolve only to |
| 8 | + * Shape.area (multi-match disambiguation path). |
| 9 | + * - single-sibling.ts — two classes: Caller (no area()) and Sibling (area()). |
| 10 | + * this.area() inside Caller.run must NOT resolve to Sibling.area even though |
| 11 | + * it is the only method with that suffix in the file (single-match path). |
| 12 | + * |
| 13 | + * Covers the Rust edge_builder fix in issue #1324. |
| 14 | + */ |
| 15 | + |
| 16 | +import fs from 'node:fs'; |
| 17 | +import os from 'node:os'; |
| 18 | +import path from 'node:path'; |
| 19 | +import Database from 'better-sqlite3'; |
| 20 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 21 | +import { buildGraph } from '../../src/domain/graph/builder.js'; |
| 22 | +import type { EngineMode } from '../../src/types.js'; |
| 23 | + |
| 24 | +const FIXTURE_DIR = path.join(import.meta.dirname, '..', 'fixtures', 'this-dispatch-scope'); |
| 25 | + |
| 26 | +interface CallEdgeRow { |
| 27 | + caller_name: string; |
| 28 | + callee_name: string; |
| 29 | +} |
| 30 | + |
| 31 | +function readCallEdges(dbPath: string): CallEdgeRow[] { |
| 32 | + const db = new Database(dbPath, { readonly: true }); |
| 33 | + try { |
| 34 | + return db |
| 35 | + .prepare( |
| 36 | + `SELECT n1.name AS caller_name, n2.name AS callee_name |
| 37 | + FROM edges e |
| 38 | + JOIN nodes n1 ON e.source_id = n1.id |
| 39 | + JOIN nodes n2 ON e.target_id = n2.id |
| 40 | + WHERE e.kind = 'calls'`, |
| 41 | + ) |
| 42 | + .all() as CallEdgeRow[]; |
| 43 | + } finally { |
| 44 | + db.close(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const ENGINES: EngineMode[] = ['wasm', 'native']; |
| 49 | + |
| 50 | +describe.each(ENGINES)('this-dispatch scope (%s)', (engine) => { |
| 51 | + let tmpDir: string; |
| 52 | + let callEdges: CallEdgeRow[] = []; |
| 53 | + |
| 54 | + beforeAll(async () => { |
| 55 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), `codegraph-this-scope-${engine}-`)); |
| 56 | + fs.cpSync(FIXTURE_DIR, tmpDir, { recursive: true }); |
| 57 | + await buildGraph(tmpDir, { incremental: false, skipRegistry: true, engine }); |
| 58 | + callEdges = readCallEdges(path.join(tmpDir, '.codegraph', 'graph.db')); |
| 59 | + }, 60_000); |
| 60 | + |
| 61 | + afterAll(() => { |
| 62 | + if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('emits Shape.describe → Shape.area (correct this-dispatch)', () => { |
| 66 | + const edge = callEdges.find( |
| 67 | + (e) => e.caller_name === 'Shape.describe' && e.callee_name === 'Shape.area', |
| 68 | + ); |
| 69 | + expect( |
| 70 | + edge, |
| 71 | + `Expected Shape.describe → Shape.area edge.\nActual edges:\n${JSON.stringify(callEdges, null, 2)}`, |
| 72 | + ).toBeDefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + // Native binary v3.11.2 does not include the edge_builder.rs fix for issue #1324 yet. |
| 76 | + // These assertions are active for WASM and will be re-enabled for native once a new |
| 77 | + // binary is published that includes the Rust fix. |
| 78 | + if (engine === 'native') { |
| 79 | + it.todo('does NOT emit Shape.describe → Calculator.area (native binary gap #1324)'); |
| 80 | + it.todo('does NOT emit Shape.describe → Formatter.area (native binary gap #1324)'); |
| 81 | + it.todo( |
| 82 | + 'does NOT emit Caller.run → Sibling.area (single-match false-positive, native binary gap #1324)', |
| 83 | + ); |
| 84 | + } else { |
| 85 | + it('does NOT emit Shape.describe → Calculator.area (unrelated class, same method name)', () => { |
| 86 | + const edge = callEdges.find( |
| 87 | + (e) => e.caller_name === 'Shape.describe' && e.callee_name === 'Calculator.area', |
| 88 | + ); |
| 89 | + expect( |
| 90 | + edge, |
| 91 | + `Expected NO Shape.describe → Calculator.area edge (false-positive from same-file scan).\nActual edges:\n${JSON.stringify(callEdges, null, 2)}`, |
| 92 | + ).toBeUndefined(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('does NOT emit Shape.describe → Formatter.area (unrelated class, same method name)', () => { |
| 96 | + const edge = callEdges.find( |
| 97 | + (e) => e.caller_name === 'Shape.describe' && e.callee_name === 'Formatter.area', |
| 98 | + ); |
| 99 | + expect( |
| 100 | + edge, |
| 101 | + `Expected NO Shape.describe → Formatter.area edge (false-positive from same-file scan).\nActual edges:\n${JSON.stringify(callEdges, null, 2)}`, |
| 102 | + ).toBeUndefined(); |
| 103 | + }); |
| 104 | + |
| 105 | + // single-sibling.ts: only one class (Sibling) has area(); Caller does not. |
| 106 | + // The single-match arm must still check the caller's own class — Caller.run |
| 107 | + // must not gain a false edge to Sibling.area. |
| 108 | + it('does NOT emit Caller.run → Sibling.area (single-match false-positive, same-file scan)', () => { |
| 109 | + const edge = callEdges.find( |
| 110 | + (e) => e.caller_name === 'Caller.run' && e.callee_name === 'Sibling.area', |
| 111 | + ); |
| 112 | + expect( |
| 113 | + edge, |
| 114 | + `Expected NO Caller.run → Sibling.area edge (false-positive from single-match suffix scan).\nActual edges:\n${JSON.stringify(callEdges, null, 2)}`, |
| 115 | + ).toBeUndefined(); |
| 116 | + }); |
| 117 | + } |
| 118 | +}); |
0 commit comments