Skip to content

Commit bdc12d6

Browse files
author
StackMemory Bot (CLI)
committed
fix(tests): mock child_process.spawn in claude-code test suite
Add spawn mocks to subagent-client and agent-bridge tests to prevent real claude CLI invocation during test runs. Fixes timeout failures in multiProvider routing and swarm launch tests.
1 parent 974f87f commit bdc12d6

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/integrations/claude-code/__tests__/agent-bridge.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@
33
*/
44

55
import { describe, it, expect, beforeEach, vi } from 'vitest';
6+
import { EventEmitter } from 'events';
67
import { ClaudeCodeAgentBridge, CLAUDE_CODE_AGENTS } from '../agent-bridge.js';
78

9+
// Mock child_process.spawn to avoid invoking real claude CLI
10+
vi.mock('child_process', () => ({
11+
spawn: vi.fn(() => {
12+
const proc = new EventEmitter() as any;
13+
proc.stdout = new EventEmitter();
14+
proc.stderr = new EventEmitter();
15+
proc.stdin = { write: vi.fn(), end: vi.fn() };
16+
setTimeout(() => {
17+
proc.stdout.emit('data', Buffer.from('Mock swarm agent response'));
18+
proc.emit('close', 0);
19+
}, 50);
20+
return proc;
21+
}),
22+
}));
23+
824
// Mock the oracle-worker-pattern module
925
vi.mock('../../ralph/patterns/oracle-worker-pattern.js', () => ({
1026
OracleWorkerCoordinator: class MockCoordinator {

src/integrations/claude-code/__tests__/subagent-client.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
66
import * as fs from 'fs';
77
import * as path from 'path';
88
import * as os from 'os';
9+
import { EventEmitter } from 'events';
10+
11+
// Mock child_process.spawn to avoid invoking real claude CLI in tests
12+
vi.mock('child_process', () => ({
13+
spawn: vi.fn(() => {
14+
const proc = new EventEmitter() as any;
15+
proc.stdout = new EventEmitter();
16+
proc.stderr = new EventEmitter();
17+
proc.stdin = { write: vi.fn(), end: vi.fn() };
18+
setTimeout(() => {
19+
// Emit stream-json format that subagent-client parses
20+
const event = JSON.stringify({
21+
type: 'assistant',
22+
message: {
23+
content: [{ type: 'text', text: '{"result": "mock CLI response"}' }],
24+
},
25+
session_id: 'test-session',
26+
});
27+
proc.stdout.emit('data', Buffer.from(event + '\n'));
28+
proc.emit('close', 0);
29+
}, 50);
30+
return proc;
31+
}),
32+
}));
933

1034
// Create hoisted mock references so they're available inside vi.mock factories
1135
const {
@@ -324,9 +348,10 @@ describe('ClaudeCodeSubagentClient', () => {
324348
type: 'code',
325349
task: 'Generate simple function',
326350
context: {},
351+
timeout: 1000, // Short timeout — we only care about routing, not CLI result
327352
};
328353

329-
// This will attempt CLI execution which may fail in test env,
354+
// This will attempt CLI execution which may fail/timeout in test env,
330355
// but the important thing is it didn't try createProvider
331356
await nonMockClient.executeSubagent(request).catch(() => {});
332357

@@ -372,9 +397,10 @@ describe('ClaudeCodeSubagentClient', () => {
372397
type: 'code',
373398
task: 'Generate code',
374399
context: {},
400+
timeout: 1000, // Short timeout — we only care about routing, not CLI result
375401
};
376402

377-
// Will try CLI path; may fail in test env but that's OK
403+
// Will try CLI path; may fail/timeout in test env but that's OK
378404
const response = await nonMockClient.executeSubagent(request);
379405

380406
// createProvider should not be called for anthropic

0 commit comments

Comments
 (0)