forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlashCommandsIT.java
More file actions
242 lines (204 loc) · 10.7 KB
/
Copy pathSlashCommandsIT.java
File metadata and controls
242 lines (204 loc) · 10.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.copilot.generated.rpc.SessionCommandsListResult;
import com.github.copilot.generated.rpc.SessionCommandsInvokeParams;
import com.github.copilot.generated.rpc.SlashCommandAgentPromptResult;
import com.github.copilot.generated.rpc.SlashCommandCompletedResult;
import com.github.copilot.generated.rpc.SlashCommandInfo;
import com.github.copilot.generated.rpc.SlashCommandInvocationResult;
import com.github.copilot.generated.rpc.SlashCommandSelectSubcommandResult;
import com.github.copilot.generated.rpc.SlashCommandTextResult;
import com.github.copilot.rpc.CopilotClientOptions;
import com.github.copilot.rpc.PermissionHandler;
import com.github.copilot.rpc.SessionConfig;
/**
* Failsafe integration test that exercises slash commands against the live
* Copilot CLI (not the replay proxy).
* <p>
* Requires the CLI to be installed and the user to be signed in. Uses
* {@link TestUtil#findCliPath()} so the test harness binary is found in CI.
*/
class SlashCommandsIT {
private static CopilotClient client;
private static CopilotSession session;
@BeforeAll
static void setup() throws Exception {
String cliPath = TestUtil.findCliPath();
CopilotClientOptions options = new CopilotClientOptions().setCliPath(cliPath).setUseLoggedInUser(true);
client = new CopilotClient(options);
client.start().get(30, TimeUnit.SECONDS);
session = client.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL))
.get(30, TimeUnit.SECONDS);
}
@AfterAll
static void teardown() throws Exception {
if (session != null) {
session.close();
}
if (client != null) {
client.close();
}
}
@Test
void listCommandsReturnsAtLeast20() throws Exception {
SessionCommandsListResult result = session.getRpc().commands.list().get(15, TimeUnit.SECONDS);
assertNotNull(result, "commands.list result must not be null");
assertNotNull(result.commands(), "commands list must not be null");
assertTrue(result.commands().size() >= 20, "Expected at least 20 commands but got " + result.commands().size());
Pattern namePattern = Pattern.compile("^[a-z].*$");
// Print every command so we can pick one for the next iteration
System.out.println("=== Available slash commands ===");
for (SlashCommandInfo cmd : result.commands()) {
System.out.printf(" /%s kind=%s desc=%s aliases=%s%n", cmd.name(), cmd.kind(), cmd.description(),
cmd.aliases());
assertTrue(namePattern.matcher(cmd.name()).matches(),
"Command name should match /^[a-z].*$/ but was: " + cmd.name());
}
System.out.println("=== Total: " + result.commands().size() + " commands ===");
}
@Test
void autoPilotToggle() throws Exception {
SlashCommandInvocationResult first = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "autopilot", null)).get(15, TimeUnit.SECONDS);
SlashCommandInvocationResult second = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "autopilot", null)).get(15, TimeUnit.SECONDS);
String firstOutput = extractDisplayText(first);
String secondOutput = extractDisplayText(second);
assertTrue(!firstOutput.isBlank(), "First /autopilot invocation should return non-empty output");
assertTrue(!secondOutput.isBlank(), "Second /autopilot invocation should return non-empty output");
assertNotEquals(firstOutput, secondOutput,
"Two consecutive /autopilot invocations should produce different output because mode toggles");
List<String> firstTokens = tokenizeForComparison(firstOutput);
List<String> secondTokens = tokenizeForComparison(secondOutput);
assertTrue(!firstTokens.isEmpty(), "First /autopilot output should include at least one token");
assertTrue(!secondTokens.isEmpty(), "Second /autopilot output should include at least one token");
List<String> commonInOrder = commonTokensInOrder(firstTokens, secondTokens);
assertTrue(!commonInOrder.isEmpty(),
"Outputs should share at least one token in the same order to indicate similar structure");
Set<String> firstOnly = new HashSet<>(firstTokens);
firstOnly.removeAll(new HashSet<>(secondTokens));
Set<String> secondOnly = new HashSet<>(secondTokens);
secondOnly.removeAll(new HashSet<>(firstTokens));
assertTrue(!firstOnly.isEmpty() || !secondOnly.isEmpty(),
"Outputs should differ by at least one token to reflect the toggle change");
System.out.println("First /autopilot result: " + firstOutput);
System.out.println("Second /autopilot result: " + secondOutput);
}
@Test
void listDirs() throws Exception {
SlashCommandInvocationResult result = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "list-dirs", null)).get(15, TimeUnit.SECONDS);
String output = extractDisplayText(result);
assertTrue(Pattern.compile("(?s)^.*Total: [0-9]+ directories.*$").matcher(output).matches(),
"Expected /list-dirs output to include total directories count");
System.out.println("/list-dirs result:");
System.out.println(output);
}
@Test
void addDir() throws Exception {
String buildDirectory = System.getProperty("project.build.directory");
assertNotNull(buildDirectory, "System property 'project.build.directory' must be set by failsafe");
Path addDirPath = Path.of(buildDirectory, "addDirTest").toAbsolutePath().normalize();
Files.createDirectories(addDirPath);
String addDirPathString = addDirPath.toString();
SlashCommandInvocationResult beforeListResult = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "list-dirs", null)).get(15, TimeUnit.SECONDS);
String beforeListOutput = extractDisplayText(beforeListResult);
System.out.println("/list-dirs (before /add-dir) result:");
System.out.println(beforeListOutput);
SlashCommandInvocationResult addDirResult = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "add-dir", addDirPathString)).get(15, TimeUnit.SECONDS);
String addDirOutput = extractDisplayText(addDirResult);
System.out.println("/add-dir result:");
System.out.println(addDirOutput);
SlashCommandInvocationResult afterListResult = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "list-dirs", null)).get(15, TimeUnit.SECONDS);
String afterListOutput = extractDisplayText(afterListResult);
System.out.println("/list-dirs (after /add-dir) result:");
System.out.println(afterListOutput);
assertTrue(afterListOutput.contains(addDirPathString),
"Expected /list-dirs output to contain added directory path: " + addDirPathString);
}
@Test
void usage() throws Exception {
SlashCommandInvocationResult result = session.getRpc().commands
.invoke(new SessionCommandsInvokeParams(null, "usage", null)).get(15, TimeUnit.SECONDS);
String output = extractDisplayText(result);
assertTrue(Pattern.compile("(?s)^.*Changes:.*$").matcher(output).matches(),
"Expected /usage output to include a Changes summary line");
assertTrue(Pattern.compile("(?s)^.*Requests:.*$").matcher(output).matches(),
"Expected /usage output to include a Requests/AI Units summary line");
System.out.println("/usage result:");
System.out.println(output);
}
private static String extractDisplayText(SlashCommandInvocationResult result) {
assertNotNull(result, "slash command result must not be null");
if (result instanceof SlashCommandTextResult textResult) {
return valueOrEmpty(textResult.getText());
}
if (result instanceof SlashCommandCompletedResult completedResult) {
return valueOrEmpty(completedResult.getMessage());
}
if (result instanceof SlashCommandAgentPromptResult promptResult) {
String display = valueOrEmpty(promptResult.getDisplayPrompt());
if (!display.isBlank()) {
return display;
}
return valueOrEmpty(promptResult.getPrompt());
}
if (result instanceof SlashCommandSelectSubcommandResult selectResult) {
String title = valueOrEmpty(selectResult.getTitle());
if (!title.isBlank()) {
return title;
}
return valueOrEmpty(selectResult.getCommand());
}
return valueOrEmpty(result.getKind());
}
private static String valueOrEmpty(String value) {
return value == null ? "" : value.trim();
}
private static List<String> tokenizeForComparison(String text) {
List<String> tokens = new ArrayList<>();
Pattern wordPattern = Pattern.compile("[\\p{L}\\p{N}]+", Pattern.UNICODE_CHARACTER_CLASS);
var matcher = wordPattern.matcher(text.toLowerCase(Locale.ROOT));
while (matcher.find()) {
tokens.add(matcher.group());
}
return tokens;
}
private static List<String> commonTokensInOrder(List<String> first, List<String> second) {
List<String> common = new ArrayList<>();
int secondIndex = 0;
for (String token : first) {
while (secondIndex < second.size()) {
String candidate = second.get(secondIndex++);
if (token.equals(candidate)) {
common.add(token);
break;
}
}
if (secondIndex >= second.size()) {
break;
}
}
return common;
}
}