|
| 1 | +/** |
| 2 | + * Integration test for #1317: prototype-based method call resolution. |
| 3 | + * |
| 4 | + * Verifies that the call-resolver correctly builds edges for: |
| 5 | + * 1. `Foo.prototype.bar = function(){}` — direct method definition |
| 6 | + * 2. `(new Foo).bar()` — inline new-expression receiver |
| 7 | + */ |
| 8 | + |
| 9 | +import fs from 'node:fs'; |
| 10 | +import os from 'node:os'; |
| 11 | +import path from 'node:path'; |
| 12 | +import Database from 'better-sqlite3'; |
| 13 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 14 | +import { buildGraph } from '../../src/domain/graph/builder.js'; |
| 15 | + |
| 16 | +const FIXTURE = { |
| 17 | + 'proto.js': ` |
| 18 | +function Animal(name) { |
| 19 | + this.name = name; |
| 20 | +} |
| 21 | +Animal.prototype.speak = function() { |
| 22 | + return this.name + ' speaks'; |
| 23 | +}; |
| 24 | +
|
| 25 | +function Dog(name) { |
| 26 | + Animal.call(this, name); |
| 27 | +} |
| 28 | +Dog.prototype = Object.create(Animal.prototype); |
| 29 | +Dog.prototype.bark = function() { |
| 30 | + return this.name + ' barks'; |
| 31 | +}; |
| 32 | +
|
| 33 | +function makeAndBark() { |
| 34 | + // inline new-expression receiver: (new Dog('Rex')).bark() |
| 35 | + return (new Dog('Rex')).bark(); |
| 36 | +} |
| 37 | +
|
| 38 | +const d = new Dog('Buddy'); |
| 39 | +d.speak(); |
| 40 | +d.bark(); |
| 41 | +`, |
| 42 | +}; |
| 43 | + |
| 44 | +let tmpDir: string; |
| 45 | + |
| 46 | +beforeAll(async () => { |
| 47 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1317-')); |
| 48 | + for (const [rel, content] of Object.entries(FIXTURE)) { |
| 49 | + fs.writeFileSync(path.join(tmpDir, rel), content); |
| 50 | + } |
| 51 | + await buildGraph(tmpDir, { incremental: false, skipRegistry: true }); |
| 52 | +}); |
| 53 | + |
| 54 | +afterAll(() => { |
| 55 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 56 | +}); |
| 57 | + |
| 58 | +function readCallEdges(dbPath: string) { |
| 59 | + const db = new Database(dbPath, { readonly: true }); |
| 60 | + try { |
| 61 | + return db |
| 62 | + .prepare( |
| 63 | + `SELECT n1.name AS src, n2.name AS tgt |
| 64 | + FROM edges e |
| 65 | + JOIN nodes n1 ON e.source_id = n1.id |
| 66 | + JOIN nodes n2 ON e.target_id = n2.id |
| 67 | + WHERE e.kind = 'calls' |
| 68 | + ORDER BY n1.name, n2.name`, |
| 69 | + ) |
| 70 | + .all() as Array<{ src: string; tgt: string }>; |
| 71 | + } finally { |
| 72 | + db.close(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +describe('prototype method resolution (#1317)', () => { |
| 77 | + it('emits Dog.bark as a method definition', () => { |
| 78 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 79 | + const db = new Database(dbPath, { readonly: true }); |
| 80 | + try { |
| 81 | + const node = db.prepare(`SELECT name, kind FROM nodes WHERE name = 'Dog.bark'`).get() as |
| 82 | + | { name: string; kind: string } |
| 83 | + | undefined; |
| 84 | + expect(node).toBeDefined(); |
| 85 | + expect(node?.kind).toBe('method'); |
| 86 | + } finally { |
| 87 | + db.close(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it('resolves d.bark() call to Dog.bark via typeMap receiver type', () => { |
| 92 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 93 | + const edges = readCallEdges(dbPath); |
| 94 | + const barkEdge = edges.find((e) => e.tgt === 'Dog.bark'); |
| 95 | + expect(barkEdge).toBeDefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('resolves (new Dog(...)).bark() inline-new receiver call to Dog.bark', () => { |
| 99 | + const dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); |
| 100 | + const edges = readCallEdges(dbPath); |
| 101 | + // makeAndBark calls (new Dog('Rex')).bark() — inline new receiver |
| 102 | + const inlineNewEdge = edges.find((e) => e.src === 'makeAndBark' && e.tgt === 'Dog.bark'); |
| 103 | + expect(inlineNewEdge).toBeDefined(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments