forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationTests.cs
More file actions
284 lines (248 loc) · 12.3 KB
/
Copy pathSerializationTests.cs
File metadata and controls
284 lines (248 loc) · 12.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using Xunit;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace GitHub.Copilot.SDK.Test.Unit;
/// <summary>
/// Tests for JSON serialization compatibility with the SDK's configured options.
/// </summary>
public class SerializationTests
{
[Fact]
public void ProviderConfig_CanSerializeHeaders_WithSdkOptions()
{
var options = GetSerializerOptions();
var original = new ProviderConfig
{
BaseUrl = "https://example.com/provider",
Headers = new Dictionary<string, string> { ["Authorization"] = "Bearer provider-token" },
ModelId = "gpt-4o",
WireModel = "my-finetune-v3",
MaxInputTokens = 100_000,
MaxOutputTokens = 4096
};
var json = JsonSerializer.Serialize(original, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.Equal("https://example.com/provider", root.GetProperty("baseUrl").GetString());
Assert.Equal("Bearer provider-token", root.GetProperty("headers").GetProperty("Authorization").GetString());
Assert.Equal("gpt-4o", root.GetProperty("modelId").GetString());
Assert.Equal("my-finetune-v3", root.GetProperty("wireModel").GetString());
Assert.Equal(100_000, root.GetProperty("maxPromptTokens").GetInt32());
Assert.Equal(4096, root.GetProperty("maxOutputTokens").GetInt32());
var deserialized = JsonSerializer.Deserialize<ProviderConfig>(json, options);
Assert.NotNull(deserialized);
Assert.Equal("https://example.com/provider", deserialized.BaseUrl);
Assert.Equal("Bearer provider-token", deserialized.Headers!["Authorization"]);
Assert.Equal("gpt-4o", deserialized.ModelId);
Assert.Equal("my-finetune-v3", deserialized.WireModel);
Assert.Equal(100_000, deserialized.MaxInputTokens);
Assert.Equal(4096, deserialized.MaxOutputTokens);
}
[Fact]
public void MessageOptions_CanSerializeRequestHeaders_WithSdkOptions()
{
var options = GetSerializerOptions();
var original = new MessageOptions
{
Prompt = "real prompt",
Mode = "plan",
RequestHeaders = new Dictionary<string, string> { ["X-Trace"] = "trace-value" }
};
var json = JsonSerializer.Serialize(original, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.Equal("real prompt", root.GetProperty("prompt").GetString());
Assert.Equal("plan", root.GetProperty("mode").GetString());
Assert.Equal("trace-value", root.GetProperty("requestHeaders").GetProperty("X-Trace").GetString());
var deserialized = JsonSerializer.Deserialize<MessageOptions>(json, options);
Assert.NotNull(deserialized);
Assert.Equal("real prompt", deserialized.Prompt);
Assert.Equal("plan", deserialized.Mode);
Assert.Equal("trace-value", deserialized.RequestHeaders!["X-Trace"]);
}
[Fact]
public void SendMessageRequest_CanSerializeRequestHeaders_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotSession), "SendMessageRequest");
var request = CreateInternalRequest(
requestType,
("SessionId", "session-id"),
("Prompt", "real prompt"),
("Mode", "plan"),
("RequestHeaders", new Dictionary<string, string> { ["X-Trace"] = "trace-value" }));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.Equal("session-id", root.GetProperty("sessionId").GetString());
Assert.Equal("real prompt", root.GetProperty("prompt").GetString());
Assert.Equal("plan", root.GetProperty("mode").GetString());
Assert.Equal("trace-value", root.GetProperty("requestHeaders").GetProperty("X-Trace").GetString());
}
[Fact]
public void CreateSessionRequest_CanSerializeInstructionDirectories_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotClient), "CreateSessionRequest");
var request = CreateInternalRequest(
requestType,
("SessionId", "session-id"),
("InstructionDirectories", new List<string> { "C:\\extra-instructions", "C:\\more-instructions" }));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.Equal("C:\\extra-instructions", root.GetProperty("instructionDirectories")[0].GetString());
Assert.Equal("C:\\more-instructions", root.GetProperty("instructionDirectories")[1].GetString());
}
[Fact]
public void CreateSessionRequest_CanSerializeModeRequestFlags_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotClient), "CreateSessionRequest");
var request = CreateInternalRequest(
requestType,
("RequestExitPlanMode", true),
("RequestAutoModeSwitch", true));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.True(root.GetProperty("requestExitPlanMode").GetBoolean());
Assert.True(root.GetProperty("requestAutoModeSwitch").GetBoolean());
}
[Fact]
public void ResumeSessionRequest_CanSerializeInstructionDirectories_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotClient), "ResumeSessionRequest");
var request = CreateInternalRequest(
requestType,
("SessionId", "session-id"),
("InstructionDirectories", new List<string> { "C:\\resume-instructions" }));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.Equal("C:\\resume-instructions", root.GetProperty("instructionDirectories")[0].GetString());
}
[Fact]
public void CreateSessionRequest_CanSerializeEnableSessionTelemetry_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotClient), "CreateSessionRequest");
var request = CreateInternalRequest(
requestType,
("SessionId", "session-id"),
("EnableSessionTelemetry", false));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.False(root.GetProperty("enableSessionTelemetry").GetBoolean());
}
[Fact]
public void ResumeSessionRequest_CanSerializeEnableSessionTelemetry_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotClient), "ResumeSessionRequest");
var request = CreateInternalRequest(
requestType,
("SessionId", "session-id"),
("EnableSessionTelemetry", false));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.False(root.GetProperty("enableSessionTelemetry").GetBoolean());
}
[Fact]
public void ResumeSessionRequest_CanSerializeModeRequestFlags_WithSdkOptions()
{
var options = GetSerializerOptions();
var requestType = GetNestedType(typeof(CopilotClient), "ResumeSessionRequest");
var request = CreateInternalRequest(
requestType,
("SessionId", "session-id"),
("RequestExitPlanMode", true),
("RequestAutoModeSwitch", true));
var json = JsonSerializer.Serialize(request, requestType, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.True(root.GetProperty("requestExitPlanMode").GetBoolean());
Assert.True(root.GetProperty("requestAutoModeSwitch").GetBoolean());
}
[Fact]
public void AutoModeSwitchResponse_CanSerialize_WithSdkOptions()
{
var options = GetSerializerOptions();
var json = JsonSerializer.Serialize(AutoModeSwitchResponse.YesAlways, options);
Assert.Equal("\"yes_always\"", json);
}
[Fact]
public void McpHttpServerConfig_CanSerializeOauthOptions_WithSdkOptions()
{
var options = GetSerializerOptions();
McpServerConfig original = new McpHttpServerConfig
{
Url = "https://example.com/mcp",
Headers = new Dictionary<string, string> { ["Authorization"] = "Bearer token" },
OauthClientId = "client-id",
OauthPublicClient = false,
OauthGrantType = McpHttpServerConfigOauthGrantType.ClientCredentials,
Tools = ["*"],
Timeout = 3000
};
var json = JsonSerializer.Serialize(original, options);
using var document = JsonDocument.Parse(json);
var root = document.RootElement;
Assert.Equal("http", root.GetProperty("type").GetString());
Assert.Equal("https://example.com/mcp", root.GetProperty("url").GetString());
Assert.Equal("Bearer token", root.GetProperty("headers").GetProperty("Authorization").GetString());
Assert.Equal("client-id", root.GetProperty("oauthClientId").GetString());
Assert.False(root.GetProperty("oauthPublicClient").GetBoolean());
Assert.Equal("client_credentials", root.GetProperty("oauthGrantType").GetString());
Assert.Equal("*", root.GetProperty("tools")[0].GetString());
Assert.Equal(3000, root.GetProperty("timeout").GetInt32());
var deserialized = JsonSerializer.Deserialize<McpServerConfig>(json, options);
var httpConfig = Assert.IsType<McpHttpServerConfig>(deserialized);
Assert.Equal("https://example.com/mcp", httpConfig.Url);
Assert.Equal("Bearer token", httpConfig.Headers!["Authorization"]);
Assert.Equal("client-id", httpConfig.OauthClientId);
Assert.False(httpConfig.OauthPublicClient);
Assert.Equal(McpHttpServerConfigOauthGrantType.ClientCredentials, httpConfig.OauthGrantType);
Assert.Equal("*", Assert.Single(httpConfig.Tools));
Assert.Equal(3000, httpConfig.Timeout);
}
private static JsonSerializerOptions GetSerializerOptions()
{
var prop = typeof(CopilotClient)
.GetProperty("SerializerOptionsForMessageFormatter",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
var options = (JsonSerializerOptions?)prop?.GetValue(null);
Assert.NotNull(options);
return options;
}
private static Type GetNestedType(Type containingType, string name)
{
var type = containingType.GetNestedType(name, System.Reflection.BindingFlags.NonPublic);
Assert.NotNull(type);
return type!;
}
private static object CreateInternalRequest(Type type, params (string Name, object? Value)[] properties)
{
var instance = System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(type);
foreach (var (name, value) in properties)
{
var property = type.GetProperty(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
Assert.NotNull(property);
if (property!.SetMethod is not null)
{
property.SetValue(instance, value);
continue;
}
var field = type.GetField($"<{name}>k__BackingField", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
Assert.NotNull(field);
field!.SetValue(instance, value);
}
return instance;
}
}