Repository navigation
Expand file tree
/
Copy pathClientE2ETests.cs
More file actions
369 lines (320 loc) · 13.2 KB
/
Copy pathClientE2ETests.cs
File metadata and controls
369 lines (320 loc) · 13.2 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.Test.Harness;
using Xunit;
namespace GitHub.Copilot.Test.E2E;
// These tests bypass E2ETestBase because they are about how the CLI subprocess is started
// Other test classes should instead inherit from E2ETestBase
public class ClientE2ETests(E2ETestFixture fixture) : IClassFixture<E2ETestFixture>
{
private const string FailingCliScript =
"process.stderr.write('nonexistent test flag on stderr\\n'); process.exit(1);";
private E2ETestContext Ctx => fixture.Ctx;
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Start_And_Connect_To_Server(bool useStdio)
{
using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
try
{
await client.StartAsync();
var pong = await client.PingAsync("test message");
Assert.Equal("pong: test message", pong.Message);
Assert.NotEqual(default, pong.Timestamp);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Fact]
public async Task Should_Start_And_Connect_Over_InProcess_Ffi()
{
// In-process FFI hosting loads the bundled runtime library directly; if it is
// unavailable, StartAsync throws and the test fails hard.
using var client = new CopilotClient(new CopilotClientOptions
{
Connection = RuntimeConnection.ForInProcess(),
});
try
{
await client.StartAsync();
var pong = await client.PingAsync("ffi message");
Assert.Equal("pong: ffi message", pong.Message);
Assert.NotEqual(default, pong.Timestamp);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Force_Stop_Without_Cleanup(bool useStdio)
{
using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
await Ctx.CreateSessionAsync(client, new SessionConfig { OnPermissionRequest = PermissionHandler.ApproveAll });
await client.ForceStopAsync();
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Get_Status_With_Version_And_Protocol_Info(bool useStdio)
{
using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
try
{
await client.StartAsync();
var status = await client.GetStatusAsync();
Assert.NotNull(status.Version);
Assert.NotEmpty(status.Version);
Assert.True(status.ProtocolVersion >= 1);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Get_Auth_Status(bool useStdio)
{
using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
try
{
await client.StartAsync();
var authStatus = await client.GetAuthStatusAsync();
// isAuthenticated is a bool, just verify we got a response
if (authStatus.IsAuthenticated)
{
Assert.NotNull(authStatus.AuthType);
Assert.NotNull(authStatus.StatusMessage);
}
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_List_Models_When_Authenticated(bool useStdio)
{
using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
try
{
await client.StartAsync();
var authStatus = await client.GetAuthStatusAsync();
if (!authStatus.IsAuthenticated)
{
// Skip if not authenticated - models.list requires auth
await client.StopAsync();
return;
}
var models = await client.ListModelsAsync();
Assert.NotNull(models);
if (models.Count > 0)
{
var model = models[0];
Assert.NotNull(model.Id);
Assert.NotEmpty(model.Id);
Assert.NotNull(model.Name);
Assert.NotNull(model.Capabilities);
}
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Not_Throw_When_Disposing_Session_After_Stopping_Client(bool useStdio)
{
await using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
await using var session = await Ctx.CreateSessionAsync(client, new SessionConfig { OnPermissionRequest = PermissionHandler.ApproveAll });
await client.StopAsync();
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Report_Error_With_Stderr_When_CLI_Fails_To_Start(bool useStdio)
{
var cliPath = Path.Join(Ctx.WorkDir, $"failing-cli-{Guid.NewGuid():N}.js");
await File.WriteAllTextAsync(cliPath, FailingCliScript);
var client = new CopilotClient(new CopilotClientOptions
{
Connection = useStdio
? RuntimeConnection.ForStdio(path: cliPath)
: RuntimeConnection.ForTcp(path: cliPath)
});
var ex = await Assert.ThrowsAsync<IOException>(() => client.StartAsync());
var errorMessage = ex.Message;
#if NETFRAMEWORK
// .NET Framework can surface the stdio pipe failure before redirected stderr is readable.
if (useStdio)
{
Assert.True(
errorMessage.Contains("CLI", StringComparison.OrdinalIgnoreCase)
|| errorMessage.Contains("pipe", StringComparison.OrdinalIgnoreCase),
$"Expected a CLI process or pipe error, got: {errorMessage}");
}
else
#endif
{
Assert.Contains("stderr", errorMessage, StringComparison.OrdinalIgnoreCase);
Assert.Contains("nonexistent", errorMessage, StringComparison.OrdinalIgnoreCase);
}
// Verify subsequent calls also fail (don't hang)
var ex2 = await Assert.ThrowsAnyAsync<Exception>(async () =>
{
var session = await Ctx.CreateSessionAsync(client, new SessionConfig { OnPermissionRequest = PermissionHandler.ApproveAll });
await session.SendAsync(new MessageOptions { Prompt = "test" });
});
Assert.True(
ex2.Message.Contains("exited", StringComparison.OrdinalIgnoreCase) ||
ex2.Message.Contains("pipe", StringComparison.OrdinalIgnoreCase),
$"Expected error about process exit or pipe, got: {ex2.Message}");
// Cleanup - ForceStop should handle the disconnected state gracefully
try { await client.ForceStopAsync(); } catch (Exception) { /* Expected */ }
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task Should_Allow_CreateSession_Called_Without_PermissionHandler(bool useStdio)
{
await using var client = new CopilotClient(new CopilotClientOptions { Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp() });
await using var session = await Ctx.CreateSessionAsync(client, new SessionConfig());
Assert.NotNull(session.SessionId);
}
[Fact]
public async Task Should_Allow_ResumeSession_Called_Without_PermissionHandler()
{
const string connectionToken = "client-e2e-resume-token";
await using var ctx = await E2ETestContext.CreateAsync();
await using var client = ctx.CreateClient(options: new CopilotClientOptions
{
Connection = RuntimeConnection.ForTcp(connectionToken: connectionToken),
});
await using var originalSession = await ctx.CreateSessionAsync(client, new SessionConfig());
var port = client.RuntimePort
?? throw new InvalidOperationException("Client must be using TCP transport to support multi-client resume.");
await using var resumeClient = ctx.CreateClient(options: new CopilotClientOptions
{
Connection = RuntimeConnection.ForUri($"localhost:{port}", connectionToken: connectionToken),
});
await using var resumedSession = await ctx.ResumeSessionAsync(resumeClient, originalSession.SessionId, new());
Assert.Equal(originalSession.SessionId, resumedSession.SessionId);
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task ListModels_WithCustomHandler_CallsHandler(bool useStdio)
{
IList<ModelInfo> customModels = new List<ModelInfo>
{
new()
{
Id = "my-custom-model",
Name = "My Custom Model",
Capabilities = new ModelCapabilities
{
Supports = new ModelSupports { Vision = false, ReasoningEffort = false },
Limits = new ModelLimits { MaxContextWindowTokens = 128000 }
}
}
};
var callCount = 0;
await using var client = new CopilotClient(new CopilotClientOptions
{
Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp(),
OnListModels = (ct) =>
{
callCount++;
return Task.FromResult(customModels);
}
});
await client.StartAsync();
var models = await client.ListModelsAsync();
Assert.Equal(1, callCount);
Assert.Single(models);
Assert.Equal("my-custom-model", models[0].Id);
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task ListModels_WithCustomHandler_CachesResults(bool useStdio)
{
IList<ModelInfo> customModels = new List<ModelInfo>
{
new()
{
Id = "cached-model",
Name = "Cached Model",
Capabilities = new ModelCapabilities
{
Supports = new ModelSupports { Vision = false, ReasoningEffort = false },
Limits = new ModelLimits { MaxContextWindowTokens = 128000 }
}
}
};
var callCount = 0;
await using var client = new CopilotClient(new CopilotClientOptions
{
Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp(),
OnListModels = (ct) =>
{
callCount++;
return Task.FromResult(customModels);
}
});
await client.StartAsync();
await client.ListModelsAsync();
await client.ListModelsAsync();
Assert.Equal(1, callCount); // Only called once due to caching
}
[Theory]
[InlineData(true)] // stdio transport
[InlineData(false)] // TCP transport
public async Task ListModels_WithCustomHandler_WorksWithoutStart(bool useStdio)
{
IList<ModelInfo> customModels = new List<ModelInfo>
{
new()
{
Id = "no-start-model",
Name = "No Start Model",
Capabilities = new ModelCapabilities
{
Supports = new ModelSupports { Vision = false, ReasoningEffort = false },
Limits = new ModelLimits { MaxContextWindowTokens = 128000 }
}
}
};
var callCount = 0;
await using var client = new CopilotClient(new CopilotClientOptions
{
Connection = useStdio ? RuntimeConnection.ForStdio() : RuntimeConnection.ForTcp(),
OnListModels = (ct) =>
{
callCount++;
return Task.FromResult(customModels);
}
});
var models = await client.ListModelsAsync();
Assert.Equal(1, callCount);
Assert.Single(models);
Assert.Equal("no-start-model", models[0].Id);
}
}