1
0

daemon-attach-log.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * #618 — the "attached to shared daemon" line is benign INFO, but MCP hosts
  3. * render server stderr at error level (and tack on an `undefined` data field),
  4. * so on every session start a healthy attach showed up as `[error] … undefined`.
  5. * It's now gated behind CODEGRAPH_MCP_LOG_ATTACH=1 — silent by default, opt-in
  6. * for debugging. Approach from #640 by @mturac.
  7. */
  8. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  9. import { logAttachedDaemon } from '../src/mcp/proxy';
  10. const hello = { pid: 4242, codegraph: '9.9.9' } as any;
  11. describe('daemon attach log gating (#618)', () => {
  12. let spy: ReturnType<typeof vi.spyOn>;
  13. beforeEach(() => {
  14. spy = vi.spyOn(process.stderr, 'write').mockImplementation((() => true) as any);
  15. });
  16. afterEach(() => {
  17. spy.mockRestore();
  18. delete process.env.CODEGRAPH_MCP_LOG_ATTACH;
  19. });
  20. it('is silent by default (no [error]/undefined noise in MCP hosts)', () => {
  21. delete process.env.CODEGRAPH_MCP_LOG_ATTACH;
  22. logAttachedDaemon('/tmp/cg.sock', hello);
  23. expect(spy).not.toHaveBeenCalled();
  24. });
  25. it('logs the attach line only when CODEGRAPH_MCP_LOG_ATTACH=1 (opt-in debug)', () => {
  26. process.env.CODEGRAPH_MCP_LOG_ATTACH = '1';
  27. logAttachedDaemon('/tmp/cg.sock', hello);
  28. const out = spy.mock.calls.map((c) => String(c[0])).join('');
  29. expect(out).toContain('Attached to shared daemon on /tmp/cg.sock');
  30. expect(out).toContain('pid 4242');
  31. });
  32. });