diff --git a/src/StevesBot.Worker.Tests/Integration/DiscordEventConverterTests.cs b/src/StevesBot.Worker.Tests/Integration/DiscordEventConverterTests.cs deleted file mode 100644 index 2939c28..0000000 --- a/src/StevesBot.Worker.Tests/Integration/DiscordEventConverterTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace StevesBot.Worker.Tests.Integration; - -public class DiscordEventConverterTests -{ - private readonly JsonSerializerOptions _jsonSerializerOptions = new() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - Converters = - { - new DiscordEventConverter() - } - }; - - [Fact] - public void Deserialize_WhenCalledWithUnknownOpCode_ItShouldReturnADiscordEvent() - { - - } - - private string Serialize(object obj) - { - return JsonSerializer.Serialize(obj, _jsonSerializerOptions); - } - - private T? Deserialize(string json) - { - return JsonSerializer.Deserialize(json, _jsonSerializerOptions); - } -} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/DiscordEventConverterTests.cs b/src/StevesBot.Worker.Tests/Unit/DiscordEventConverterTests.cs new file mode 100644 index 0000000..a64a528 --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/DiscordEventConverterTests.cs @@ -0,0 +1,54 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class DiscordEventConverterTests +{ + private readonly JsonSerializerOptions _options = new() + { + Converters = + { + new DiscordEventConverter() + } + }; + private readonly Type _discordEventType = typeof(DiscordEvent); + private readonly DiscordEventConverter _converter = new(); + + [Fact] + public void Read_WhenCalledWithUnknownOpCode_ItShouldReturnDiscordEvent() + { + var data = new + { + op = 99, + s = null as int?, + t = null as string, + d = null as object + }; + + var result = Read(data); + + result.Should().BeOfType(); + } + + [Fact] + public void Read_WhenCalledWithDispatchOpCodeAndNoType_ItShouldReturnDispatchEvent() + { + var data = new + { + op = DiscordOpCodes.Dispatch, + s = null as int?, + t = null as string, + d = null as object + }; + + var result = Read(data); + + result.Should().BeOfType(); + } + + private DiscordEvent? Read(object data) + { + var json = JsonSerializer.Serialize(data); + var utf8Json = Encoding.UTF8.GetBytes(json); + var reader = new Utf8JsonReader(utf8Json); + return _converter.Read(ref reader, _discordEventType, _options); + } +} \ No newline at end of file diff --git a/src/StevesBot.Worker/Discord/Events/DiscordEventConverter.cs b/src/StevesBot.Worker/Discord/Events/DiscordEventConverter.cs index cb611af..2bb3592 100644 --- a/src/StevesBot.Worker/Discord/Events/DiscordEventConverter.cs +++ b/src/StevesBot.Worker/Discord/Events/DiscordEventConverter.cs @@ -2,6 +2,12 @@ namespace StevesBot.Worker.Discord.Events; internal sealed class DiscordEventConverter : JsonConverter { + private readonly JsonSerializerOptions _jsonSerializerOptions = new() + { + ReferenceHandler = ReferenceHandler.IgnoreCycles, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }; + private const string OpPropertyName = "op"; public override DiscordEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) @@ -15,7 +21,7 @@ internal sealed class DiscordEventConverter : JsonConverter DiscordOpCodes.Dispatch => DeserializeDispatchEvent(root, options), DiscordOpCodes.Hello => JsonSerializer.Deserialize(root.GetRawText(), options), DiscordOpCodes.HeartbeatAck => JsonSerializer.Deserialize(root.GetRawText(), options), - _ => JsonSerializer.Deserialize(root.GetRawText(), options) + _ => JsonSerializer.Deserialize(root.GetRawText(), _jsonSerializerOptions), }; } @@ -24,14 +30,14 @@ internal sealed class DiscordEventConverter : JsonConverter JsonSerializer.Serialize(writer, value, value.GetType(), options); } - private static DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options) + private DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options) { var type = root.GetProperty("t").GetString(); return type switch { DiscordEventTypes.Ready => JsonSerializer.Deserialize(root.GetRawText(), options), - _ => JsonSerializer.Deserialize(root.GetRawText(), options), + _ => JsonSerializer.Deserialize(root.GetRawText(), _jsonSerializerOptions), }; } } \ No newline at end of file