explore-blast-radius.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * codegraph_explore blast-radius section.
  3. *
  4. * explore now appends a compact, always-on "Blast radius" for the entry
  5. * symbols: who depends on each (locations only — no source) and which test
  6. * files cover it, so the agent knows what to update/verify before editing
  7. * without a separate impact call. Symbols with no dependents are skipped, and
  8. * the section is omitted entirely when nothing qualifies.
  9. */
  10. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  11. import * as fs from 'fs';
  12. import * as path from 'path';
  13. import * as os from 'os';
  14. import CodeGraph from '../src/index';
  15. import { ToolHandler } from '../src/mcp/tools';
  16. describe('codegraph_explore — blast radius', () => {
  17. let testDir: string;
  18. let cg: CodeGraph;
  19. let handler: ToolHandler;
  20. beforeEach(async () => {
  21. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-blast-'));
  22. const src = path.join(testDir, 'src');
  23. fs.mkdirSync(src, { recursive: true });
  24. // `target` is depended on by a sibling (caller) and a test file.
  25. fs.writeFileSync(
  26. path.join(src, 'feature.ts'),
  27. `export function target() { return 1; }\n` +
  28. `export function caller() { return target(); }\n`,
  29. );
  30. fs.writeFileSync(
  31. path.join(src, 'feature.test.ts'),
  32. `import { target } from './feature';\n` +
  33. `export function checkTarget() { return target(); }\n`,
  34. );
  35. // A leaf with no dependents — must NOT show up in the blast radius.
  36. fs.writeFileSync(
  37. path.join(src, 'leaf.ts'),
  38. `export function lonelyLeaf() { return 42; }\n`,
  39. );
  40. cg = CodeGraph.initSync(testDir, { config: { include: ['**/*.ts'], exclude: [] } });
  41. await cg.indexAll();
  42. handler = new ToolHandler(cg);
  43. });
  44. afterEach(() => {
  45. if (cg) cg.destroy();
  46. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  47. });
  48. it('lists dependents (locations only) and covering tests for an entry symbol', async () => {
  49. const res = await handler.execute('codegraph_explore', { query: 'target' });
  50. const text = res.content[0].text;
  51. expect(text).toContain('**Blast radius');
  52. expect(text).toContain('`target`');
  53. expect(text).toMatch(/caller/); // a caller count is reported
  54. // It names WHERE (the caller file) — not the caller's source body.
  55. expect(text).toContain('feature.ts');
  56. // Test coverage is surfaced (either the covering test file, or the warning).
  57. expect(text).toMatch(/tests:.*feature\.test\.ts|no covering tests/);
  58. });
  59. it('omits symbols that have no dependents from the blast radius', async () => {
  60. const res = await handler.execute('codegraph_explore', { query: 'lonelyLeaf' });
  61. const text = res.content[0].text;
  62. // lonelyLeaf has zero callers — it must never appear under a blast-radius bullet.
  63. expect(text).not.toMatch(/Blast radius[\s\S]*`lonelyLeaf`/);
  64. });
  65. });