|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +using System.Reflection; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Nodes; |
| 8 | +using System.Text.Json.Serialization; |
| 9 | +using System.Text.Json.Serialization.Metadata; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace GitHub.Copilot.Test.Unit; |
| 13 | + |
| 14 | +public class SessionEventDispatchTests |
| 15 | +{ |
| 16 | + public static IEnumerable<object[]> EventTypes => |
| 17 | + typeof(SessionEvent).GetCustomAttributes<JsonDerivedTypeAttribute>() |
| 18 | + .Select(attribute => new object[] { attribute.TypeDiscriminator!, attribute.DerivedType }); |
| 19 | + |
| 20 | + public static IEnumerable<object[]> EventDiscriminators => |
| 21 | + EventTypes.Select(eventType => new object[] { eventType[0] }); |
| 22 | + |
| 23 | + [Theory] |
| 24 | + [MemberData(nameof(EventTypes))] |
| 25 | + public void FromJson_DispatchesEveryRegisteredEvent(string discriminator, Type expectedType) |
| 26 | + { |
| 27 | + var json = $$""" |
| 28 | + { |
| 29 | + "data":null, |
| 30 | + "agentId":"child-agent", |
| 31 | + "ephemeral":true, |
| 32 | + "id":"11111111-1111-1111-1111-111111111111", |
| 33 | + "parentId":"22222222-2222-2222-2222-222222222222", |
| 34 | + "timestamp":"2026-09-30T00:00:00Z", |
| 35 | + "type":"{{discriminator}}" |
| 36 | + } |
| 37 | + """; |
| 38 | + |
| 39 | + var result = SessionEvent.FromJson(json); |
| 40 | + |
| 41 | + Assert.Equal(expectedType, result.GetType()); |
| 42 | + Assert.Equal(discriminator, result.Type); |
| 43 | + Assert.Equal("child-agent", result.AgentId); |
| 44 | + Assert.True(result.Ephemeral); |
| 45 | + Assert.Equal(Guid.Parse("11111111-1111-1111-1111-111111111111"), result.Id); |
| 46 | + Assert.Equal(Guid.Parse("22222222-2222-2222-2222-222222222222"), result.ParentId); |
| 47 | + Assert.Equal(DateTimeOffset.Parse("2026-09-30T00:00:00Z"), result.Timestamp); |
| 48 | + } |
| 49 | + |
| 50 | + [Theory] |
| 51 | + [MemberData(nameof(EventDiscriminators))] |
| 52 | + public void FromJson_DoesNotDispatchPartialDiscriminators(string discriminator) |
| 53 | + { |
| 54 | + var candidates = Enumerable.Range(0, discriminator.Length) |
| 55 | + .Select(index => discriminator[..index] + "?" + discriminator[(index + 1)..]) |
| 56 | + .Prepend(discriminator[..^1]) |
| 57 | + .Append(discriminator + "x"); |
| 58 | + |
| 59 | + foreach (var candidate in candidates) |
| 60 | + { |
| 61 | + var json = $$"""{"type":"{{candidate}}","agentId":"child","data":null}"""; |
| 62 | + var actual = SessionEvent.FromJson(json); |
| 63 | + |
| 64 | + Assert.IsType<SessionEvent>(actual); |
| 65 | + Assert.Equal("child", actual.AgentId); |
| 66 | + Assert.Equal(JsonSerializer.Deserialize(json, PolymorphicTypeInfo)?.ToJson(), actual.ToJson()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Theory] |
| 71 | + [InlineData("""{"type":"user.message","data":{"content":"hello"}}""")] |
| 72 | + [InlineData("""{"data":{"nested":[{"type":"session.idle"}],"content":"hello"},"type":"user.message"}""")] |
| 73 | + [InlineData("""{"ty\u0070e":"user.message","data":{"content":"hello"}}""")] |
| 74 | + [InlineData("""{"type":"user\u002Emessage","data":{"content":"hello"}}""")] |
| 75 | + [InlineData("""{"type":"future.\u00E9","agentId":"child"}""")] |
| 76 | + [InlineData("""{"TYPE":"user.message","data":{"content":"hello"}}""")] |
| 77 | + [InlineData("""{"type":"USER.MESSAGE","data":{"content":"hello"}}""")] |
| 78 | + [InlineData("""{"type":"future.event","agentId":"child","ephemeral":true,"data":[null,{"type":"user.message"}]}""")] |
| 79 | + [InlineData("""{"agentId":"child","ephemeral":true,"data":{"type":"user.message"}}""")] |
| 80 | + [InlineData("""{"type":42,"agentId":"child","ephemeral":true}""")] |
| 81 | + [InlineData("""{"type":-2147483648}""")] |
| 82 | + [InlineData("""{"type":2147483647}""")] |
| 83 | + [InlineData("""{"type":"user.message","DATA":{"CONTENT":"hello"}}""")] |
| 84 | + [InlineData("""{"type":"user.message","data":{"content":"hello"},"data":{"content":"last"}}""")] |
| 85 | + [InlineData("null")] |
| 86 | + public void FromJson_PreservesPolymorphicDeserialization(string json) |
| 87 | + { |
| 88 | + var expected = JsonSerializer.Deserialize(json, PolymorphicTypeInfo); |
| 89 | + var actual = SessionEvent.FromJson(json); |
| 90 | + |
| 91 | + Assert.Equal(expected?.GetType(), actual?.GetType()); |
| 92 | + Assert.True(JsonNode.DeepEquals( |
| 93 | + JsonNode.Parse(expected?.ToJson() ?? "null"), |
| 94 | + JsonNode.Parse(actual?.ToJson() ?? "null"))); |
| 95 | + } |
| 96 | + |
| 97 | + [Theory] |
| 98 | + [InlineData("""{"type":null}""")] |
| 99 | + [InlineData("""{"type":true}""")] |
| 100 | + [InlineData("""{"type":{}}""")] |
| 101 | + [InlineData("""{"type":[]}""")] |
| 102 | + [InlineData("""{"type":1.5}""")] |
| 103 | + [InlineData("""{"type":2147483648}""")] |
| 104 | + [InlineData("""{"type":"\uD800"}""")] |
| 105 | + [InlineData("""{"type":"session.idle","\uD800":true,"data":{}}""")] |
| 106 | + [InlineData("""{"type":"user.message","data":{"content":"hello"}""")] |
| 107 | + [InlineData("""{"type":"user.message","data":{"content":"hello"},}""")] |
| 108 | + [InlineData("""{"type":"future.event","ty\u0070e":"future.event"}""")] |
| 109 | + [InlineData("""{"\u0024future":true,"type":"session.idle","data":{}}""")] |
| 110 | + [InlineData("""{"type":"user.message","type":"session.idle","data":{"content":"hello"}}""")] |
| 111 | + [InlineData("""{"type":"user.message","data":{"content":"hello"},"type":"user.message"}""")] |
| 112 | + [InlineData("""{"type":"future.event","data":{},"type":"future.event"}""")] |
| 113 | + [InlineData("""{"type":"user.message","data":{}}""")] |
| 114 | + [InlineData("""{"type":"user.message"}""")] |
| 115 | + [InlineData("""{"$id":"1","type":"user.message","data":{"content":"hello"}}""")] |
| 116 | + [InlineData("""{"type":"user.message","data":{"content":"hello"},"$ref":"1"}""")] |
| 117 | + [InlineData("""{"type":"future.event","$values":[]}""")] |
| 118 | + [InlineData("""{"$future":"ignored","type":"user.message","data":{"content":"hello"}}""")] |
| 119 | + [InlineData("""{"type":"session.idle","id":"invalid","data":{}}""")] |
| 120 | + [InlineData("""{"type":"session.idle","timestamp":"invalid","data":{}}""")] |
| 121 | + [InlineData("""{"type":"session.idle","ephemeral":"invalid","data":{}}""")] |
| 122 | + [InlineData("""[{"type":"session.idle","data":{}}]""")] |
| 123 | + [InlineData("42")] |
| 124 | + [InlineData("true")] |
| 125 | + [InlineData("@")] |
| 126 | + [InlineData("")] |
| 127 | + [InlineData("\"text\"")] |
| 128 | + [InlineData("""{"type":"user.message","data":{"content":"hello"}} trailing""")] |
| 129 | + public void FromJson_RejectsInvalidEventsLikePolymorphicDeserialization(string json) |
| 130 | + { |
| 131 | + Assert.Throws<JsonException>(() => JsonSerializer.Deserialize(json, PolymorphicTypeInfo)); |
| 132 | + Assert.Throws<JsonException>(() => SessionEvent.FromJson(json)); |
| 133 | + } |
| 134 | + |
| 135 | + [Theory] |
| 136 | + [InlineData(0)] |
| 137 | + [InlineData(32)] |
| 138 | + [InlineData(256)] |
| 139 | + [InlineData(100_000)] |
| 140 | + public void FromJson_PreservesUnicodeAcrossBufferSizes(int repetitions) |
| 141 | + { |
| 142 | + var content = string.Concat(Enumerable.Repeat("a\u00E9\u4E2D\U0001F600", repetitions)); |
| 143 | + var json = "{\"data\":{\"content\":\"" + content + "\"},\"ty\\u0070e\":\"user.message\"}"; |
| 144 | + |
| 145 | + var actual = Assert.IsType<UserMessageEvent>(SessionEvent.FromJson(json)); |
| 146 | + |
| 147 | + Assert.Equal(content, actual.Data.Content); |
| 148 | + Assert.Equal(JsonSerializer.Deserialize(json, PolymorphicTypeInfo)?.ToJson(), actual.ToJson()); |
| 149 | + } |
| 150 | + |
| 151 | + [Theory] |
| 152 | + [InlineData('\uD800', 0)] |
| 153 | + [InlineData('\uDC00', 0)] |
| 154 | + [InlineData('\uD800', 1000)] |
| 155 | + [InlineData('\uDC00', 1000)] |
| 156 | + public void FromJson_RejectsInvalidUtf16LikePolymorphicDeserialization(char invalid, int padding) |
| 157 | + { |
| 158 | + var json = "{\"type\":\"user.message\",\"data\":{\"content\":\"" + new string('a', padding) + invalid + "\"}}"; |
| 159 | + |
| 160 | + Assert.Throws<ArgumentException>(() => JsonSerializer.Deserialize(json, PolymorphicTypeInfo)); |
| 161 | + Assert.Throws<ArgumentException>(() => SessionEvent.FromJson(json)); |
| 162 | + } |
| 163 | + |
| 164 | + [Fact] |
| 165 | + public void FromJson_RejectsNullInputLikePolymorphicDeserialization() |
| 166 | + { |
| 167 | + Assert.Throws<ArgumentNullException>(() => JsonSerializer.Deserialize((string)null!, PolymorphicTypeInfo)); |
| 168 | + Assert.Throws<ArgumentNullException>(() => SessionEvent.FromJson(null!)); |
| 169 | + } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + public async Task FromJson_IsSafeForConcurrentDeserialization() |
| 173 | + { |
| 174 | + var events = EventTypes.ToArray(); |
| 175 | + var tasks = Enumerable.Range(0, 32).Select(i => Task.Run(() => |
| 176 | + { |
| 177 | + foreach (var item in events) |
| 178 | + { |
| 179 | + var discriminator = (string)item[0]; |
| 180 | + var expectedType = (Type)item[1]; |
| 181 | + var json = $$"""{"type":"{{discriminator}}","agentId":"{{i}}","data":null}"""; |
| 182 | + var actual = SessionEvent.FromJson(json); |
| 183 | + |
| 184 | + Assert.Equal(expectedType, actual.GetType()); |
| 185 | + Assert.Equal(i.ToString(), actual.AgentId); |
| 186 | + } |
| 187 | + })); |
| 188 | + |
| 189 | + await Task.WhenAll(tasks); |
| 190 | + } |
| 191 | + |
| 192 | + [Fact] |
| 193 | + public void LiveOptions_SerializeEventsWithoutChangingTheWireFormat() |
| 194 | + { |
| 195 | + var options = (JsonSerializerOptions)typeof(CopilotClient).GetProperty( |
| 196 | + "SerializerOptionsForMessageFormatter", BindingFlags.Static | BindingFlags.NonPublic)! |
| 197 | + .GetValue(null)!; |
| 198 | + var sessionEvent = new UserMessageEvent |
| 199 | + { |
| 200 | + Id = Guid.Parse("11111111-1111-1111-1111-111111111111"), |
| 201 | + Timestamp = DateTimeOffset.Parse("2026-09-30T00:00:00Z"), |
| 202 | + Data = new UserMessageData { Content = "hello" }, |
| 203 | + }; |
| 204 | + |
| 205 | + var json = JsonSerializer.Serialize<SessionEvent>(sessionEvent, options); |
| 206 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse(sessionEvent.ToJson()), JsonNode.Parse(json))); |
| 207 | + var decoded = Assert.IsType<UserMessageEvent>(JsonSerializer.Deserialize<SessionEvent>(json, options)); |
| 208 | + Assert.Equal("hello", decoded.Data.Content); |
| 209 | + } |
| 210 | + |
| 211 | + // The unchanged source-generated context is the reference contract, not reflection metadata. |
| 212 | + private static JsonTypeInfo<SessionEvent> PolymorphicTypeInfo |
| 213 | + { |
| 214 | + get |
| 215 | + { |
| 216 | + var contextType = typeof(SessionEvent).Assembly.GetType("GitHub.Copilot.SessionEventsJsonContext", true)!; |
| 217 | + var context = (JsonSerializerContext)contextType.GetProperty("Default")!.GetValue(null)!; |
| 218 | + return (JsonTypeInfo<SessionEvent>)context.GetTypeInfo(typeof(SessionEvent))!; |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments