-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCapiProxy.java
More file actions
382 lines (338 loc) · 12.7 KB
/
CapiProxy.java
File metadata and controls
382 lines (338 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Manages a replaying proxy server for E2E tests.
*
* <p>
* This spawns the shared test harness server from test/harness/server.ts which
* acts as a replaying proxy to AI endpoints. It captures and stores
* request/response pairs in YAML snapshot files and replays stored responses on
* subsequent runs for deterministic testing.
* </p>
*
* <p>
* Usage example:
* </p>
*
* <pre>
* {@code
* CapiProxy proxy = new CapiProxy();
* String proxyUrl = proxy.start();
*
* // Configure for a specific test
* proxy.configure("test/snapshots/tools/my_test.yaml", workDir);
*
* // ... run tests with proxyUrl ...
*
* // Get captured exchanges
* List<Map<String, Object>> exchanges = proxy.getExchanges();
*
* proxy.stop();
* }
* </pre>
*/
public class CapiProxy implements AutoCloseable {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Pattern LISTENING_PATTERN = Pattern.compile("Listening: (http://[^\\s]+)");
private Process process;
private String proxyUrl;
private final HttpClient httpClient;
private BufferedReader stdoutReader;
public CapiProxy() {
this.httpClient = HttpClient.newHttpClient();
}
/**
* Starts the proxy server and returns its URL.
*
* @return the proxy URL (e.g., "http://localhost:12345")
* @throws IOException
* if the server fails to start
* @throws InterruptedException
* if the startup is interrupted
*/
public String start() throws IOException, InterruptedException {
if (proxyUrl != null) {
return proxyUrl;
}
// Find the repo root by looking for the test/harness directory
Path harnessDir = findHarnessDirectory();
if (harnessDir == null) {
throw new IOException("Could not find test/harness directory. "
+ "Make sure you are running from within the copilot-sdk repository.");
}
// Start the harness server using npx tsx
var pb = new ProcessBuilder("npx", "tsx", "server.ts");
pb.directory(harnessDir.toFile());
pb.redirectErrorStream(false);
process = pb.start();
// Read stdout to get the listening URL
// Note: We keep the reader open to avoid closing the process input stream
stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// Also consume stderr in a background thread to prevent blocking
Thread stderrThread = new Thread(() -> {
try (BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String errLine;
while ((errLine = errReader.readLine()) != null) {
System.err.println("[CapiProxy stderr] " + errLine);
}
} catch (IOException e) {
// Ignore
}
});
stderrThread.setDaemon(true);
stderrThread.start();
String line = stdoutReader.readLine();
if (line == null) {
// Try to get error info
StringBuilder errInfo = new StringBuilder();
try (BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String errLine;
while ((errLine = errReader.readLine()) != null) {
errInfo.append(errLine).append("\n");
}
}
process.destroyForcibly();
throw new IOException("Failed to read proxy URL - server may have crashed. Stderr: " + errInfo);
}
Matcher matcher = LISTENING_PATTERN.matcher(line);
if (!matcher.find()) {
process.destroyForcibly();
throw new IOException("Unexpected proxy output: " + line);
}
proxyUrl = matcher.group(1);
return proxyUrl;
}
/**
* Configures the proxy for a specific test file.
*
* @param filePath
* the path to the YAML snapshot file (relative to repo root)
* @param workDir
* the working directory for path normalization
* @throws IOException
* if the configuration fails
* @throws InterruptedException
* if the request is interrupted
*/
public void configure(String filePath, String workDir) throws IOException, InterruptedException {
configure(filePath, workDir, null);
}
/**
* Configures the proxy for a specific test file.
*
* @param filePath
* the path to the YAML snapshot file (relative to repo root)
* @param workDir
* the working directory for path normalization
* @param testInfo
* optional test information (file and line number)
* @throws IOException
* if the configuration fails
* @throws InterruptedException
* if the request is interrupted
*/
public void configure(String filePath, String workDir, TestInfo testInfo) throws IOException, InterruptedException {
if (proxyUrl == null) {
throw new IllegalStateException("Proxy not started");
}
Map<String, Object> config = new java.util.HashMap<>();
config.put("filePath", filePath);
config.put("workDir", workDir);
if (testInfo != null) {
config.put("testInfo", Map.of("file", testInfo.file(), "line", testInfo.line()));
}
String body = MAPPER.writeValueAsString(config);
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(proxyUrl + "/config"))
.header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(body)).build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new IOException("Proxy config failed with status " + response.statusCode() + ": " + response.body());
}
}
/**
* Gets the captured HTTP exchanges from the proxy.
*
* @return list of exchange maps containing request/response data
* @throws IOException
* if the request fails
* @throws InterruptedException
* if the request is interrupted
*/
public List<Map<String, Object>> getExchanges() throws IOException, InterruptedException {
if (proxyUrl == null) {
throw new IllegalStateException("Proxy not started");
}
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(proxyUrl + "/exchanges")).GET().build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new IOException("Failed to get exchanges: " + response.statusCode());
}
return MAPPER.readValue(response.body(), new TypeReference<List<Map<String, Object>>>() {
});
}
/**
* Stops the proxy server gracefully.
*
* @throws IOException
* if the stop request fails
* @throws InterruptedException
* if the request is interrupted
*/
public void stop() throws IOException, InterruptedException {
stop(false);
}
/**
* Stops the proxy server.
*
* @param skipWritingCache
* if true, won't write captured exchanges to disk
* @throws IOException
* if the stop request fails
* @throws InterruptedException
* if the request is interrupted
*/
public void stop(boolean skipWritingCache) throws IOException, InterruptedException {
if (process == null) {
return;
}
// Send stop request to the server
if (proxyUrl != null) {
try {
String stopUrl = proxyUrl + "/stop";
if (skipWritingCache) {
stopUrl += "?skipWritingCache=true";
}
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(stopUrl))
.POST(HttpRequest.BodyPublishers.noBody()).build();
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (Exception e) {
// Best effort - ignore errors
}
}
// Wait for the process to exit
process.waitFor(5, TimeUnit.SECONDS);
if (process.isAlive()) {
process.destroyForcibly();
}
// Close the stdout reader
if (stdoutReader != null) {
try {
stdoutReader.close();
} catch (IOException e) {
// Ignore
}
stdoutReader = null;
}
process = null;
proxyUrl = null;
}
/**
* Gets the proxy URL.
*
* @return the proxy URL, or null if not started
*/
public String getProxyUrl() {
return proxyUrl;
}
/**
* Checks if the proxy process is still alive and responsive. This does both a
* process alive check AND an HTTP health check.
*
* @return true if the proxy is running and responsive, false otherwise
*/
public boolean isAlive() {
if (process == null || !process.isAlive()) {
return false;
}
// Also verify the proxy is responsive via HTTP
if (proxyUrl != null) {
try {
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) new java.net.URL(proxyUrl + "/exchanges")
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);
int responseCode = conn.getResponseCode();
conn.disconnect();
return responseCode == 200;
} catch (Exception e) {
// If HTTP check fails, the proxy is not responsive
return false;
}
}
return true;
}
/**
* Restarts the proxy server. This stops the current instance (if any) and
* starts a new one.
*
* @return the new proxy URL
* @throws IOException
* if the server fails to start
* @throws InterruptedException
* if the startup is interrupted
*/
public String restart() throws IOException, InterruptedException {
try {
stop(true); // Skip writing cache on restart
} catch (Exception e) {
// Best effort - force cleanup
if (process != null) {
process.destroyForcibly();
process = null;
}
proxyUrl = null;
}
return start();
}
@Override
public void close() throws Exception {
stop();
}
/**
* Finds the test/harness directory by walking up from the current directory.
*/
private Path findHarnessDirectory() {
// First, check for copilot.sdk.dir system property (set by Maven during tests)
String sdkDir = System.getProperty("copilot.sdk.dir");
if (sdkDir != null && !sdkDir.isEmpty()) {
Path harnessDir = Paths.get(sdkDir).resolve("test").resolve("harness");
if (harnessDir.toFile().exists() && harnessDir.resolve("server.ts").toFile().exists()) {
return harnessDir;
}
}
// Fallback: walk up the directory tree looking for test/harness
Path current = Paths.get(System.getProperty("user.dir"));
while (current != null) {
Path harnessDir = current.resolve("test").resolve("harness");
if (harnessDir.toFile().exists() && harnessDir.resolve("server.ts").toFile().exists()) {
return harnessDir;
}
current = current.getParent();
}
return null;
}
/**
* Test information record for configuring the proxy.
*/
public record TestInfo(String file, int line) {
}
}