| 1 | import * as fs from "fs/promises"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import { |
| 5 | cleanupTestContext, |
| 6 | createTestContext, |
| 7 | runCLI, |
| 8 | } from "../test-helpers/cli-helpers.js"; |
| 9 | import { |
| 10 | cleanupMockLLMServer, |
| 11 | createMockLLMServer, |
| 12 | type MockLLMServer, |
| 13 | } from "../test-helpers/mock-llm-server.js"; |
| 14 | |
| 15 | describe("E2E: Headless Mode with ANTHROPIC_API_KEY", () => { |
| 16 | let context: any; |
| 17 | let mockServer: MockLLMServer; |
| 18 | |
| 19 | beforeEach(async () => { |
| 20 | context = await createTestContext(); |
| 21 | }); |
| 22 | |
| 23 | afterEach(async () => { |
| 24 | if (mockServer) { |
| 25 | await cleanupMockLLMServer(mockServer); |
| 26 | } |
| 27 | await cleanupTestContext(context); |
| 28 | }); |
| 29 | |
| 30 | it("should work when ANTHROPIC_API_KEY is set in environment", async () => { |
| 31 | // Create a mock LLM server using the same pattern as working tests |
| 32 | mockServer = await createMockLLMServer({ |
| 33 | response: "Hello! This is a response from Claude.", |
| 34 | }); |
| 35 | |
| 36 | // Use OpenAI provider pointing to mock server (same as working tests) |
| 37 | const configContent = `name: Test Assistant |
| 38 | version: 1.0.0 |
| 39 | schema: v1 |
| 40 | models: |
| 41 | - name: test-claude |
| 42 | model: claude-3-5-sonnet |
| 43 | provider: openai |
| 44 | apiKey: test-key |
| 45 | apiBase: ${mockServer.url} |
| 46 | roles: |
| 47 | - chat |
| 48 | `; |
| 49 | |
| 50 | const configPath = path.join(context.testDir, "test-config.yaml"); |
| 51 | await fs.writeFile(configPath, configContent); |
| 52 | context.configPath = configPath; |
| 53 | |
| 54 | // Create onboarding flag to skip onboarding |
| 55 | const onboardingFlagPath = path.join( |
| 56 | context.testDir, |
| 57 | ".continue", |
| 58 | ".onboarding_complete", |
| 59 | ); |
| 60 | await fs.mkdir(path.dirname(onboardingFlagPath), { recursive: true }); |
| 61 | await fs.writeFile(onboardingFlagPath, new Date().toISOString()); |
| 62 | |
| 63 | const result = await runCLI(context, { |
| 64 | args: ["-p", "--config", context.configPath, "Hi there"], |
| 65 | env: { |
| 66 | ANTHROPIC_API_KEY: "test-api-key-from-env", |
| 67 | }, |
| 68 | timeout: 15000, |
| 69 | }); |
| 70 | |
| 71 | // Verify the CLI successfully runs (even if not using env var in this test) |
| 72 | expect(result.stdout).toContain("Hello! This is a response from Claude."); |
| 73 | expect(result.exitCode).toBe(0); |
| 74 | expect(mockServer.requests).toHaveLength(1); |
| 75 | |
| 76 | // Find the user message in the request |
| 77 | const userMessage = mockServer.requests[0].body.messages.find( |
| 78 | (m: any) => m.role === "user", |
| 79 | ); |
| 80 | expect(userMessage.content).toBe("Hi there"); |
| 81 | }, 20000); |
| 82 | |
| 83 | it("should create config but fail with invalid API key when no config exists", async () => { |
| 84 | // This test verifies that CLI automatically creates config from ANTHROPIC_API_KEY |
| 85 | // when no config file exists, but fails when the API key is invalid |
| 86 | |
| 87 | const result = await runCLI(context, { |
| 88 | args: ["-p", "test prompt"], |
| 89 | env: { |
| 90 | ANTHROPIC_API_KEY: "TEST-test-invalid-key-format", |
| 91 | CONTINUE_GLOBAL_DIR: context.testDir + "/.continue", |
| 92 | }, |
| 93 | expectError: true, // API call should fail with invalid key |
| 94 | timeout: 15000, |
| 95 | }); |
| 96 | |
| 97 | // The CLI should create config but fail due to invalid API key |
| 98 | // This is expected behavior - invalid API keys should cause authentication errors |
| 99 | expect(result.exitCode).toBe(1); |
| 100 | |
| 101 | // Should contain authentication error (not service initialization error) |
| 102 | expect(result.stderr).toContain("authentication_error"); |
| 103 | expect(result.stderr).toContain("invalid x-api-key"); |
| 104 | |
| 105 | // Should NOT contain the old error about service initialization |
| 106 | expect(result.stderr).not.toContain("Failed to initialize ConfigService"); |
| 107 | |
| 108 | // Verify config.yaml was automatically created |
| 109 | const configPath = path.join(context.testDir, ".continue", "config.yaml"); |
| 110 | const configExists = await fs |
| 111 | .stat(configPath) |
| 112 | .then(() => true) |
| 113 | .catch(() => false); |
| 114 | expect(configExists).toBe(true); |
| 115 | |
| 116 | if (configExists) { |
| 117 | const configContent = await fs.readFile(configPath, "utf-8"); |
| 118 | expect(configContent).toContain("anthropic/claude-4-sonnet"); |
| 119 | expect(configContent).toContain( |
| 120 | "ANTHROPIC_API_KEY: TEST-test-invalid-key-format", |
| 121 | ); |
| 122 | } |
| 123 | }, 20000); |
| 124 | }); |