symbol-lookup.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Module-qualified symbol lookup (`stage_apply::run`, `Session.request`,
  3. * `configurator/stage_apply`).
  4. *
  5. * Pinned because the lookup vocabulary is what makes codegraph useful
  6. * in workspaces with same-named symbols across modules — Rust
  7. * sub-pipelines, Python `__init__.py` packages, Java packages, etc.
  8. * See #173 for the original report: a `run` function in
  9. * `src/configurator/stage_apply.rs` was indexed but `stage_apply::run`
  10. * returned "not found" because (a) FTS strips colons to nothing,
  11. * leaving a useless query, and (b) `matchesSymbol` only understood
  12. * `.`-style qualifiers.
  13. */
  14. import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
  15. import * as fs from 'fs';
  16. import * as path from 'path';
  17. import * as os from 'os';
  18. import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
  19. beforeAll(async () => {
  20. await initGrammars();
  21. await loadAllGrammars();
  22. });
  23. function hasSqliteBindings(): boolean {
  24. try {
  25. const { DatabaseSync } = require('node:sqlite');
  26. const db = new DatabaseSync(':memory:');
  27. db.close();
  28. return true;
  29. } catch {
  30. return false;
  31. }
  32. }
  33. const HAS_SQLITE = hasSqliteBindings();
  34. function tmpRoot(): string {
  35. return fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-symbol-lookup-'));
  36. }
  37. function rmTree(dir: string): void {
  38. if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  39. }
  40. async function buildRustWorkspace(): Promise<string> {
  41. const root = tmpRoot();
  42. const cfgDir = path.join(root, 'src', 'configurator');
  43. fs.mkdirSync(cfgDir, { recursive: true });
  44. fs.writeFileSync(
  45. path.join(root, 'Cargo.toml'),
  46. `[package]\nname = "fixture"\nversion = "0.1.0"\nedition = "2021"\n[lib]\npath = "src/lib.rs"\n`
  47. );
  48. fs.writeFileSync(path.join(root, 'src', 'lib.rs'), `pub mod configurator;\npub mod scheduler;\n`);
  49. fs.writeFileSync(
  50. path.join(cfgDir, 'mod.rs'),
  51. `pub mod stage_apply;\npub mod stage_detect;\n`
  52. );
  53. fs.writeFileSync(
  54. path.join(cfgDir, 'stage_apply.rs'),
  55. `pub async fn run() -> Result<(), ()> {\n render_and_write();\n Ok(())\n}\n\nfn render_and_write() {}\n`
  56. );
  57. fs.writeFileSync(
  58. path.join(cfgDir, 'stage_detect.rs'),
  59. `pub async fn run() -> Result<(), ()> { Ok(()) }\n`
  60. );
  61. fs.writeFileSync(
  62. path.join(root, 'src', 'scheduler.rs'),
  63. `pub fn run_due_tasks() -> Result<(), ()> { Ok(()) }\n`
  64. );
  65. return root;
  66. }
  67. describe.skipIf(!HAS_SQLITE)('matchesSymbol — module-qualified lookups (#173)', () => {
  68. let projectRoot: string;
  69. let cg: any;
  70. let handler: any;
  71. // findSymbolMatches returns ALL ranked matches; [0] is the resolved/picked one.
  72. let findSymbolMatches: (cg: any, s: string) => any[];
  73. let findAllSymbols: (cg: any, s: string) => { nodes: any[]; note: string };
  74. beforeEach(async () => {
  75. projectRoot = await buildRustWorkspace();
  76. const CodeGraph = (await import('../src/index')).default;
  77. const { ToolHandler } = await import('../src/mcp/tools');
  78. cg = CodeGraph.initSync(projectRoot, {
  79. config: { include: ['**/*.rs'], exclude: [] },
  80. });
  81. await cg.indexAll();
  82. handler = new ToolHandler(cg);
  83. findSymbolMatches = (handler as any).findSymbolMatches.bind(handler);
  84. findAllSymbols = (handler as any).findAllSymbols.bind(handler);
  85. });
  86. afterEach(() => {
  87. handler?.closeAll();
  88. cg?.destroy();
  89. rmTree(projectRoot);
  90. });
  91. it('resolves `stage_apply::run` to the run in stage_apply.rs (not stage_detect.rs)', () => {
  92. const matches = findSymbolMatches(cg, 'stage_apply::run');
  93. expect(matches.length).toBeGreaterThan(0);
  94. expect(matches[0]!.name).toBe('run');
  95. // Every match must be in stage_apply.rs — never stage_detect.rs.
  96. for (const n of matches) expect(n.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  97. });
  98. it('rejects `stage_apply::run` for the same-named function in a different module', () => {
  99. const all = findAllSymbols(cg, 'stage_apply::run');
  100. // All returned nodes must be in stage_apply.rs — never in stage_detect.rs
  101. for (const node of all.nodes) {
  102. expect(node.filePath).toMatch(/stage_apply\.rs$/);
  103. }
  104. expect(all.nodes.length).toBeGreaterThan(0);
  105. });
  106. it('resolves `configurator::stage_apply::run` (multi-level qualifier)', () => {
  107. const matches = findSymbolMatches(cg, 'configurator::stage_apply::run');
  108. expect(matches.length).toBeGreaterThan(0);
  109. expect(matches[0]!.name).toBe('run');
  110. expect(matches[0]!.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  111. });
  112. it('resolves `crate::configurator::stage_apply::run` (Rust path prefix stripped)', () => {
  113. const matches = findSymbolMatches(cg, 'crate::configurator::stage_apply::run');
  114. expect(matches.length).toBeGreaterThan(0);
  115. expect(matches[0]!.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  116. });
  117. it('resolves `configurator/stage_apply` (slash qualifier)', () => {
  118. const matches = findSymbolMatches(cg, 'configurator/stage_apply/run');
  119. expect(matches.length).toBeGreaterThan(0);
  120. expect(matches[0]!.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  121. });
  122. it('does not silently collide bare `run` with `run_due_tasks`', () => {
  123. const matches = findSymbolMatches(cg, 'run');
  124. expect(matches.length).toBeGreaterThan(0);
  125. // Whatever it picks, every match must be an exact-name match, not a partial.
  126. for (const n of matches) expect(n.name).toBe('run');
  127. });
  128. it('aggregates all bare-name `run` matches across modules', () => {
  129. const all = findAllSymbols(cg, 'run');
  130. const names = all.nodes.map((n: any) => n.name);
  131. expect(names.every((n: string) => n === 'run')).toBe(true);
  132. expect(all.nodes.length).toBeGreaterThanOrEqual(2); // stage_apply + stage_detect
  133. // The note should call out the ambiguity.
  134. expect(all.note).toMatch(/Aggregated|symbols named "run"/);
  135. });
  136. it('still returns nothing for genuinely unknown qualified lookups', () => {
  137. const matches = findSymbolMatches(cg, 'stage_apply::nonexistent_fn');
  138. expect(matches.length).toBe(0);
  139. });
  140. it('codegraph_node with a `file` hint pins an overloaded name to that file', async () => {
  141. // `run` is defined in BOTH stage_apply.rs and stage_detect.rs. A bare lookup
  142. // returns both; the `file` hint narrows to the one the caller saw in a trail.
  143. const res = await handler.execute('codegraph_node', {
  144. symbol: 'run',
  145. includeCode: true,
  146. file: 'stage_detect.rs',
  147. });
  148. const text = res.content?.[0]?.text ?? '';
  149. expect(text).toMatch(/stage_detect\.rs/);
  150. expect(text).not.toMatch(/stage_apply\.rs/);
  151. });
  152. });
  153. describe.skipIf(!HAS_SQLITE)('matchesSymbol — dotted lookups (regression for #173 fix)', () => {
  154. let projectRoot: string;
  155. let cg: any;
  156. let handler: any;
  157. let findSymbolMatches: (cg: any, s: string) => any[];
  158. beforeEach(async () => {
  159. projectRoot = tmpRoot();
  160. const src = path.join(projectRoot, 'src');
  161. fs.mkdirSync(src, { recursive: true });
  162. fs.writeFileSync(
  163. path.join(src, 'session.ts'),
  164. `export class Session {\n request(): void { fetch('x'); }\n}\nexport function request(): void {}\n`
  165. );
  166. const CodeGraph = (await import('../src/index')).default;
  167. const { ToolHandler } = await import('../src/mcp/tools');
  168. cg = CodeGraph.initSync(projectRoot, {
  169. config: { include: ['src/**/*.ts'], exclude: [] },
  170. });
  171. await cg.indexAll();
  172. handler = new ToolHandler(cg);
  173. findSymbolMatches = (handler as any).findSymbolMatches.bind(handler);
  174. });
  175. afterEach(() => {
  176. handler?.closeAll();
  177. cg?.destroy();
  178. rmTree(projectRoot);
  179. });
  180. it('`Session.request` resolves to the method, not the bare function', () => {
  181. const matches = findSymbolMatches(cg, 'Session.request');
  182. expect(matches.length).toBeGreaterThan(0);
  183. expect(matches[0]!.kind).toBe('method');
  184. expect(matches[0]!.qualifiedName).toContain('Session::request');
  185. });
  186. it('codegraph_node on an ambiguous bare name returns ALL overloads with bodies (no guess)', async () => {
  187. // `request` is BOTH a method (Session.request) and a free function. The old
  188. // behavior returned one + a dead-end "Others:" note, forcing a Read to get
  189. // the other overload; now both bodies come back in one call.
  190. const res = await handler.execute('codegraph_node', { symbol: 'request', includeCode: true });
  191. const text = res.content?.[0]?.text ?? '';
  192. expect(text).toContain('2 definitions named "request"');
  193. // Both definitions are rendered (method + function), each with a Location.
  194. expect(text).toMatch(/\(method\)/);
  195. expect(text).toMatch(/\(function\)/);
  196. expect((text.match(/\*\*Location:\*\*/g) || []).length).toBeGreaterThanOrEqual(2);
  197. });
  198. });