From 32c4abf8e93d30dcd64b537662eb9f5d80b07a76 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 29 Jul 2025 15:23:40 -0700 Subject: [PATCH] Add validation for missing prompt in headless mode with -p flag --- src/e2e/headless-missing-prompt.test.ts | 122 ++++++++++++++++++++++++ src/index.ts | 12 ++- 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 src/e2e/headless-missing-prompt.test.ts diff --git a/src/e2e/headless-missing-prompt.test.ts b/src/e2e/headless-missing-prompt.test.ts new file mode 100644 index 000000000..19f69313f --- /dev/null +++ b/src/e2e/headless-missing-prompt.test.ts @@ -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"); + }); +}); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index bcc4ce6dd..e06b79304 100644 --- a/src/index.ts +++ b/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); -} +} \ No newline at end of file