1
0

mcp-staleness-banner.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Per-file staleness banner on MCP tool responses (issue #403).
  3. *
  4. * The watcher tracks every file event since the last successful sync; the
  5. * tool dispatcher intersects "files referenced in this response" with that
  6. * pending set and prepends a banner ("⚠️ Some files referenced below were
  7. * edited since the last index sync…") plus an optional footer ("(Note: N
  8. * file(s) elsewhere in this project are pending index sync…)").
  9. *
  10. * No auto-flush, no static wait — the response is instant and the agent
  11. * decides whether to Read the specific stale file. These tests exercise
  12. * the full real path: real CodeGraph index + real ToolHandler.execute().
  13. *
  14. * **Event delivery uses a synthetic seam** (`__emitWatchEventForTests`): the
  15. * real native fs.watch (FSEvents/inotify) delivery is non-deterministic under
  16. * parallel vitest execution and produced a consistent ~30% failure rate on
  17. * these tests when run inside the full suite. The seam drives the watcher's
  18. * pending-set pipeline directly so the tests synthesize file events
  19. * deterministically. The watcher's actual debounce timer (real setTimeout) is
  20. * left untouched.
  21. */
  22. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  23. import * as fs from 'fs';
  24. import * as path from 'path';
  25. import * as os from 'os';
  26. import CodeGraph from '../src/index';
  27. import { ToolHandler } from '../src/mcp/tools';
  28. import { __emitWatchEventForTests } from '../src/sync/watcher';
  29. function waitFor(condition: () => boolean, timeoutMs = 2000, intervalMs = 25): Promise<void> {
  30. return new Promise((resolve, reject) => {
  31. const start = Date.now();
  32. const tick = () => {
  33. if (condition()) return resolve();
  34. if (Date.now() - start > timeoutMs) return reject(new Error('waitFor timed out'));
  35. setTimeout(tick, intervalMs);
  36. };
  37. tick();
  38. });
  39. }
  40. describe('MCP staleness banner', () => {
  41. let testDir: string;
  42. let cg: CodeGraph;
  43. let handler: ToolHandler;
  44. beforeEach(async () => {
  45. testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-stale-banner-'));
  46. fs.mkdirSync(path.join(testDir, 'src'));
  47. // Three isolated files with no cross-references — keeps each test's
  48. // "which path does the response mention?" assertion unambiguous. If the
  49. // files shared imports/calls, codegraph_search responses would surface
  50. // multiple file paths and the banner-vs-footer split would be racy.
  51. fs.writeFileSync(
  52. path.join(testDir, 'src', 'alpha-only.ts'),
  53. 'export function alphaOnly() { return 1; }\n',
  54. );
  55. fs.writeFileSync(
  56. path.join(testDir, 'src', 'bravo-only.ts'),
  57. 'export function bravoOnly() { return 2; }\n',
  58. );
  59. fs.writeFileSync(
  60. path.join(testDir, 'src', 'charlie-only.ts'),
  61. 'export function charlieOnly() { return 3; }\n',
  62. );
  63. cg = CodeGraph.initSync(testDir, { config: { include: ['**/*.ts'], exclude: [] } });
  64. await cg.indexAll();
  65. handler = new ToolHandler(cg);
  66. });
  67. afterEach(() => {
  68. try { cg.unwatch(); } catch { /* ignore */ }
  69. try { cg.close(); } catch { /* ignore */ }
  70. if (fs.existsSync(testDir)) fs.rmSync(testDir, { recursive: true, force: true });
  71. });
  72. it('prepends a stale banner when the response references a pending file', async () => {
  73. // Long debounce so the edit lingers in pendingFiles while we query.
  74. cg.watch({ debounceMs: 4000, inertForTests: true });
  75. await cg.waitUntilWatcherReady();
  76. // Real disk write so a later sync (if it fires) sees the new content,
  77. // plus a synthesized chokidar event so the watcher's pendingFiles set
  78. // updates immediately without waiting on OS-level event delivery.
  79. fs.writeFileSync(
  80. path.join(testDir, 'src', 'alpha-only.ts'),
  81. 'export function alphaOnly() { return 99; }\n',
  82. );
  83. __emitWatchEventForTests(testDir, 'src/alpha-only.ts');
  84. // With mocked chokidar this is synchronous — keep the wait just to
  85. // exercise the realistic shape (the watcher's `chokidarReady` gate
  86. // and the small window before the pending-file Map is populated).
  87. await waitFor(() => cg.getPendingFiles().some((p) => p.path === 'src/alpha-only.ts'));
  88. const res = await handler.execute('codegraph_search', { query: 'alphaOnly' });
  89. expect(res.isError).toBeFalsy();
  90. const text = res.content[0].text;
  91. // Banner shape: warning glyph + filename + actionable instruction.
  92. expect(text.startsWith('⚠️')).toBe(true);
  93. expect(text).toContain('src/alpha-only.ts');
  94. expect(text).toMatch(/edited \d+ms ago/);
  95. expect(text).toMatch(/Read them directly/);
  96. // The actual result must still follow the banner.
  97. expect(text).toMatch(/alphaOnly/);
  98. });
  99. it('uses the footer (not the banner) when pending files are not referenced', async () => {
  100. cg.watch({ debounceMs: 4000, inertForTests: true });
  101. await cg.waitUntilWatcherReady();
  102. // Edit bravo-only.ts but search for the alphaOnly symbol, whose hit is
  103. // only in alpha-only.ts. The two files share no imports/calls so the
  104. // response text won't mention bravo-only.ts.
  105. fs.writeFileSync(
  106. path.join(testDir, 'src', 'bravo-only.ts'),
  107. 'export function bravoOnly() { return 22; }\n',
  108. );
  109. __emitWatchEventForTests(testDir, 'src/bravo-only.ts');
  110. await waitFor(() => cg.getPendingFiles().some((p) => p.path === 'src/bravo-only.ts'));
  111. const res = await handler.execute('codegraph_search', { query: 'alphaOnly' });
  112. const text = res.content[0].text;
  113. expect(text.startsWith('⚠️')).toBe(false);
  114. expect(text).toMatch(/elsewhere in this project are pending index sync/);
  115. expect(text).toContain('src/bravo-only.ts');
  116. });
  117. it('drops the banner once the sync completes and clears the pending entry', async () => {
  118. cg.watch({ debounceMs: 200, inertForTests: true });
  119. await cg.waitUntilWatcherReady();
  120. fs.writeFileSync(
  121. path.join(testDir, 'src', 'alpha-only.ts'),
  122. 'export function alphaOnly() { return 7; }\n',
  123. );
  124. __emitWatchEventForTests(testDir, 'src/alpha-only.ts');
  125. // Wait through debounce (200ms) + sync; pendingFiles drains back to empty.
  126. await waitFor(() => cg.getPendingFiles().length === 0, 3000);
  127. const res = await handler.execute('codegraph_search', { query: 'alphaOnly' });
  128. const text = res.content[0].text;
  129. expect(text.startsWith('⚠️')).toBe(false);
  130. expect(text).not.toMatch(/elsewhere in this project are pending index sync/);
  131. });
  132. it('lists pending files under "Pending sync" in codegraph_status', async () => {
  133. cg.watch({ debounceMs: 4000, inertForTests: true });
  134. await cg.waitUntilWatcherReady();
  135. fs.writeFileSync(
  136. path.join(testDir, 'src', 'charlie-only.ts'),
  137. 'export function charlieOnly() { return 33; }\n',
  138. );
  139. __emitWatchEventForTests(testDir, 'src/charlie-only.ts');
  140. await waitFor(() => cg.getPendingFiles().some((p) => p.path === 'src/charlie-only.ts'));
  141. const res = await handler.execute('codegraph_status', {});
  142. const text = res.content[0].text;
  143. expect(text).toContain('### Pending sync:');
  144. expect(text).toContain('src/charlie-only.ts');
  145. // Status embeds the info first-class, so the auto-banner is suppressed.
  146. expect(text.startsWith('⚠️')).toBe(false);
  147. });
  148. it('returns zero pending files when no watcher is active', () => {
  149. expect(cg.getPendingFiles()).toEqual([]);
  150. });
  151. });