forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateSessionOptionsForModeTest.java
More file actions
260 lines (217 loc) · 12.1 KB
/
Copy pathUpdateSessionOptionsForModeTest.java
File metadata and controls
260 lines (217 loc) · 12.1 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static org.junit.jupiter.api.Assertions.*;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.copilot.rpc.CopilotClientMode;
import com.github.copilot.rpc.CopilotClientOptions;
/**
* Tests for {@link CopilotClient#updateSessionOptionsForMode}.
*/
class UpdateSessionOptionsForModeTest {
private static final ObjectMapper MAPPER = JsonRpcClient.getObjectMapper();
/**
* A connected socket pair where the "server" side auto-replies to every
* JSON-RPC request with {@code {"success": true}}.
*/
private static final class AutoReplyPair implements AutoCloseable {
final Socket clientSocket;
final Socket serverSocket;
final JsonRpcClient rpcClient;
private volatile boolean running = true;
private final Thread replyThread;
/** The last JSON-RPC params node received by the stub server. */
volatile JsonNode lastParams;
/** The last JSON-RPC method received by the stub server. */
volatile String lastMethod;
AutoReplyPair() throws Exception {
try (var ss = new ServerSocket(0)) {
clientSocket = new Socket("localhost", ss.getLocalPort());
serverSocket = ss.accept();
}
serverSocket.setSoTimeout(5000);
rpcClient = JsonRpcClient.fromSocket(clientSocket);
// Background thread that reads requests and sends back success responses
replyThread = new Thread(() -> {
try {
var in = serverSocket.getInputStream();
var out = serverSocket.getOutputStream();
while (running) {
// Read Content-Length header
var header = new StringBuilder();
int b;
while ((b = in.read()) != -1) {
if (b == '\n' && header.toString().endsWith("\r")) {
break;
}
header.append((char) b);
}
if (b == -1)
break;
// Skip blank line
in.read(); // '\r'
in.read(); // '\n'
String hdr = header.toString().trim();
int colon = hdr.indexOf(':');
int len = Integer.parseInt(hdr.substring(colon + 1).trim());
byte[] body = in.readNBytes(len);
JsonNode msg = MAPPER.readTree(body);
lastMethod = msg.get("method").asText();
lastParams = msg.get("params");
long id = msg.get("id").asLong();
// Send back a success response
String response = MAPPER.writeValueAsString(MAPPER.createObjectNode().put("jsonrpc", "2.0")
.put("id", id).set("result", MAPPER.createObjectNode().put("success", true)));
sendRpcMessage(out, response);
}
} catch (Exception e) {
if (running) {
// Ignore expected exceptions on shutdown
}
}
});
replyThread.setDaemon(true);
replyThread.start();
}
private static void sendRpcMessage(OutputStream out, String json) throws Exception {
byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
String header = "Content-Length: " + bytes.length + "\r\n\r\n";
out.write(header.getBytes(StandardCharsets.UTF_8));
out.write(bytes);
out.flush();
}
@Override
public void close() throws Exception {
running = false;
rpcClient.close();
clientSocket.close();
serverSocket.close();
replyThread.join(3000);
}
}
// ── COPILOT_CLI mode tests ────────────────────────────────────────────────
@Test
void copilotCliMode_noFieldsSet_noPatchSent() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
client.updateSessionOptionsForMode(session, null, null, null, null).get();
assertNull(pair.lastMethod, "No RPC call should be made when no fields are set in COPILOT_CLI mode");
client.close();
}
}
@Test
void copilotCliMode_skipCustomInstructionsSet_patchContainsOnlyThatField() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
client.updateSessionOptionsForMode(session, true, null, null, null).get();
assertEquals("session.options.update", pair.lastMethod);
assertTrue(pair.lastParams.get("skipCustomInstructions").asBoolean());
assertTrue(pair.lastParams.path("customAgentsLocalOnly").isMissingNode(),
"customAgentsLocalOnly should be absent");
assertTrue(pair.lastParams.path("coauthorEnabled").isMissingNode(), "coauthorEnabled should be absent");
assertTrue(pair.lastParams.path("manageScheduleEnabled").isMissingNode(),
"manageScheduleEnabled should be absent");
assertTrue(pair.lastParams.path("installedPlugins").isMissingNode(), "installedPlugins should be absent");
client.close();
}
}
@Test
void copilotCliMode_allFieldsSet_allPropagated() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
client.updateSessionOptionsForMode(session, false, true, true, false).get();
assertEquals("session.options.update", pair.lastMethod);
assertFalse(pair.lastParams.get("skipCustomInstructions").asBoolean());
assertTrue(pair.lastParams.get("customAgentsLocalOnly").asBoolean());
assertTrue(pair.lastParams.get("coauthorEnabled").asBoolean());
assertFalse(pair.lastParams.get("manageScheduleEnabled").asBoolean());
client.close();
}
}
@Test
void copilotCliMode_onlyCoauthorEnabled_patchSent() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
client.updateSessionOptionsForMode(session, null, null, true, null).get();
assertEquals("session.options.update", pair.lastMethod);
assertTrue(pair.lastParams.get("coauthorEnabled").asBoolean());
client.close();
}
}
// ── EMPTY mode tests ──────────────────────────────────────────────────────
@Test
void emptyMode_noFieldsSet_safeDefaultsSent() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.EMPTY)
.setCopilotHome("/tmp/copilot-home").setAutoStart(false));
client.updateSessionOptionsForMode(session, null, null, null, null).get();
assertEquals("session.options.update", pair.lastMethod);
assertTrue(pair.lastParams.get("skipCustomInstructions").asBoolean(), "default: skip custom instructions");
assertTrue(pair.lastParams.get("customAgentsLocalOnly").asBoolean(), "default: local agents only");
assertFalse(pair.lastParams.get("coauthorEnabled").asBoolean(), "default: coauthor disabled");
assertFalse(pair.lastParams.get("manageScheduleEnabled").asBoolean(), "default: schedule disabled");
assertTrue(pair.lastParams.get("installedPlugins").isArray(), "installedPlugins should be empty array");
assertEquals(0, pair.lastParams.get("installedPlugins").size());
client.close();
}
}
@Test
void emptyMode_callerOverridesWin() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.EMPTY)
.setCopilotHome("/tmp/copilot-home").setAutoStart(false));
client.updateSessionOptionsForMode(session, false, false, true, true).get();
assertEquals("session.options.update", pair.lastMethod);
assertFalse(pair.lastParams.get("skipCustomInstructions").asBoolean(), "caller override: don't skip");
assertFalse(pair.lastParams.get("customAgentsLocalOnly").asBoolean(), "caller override: not local only");
assertTrue(pair.lastParams.get("coauthorEnabled").asBoolean(), "caller override: coauthor enabled");
assertTrue(pair.lastParams.get("manageScheduleEnabled").asBoolean(), "caller override: schedule enabled");
assertTrue(pair.lastParams.get("installedPlugins").isArray(),
"installedPlugins always empty in EMPTY mode");
assertEquals(0, pair.lastParams.get("installedPlugins").size());
client.close();
}
}
@Test
void emptyMode_partialOverrides_restGetDefaults() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("sess-1", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.EMPTY)
.setCopilotHome("/tmp/copilot-home").setAutoStart(false));
// Only override coauthorEnabled, rest should use safe defaults
client.updateSessionOptionsForMode(session, null, null, true, null).get();
assertEquals("session.options.update", pair.lastMethod);
assertTrue(pair.lastParams.get("skipCustomInstructions").asBoolean(), "default: skip");
assertTrue(pair.lastParams.get("customAgentsLocalOnly").asBoolean(), "default: local only");
assertTrue(pair.lastParams.get("coauthorEnabled").asBoolean(), "override: coauthor enabled");
assertFalse(pair.lastParams.get("manageScheduleEnabled").asBoolean(), "default: schedule disabled");
client.close();
}
}
// ── SessionId injection ───────────────────────────────────────────────────
@Test
void sessionIdInjectedBySessionOptionsApi() throws Exception {
try (var pair = new AutoReplyPair()) {
var session = new CopilotSession("my-session-id", pair.rpcClient);
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
client.updateSessionOptionsForMode(session, true, null, null, null).get();
assertEquals("session.options.update", pair.lastMethod);
assertEquals("my-session-id", pair.lastParams.get("sessionId").asText(),
"SessionOptionsApi should inject sessionId");
client.close();
}
}
}