-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathPublicDtoTests.cs
More file actions
232 lines (199 loc) · 7.56 KB
/
Copy pathPublicDtoTests.cs
File metadata and controls
232 lines (199 loc) · 7.56 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Collections;
using System.Reflection;
using System.Text.Json;
using Xunit;
namespace GitHub.Copilot.Test.Unit;
/// <summary>
/// Reflection-based safety net that exercises the get/set surface of every public DTO in
/// the SDK assembly. The intent is to (1) keep System.Text.Json source-generation
/// configurations from drifting (NativeAOT-friendly serializer must know every public DTO),
/// and (2) catch accidental property-shape regressions (read-only setters, mismatched
/// nullability, generated bridge types). It is **not** a serialization-correctness test;
/// for that, write targeted serializer tests against fixed JSON payloads (see
/// <c>SessionEventSerializationTests</c> for the pattern).
/// </summary>
public class PublicDtoTests
{
[Fact]
public void McpAuth_Result_Factories_Represent_Token_And_Cancellation()
{
var token = new McpAuthToken
{
AccessToken = "host-token",
TokenType = "Bearer",
ExpiresIn = 3600,
};
var tokenResult = McpAuthResult.FromToken(token);
Assert.Same(token, tokenResult.Token);
Assert.False(tokenResult.Cancelled);
var cancelled = McpAuthResult.Cancel();
Assert.True(cancelled.Cancelled);
Assert.Null(cancelled.Token);
}
[Fact]
public void Public_Dto_Properties_Can_Be_Set_And_Read()
{
var exercisedProperties = 0;
var assembly = typeof(CopilotClient).Assembly;
var candidateTypes = assembly
.GetTypes()
.Where(type =>
type is { IsClass: true, IsAbstract: false, IsPublic: true } &&
type.Namespace?.StartsWith("GitHub.Copilot", StringComparison.Ordinal) == true &&
type.GetConstructor(Type.EmptyTypes) is not null)
.OrderBy(type => type.FullName, StringComparer.Ordinal);
foreach (var type in candidateTypes)
{
var instance = Activator.CreateInstance(type)!;
foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (property.GetIndexParameters().Length != 0)
{
continue;
}
if (property.SetMethod?.IsPublic == true &&
TryCreateSampleValue(property.PropertyType, [], out var sampleValue))
{
property.SetValue(instance, sampleValue);
}
if (property.GetMethod?.IsPublic == true)
{
_ = property.GetValue(instance);
exercisedProperties++;
}
}
}
Assert.True(exercisedProperties > 1_000, $"Expected to exercise many DTO properties, but only exercised {exercisedProperties}.");
}
private static bool TryCreateSampleValue(Type type, HashSet<Type> visited, out object? value)
{
var nullableType = Nullable.GetUnderlyingType(type);
if (nullableType is not null)
{
return TryCreateSampleValue(nullableType, visited, out value);
}
if (type == typeof(string))
{
value = "value";
return true;
}
if (type == typeof(bool))
{
value = true;
return true;
}
if (type == typeof(int))
{
value = 1;
return true;
}
if (type == typeof(long))
{
value = 1L;
return true;
}
if (type == typeof(double))
{
value = 1.0;
return true;
}
if (type == typeof(DateTimeOffset))
{
value = DateTimeOffset.UnixEpoch;
return true;
}
if (type == typeof(DateTime))
{
value = DateTime.UnixEpoch;
return true;
}
if (type == typeof(TimeSpan))
{
value = TimeSpan.FromMilliseconds(1);
return true;
}
if (type == typeof(JsonElement))
{
using var document = JsonDocument.Parse("""{"value":1}""");
value = document.RootElement.Clone();
return true;
}
if (type == typeof(object))
{
value = "value";
return true;
}
if (type.IsEnum)
{
var values = Enum.GetValues(type);
value = values.Length > 0 ? values.GetValue(0) : Activator.CreateInstance(type);
return true;
}
if (type.IsArray)
{
var elementType = type.GetElementType()!;
if (!TryCreateSampleValue(elementType, visited, out var elementValue))
{
elementValue = elementType.IsValueType ? Activator.CreateInstance(elementType) : null;
}
var array = Array.CreateInstance(elementType, 1);
array.SetValue(elementValue, 0);
value = array;
return true;
}
if (TryCreateGenericCollection(type, visited, out value))
{
return true;
}
if (!type.IsValueType && type.GetConstructor(Type.EmptyTypes) is not null && visited.Add(type))
{
value = Activator.CreateInstance(type);
visited.Remove(type);
return true;
}
value = type.IsValueType ? Activator.CreateInstance(type) : null;
return true;
}
private static bool TryCreateGenericCollection(Type type, HashSet<Type> visited, out object? value)
{
var dictionaryInterface = type.GetInterfaces()
.Append(type)
.FirstOrDefault(candidate =>
candidate.IsGenericType &&
(candidate.GetGenericTypeDefinition() == typeof(IDictionary<,>) ||
candidate.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>)));
if (dictionaryInterface is not null)
{
var keyType = dictionaryInterface.GetGenericArguments()[0];
var valueType = dictionaryInterface.GetGenericArguments()[1];
TryCreateSampleValue(keyType, visited, out var sampleKey);
TryCreateSampleValue(valueType, visited, out var sampleValue);
var dictionaryType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
var dictionary = (IDictionary)Activator.CreateInstance(dictionaryType)!;
dictionary[sampleKey!] = sampleValue;
value = dictionary;
return true;
}
var enumerableInterface = type.GetInterfaces()
.Append(type)
.FirstOrDefault(candidate =>
candidate.IsGenericType &&
(candidate.GetGenericTypeDefinition() == typeof(IList<>) ||
candidate.GetGenericTypeDefinition() == typeof(IReadOnlyList<>) ||
candidate.GetGenericTypeDefinition() == typeof(IEnumerable<>)));
if (enumerableInterface is not null)
{
var elementType = enumerableInterface.GetGenericArguments()[0];
TryCreateSampleValue(elementType, visited, out var sampleValue);
var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType))!;
list.Add(sampleValue);
value = list;
return true;
}
value = null;
return false;
}
}