Repository navigation
Expand file tree
/
Copy pathCommandsE2ETests.cs
More file actions
324 lines (274 loc) · 11.3 KB
/
Copy pathCommandsE2ETests.cs
File metadata and controls
324 lines (274 loc) · 11.3 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.Rpc;
using GitHub.Copilot.Test.Harness;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.Test.E2E;
public class CommandsE2ETests(E2ETestFixture fixture, ITestOutputHelper output)
: E2ETestBase(fixture, "commands", output)
{
private static readonly string[] KnownBuiltinCommands = ["help", "model", "compact"];
[Fact]
public async Task Session_Commands_List_Returns_Builtins_And_Respects_Client_Command_Filter()
{
var session = await CreateSessionAsync(new SessionConfig
{
Commands =
[
new CommandDefinition { Name = "deploy", Description = "Deploy the app", Handler = _ => Task.CompletedTask },
new CommandDefinition { Name = "rollback", Description = "Rollback the app", Handler = _ => Task.CompletedTask },
],
});
CommandList? clientCommands = null;
await TestHelper.WaitForConditionAsync(
async () =>
{
clientCommands = await session.Rpc.Commands.ListAsync(new SessionCommandsListRequest
{
IncludeBuiltins = false,
IncludeClientCommands = true,
IncludeSkills = false,
});
return clientCommands.Commands.Any(c => IsCommand(c, "deploy", SlashCommandKind.Client)) &&
clientCommands.Commands.Any(c => IsCommand(c, "rollback", SlashCommandKind.Client));
},
timeout: TimeSpan.FromSeconds(30),
timeoutMessage: "Timed out waiting for client commands to be listed.");
Assert.Contains(clientCommands!.Commands, c => IsCommand(c, "deploy", SlashCommandKind.Client));
Assert.Contains(clientCommands.Commands, c => IsCommand(c, "rollback", SlashCommandKind.Client));
Assert.DoesNotContain(clientCommands.Commands, c => c.Kind == SlashCommandKind.Builtin);
var builtinCommands = await session.Rpc.Commands.ListAsync(new SessionCommandsListRequest
{
IncludeBuiltins = true,
IncludeClientCommands = false,
IncludeSkills = false,
});
Assert.True(
builtinCommands.Commands.Any(IsKnownBuiltin),
$"Expected a known built-in command. Actual commands: {FormatCommands(builtinCommands.Commands)}");
Assert.DoesNotContain(builtinCommands.Commands, c => string.Equals(c.Name, "deploy", StringComparison.OrdinalIgnoreCase));
await session.DisposeAsync();
}
[Fact]
public async Task Session_Commands_Invoke_Known_Builtin_Returns_Expected_Result()
{
var session = await CreateSessionAsync();
var builtinCommands = await session.Rpc.Commands.ListAsync(new SessionCommandsListRequest
{
IncludeBuiltins = true,
IncludeClientCommands = false,
IncludeSkills = false,
});
var commandName = KnownBuiltinCommands.FirstOrDefault(name =>
builtinCommands.Commands.Any(c => IsCommand(c, name, SlashCommandKind.Builtin)));
Assert.NotNull(commandName);
var result = await session.Rpc.Commands.InvokeAsync(commandName);
switch (result)
{
case SlashCommandInvocationResultText text:
Assert.False(string.IsNullOrWhiteSpace(text.Text));
break;
case SlashCommandInvocationResultSelectSubcommand select:
Assert.False(string.IsNullOrWhiteSpace(select.Title));
Assert.NotEmpty(select.Options);
break;
case SlashCommandInvocationResultAgentPrompt prompt:
Assert.False(string.IsNullOrWhiteSpace(prompt.DisplayPrompt));
Assert.False(string.IsNullOrWhiteSpace(prompt.Prompt));
break;
case SlashCommandInvocationResultCompleted completed:
Assert.True(completed.Message is null || !string.IsNullOrWhiteSpace(completed.Message));
break;
default:
Assert.Fail($"Unexpected invocation result: {result.GetType().Name}");
break;
}
await session.DisposeAsync();
}
[Fact]
public async Task Session_Commands_Execute_Runs_Registered_Command_Handler()
{
CommandContext? capturedContext = null;
var session = await CreateSessionAsync(new SessionConfig
{
Commands =
[
new CommandDefinition
{
Name = "deploy",
Description = "Deploy the app",
Handler = ctx =>
{
capturedContext = ctx;
return Task.CompletedTask;
},
},
],
});
await TestHelper.WaitForConditionAsync(
async () =>
{
var commands = await session.Rpc.Commands.ListAsync(new SessionCommandsListRequest
{
IncludeBuiltins = false,
IncludeClientCommands = true,
IncludeSkills = false,
});
return commands.Commands.Any(c => IsCommand(c, "deploy", SlashCommandKind.Client));
},
timeout: TimeSpan.FromSeconds(30),
timeoutMessage: "Timed out waiting for registered command to be listed.");
var result = await session.Rpc.Commands.ExecuteAsync("deploy", "production");
Assert.Null(result.Error);
await TestHelper.WaitForConditionAsync(
() => capturedContext is not null,
timeout: TimeSpan.FromSeconds(10),
timeoutMessage: "Timed out waiting for command handler execution.");
Assert.Equal(session.SessionId, capturedContext!.SessionId);
Assert.Equal("/deploy production", capturedContext.Command);
Assert.Equal("deploy", capturedContext.CommandName);
Assert.Equal("production", capturedContext.Args);
await session.DisposeAsync();
}
[Fact]
public async Task Session_Commands_Enqueue_Accepts_Deterministic_Command()
{
var session = await CreateSessionAsync();
var result = await session.Rpc.Commands.EnqueueAsync("/help");
Assert.True(result.Queued);
await session.DisposeAsync();
}
[Fact]
public async Task Session_Commands_RespondToQueuedCommand_Returns_False_For_Unknown_RequestId()
{
var session = await CreateSessionAsync();
var result = await session.Rpc.Commands.RespondToQueuedCommandAsync(
"missing-queued-command-request",
new QueuedCommandResult { Handled = false });
Assert.False(result.Success);
await session.DisposeAsync();
}
[Fact]
public async Task Session_With_Commands_Creates_Successfully()
{
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Commands =
[
new CommandDefinition { Name = "deploy", Description = "Deploy the app", Handler = _ => Task.CompletedTask },
new CommandDefinition { Name = "rollback", Handler = _ => Task.CompletedTask },
],
});
// Session should be created successfully with commands
Assert.NotNull(session);
Assert.NotNull(session.SessionId);
await session.DisposeAsync();
}
[Fact]
public async Task Session_With_Commands_Resumes_Successfully()
{
await using var session1 = await CreateSessionAsync();
var sessionId = session1.SessionId;
await SuspendAndUntrackSessionForResumeAsync(session1);
var session2 = await ResumeSessionAsync(sessionId, new ResumeSessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Commands =
[
new CommandDefinition { Name = "deploy", Description = "Deploy", Handler = _ => Task.CompletedTask },
],
});
Assert.NotNull(session2);
Assert.Equal(sessionId, session2.SessionId);
await session2.DisposeAsync();
}
[Fact]
public void CommandDefinition_Has_Required_Properties()
{
var cmd = new CommandDefinition
{
Name = "deploy",
Description = "Deploy the app",
Handler = _ => Task.CompletedTask,
};
Assert.Equal("deploy", cmd.Name);
Assert.Equal("Deploy the app", cmd.Description);
Assert.NotNull(cmd.Handler);
}
[Fact]
public void CommandContext_Has_All_Properties()
{
var ctx = new CommandContext
{
SessionId = "session-1",
Command = "/deploy production",
CommandName = "deploy",
Args = "production",
};
Assert.Equal("session-1", ctx.SessionId);
Assert.Equal("/deploy production", ctx.Command);
Assert.Equal("deploy", ctx.CommandName);
Assert.Equal("production", ctx.Args);
}
[Fact]
public async Task Session_With_No_Commands_Creates_Successfully()
{
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
});
Assert.NotNull(session);
await session.DisposeAsync();
}
[Fact]
public async Task Session_Config_Commands_Are_Cloned()
{
var config = new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Commands =
[
new CommandDefinition { Name = "deploy", Handler = _ => Task.CompletedTask },
],
};
var clone = config.Clone();
Assert.NotNull(clone.Commands);
Assert.Single(clone.Commands!);
Assert.Equal("deploy", clone.Commands![0].Name);
// Verify collections are independent
clone.Commands!.Add(new CommandDefinition { Name = "rollback", Handler = _ => Task.CompletedTask });
Assert.Single(config.Commands!);
}
[Fact]
public void Resume_Config_Commands_Are_Cloned()
{
var config = new ResumeSessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
Commands =
[
new CommandDefinition { Name = "deploy", Handler = _ => Task.CompletedTask },
],
};
var clone = config.Clone();
Assert.NotNull(clone.Commands);
Assert.Single(clone.Commands!);
Assert.Equal("deploy", clone.Commands![0].Name);
}
private static bool IsCommand(SlashCommandInfo command, string name, SlashCommandKind kind)
{
return string.Equals(command.Name, name, StringComparison.OrdinalIgnoreCase) && command.Kind == kind;
}
private static bool IsKnownBuiltin(SlashCommandInfo command)
{
return command.Kind == SlashCommandKind.Builtin &&
KnownBuiltinCommands.Contains(command.Name, StringComparer.OrdinalIgnoreCase);
}
private static string FormatCommands(IEnumerable<SlashCommandInfo> commands)
{
return string.Join(", ", commands.Select(c => $"{c.Name}:{c.Kind.Value}"));
}
}