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