mcp-callers-truncation.test.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * The MCP `codegraph_callers` / `codegraph_callees` answers say when their
  3. * `limit` cut the list (#1639, #1674). A capped list with no marker reads as
  4. * the complete set, and an agent under-counts "who calls this" from it.
  5. */
  6. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  7. import * as fs from 'fs';
  8. import * as path from 'path';
  9. import * as os from 'os';
  10. import { CodeGraph } from '../src';
  11. import { ToolHandler } from '../src/mcp/tools';
  12. import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
  13. let tmpDir: string;
  14. let cg: CodeGraph;
  15. let handler: ToolHandler;
  16. const text = async (tool: string, args: Record<string, unknown>): Promise<string> => {
  17. const res = await handler.execute(tool, args);
  18. return res.content?.[0]?.text ?? '';
  19. };
  20. const CALLERS = 25;
  21. beforeAll(async () => {
  22. await initGrammars();
  23. await loadAllGrammars();
  24. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1674-'));
  25. fs.mkdirSync(path.join(tmpDir, 'src'));
  26. // `warm` lives in a file of another name: one definition, the flat list.
  27. fs.writeFileSync(path.join(tmpDir, 'src', 'target.ts'), 'export function warm(n: number): number { return n; }\n');
  28. fs.writeFileSync(
  29. path.join(tmpDir, 'src', 'callers.ts'),
  30. "import { warm } from './target';\n" +
  31. Array.from({ length: CALLERS }, (_, i) => `export function caller${i}(): number { return warm(${i}); }`).join('\n') +
  32. '\n'
  33. );
  34. // `hot` shares its name with its file, so the answer groups per definition.
  35. fs.writeFileSync(path.join(tmpDir, 'src', 'hot.ts'), 'export function hot(n: number): number { return n; }\n');
  36. fs.writeFileSync(
  37. path.join(tmpDir, 'src', 'hot-callers.ts'),
  38. "import { hot } from './hot';\n" +
  39. Array.from({ length: CALLERS }, (_, i) => `export function hotCaller${i}(): number { return hot(${i}); }`).join('\n') +
  40. '\n'
  41. );
  42. fs.writeFileSync(
  43. path.join(tmpDir, 'src', 'fan.ts'),
  44. Array.from({ length: CALLERS }, (_, i) => `export function helper${i}(): number { return ${i}; }`).join('\n') +
  45. `\nexport function fanout(): number { return ${Array.from({ length: CALLERS }, (_, i) => `helper${i}()`).join(' + ')}; }\n`
  46. );
  47. cg = CodeGraph.initSync(tmpDir);
  48. await cg.indexAll();
  49. handler = new ToolHandler(cg);
  50. });
  51. afterAll(() => {
  52. cg.destroy();
  53. fs.rmSync(tmpDir, { recursive: true, force: true });
  54. });
  55. describe('codegraph_callers truncation', () => {
  56. it('says how many callers the default limit hid', async () => {
  57. const out = await text('codegraph_callers', { symbol: 'warm' });
  58. // The importing file counts as a caller too, so the total is at least CALLERS.
  59. const m = out.match(/Showing 20 of (\d+) callers; pass `limit`/);
  60. expect(m).not.toBeNull();
  61. expect(Number(m![1])).toBeGreaterThanOrEqual(CALLERS);
  62. expect(out.match(/^- caller\d+ /gm)?.length).toBe(20);
  63. });
  64. it('is silent when the list is complete', async () => {
  65. const out = await text('codegraph_callers', { symbol: 'warm', limit: 100 });
  66. expect(out).not.toContain('Showing');
  67. expect(out.match(/^- caller\d+ /gm)?.length).toBe(CALLERS);
  68. });
  69. it('marks the cut inside each per-definition section too', async () => {
  70. const out = await text('codegraph_callers', { symbol: 'hot' });
  71. expect(out).toContain('distinct definitions');
  72. expect(out).toMatch(/- … \+\d+ more \(pass `limit` to widen\)/);
  73. expect(await text('codegraph_callers', { symbol: 'hot', limit: 100 })).not.toContain('more (pass');
  74. });
  75. });
  76. describe('codegraph_callees truncation', () => {
  77. it('says how many callees the default limit hid', async () => {
  78. const out = await text('codegraph_callees', { symbol: 'fanout' });
  79. const m = out.match(/Showing 20 of (\d+) callees; pass `limit`/);
  80. expect(m).not.toBeNull();
  81. expect(Number(m![1])).toBe(CALLERS);
  82. });
  83. it('is silent when the list is complete', async () => {
  84. const out = await text('codegraph_callees', { symbol: 'fanout', limit: 100 });
  85. expect(out).not.toContain('Showing');
  86. });
  87. });