forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliServerManagerTest.java
More file actions
253 lines (208 loc) · 9.55 KB
/
CliServerManagerTest.java
File metadata and controls
253 lines (208 loc) · 9.55 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.json.CopilotClientOptions;
import com.github.copilot.sdk.json.TelemetryConfig;
/**
* Unit tests for {@link CliServerManager} covering parseCliUrl,
* connectToServer, resolveCliCommand, and ProcessInfo coverage gaps identified
* by JaCoCo.
*/
class CliServerManagerTest {
// ===== parseCliUrl tests =====
@Test
void parseCliUrlWithPortNumber() {
URI uri = CliServerManager.parseCliUrl("8080");
assertEquals("http://localhost:8080", uri.toString());
}
@Test
void parseCliUrlWithHostColonPort() {
URI uri = CliServerManager.parseCliUrl("myhost:9090");
assertEquals("https://myhost:9090", uri.toString());
}
@Test
void parseCliUrlWithHttpPrefix() {
URI uri = CliServerManager.parseCliUrl("http://example.com:3000");
assertEquals("http://example.com:3000", uri.toString());
}
@Test
void parseCliUrlWithHttpsPrefix() {
URI uri = CliServerManager.parseCliUrl("https://secure.host:443");
assertEquals("https://secure.host:443", uri.toString());
}
@Test
void parseCliUrlWithHostOnly() {
URI uri = CliServerManager.parseCliUrl("copilot.example.com");
assertEquals("https://copilot.example.com", uri.toString());
}
// ===== connectToServer tests =====
@Test
void connectToServerTcpMode() throws Exception {
var options = new CopilotClientOptions();
var manager = new CliServerManager(options);
// Start a temporary server socket to connect to
try (ServerSocket ss = new ServerSocket(0)) {
int port = ss.getLocalPort();
JsonRpcClient client = manager.connectToServer(null, "localhost", port);
assertNotNull(client);
client.close();
}
}
private static Process startBlockingProcess() throws IOException {
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
return (isWindows ? new ProcessBuilder("cmd", "/c", "more") : new ProcessBuilder("cat")).start();
}
@Test
void connectToServerStdioMode() throws Exception {
var options = new CopilotClientOptions();
var manager = new CliServerManager(options);
// Create a dummy process for stdio mode
Process process = startBlockingProcess();
try {
JsonRpcClient client = manager.connectToServer(process, null, null);
assertNotNull(client);
client.close();
} finally {
process.destroyForcibly();
}
}
@Test
void connectToServerNoProcessNoHost() {
var options = new CopilotClientOptions();
var manager = new CliServerManager(options);
var ex = assertThrows(IllegalStateException.class, () -> manager.connectToServer(null, null, null));
assertTrue(ex.getMessage().contains("Cannot connect"));
}
@Test
void connectToServerNullHostNonNullPort() {
var options = new CopilotClientOptions();
var manager = new CliServerManager(options);
// tcpHost is null but tcpPort is non-null → falls to process check → process
// null → exception
var ex = assertThrows(IllegalStateException.class, () -> manager.connectToServer(null, null, 8080));
assertTrue(ex.getMessage().contains("Cannot connect"));
}
// ===== ProcessInfo record tests =====
@Test
void processInfoRecord() {
var info = new CliServerManager.ProcessInfo(null, 12345);
assertNull(info.process());
assertEquals(12345, info.port());
}
@Test
void processInfoWithNullPort() {
var info = new CliServerManager.ProcessInfo(null, null);
assertNull(info.process());
assertNull(info.port());
}
// ===== resolveCliCommand tests (via startCliServer) =====
// resolveCliCommand is private, so we test indirectly through startCliServer
// with specific cliPath values.
// On Windows, "/nonexistent/copilot" is not an absolute path (no drive letter),
// so resolveCliCommand wraps it with "cmd /c" and ProcessBuilder.start()
// succeeds
// (launching cmd.exe). Use a Windows-absolute path to ensure IOException.
private static final String NONEXISTENT_CLI = System.getProperty("os.name").toLowerCase().contains("win")
? "C:\\nonexistent\\copilot"
: "/nonexistent/copilot";
@Test
void startCliServerWithJsFile() throws Exception {
// Using a .js file path causes resolveCliCommand to prepend "node"
// node is on PATH so the process starts, but the script doesn't exist
// so node exits quickly — verifying the .js branch was taken
var options = new CopilotClientOptions().setCliPath("/nonexistent/script.js").setUseStdio(true);
var manager = new CliServerManager(options);
try {
var info = manager.startCliServer();
// If process started, clean it up
info.process().destroyForcibly();
} catch (IOException e) {
// Expected — node may fail or not be present; either way the branch is hit
assertNotNull(e);
}
}
@Test
void startCliServerWithCliArgs() throws Exception {
// Test that cliArgs are included in the command
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setCliArgs(new String[]{"--extra-flag"})
.setUseStdio(true);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
@Test
void startCliServerWithExplicitPort() throws Exception {
// Test the explicit port branch (useStdio=false, port > 0)
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setUseStdio(false).setPort(9999);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
@Test
void startCliServerWithGitHubToken() throws Exception {
// Test the github token branch
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setGitHubToken("ghp_test123")
.setUseStdio(true);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
@Test
void startCliServerWithUseLoggedInUserExplicit() throws Exception {
// Test the explicit useLoggedInUser=false branch (adds --no-auto-login)
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setUseLoggedInUser(false)
.setUseStdio(true);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
@Test
void startCliServerWithGitHubTokenAndNoExplicitUseLoggedInUser() throws Exception {
// When gitHubToken is set and useLoggedInUser is null, defaults to false
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setGitHubToken("ghp_test123")
.setUseStdio(true);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
@Test
void startCliServerWithNullCliPath() throws Exception {
// Test the default cliPath branch (defaults to "copilot" when not set)
var options = new CopilotClientOptions().setUseStdio(true);
var manager = new CliServerManager(options);
// "copilot" likely doesn't exist in the test env — that's fine
try {
var info = manager.startCliServer();
info.process().destroyForcibly();
} catch (IOException e) {
// Expected if "copilot" is not on PATH
assertNotNull(e);
}
}
@Test
void startCliServerWithTelemetryAllOptions() throws Exception {
// The telemetry env vars are applied before ProcessBuilder.start()
// so even with a nonexistent CLI path, the telemetry code path is exercised
var telemetry = new TelemetryConfig().setOtlpEndpoint("http://localhost:4318").setFilePath("/tmp/telemetry.log")
.setExporterType("otlp-http").setSourceName("test-app").setCaptureContent(true);
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setTelemetry(telemetry).setUseStdio(true);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
@Test
void startCliServerWithTelemetryCaptureContentFalse() throws Exception {
// Test the false branch of getCaptureContent()
var telemetry = new TelemetryConfig().setCaptureContent(false);
var options = new CopilotClientOptions().setCliPath(NONEXISTENT_CLI).setTelemetry(telemetry).setUseStdio(true);
var manager = new CliServerManager(options);
var ex = assertThrows(IOException.class, () -> manager.startCliServer());
assertNotNull(ex);
}
}