1
0

symbol-lookup.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. let findSymbol: (cg: any, s: string) => { node: any; note: string } | null;
  72. let findAllSymbols: (cg: any, s: string) => { nodes: any[]; note: string };
  73. beforeEach(async () => {
  74. projectRoot = await buildRustWorkspace();
  75. const CodeGraph = (await import('../src/index')).default;
  76. const { ToolHandler } = await import('../src/mcp/tools');
  77. cg = CodeGraph.initSync(projectRoot, {
  78. config: { include: ['**/*.rs'], exclude: [] },
  79. });
  80. await cg.indexAll();
  81. handler = new ToolHandler(cg);
  82. findSymbol = (handler as any).findSymbol.bind(handler);
  83. findAllSymbols = (handler as any).findAllSymbols.bind(handler);
  84. });
  85. afterEach(() => {
  86. handler?.closeAll();
  87. cg?.destroy();
  88. rmTree(projectRoot);
  89. });
  90. it('resolves `stage_apply::run` to the run in stage_apply.rs (not stage_detect.rs)', () => {
  91. const match = findSymbol(cg, 'stage_apply::run');
  92. expect(match).not.toBeNull();
  93. expect(match!.node.name).toBe('run');
  94. expect(match!.node.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  95. });
  96. it('rejects `stage_apply::run` for the same-named function in a different module', () => {
  97. const all = findAllSymbols(cg, 'stage_apply::run');
  98. // All returned nodes must be in stage_apply.rs — never in stage_detect.rs
  99. for (const node of all.nodes) {
  100. expect(node.filePath).toMatch(/stage_apply\.rs$/);
  101. }
  102. expect(all.nodes.length).toBeGreaterThan(0);
  103. });
  104. it('resolves `configurator::stage_apply::run` (multi-level qualifier)', () => {
  105. const match = findSymbol(cg, 'configurator::stage_apply::run');
  106. expect(match).not.toBeNull();
  107. expect(match!.node.name).toBe('run');
  108. expect(match!.node.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  109. });
  110. it('resolves `crate::configurator::stage_apply::run` (Rust path prefix stripped)', () => {
  111. const match = findSymbol(cg, 'crate::configurator::stage_apply::run');
  112. expect(match).not.toBeNull();
  113. expect(match!.node.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  114. });
  115. it('resolves `configurator/stage_apply` (slash qualifier)', () => {
  116. const match = findSymbol(cg, 'configurator/stage_apply/run');
  117. expect(match).not.toBeNull();
  118. expect(match!.node.filePath).toMatch(/configurator\/stage_apply\.rs$/);
  119. });
  120. it('does not silently collide bare `run` with `run_due_tasks`', () => {
  121. const match = findSymbol(cg, 'run');
  122. expect(match).not.toBeNull();
  123. // Whatever it picks, it must be an exact-name match, not a partial.
  124. expect(match!.node.name).toBe('run');
  125. });
  126. it('aggregates all bare-name `run` matches across modules', () => {
  127. const all = findAllSymbols(cg, 'run');
  128. const names = all.nodes.map((n: any) => n.name);
  129. expect(names.every((n: string) => n === 'run')).toBe(true);
  130. expect(all.nodes.length).toBeGreaterThanOrEqual(2); // stage_apply + stage_detect
  131. // The note should call out the ambiguity.
  132. expect(all.note).toMatch(/Aggregated|symbols named "run"/);
  133. });
  134. it('still returns null for genuinely unknown qualified lookups', () => {
  135. const match = findSymbol(cg, 'stage_apply::nonexistent_fn');
  136. expect(match).toBeNull();
  137. });
  138. });
  139. describe.skipIf(!HAS_SQLITE)('matchesSymbol — dotted lookups (regression for #173 fix)', () => {
  140. let projectRoot: string;
  141. let cg: any;
  142. let handler: any;
  143. let findSymbol: (cg: any, s: string) => { node: any; note: string } | null;
  144. beforeEach(async () => {
  145. projectRoot = tmpRoot();
  146. const src = path.join(projectRoot, 'src');
  147. fs.mkdirSync(src, { recursive: true });
  148. fs.writeFileSync(
  149. path.join(src, 'session.ts'),
  150. `export class Session {\n request(): void {}\n}\nexport function request(): void {}\n`
  151. );
  152. const CodeGraph = (await import('../src/index')).default;
  153. const { ToolHandler } = await import('../src/mcp/tools');
  154. cg = CodeGraph.initSync(projectRoot, {
  155. config: { include: ['src/**/*.ts'], exclude: [] },
  156. });
  157. await cg.indexAll();
  158. handler = new ToolHandler(cg);
  159. findSymbol = (handler as any).findSymbol.bind(handler);
  160. });
  161. afterEach(() => {
  162. handler?.closeAll();
  163. cg?.destroy();
  164. rmTree(projectRoot);
  165. });
  166. it('`Session.request` resolves to the method, not the bare function', () => {
  167. const match = findSymbol(cg, 'Session.request');
  168. expect(match).not.toBeNull();
  169. expect(match!.node.kind).toBe('method');
  170. expect(match!.node.qualifiedName).toContain('Session::request');
  171. });
  172. });