forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigCloneTest.java
More file actions
329 lines (258 loc) · 11.6 KB
/
ConfigCloneTest.java
File metadata and controls
329 lines (258 loc) · 11.6 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.generated.SessionEvent;
import com.github.copilot.sdk.json.CopilotClientOptions;
import com.github.copilot.sdk.json.DefaultAgentConfig;
import com.github.copilot.sdk.json.InfiniteSessionConfig;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.ModelInfo;
import com.github.copilot.sdk.json.ResumeSessionConfig;
import com.github.copilot.sdk.json.SessionConfig;
import com.github.copilot.sdk.json.SystemMessageConfig;
import com.github.copilot.sdk.json.TelemetryConfig;
class ConfigCloneTest {
@Test
void copilotClientOptionsCloneBasic() {
CopilotClientOptions original = new CopilotClientOptions();
original.setCliPath("/usr/local/bin/copilot");
original.setLogLevel("debug");
original.setPort(9000);
original.setGitHubToken("ghp_test");
original.setUseLoggedInUser(false);
CopilotClientOptions cloned = original.clone();
assertEquals(original.getCliPath(), cloned.getCliPath());
assertEquals(original.getLogLevel(), cloned.getLogLevel());
assertEquals(original.getPort(), cloned.getPort());
assertEquals(original.getGitHubToken(), cloned.getGitHubToken());
assertEquals(original.getUseLoggedInUser(), cloned.getUseLoggedInUser());
}
@Test
void copilotClientOptionsArrayIndependence() {
CopilotClientOptions original = new CopilotClientOptions();
String[] args = {"--flag1", "--flag2"};
original.setCliArgs(args);
CopilotClientOptions cloned = original.clone();
// Mutate the source array after set — should not affect original or clone
args[0] = "--changed";
assertEquals("--flag1", original.getCliArgs()[0]);
assertEquals("--flag1", cloned.getCliArgs()[0]);
// getCliArgs() returns a copy, so mutating it should not affect internals
original.getCliArgs()[0] = "--mutated";
assertEquals("--flag1", original.getCliArgs()[0]);
}
@Test
void copilotClientOptionsEnvironmentIndependence() {
CopilotClientOptions original = new CopilotClientOptions();
Map<String, String> env = new HashMap<>();
env.put("KEY1", "value1");
original.setEnvironment(env);
CopilotClientOptions cloned = original.clone();
// Mutate the source map after set — should not affect original or clone
env.put("KEY2", "value2");
assertEquals(1, original.getEnvironment().size());
assertEquals(1, cloned.getEnvironment().size());
// getEnvironment() returns a copy, so mutating it should not affect internals
original.getEnvironment().put("KEY3", "value3");
assertEquals(1, original.getEnvironment().size());
}
@Test
void copilotClientOptionsOnListModelsCloned() {
CopilotClientOptions original = new CopilotClientOptions();
List<ModelInfo> models = List.of(new ModelInfo());
original.setOnListModels(() -> CompletableFuture.completedFuture(models));
CopilotClientOptions cloned = original.clone();
assertNotNull(cloned.getOnListModels());
assertSame(original.getOnListModels(), cloned.getOnListModels());
}
@Test
void sessionConfigCloneBasic() {
SessionConfig original = new SessionConfig();
original.setSessionId("my-session");
original.setClientName("my-app");
original.setModel("gpt-4o");
original.setStreaming(true);
SessionConfig cloned = original.clone();
assertEquals(original.getSessionId(), cloned.getSessionId());
assertEquals(original.getClientName(), cloned.getClientName());
assertEquals(original.getModel(), cloned.getModel());
assertEquals(original.isStreaming(), cloned.isStreaming());
}
@Test
void sessionConfigListIndependence() {
SessionConfig original = new SessionConfig();
List<String> toolList = new ArrayList<>();
toolList.add("grep");
toolList.add("bash");
original.setAvailableTools(toolList);
SessionConfig cloned = original.clone();
// Mutate the original list directly to test independence
toolList.add("web");
// The cloned config should be unaffected by mutations to the original list
assertEquals(2, cloned.getAvailableTools().size());
assertEquals(3, original.getAvailableTools().size());
}
@Test
void sessionConfigAgentAndOnEventCloned() {
Consumer<SessionEvent> handler = event -> {
};
SessionConfig original = new SessionConfig();
original.setAgent("my-agent");
original.setOnEvent(handler);
SessionConfig cloned = original.clone();
assertEquals("my-agent", cloned.getAgent());
assertSame(handler, cloned.getOnEvent());
}
@Test
void resumeSessionConfigCloneBasic() {
ResumeSessionConfig original = new ResumeSessionConfig();
original.setModel("o1");
original.setStreaming(false);
ResumeSessionConfig cloned = original.clone();
assertEquals(original.getModel(), cloned.getModel());
assertEquals(original.isStreaming(), cloned.isStreaming());
}
@Test
void resumeSessionConfigAgentAndOnEventCloned() {
Consumer<SessionEvent> handler = event -> {
};
ResumeSessionConfig original = new ResumeSessionConfig();
original.setAgent("my-agent");
original.setOnEvent(handler);
ResumeSessionConfig cloned = original.clone();
assertEquals("my-agent", cloned.getAgent());
assertSame(handler, cloned.getOnEvent());
}
@Test
void messageOptionsCloneBasic() {
MessageOptions original = new MessageOptions();
original.setPrompt("What is 2+2?");
original.setMode("immediate");
MessageOptions cloned = original.clone();
assertEquals(original.getPrompt(), cloned.getPrompt());
assertEquals(original.getMode(), cloned.getMode());
}
@Test
void clonePreservesNullFields() {
CopilotClientOptions opts = new CopilotClientOptions();
CopilotClientOptions optsClone = opts.clone();
assertNull(optsClone.getCliPath());
SessionConfig cfg = new SessionConfig();
SessionConfig cfgClone = cfg.clone();
assertNull(cfgClone.getModel());
MessageOptions msg = new MessageOptions();
MessageOptions msgClone = msg.clone();
assertNull(msgClone.getMode());
}
@Test
@SuppressWarnings("deprecation")
void copilotClientOptionsDeprecatedAutoRestart() {
CopilotClientOptions opts = new CopilotClientOptions();
assertFalse(opts.isAutoRestart());
opts.setAutoRestart(true);
assertTrue(opts.isAutoRestart());
}
@Test
void copilotClientOptionsSetCliArgsNullClearsExisting() {
CopilotClientOptions opts = new CopilotClientOptions();
opts.setCliArgs(new String[]{"--flag1"});
assertNotNull(opts.getCliArgs());
// Setting null should clear the existing array
opts.setCliArgs(null);
assertNotNull(opts.getCliArgs());
assertEquals(0, opts.getCliArgs().length);
}
@Test
void copilotClientOptionsSetEnvironmentNullClearsExisting() {
CopilotClientOptions opts = new CopilotClientOptions();
opts.setEnvironment(Map.of("KEY", "VALUE"));
assertNotNull(opts.getEnvironment());
// Setting null should clear the existing map (clears in-place → returns empty
// map)
opts.setEnvironment(null);
var env = opts.getEnvironment();
assertTrue(env == null || env.isEmpty());
}
@Test
@SuppressWarnings("deprecation")
void copilotClientOptionsDeprecatedGithubToken() {
CopilotClientOptions opts = new CopilotClientOptions();
opts.setGithubToken("ghp_deprecated_token");
assertEquals("ghp_deprecated_token", opts.getGithubToken());
assertEquals("ghp_deprecated_token", opts.getGitHubToken());
}
@Test
void copilotClientOptionsSetTelemetry() {
var telemetry = new TelemetryConfig().setOtlpEndpoint("http://localhost:4318");
var opts = new CopilotClientOptions();
opts.setTelemetry(telemetry);
assertSame(telemetry, opts.getTelemetry());
}
@Test
void copilotClientOptionsSetUseLoggedInUserNull() {
var opts = new CopilotClientOptions();
opts.setUseLoggedInUser(null);
// null → Boolean.FALSE
assertEquals(Boolean.FALSE, opts.getUseLoggedInUser());
}
@Test
void resumeSessionConfigAllSetters() {
var config = new ResumeSessionConfig();
var sysMsg = new SystemMessageConfig();
config.setSystemMessage(sysMsg);
assertSame(sysMsg, config.getSystemMessage());
config.setAvailableTools(List.of("bash", "read_file"));
assertEquals(List.of("bash", "read_file"), config.getAvailableTools());
config.setExcludedTools(List.of("write_file"));
assertEquals(List.of("write_file"), config.getExcludedTools());
config.setReasoningEffort("high");
assertEquals("high", config.getReasoningEffort());
config.setWorkingDirectory("/project/src");
assertEquals("/project/src", config.getWorkingDirectory());
config.setConfigDir("/home/user/.config/copilot");
assertEquals("/home/user/.config/copilot", config.getConfigDir());
config.setSkillDirectories(List.of("/skills/custom"));
assertEquals(List.of("/skills/custom"), config.getSkillDirectories());
config.setDisabledSkills(List.of("some-skill"));
assertEquals(List.of("some-skill"), config.getDisabledSkills());
var infiniteConfig = new InfiniteSessionConfig().setEnabled(true);
config.setInfiniteSessions(infiniteConfig);
assertSame(infiniteConfig, config.getInfiniteSessions());
}
@Test
void sessionConfigNewFieldsCloned() {
SessionConfig original = new SessionConfig();
original.setGitHubToken("ghp_per_session_token");
DefaultAgentConfig defaultAgent = new DefaultAgentConfig().setExcludedTools(List.of("secret_tool"));
original.setDefaultAgent(defaultAgent);
SessionConfig cloned = original.clone();
assertEquals("ghp_per_session_token", cloned.getGitHubToken());
assertSame(defaultAgent, cloned.getDefaultAgent());
}
@Test
void resumeSessionConfigNewFieldsCloned() {
ResumeSessionConfig original = new ResumeSessionConfig();
original.setGitHubToken("ghp_per_session_token");
DefaultAgentConfig defaultAgent = new DefaultAgentConfig().setExcludedTools(List.of("secret_tool"));
original.setDefaultAgent(defaultAgent);
ResumeSessionConfig cloned = original.clone();
assertEquals("ghp_per_session_token", cloned.getGitHubToken());
assertSame(defaultAgent, cloned.getDefaultAgent());
}
@Test
void copilotClientOptionsSessionIdleTimeoutCloned() {
CopilotClientOptions original = new CopilotClientOptions();
original.setSessionIdleTimeoutSeconds(600);
CopilotClientOptions cloned = original.clone();
assertEquals(600, cloned.getSessionIdleTimeoutSeconds());
}
}