cli-mock-stdin
Scannednpx machina-cli add skill a5c-ai/babysitter/cli-mock-stdin --openclawFiles (1)
SKILL.md
996 B
CLI Mock Stdin
Create mock stdin utilities for testing.
Generated Patterns
import { Readable } from 'stream';
export function mockStdin(inputs: string[]): Readable {
let index = 0;
return new Readable({
read() {
if (index < inputs.length) {
setTimeout(() => {
this.push(inputs[index++] + '\n');
}, 10);
} else {
this.push(null);
}
},
});
}
export async function runWithStdin(
cmd: () => Promise<void>,
inputs: string[]
): Promise<void> {
const originalStdin = process.stdin;
Object.defineProperty(process, 'stdin', { value: mockStdin(inputs) });
try {
await cmd();
} finally {
Object.defineProperty(process, 'stdin', { value: originalStdin });
}
}
Target Processes
- cli-unit-integration-testing
- interactive-prompt-system
Source
git clone https://github.com/a5c-ai/babysitter/blob/main/plugins/babysitter/skills/babysit/process/specializations/cli-mcp-development/skills/cli-mock-stdin/SKILL.mdView on GitHub Overview
cli-mock-stdin provides utilities to simulate user input in Node.js CLI tests. It includes mockStdin to create a stream of predefined lines and runWithStdin to swap process.stdin during test execution. This makes interactive prompts deterministic and testable in unit and integration tests.
How This Skill Works
mockStdin(inputs) returns a Readable stream that pushes each input line with a newline. runWithStdin(cmd, inputs) temporarily replaces process.stdin with the mock stream, executes cmd, and restores the original stdin in a finally block.
When to Use It
- Unit tests for CLI commands that read from stdin
- Integration tests for interactive prompts and workflows
- Automated end-to-end tests involving user input sequences
- Testing prompts with multiple or optional questions
- Verifying behavior with delayed or repeated inputs
Quick Start
- Step 1: Import mockStdin and runWithStdin from the module
- Step 2: Prepare an inputs array representing user responses
- Step 3: Call runWithStdin(() => yourTestCommand(), inputs) and await it
Best Practices
- Keep inputs realistic and cover edge cases (empty lines, invalid input)
- Ensure process.stdin is restored in a finally block after cmd completes
- Use small, deterministic delays to mimic user pacing
- Combine with mocks for external dependencies to stabilize tests
- Test multiple prompt paths by varying the inputs array
Example Use Cases
- Test a login CLI that asks for username and password in sequence
- Validate a survey CLI that records multiple user answers
- Verify a confirmation prompt (y/n) flow before performing an action
- Test a CLI that reads data from stdin and processes lines
- Check error handling when users provide unexpected inputs
Frequently Asked Questions
Add this skill to your agents