Add validation for missing prompt in headless mode with -p flag
This commit is contained in:
parent
331de538b6
commit
32c4abf8e9
2 changed files with 133 additions and 1 deletions
122
src/e2e/headless-missing-prompt.test.ts
Normal file
122
src/e2e/headless-missing-prompt.test.ts
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
import { createTestContext, cleanupTestContext, runCLI, createTestConfig } from "../test-helpers/cli-helpers.js";
|
||||
|
||||
describe("E2E: Headless Mode - Missing Prompt", () => {
|
||||
let context: any;
|
||||
|
||||
beforeEach(async () => {
|
||||
context = await createTestContext();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await cleanupTestContext(context);
|
||||
});
|
||||
|
||||
it("should exit with helpful error message when no prompt is provided with -p flag", async () => {
|
||||
await createTestConfig(context, `name: Test Assistant
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
models:
|
||||
- model: gpt-4
|
||||
provider: openai
|
||||
apiKey: test-key
|
||||
roles:
|
||||
- chat`);
|
||||
|
||||
const result = await runCLI(context, {
|
||||
args: ["-p", "--config", context.configPath],
|
||||
env: { OPENAI_API_KEY: "test-key" },
|
||||
expectError: true,
|
||||
timeout: 5000, // Short timeout to avoid hanging
|
||||
});
|
||||
|
||||
// Should exit with non-zero exit code
|
||||
expect(result.exitCode).toBe(1);
|
||||
|
||||
// Should provide helpful error message
|
||||
const output = result.stderr + result.stdout;
|
||||
expect(output).toContain("A prompt is required when using the -p/--print flag");
|
||||
expect(output).toContain('cn -p "please review my current git diff"');
|
||||
expect(output).toContain('echo "hello" | cn -p');
|
||||
expect(output).toContain('cn -p "analyze the code in src/"');
|
||||
});
|
||||
|
||||
it("should work correctly when prompt is provided with -p flag", async () => {
|
||||
await createTestConfig(context, `name: Test Assistant
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
models:
|
||||
- model: gpt-4
|
||||
provider: openai
|
||||
apiKey: test-key
|
||||
roles:
|
||||
- chat`);
|
||||
|
||||
const result = await runCLI(context, {
|
||||
args: ["-p", "--config", context.configPath, "Hello assistant"],
|
||||
env: { OPENAI_API_KEY: "test-key" },
|
||||
expectError: true, // Will fail due to invalid API key, but should process the prompt
|
||||
timeout: 5000,
|
||||
});
|
||||
|
||||
// Should attempt to process the prompt (will fail due to invalid API key)
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
|
||||
// Should NOT show the "prompt required" error message
|
||||
const output = result.stderr + result.stdout;
|
||||
expect(output).not.toContain("A prompt is required when using the -p/--print flag");
|
||||
});
|
||||
|
||||
it("should work correctly when prompt is provided via stdin with -p flag", async () => {
|
||||
await createTestConfig(context, `name: Test Assistant
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
models:
|
||||
- model: gpt-4
|
||||
provider: openai
|
||||
apiKey: test-key
|
||||
roles:
|
||||
- chat`);
|
||||
|
||||
const result = await runCLI(context, {
|
||||
args: ["-p", "--config", context.configPath],
|
||||
env: { OPENAI_API_KEY: "test-key" },
|
||||
input: "Hello from stdin",
|
||||
expectError: true, // Will fail due to invalid API key, but should process the stdin
|
||||
timeout: 5000,
|
||||
});
|
||||
|
||||
// Should attempt to process the stdin input (will fail due to invalid API key)
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
|
||||
// Should NOT show the "prompt required" error message
|
||||
const output = result.stderr + result.stdout;
|
||||
expect(output).not.toContain("A prompt is required when using the -p/--print flag");
|
||||
});
|
||||
|
||||
it("should prioritize command line prompt over stdin when both are provided", async () => {
|
||||
await createTestConfig(context, `name: Test Assistant
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
models:
|
||||
- model: gpt-4
|
||||
provider: openai
|
||||
apiKey: test-key
|
||||
roles:
|
||||
- chat`);
|
||||
|
||||
const result = await runCLI(context, {
|
||||
args: ["-p", "--config", context.configPath, "Command line prompt"],
|
||||
env: { OPENAI_API_KEY: "test-key" },
|
||||
input: "Stdin input that should be ignored",
|
||||
expectError: true, // Will fail due to invalid API key
|
||||
timeout: 5000,
|
||||
});
|
||||
|
||||
// Should use command line prompt over stdin
|
||||
expect(result.exitCode).not.toBe(0);
|
||||
|
||||
// Should NOT show the "prompt required" error message
|
||||
const output = result.stderr + result.stdout;
|
||||
expect(output).not.toContain("A prompt is required when using the -p/--print flag");
|
||||
});
|
||||
});
|
||||
12
src/index.ts
12
src/index.ts
|
|
@ -67,6 +67,16 @@ addCommonOptions(program)
|
|||
}
|
||||
}
|
||||
|
||||
// In headless mode, ensure we have a prompt
|
||||
if (options.print && !prompt) {
|
||||
console.error("Error: A prompt is required when using the -p/--print flag.\n");
|
||||
console.error("Usage examples:");
|
||||
console.error(' cn -p "please review my current git diff"');
|
||||
console.error(' echo "hello" | cn -p');
|
||||
console.error(' cn -p "analyze the code in src/"');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Map --print to headless mode
|
||||
options.headless = options.print;
|
||||
options.print = undefined;
|
||||
|
|
@ -145,4 +155,4 @@ try {
|
|||
} catch (error) {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue