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