| 1 | import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | |
| 3 | import { |
| 4 | createTestContext, |
| 5 | cleanupTestContext, |
| 6 | runCLI, |
| 7 | createTestConfig, |
| 8 | type CLITestContext, |
| 9 | } from "../test-helpers/cli-helpers.js"; |
| 10 | |
| 11 | describe("E2E: Pipe Input to TUI Mode", () => { |
| 12 | let context: CLITestContext; |
| 13 | |
| 14 | const testConfig = `name: Test Assistant |
| 15 | version: 1.0.0 |
| 16 | schema: v1 |
| 17 | models: |
| 18 | - model: gpt-4 |
| 19 | provider: openai |
| 20 | apiKey: test-key |
| 21 | roles: |
| 22 | - chat`; |
| 23 | |
| 24 | const testEnv = { OPENAI_API_KEY: "test-key" }; |
| 25 | |
| 26 | beforeEach(async () => { |
| 27 | context = await createTestContext(); |
| 28 | }); |
| 29 | |
| 30 | afterEach(async () => { |
| 31 | await cleanupTestContext(context); |
| 32 | }); |
| 33 | |
| 34 | describe("piped input handling in test environment", () => { |
| 35 | it("should maintain backward compatibility with explicit -p flag", async () => { |
| 36 | await createTestConfig(context, testConfig); |
| 37 | |
| 38 | const result = await runCLI(context, { |
| 39 | args: ["-p", "--config", context.configPath!], |
| 40 | env: testEnv, |
| 41 | input: "Explicit headless mode", |
| 42 | timeout: 5000, |
| 43 | expectError: true, |
| 44 | }); |
| 45 | |
| 46 | // With explicit -p flag, should be in headless mode |
| 47 | // Should show headless mode error (no prompt provided when input is ignored in test) |
| 48 | expect(result.stderr).toContain( |
| 49 | "A prompt is required when using the -p/--print flag", |
| 50 | ); |
| 51 | expect(result.exitCode).not.toBe(0); |
| 52 | }); |
| 53 | |
| 54 | it("should handle explicit -p flag with prompt argument", async () => { |
| 55 | await createTestConfig(context, testConfig); |
| 56 | |
| 57 | const result = await runCLI(context, { |
| 58 | args: ["-p", "--config", context.configPath!, "Test prompt"], |
| 59 | env: testEnv, |
| 60 | timeout: 5000, |
| 61 | expectError: true, |
| 62 | }); |
| 63 | |
| 64 | // With explicit -p flag and prompt, should attempt headless mode |
| 65 | // Will fail due to missing LLM configuration, but that's expected |
| 66 | expect(result.exitCode).not.toBe(0); |
| 67 | |
| 68 | // Should not show the "prompt required" error since we provided one |
| 69 | expect(result.stderr).not.toContain( |
| 70 | "A prompt is required when using the -p/--print flag", |
| 71 | ); |
| 72 | }); |
| 73 | |
| 74 | it("should try to start TUI mode when no explicit flags are provided", async () => { |
| 75 | await createTestConfig(context, testConfig); |
| 76 | |
| 77 | const result = await runCLI(context, { |
| 78 | args: ["--config", context.configPath!], |
| 79 | env: testEnv, |
| 80 | input: "This will be ignored in test environment", |
| 81 | timeout: 3000, // Shorter timeout since we expect quick failure |
| 82 | expectError: true, |
| 83 | }); |
| 84 | |
| 85 | // Without -p flag, should attempt TUI mode |
| 86 | // In test environment, this will likely fail quickly due to stdin/TTY issues |
| 87 | // but the important thing is that it doesn't immediately error about missing prompt |
| 88 | expect(result.stderr).not.toContain( |
| 89 | "A prompt is required when using the -p/--print flag", |
| 90 | ); |
| 91 | |
| 92 | // Should exit with error due to test environment limitations |
| 93 | expect(result.exitCode).not.toBe(0); |
| 94 | }); |
| 95 | |
| 96 | it("should handle platform differences in subprocess execution", async () => { |
| 97 | await createTestConfig(context, testConfig); |
| 98 | |
| 99 | // Test that the CLI can be executed on different platforms without crashing |
| 100 | const result = await runCLI(context, { |
| 101 | args: ["--config", context.configPath!, "Test cross-platform"], |
| 102 | env: testEnv, |
| 103 | timeout: 3000, |
| 104 | expectError: true, |
| 105 | }); |
| 106 | |
| 107 | // The key is that it should not crash immediately |
| 108 | // Platform-specific behavior may vary, but basic execution should work |
| 109 | expect(result.exitCode).toBeDefined(); |
| 110 | expect(typeof result.stdout).toBe("string"); |
| 111 | expect(typeof result.stderr).toBe("string"); |
| 112 | |
| 113 | // On any platform, should not show headless-mode specific errors |
| 114 | // when no -p flag is provided |
| 115 | expect(result.stderr).not.toContain( |
| 116 | "A prompt is required when using the -p/--print flag", |
| 117 | ); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | describe("configuration and environment handling", () => { |
| 122 | it("should handle invalid configuration gracefully", async () => { |
| 123 | // Test with malformed config |
| 124 | await createTestConfig(context, "invalid: yaml\nno: models: here"); |
| 125 | |
| 126 | const result = await runCLI(context, { |
| 127 | args: ["--config", context.configPath!], |
| 128 | env: testEnv, |
| 129 | timeout: 3000, |
| 130 | expectError: true, |
| 131 | }); |
| 132 | |
| 133 | // Should fail due to invalid config, not due to pipe handling |
| 134 | expect(result.exitCode).not.toBe(0); |
| 135 | }); |
| 136 | |
| 137 | it("should handle missing config file", async () => { |
| 138 | const result = await runCLI(context, { |
| 139 | args: ["--config", "/nonexistent/config.yaml"], |
| 140 | env: testEnv, |
| 141 | timeout: 3000, |
| 142 | expectError: true, |
| 143 | }); |
| 144 | |
| 145 | // Should fail due to missing config |
| 146 | expect(result.exitCode).not.toBe(0); |
| 147 | }); |
| 148 | |
| 149 | it("should handle various environment variables", async () => { |
| 150 | await createTestConfig(context, testConfig); |
| 151 | |
| 152 | // Test with different environment configurations |
| 153 | const testEnvs = [ |
| 154 | { ...testEnv, NODE_ENV: "production" }, |
| 155 | { ...testEnv, NODE_ENV: "development" }, |
| 156 | { ...testEnv, CI: "true" }, |
| 157 | ]; |
| 158 | |
| 159 | for (const env of testEnvs) { |
| 160 | const result = await runCLI(context, { |
| 161 | args: ["--config", context.configPath!], |
| 162 | env, |
| 163 | timeout: 3000, |
| 164 | expectError: true, |
| 165 | }); |
| 166 | |
| 167 | // Should handle different environments without crashing |
| 168 | expect(result.exitCode).toBeDefined(); |
| 169 | } |
| 170 | }); |
| 171 | }); |
| 172 | |
| 173 | describe("argument processing", () => { |
| 174 | it("should handle various flag combinations", async () => { |
| 175 | await createTestConfig(context, testConfig); |
| 176 | |
| 177 | const flagCombinations = [ |
| 178 | ["--verbose", "--config", context.configPath!], |
| 179 | ["--config", context.configPath!, "--verbose"], |
| 180 | ["--config", context.configPath!, "prompt with spaces"], |
| 181 | ]; |
| 182 | |
| 183 | for (const args of flagCombinations) { |
| 184 | const result = await runCLI(context, { |
| 185 | args, |
| 186 | env: testEnv, |
| 187 | timeout: 3000, |
| 188 | expectError: true, |
| 189 | }); |
| 190 | |
| 191 | // Should process arguments without immediate crashes |
| 192 | expect(result.exitCode).toBeDefined(); |
| 193 | } |
| 194 | }); |
| 195 | |
| 196 | it("should handle help and version flags", async () => { |
| 197 | // These should work without config |
| 198 | const helpResult = await runCLI(context, { |
| 199 | args: ["--help"], |
| 200 | timeout: 3000, |
| 201 | }); |
| 202 | |
| 203 | expect(helpResult.exitCode).toBe(0); |
| 204 | expect(helpResult.stdout).toContain("Continue CLI"); |
| 205 | |
| 206 | const versionResult = await runCLI(context, { |
| 207 | args: ["--version"], |
| 208 | timeout: 3000, |
| 209 | }); |
| 210 | |
| 211 | expect(versionResult.exitCode).toBe(0); |
| 212 | expect(versionResult.stdout).toMatch(/\d+\.\d+\.\d+/); |
| 213 | }); |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | /** |
| 218 | * Note: This test suite focuses on testing the CLI behavior within the constraints |
| 219 | * of the test environment. The actual pipe input detection cannot be fully tested |
| 220 | * here because: |
| 221 | * |
| 222 | * 1. readStdinSync() returns null when CONTINUE_CLI_TEST=true to prevent hanging |
| 223 | * 2. TUI mode requires real TTY access which isn't available in test subprocesses |
| 224 | * 3. Cross-platform TTY behavior varies significantly |
| 225 | * |
| 226 | * However, we test: |
| 227 | * - Backward compatibility with explicit flags |
| 228 | * - Proper argument processing |
| 229 | * - Error handling for various scenarios |
| 230 | * - Platform-agnostic behavior |
| 231 | * |
| 232 | * For manual testing of the full pipe functionality, use: |
| 233 | * npm run build && echo "test" | node dist/index.js |
| 234 | */ |