| 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 | setupMockLLMTest, |
| 11 | cleanupMockLLMServer, |
| 12 | createMockLLMServer, |
| 13 | createMockLLMConfig, |
| 14 | type MockLLMServer, |
| 15 | } from "../test-helpers/mock-llm-server.js"; |
| 16 | |
| 17 | describe("E2E: Headless Mode with Dynamic Responses", () => { |
| 18 | let context: any; |
| 19 | let mockServer: MockLLMServer; |
| 20 | let extraServers: MockLLMServer[] = []; |
| 21 | |
| 22 | beforeEach(async () => { |
| 23 | context = undefined; |
| 24 | mockServer = undefined as any; |
| 25 | extraServers = []; |
| 26 | }); |
| 27 | |
| 28 | afterEach(async () => { |
| 29 | // Clean up all extra servers first |
| 30 | if (extraServers.length > 0) { |
| 31 | await Promise.all(extraServers.map(server => cleanupMockLLMServer(server))); |
| 32 | extraServers = []; |
| 33 | } |
| 34 | |
| 35 | // Clean up main server |
| 36 | if (mockServer) { |
| 37 | await cleanupMockLLMServer(mockServer); |
| 38 | mockServer = undefined as any; |
| 39 | } |
| 40 | |
| 41 | // Clean up context last |
| 42 | if (context) { |
| 43 | await cleanupTestContext(context); |
| 44 | context = undefined; |
| 45 | } |
| 46 | |
| 47 | // Small delay to ensure cleanup completes |
| 48 | await new Promise(resolve => setTimeout(resolve, 50)); |
| 49 | }); |
| 50 | |
| 51 | it("should respond dynamically based on prompt content", async () => { |
| 52 | context = await createTestContext(); |
| 53 | |
| 54 | // Create a mock server with dynamic responses |
| 55 | mockServer = await setupMockLLMTest(context, { |
| 56 | response: (prompt: string) => { |
| 57 | if (prompt.includes("weather")) { |
| 58 | return "It's sunny and 72°F today!"; |
| 59 | } else if (prompt.includes("time")) { |
| 60 | return "The current time is 3:30 PM."; |
| 61 | } else { |
| 62 | return "I don't understand your question."; |
| 63 | } |
| 64 | }, |
| 65 | }); |
| 66 | |
| 67 | // Test weather query |
| 68 | const weatherResult = await runCLI(context, { |
| 69 | args: ["-p", "--config", context.configPath, "What's the weather like?"], |
| 70 | timeout: 15000, |
| 71 | }); |
| 72 | expect(weatherResult.stdout).toContain("It's sunny and 72°F today!"); |
| 73 | |
| 74 | // Test time query |
| 75 | const timeResult = await runCLI(context, { |
| 76 | args: ["-p", "--config", context.configPath, "What time is it?"], |
| 77 | timeout: 15000, |
| 78 | }); |
| 79 | expect(timeResult.stdout).toContain("The current time is 3:30 PM."); |
| 80 | |
| 81 | // Test unknown query |
| 82 | const unknownResult = await runCLI(context, { |
| 83 | args: ["-p", "--config", context.configPath, "Tell me about quantum physics"], |
| 84 | timeout: 15000, |
| 85 | }); |
| 86 | expect(unknownResult.stdout).toContain("I don't understand your question."); |
| 87 | |
| 88 | // Verify all requests were tracked |
| 89 | expect(mockServer.requests).toHaveLength(3); |
| 90 | }); |
| 91 | |
| 92 | it("should work with non-streaming responses", async () => { |
| 93 | context = await createTestContext(); |
| 94 | |
| 95 | mockServer = await setupMockLLMTest(context, { |
| 96 | response: "Non-streaming response", |
| 97 | streaming: false, |
| 98 | }); |
| 99 | |
| 100 | const result = await runCLI(context, { |
| 101 | args: ["-p", "--config", context.configPath, "Test non-streaming"], |
| 102 | timeout: 15000, |
| 103 | }); |
| 104 | |
| 105 | expect(result.stdout).toContain("Non-streaming response"); |
| 106 | expect(result.exitCode).toBe(0); |
| 107 | }, 20000); |
| 108 | |
| 109 | it("should handle multiple mock servers for different models", async () => { |
| 110 | context = await createTestContext(); |
| 111 | |
| 112 | // Create two different mock servers and track them for cleanup |
| 113 | const gpt4Server = await createMockLLMServer({ |
| 114 | response: "Response from GPT-4", |
| 115 | }); |
| 116 | extraServers.push(gpt4Server); |
| 117 | |
| 118 | const claudeServer = await createMockLLMServer({ |
| 119 | response: "Response from Claude", |
| 120 | }); |
| 121 | extraServers.push(claudeServer); |
| 122 | |
| 123 | // Create a config with multiple models |
| 124 | const configContent = `name: Multi-Model Assistant |
| 125 | version: 1.0.0 |
| 126 | schema: v1 |
| 127 | models: |
| 128 | - name: test-gpt-4 |
| 129 | model: gpt-4 |
| 130 | provider: openai |
| 131 | apiKey: test-key-1 |
| 132 | apiBase: ${gpt4Server.url} |
| 133 | roles: |
| 134 | - chat |
| 135 | - name: test-claude |
| 136 | model: claude-2 |
| 137 | provider: anthropic |
| 138 | apiKey: test-key-2 |
| 139 | apiBase: ${claudeServer.url} |
| 140 | `; |
| 141 | |
| 142 | const configPath = path.join(context.testDir, "multi-model-config.yaml"); |
| 143 | await fs.writeFile(configPath, configContent); |
| 144 | context.configPath = configPath; |
| 145 | |
| 146 | // Create onboarding flag |
| 147 | const onboardingFlagPath = path.join(context.testDir, ".continue", ".onboarding_complete"); |
| 148 | await fs.mkdir(path.dirname(onboardingFlagPath), { recursive: true }); |
| 149 | await fs.writeFile(onboardingFlagPath, new Date().toISOString()); |
| 150 | |
| 151 | // Test with the first model (GPT-4 should be default) |
| 152 | const result = await runCLI(context, { |
| 153 | args: ["-p", "--config", context.configPath, "Hello"], |
| 154 | timeout: 15000, |
| 155 | }); |
| 156 | |
| 157 | expect(result.stdout).toContain("Response from GPT-4"); |
| 158 | expect(gpt4Server.requests).toHaveLength(1); |
| 159 | expect(claudeServer.requests).toHaveLength(0); |
| 160 | |
| 161 | // Servers will be cleaned up in afterEach |
| 162 | }); |
| 163 | |
| 164 | it("should allow custom request handling", async () => { |
| 165 | context = await createTestContext(); |
| 166 | |
| 167 | let requestCount = 0; |
| 168 | const responses = ["First response", "Second response", "Third response"]; |
| 169 | |
| 170 | // Create server with custom handler |
| 171 | mockServer = await createMockLLMServer({ |
| 172 | customHandler: (req, res) => { |
| 173 | if (req.method === "POST" && req.url === "/chat/completions") { |
| 174 | const response = responses[requestCount % responses.length]; |
| 175 | requestCount++; |
| 176 | |
| 177 | res.writeHead(200, { |
| 178 | "Content-Type": "text/event-stream", |
| 179 | "Cache-Control": "no-cache", |
| 180 | "Connection": "keep-alive", |
| 181 | }); |
| 182 | |
| 183 | res.write(`data: {"choices":[{"delta":{"content":"${response}"},"index":0}]}\n\n`); |
| 184 | res.write(`data: [DONE]\n\n`); |
| 185 | res.end(); |
| 186 | } else { |
| 187 | res.writeHead(404); |
| 188 | res.end(); |
| 189 | } |
| 190 | }, |
| 191 | }); |
| 192 | |
| 193 | // Set up the test environment |
| 194 | const configContent = createMockLLMConfig(mockServer); |
| 195 | const configPath = path.join(context.testDir, "test-config.yaml"); |
| 196 | await fs.writeFile(configPath, configContent); |
| 197 | context.configPath = configPath; |
| 198 | |
| 199 | const onboardingFlagPath = path.join(context.testDir, ".continue", ".onboarding_complete"); |
| 200 | await fs.mkdir(path.dirname(onboardingFlagPath), { recursive: true }); |
| 201 | await fs.writeFile(onboardingFlagPath, new Date().toISOString()); |
| 202 | |
| 203 | // Make multiple requests |
| 204 | for (let i = 0; i < 3; i++) { |
| 205 | const result = await runCLI(context, { |
| 206 | args: ["-p", "--config", context.configPath, `Request ${i + 1}`], |
| 207 | timeout: 15000, |
| 208 | }); |
| 209 | expect(result.stdout).toContain(responses[i]); |
| 210 | } |
| 211 | }); |
| 212 | }); |