| |
| |
| |
| |
|
|
| import { vi } from 'vitest'; |
| import type { ChildProcess } from 'child_process'; |
| import { EventEmitter } from 'events'; |
| import type { Readable } from 'stream'; |
|
|
| |
| |
| |
| export function createMockChildProcess(options: { |
| stdout?: string[]; |
| stderr?: string[]; |
| exitCode?: number | null; |
| shouldError?: boolean; |
| }): ChildProcess { |
| const { stdout = [], stderr = [], exitCode = 0, shouldError = false } = options; |
|
|
| const mockProcess = new EventEmitter() as any; |
|
|
| |
| mockProcess.stdout = new EventEmitter() as Readable; |
| mockProcess.stderr = new EventEmitter() as Readable; |
|
|
| mockProcess.kill = vi.fn(); |
|
|
| |
| process.nextTick(() => { |
| |
| for (const line of stdout) { |
| mockProcess.stdout.emit('data', Buffer.from(line + '\n')); |
| } |
|
|
| |
| for (const line of stderr) { |
| mockProcess.stderr.emit('data', Buffer.from(line + '\n')); |
| } |
|
|
| |
| if (shouldError) { |
| mockProcess.emit('error', new Error('Process error')); |
| } else { |
| mockProcess.emit('exit', exitCode); |
| } |
| }); |
|
|
| return mockProcess as ChildProcess; |
| } |
|
|
| |
| |
| |
| export function createMockFs() { |
| return { |
| readFile: vi.fn(), |
| writeFile: vi.fn(), |
| mkdir: vi.fn(), |
| access: vi.fn(), |
| stat: vi.fn(), |
| }; |
| } |
|
|
| |
| |
| |
| export function createMockExpressContext() { |
| const req = { |
| headers: {}, |
| body: {}, |
| params: {}, |
| query: {}, |
| } as any; |
|
|
| const res = { |
| status: vi.fn().mockReturnThis(), |
| json: vi.fn().mockReturnThis(), |
| send: vi.fn().mockReturnThis(), |
| } as any; |
|
|
| const next = vi.fn(); |
|
|
| return { req, res, next }; |
| } |
|
|
| |
| |
| |
| export function createMockAbortController() { |
| const controller = new AbortController(); |
| const originalAbort = controller.abort.bind(controller); |
| controller.abort = vi.fn(originalAbort); |
| return controller; |
| } |
|
|
| |
| |
| |
| export function createMockClaudeQuery(messages: any[] = []) { |
| return vi.fn(async function* ({ prompt, options }: any) { |
| for (const msg of messages) { |
| yield msg; |
| } |
| }); |
| } |
|
|