Elffuss BraidHub

kikocis/continue public

main · 5.2 KiBRenderedRawPermalink
1# End-to-End Testing for Continue CLI
2
3This document describes the principles, practices, and patterns for writing end-to-end (E2E) tests for the Continue CLI.
4
5## Overview
6
7End-to-end tests verify that the CLI functions correctly as a whole by executing the actual CLI binary and validating its behavior from the user's perspective. These tests help ensure that all components work together properly in a real-world environment.
8
9## Key Principles
10
111. **Isolation**: Each test runs in a completely isolated environment with its own temporary directory
122. **Independence**: Tests should not depend on each other and can be run in any order
133. **Realistic Usage**: Tests should simulate how users actually interact with the CLI
144. **Minimal Mocking**: Mock only external dependencies (like LLM APIs) when necessary
155. **Thorough Cleanup**: Tests should clean up after themselves to avoid leaking resources
16
17## Test Structure
18
19A typical E2E test follows this structure:
20
21```typescript
22describe("E2E: Feature", () => {
23 let context: any;
24
25 beforeEach(async () => {
26 context = await createTestContext();
27 });
28
29 afterEach(async () => {
30 await cleanupTestContext(context);
31 });
32
33 it("should do something", async () => {
34 const result = await runCLI(context, {
35 args: ["command", "--flag"],
36 });
37
38 expect(result.exitCode).toBe(0);
39 expect(result.stdout).toContain("Expected output");
40 });
41});
42```
43
44## Test Context
45
46The test context (`createTestContext()`) provides:
47
48- Temporary test directory (`context.testDir`)
49- Path to the CLI binary (`context.cliPath`)
50- Optional paths for config and session files
51
52## Core Testing Utilities
53
54### Basic CLI Execution
55
56```typescript
57// Run CLI with arguments
58const result = await runCLI(context, {
59 args: ["--help"],
60});
61
62// Verify output and exit code
63expect(result.exitCode).toBe(0);
64expect(result.stdout).toContain("Continue CLI");
65```
66
67### Testing with Input
68
69```typescript
70// Run CLI with input
71const result = await runCLI(context, {
72 args: ["-p", "Prompt with input"],
73 input: "User input\n",
74});
75```
76
77### Interactive Testing
78
79```typescript
80// For complex interactive flows
81const result = await withInteractiveInput(
82 context,
83 ["chat"],
84 ["First input", "Second input"],
85);
86```
87
88### Testing Error Cases
89
90```typescript
91// Test error scenarios
92const result = await runCLI(context, {
93 args: ["--invalid-flag"],
94 expectError: true,
95});
96
97expect(result.exitCode).not.toBe(0);
98expect(result.stderr).toContain("error");
99```
100
101## Testing with Mock LLM
102
103For tests involving AI models, we use a mock LLM server:
104
105```typescript
106let mockServer: MockLLMServer;
107
108beforeEach(async () => {
109 context = await createTestContext();
110 mockServer = await setupMockLLMTest(context, {
111 response: "Hello World!",
112 });
113});
114
115afterEach(async () => {
116 await cleanupMockLLMServer(mockServer);
117 await cleanupTestContext(context);
118});
119
120it("should get response from mock LLM", async () => {
121 const result = await runCLI(context, {
122 args: ["-p", "--config", context.configPath, "Hi"],
123 });
124
125 expect(result.stdout).toContain("Hello World!");
126});
127```
128
129### Advanced Mock LLM Features
130
131- **Dynamic responses** based on prompts:
132
133 ```typescript
134 mockServer = await setupMockLLMTest(context, {
135 response: (prompt) =>
136 prompt.includes("weather") ? "It's sunny!" : "I don't know",
137 });
138 ```
139
140- **Non-streaming responses**:
141
142 ```typescript
143 mockServer = await setupMockLLMTest(context, {
144 response: "All at once response",
145 streaming: false,
146 });
147 ```
148
149- **Request tracking**:
150 ```typescript
151 expect(mockServer.requests).toHaveLength(1);
152 expect(mockServer.requests[0].body.messages[0].content).toBe("User prompt");
153 ```
154
155## Testing Sessions and Configuration
156
157### Creating Test Configuration
158
159```typescript
160const configPath = await createTestConfig(context, {
161 // Config object
162});
163```
164
165### Creating and Reading Sessions
166
167```typescript
168// Create a mock session
169await createMockSession(context, [
170 { role: "user", content: "Hello" },
171 { role: "assistant", content: "Hi there!" },
172]);
173
174// Read the session
175const session = await readSession(context);
176```
177
178## Best Practices
179
1801. **Use descriptive test names** that explain what functionality is being tested
1812. **Keep tests focused** on specific functionality or user flows
1823. **Set appropriate timeouts** for tests that may take longer (especially with mock LLM)
1834. **Validate both happy path and error cases**
1845. **Test CLI flags and commands** thoroughly
1856. **Clean up resources** even if tests fail (use try/finally if needed)
1867. **Use realistic inputs** that match how users would interact with the CLI
187
188## Testing Specific Features
189
190### Testing Headless Mode
191
192```typescript
193const result = await runCLI(context, {
194 args: ["-p", "Prompt in headless mode"],
195});
196```
197
198### Testing Interactive Mode
199
200```typescript
201const result = await withInteractiveInput(
202 context,
203 ["chat"],
204 ["User input", "/exit"],
205);
206```
207
208### Testing Subcommands
209
210```typescript
211const result = await runCLI(context, {
212 args: ["login", "--help"],
213});
214```
215
216## Debugging Tests
217
218- Set longer timeouts for complex tests: `}, 30000);` (30 seconds)
219- Examine the full output: `console.log(result.stdout, result.stderr)`
220- Check the test directory: `console.log(context.testDir)`
221- For intermittent failures, try running the test in isolation