|
| 1 | +/** |
| 2 | + * Integration tests for zombie process prevention. |
| 3 | + * |
| 4 | + * These tests verify that the MCP server exits cleanly when no client connects |
| 5 | + * (handshake timeout) and that project initialization is deferred until after |
| 6 | + * the MCP handshake completes. |
| 7 | + * |
| 8 | + * The tests spawn real child processes to exercise the actual startup path. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect, beforeAll } from 'vitest'; |
| 12 | +import { spawn } from 'node:child_process'; |
| 13 | +import { existsSync } from 'node:fs'; |
| 14 | +import path from 'node:path'; |
| 15 | +import os from 'node:os'; |
| 16 | +import { fileURLToPath } from 'node:url'; |
| 17 | + |
| 18 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 19 | +const ENTRY_POINT = path.resolve(__dirname, '..', 'dist', 'index.js'); |
| 20 | + |
| 21 | +/** |
| 22 | + * Spawn the MCP server as a child process and wait for it to exit. |
| 23 | + * Returns { code, stderr, elapsed } where elapsed is in milliseconds. |
| 24 | + */ |
| 25 | +function spawnServer( |
| 26 | + args: string[], |
| 27 | + env: Record<string, string> = {}, |
| 28 | + timeoutMs = 45_000 |
| 29 | +): Promise<{ code: number | null; signal: string | null; stderr: string; elapsed: number }> { |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + const start = Date.now(); |
| 32 | + let stderr = ''; |
| 33 | + |
| 34 | + const child = spawn(process.execPath, [ENTRY_POINT, ...args], { |
| 35 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 36 | + env: { ...process.env, ...env }, |
| 37 | + timeout: timeoutMs |
| 38 | + }); |
| 39 | + |
| 40 | + child.stderr?.on('data', (chunk: Buffer) => { |
| 41 | + stderr += chunk.toString(); |
| 42 | + }); |
| 43 | + |
| 44 | + child.on('error', reject); |
| 45 | + child.on('close', (code, signal) => { |
| 46 | + resolve({ code, signal, stderr, elapsed: Date.now() - start }); |
| 47 | + }); |
| 48 | + |
| 49 | + // Don't write anything to stdin — simulate the zombie scenario |
| 50 | + // where no MCP client sends an `initialize` message. |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +describe('zombie process prevention', () => { |
| 55 | + beforeAll(() => { |
| 56 | + if (!existsSync(ENTRY_POINT)) { |
| 57 | + throw new Error( |
| 58 | + `dist/index.js not found - run \`npm run build\` before the zombie-guard tests.` |
| 59 | + ); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('exits with code 1 when no MCP client connects within timeout', async () => { |
| 64 | + // Use a short timeout for the test (2 seconds instead of the default 30). |
| 65 | + // Use os.tmpdir() as a real existing directory so path validation passes — |
| 66 | + // this tests the realistic scenario where a valid path IS provided but no |
| 67 | + // MCP client connects (which is exactly the Codex zombie scenario). |
| 68 | + const result = await spawnServer( |
| 69 | + [os.tmpdir()], |
| 70 | + { CODEBASE_CONTEXT_HANDSHAKE_TIMEOUT_MS: '2000' } |
| 71 | + ); |
| 72 | + |
| 73 | + expect(result.code).toBe(1); |
| 74 | + expect(result.stderr).toContain('No MCP client connected within'); |
| 75 | + expect(result.stderr).toContain('npx codebase-context --help'); |
| 76 | + // Should exit roughly around the timeout (2s), not hang forever |
| 77 | + expect(result.elapsed).toBeLessThan(10_000); |
| 78 | + }, 15_000); |
| 79 | + |
| 80 | + it('exits with code 1 even when invoked with no arguments at all', async () => { |
| 81 | + const result = await spawnServer( |
| 82 | + [], |
| 83 | + { CODEBASE_CONTEXT_HANDSHAKE_TIMEOUT_MS: '2000' } |
| 84 | + ); |
| 85 | + |
| 86 | + expect(result.code).toBe(1); |
| 87 | + expect(result.stderr).toContain('No MCP client connected within'); |
| 88 | + expect(result.elapsed).toBeLessThan(10_000); |
| 89 | + }, 15_000); |
| 90 | + |
| 91 | + it('does not start indexing or file watchers before handshake', async () => { |
| 92 | + // With DEBUG on, the server logs "[DEBUG] Server ready" inside oninitialized. |
| 93 | + // Since no client ever connects, that log must never appear. |
| 94 | + // Use os.tmpdir() so path validation passes before the handshake timer runs. |
| 95 | + const result = await spawnServer( |
| 96 | + [os.tmpdir()], |
| 97 | + { |
| 98 | + CODEBASE_CONTEXT_HANDSHAKE_TIMEOUT_MS: '2000', |
| 99 | + CODEBASE_CONTEXT_DEBUG: '1' |
| 100 | + } |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result.code).toBe(1); |
| 104 | + // "[DEBUG] Server ready" is printed inside oninitialized — should NOT appear |
| 105 | + // because no client ever sends `initialize`. |
| 106 | + expect(result.stderr).not.toContain('[DEBUG] Server ready'); |
| 107 | + }, 15_000); |
| 108 | + |
| 109 | + it('respects custom timeout via environment variable', async () => { |
| 110 | + const start = Date.now(); |
| 111 | + const result = await spawnServer( |
| 112 | + [], |
| 113 | + { CODEBASE_CONTEXT_HANDSHAKE_TIMEOUT_MS: '1000' } |
| 114 | + ); |
| 115 | + const elapsed = Date.now() - start; |
| 116 | + |
| 117 | + expect(result.code).toBe(1); |
| 118 | + // Should exit around 1 second, definitely under 5 |
| 119 | + expect(elapsed).toBeGreaterThan(800); |
| 120 | + expect(elapsed).toBeLessThan(5_000); |
| 121 | + }, 10_000); |
| 122 | +}); |
0 commit comments