| 1 | import { createTestContext, cleanupTestContext, runCLI, createTestConfig } from "../test-helpers/cli-helpers.js"; |
| 2 | |
| 3 | describe("E2E: Headless Mode - Missing Prompt", () => { |
| 4 | let context: any; |
| 5 | |
| 6 | beforeEach(async () => { |
| 7 | context = await createTestContext(); |
| 8 | }); |
| 9 | |
| 10 | afterEach(async () => { |
| 11 | await cleanupTestContext(context); |
| 12 | }); |
| 13 | |
| 14 | it("should exit with helpful error message when no prompt is provided with -p flag", async () => { |
| 15 | await createTestConfig(context, `name: Test Assistant |
| 16 | version: 1.0.0 |
| 17 | schema: v1 |
| 18 | models: |
| 19 | - model: gpt-4 |
| 20 | provider: openai |
| 21 | apiKey: test-key |
| 22 | roles: |
| 23 | - chat`); |
| 24 | |
| 25 | const result = await runCLI(context, { |
| 26 | args: ["-p", "--config", context.configPath], |
| 27 | env: { OPENAI_API_KEY: "test-key" }, |
| 28 | expectError: true, |
| 29 | timeout: 5000, // Short timeout to avoid hanging |
| 30 | }); |
| 31 | |
| 32 | // Should exit with non-zero exit code |
| 33 | expect(result.exitCode).toBe(1); |
| 34 | |
| 35 | // Should provide helpful error message |
| 36 | const output = result.stderr + result.stdout; |
| 37 | expect(output).toContain("A prompt is required when using the -p/--print flag"); |
| 38 | expect(output).toContain('cn -p "please review my current git diff"'); |
| 39 | expect(output).toContain('echo "hello" | cn -p'); |
| 40 | expect(output).toContain('cn -p "analyze the code in src/"'); |
| 41 | }); |
| 42 | |
| 43 | it("should work correctly when prompt is provided with -p flag", async () => { |
| 44 | await createTestConfig(context, `name: Test Assistant |
| 45 | version: 1.0.0 |
| 46 | schema: v1 |
| 47 | models: |
| 48 | - model: gpt-4 |
| 49 | provider: openai |
| 50 | apiKey: test-key |
| 51 | roles: |
| 52 | - chat`); |
| 53 | |
| 54 | const result = await runCLI(context, { |
| 55 | args: ["-p", "--config", context.configPath, "Hello assistant"], |
| 56 | env: { OPENAI_API_KEY: "test-key" }, |
| 57 | expectError: true, // Will fail due to invalid API key, but should process the prompt |
| 58 | timeout: 5000, |
| 59 | }); |
| 60 | |
| 61 | // Should attempt to process the prompt (will fail due to invalid API key) |
| 62 | expect(result.exitCode).not.toBe(0); |
| 63 | |
| 64 | // Should NOT show the "prompt required" error message |
| 65 | const output = result.stderr + result.stdout; |
| 66 | expect(output).not.toContain("A prompt is required when using the -p/--print flag"); |
| 67 | }); |
| 68 | |
| 69 | it("should work correctly when prompt is provided via stdin with -p flag", async () => { |
| 70 | // NOTE: In our current implementation, stdin reading is disabled in test environments |
| 71 | // for safety (to prevent hanging). This test verifies the expected behavior when |
| 72 | // stdin input is provided but cannot be read during tests. |
| 73 | await createTestConfig(context, `name: Test Assistant |
| 74 | version: 1.0.0 |
| 75 | schema: v1 |
| 76 | models: |
| 77 | - model: gpt-4 |
| 78 | provider: openai |
| 79 | apiKey: test-key |
| 80 | roles: |
| 81 | - chat`); |
| 82 | |
| 83 | const result = await runCLI(context, { |
| 84 | args: ["-p", "--config", context.configPath], |
| 85 | env: { OPENAI_API_KEY: "test-key" }, |
| 86 | input: "Hello from stdin", |
| 87 | expectError: true, // Will show "prompt required" error because stdin is disabled in tests |
| 88 | timeout: 5000, |
| 89 | }); |
| 90 | |
| 91 | // Should exit with error code due to missing prompt (stdin reading is disabled in tests) |
| 92 | expect(result.exitCode).toBe(1); |
| 93 | |
| 94 | // Should show the "prompt required" error message since stdin reading is disabled in test env |
| 95 | const output = result.stderr + result.stdout; |
| 96 | expect(output).toContain("A prompt is required when using the -p/--print flag"); |
| 97 | }); |
| 98 | |
| 99 | it("should prioritize command line prompt over stdin when both are provided", async () => { |
| 100 | await createTestConfig(context, `name: Test Assistant |
| 101 | version: 1.0.0 |
| 102 | schema: v1 |
| 103 | models: |
| 104 | - model: gpt-4 |
| 105 | provider: openai |
| 106 | apiKey: test-key |
| 107 | roles: |
| 108 | - chat`); |
| 109 | |
| 110 | const result = await runCLI(context, { |
| 111 | args: ["-p", "--config", context.configPath, "Command line prompt"], |
| 112 | env: { OPENAI_API_KEY: "test-key" }, |
| 113 | input: "Stdin input that should be ignored", |
| 114 | expectError: true, // Will fail due to invalid API key |
| 115 | timeout: 5000, |
| 116 | }); |
| 117 | |
| 118 | // Should use command line prompt over stdin |
| 119 | expect(result.exitCode).not.toBe(0); |
| 120 | |
| 121 | // Should NOT show the "prompt required" error message |
| 122 | const output = result.stderr + result.stdout; |
| 123 | expect(output).not.toContain("A prompt is required when using the -p/--print flag"); |
| 124 | }); |
| 125 | }); |