Elffuss BraidHub

kikocis/continue public

4131ae02de3e · 3.7 KiBRawPermalink
1import * as fs from "fs/promises";
2import * as path from "path";
3
4import {
5 cleanupTestContext,
6 createTestContext,
7 runCLI,
8} from "../test-helpers/cli-helpers.js";
9import {
10 cleanupMockLLMServer,
11 type MockLLMServer,
12 createMockLLMServer,
13} from "../test-helpers/mock-llm-server.js";
14
15describe("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
38version: 1.0.0
39schema: v1
40models:
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 and work with ANTHROPIC_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 in headless mode
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: false, // Config creation succeeds even with invalid key
94 timeout: 15000,
95 });
96
97 // The CLI should create config and exit successfully in headless mode
98 // (it doesn't actually make API calls unless there's a valid provider setup)
99 expect(result.exitCode).toBe(0);
100
101 // Should NOT contain the old error about service initialization
102 expect(result.stderr).not.toContain("Failed to initialize ConfigService");
103
104 // Verify config.yaml was automatically created
105 const configPath = path.join(context.testDir, ".continue", "config.yaml");
106 const configExists = await fs
107 .stat(configPath)
108 .then(() => true)
109 .catch(() => false);
110 expect(configExists).toBe(true);
111
112 if (configExists) {
113 const configContent = await fs.readFile(configPath, "utf-8");
114 expect(configContent).toContain("anthropic/claude-4-sonnet");
115 expect(configContent).toContain(
116 "ANTHROPIC_API_KEY: TEST-test-invalid-key-format",
117 );
118 }
119 }, 20000);
120});