diff --git a/src/StevesBot.Worker.Tests/Unit/HeartbeatAckDiscordEventTests.cs b/src/StevesBot.Worker.Tests/Unit/HeartbeatAckDiscordEventTests.cs new file mode 100644 index 0000000..e69de29 diff --git a/src/StevesBot.Worker.Tests/Unit/HeartbeatDiscordEventTests.cs b/src/StevesBot.Worker.Tests/Unit/HeartbeatDiscordEventTests.cs new file mode 100644 index 0000000..e69de29 diff --git a/src/StevesBot.Worker.Tests/Unit/HelloDiscordEventTests.cs b/src/StevesBot.Worker.Tests/Unit/HelloDiscordEventTests.cs new file mode 100644 index 0000000..eb8f2be --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/HelloDiscordEventTests.cs @@ -0,0 +1,71 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class HelloDiscordEventTests +{ + [Fact] + public void Constructor_WhenCalled_ItShouldInitializeProperties() + { + var helloEvent = new HelloDiscordEvent(); + var helloData = new HelloData(); + + helloEvent.OpCode.Should().Be(0); + helloEvent.Sequence.Should().BeNull(); + helloEvent.Type.Should().BeNull(); + helloEvent.Data.Should().BeEquivalentTo(helloData); + helloData.HeartbeatInterval.Should().Be(0); + } + + [Fact] + public void Constructor_WhenCalledWithParameters_ItShouldInitializeProperties() + { + var opCode = 1; + var sequence = 2; + var type = "type"; + var heartbeatInterval = 1000; + + + var helloData = new HelloData + { + HeartbeatInterval = heartbeatInterval + }; + + var helloEvent = new HelloDiscordEvent + { + OpCode = opCode, + Sequence = sequence, + Type = type, + Data = helloData + }; + + helloEvent.OpCode.Should().Be(opCode); + helloEvent.Sequence.Should().Be(sequence); + helloEvent.Type.Should().Be(type); + helloEvent.Data.Should().BeSameAs(helloData); + } + + [Fact] + public void Deserialize_WhenCalled_ItShouldReturnHelloDiscordEvent() + { + var data = new + { + op = 0, + s = null as int?, + t = null as string, + d = new + { + heartbeat_interval = 1000 + } + }; + + var json = JsonSerializer.Serialize(data); + + var helloEvent = JsonSerializer.Deserialize(json); + + helloEvent.Should().NotBeNull(); + helloEvent!.OpCode.Should().Be(data.op); + helloEvent.Sequence.Should().Be(data.s); + helloEvent.Type.Should().Be(data.t); + helloEvent.Data.Should().NotBeNull(); + helloEvent.Data!.HeartbeatInterval.Should().Be(data.d.heartbeat_interval); + } +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/ReadyDiscordEventTests.cs b/src/StevesBot.Worker.Tests/Unit/ReadyDiscordEventTests.cs new file mode 100644 index 0000000..b7b3cde --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/ReadyDiscordEventTests.cs @@ -0,0 +1,72 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class ReadyDiscordEventTests +{ + [Fact] + public void Constructor_WhenCalled_ItShouldInitializeProperties() + { + var readyEvent = new ReadyDiscordEvent(); + var readyData = new ReadyData(); + + readyEvent.OpCode.Should().Be(0); + readyEvent.Sequence.Should().BeNull(); + readyEvent.Type.Should().BeNull(); + readyEvent.Data.Should().BeEquivalentTo(readyData); + readyData.Version.Should().Be(0); + readyData.SessionId.Should().Be(string.Empty); + readyData.ResumeGatewayUrl.Should().Be(string.Empty); + } + + [Fact] + public void Constructor_WhenCalledWithParameters_ItShouldInitializeProperties() + { + var readyData = new ReadyData + { + Version = 1, + SessionId = "session_id", + ResumeGatewayUrl = "resume_gateway_url" + }; + + var readyEvent = new ReadyDiscordEvent + { + OpCode = 1, + Sequence = 2, + Type = "type", + Data = readyData + }; + + readyEvent.OpCode.Should().Be(1); + readyEvent.Sequence.Should().Be(2); + readyEvent.Type.Should().Be("type"); + readyEvent.Data.Should().BeSameAs(readyData); + } + + [Fact] + public void Deserialize_WhenCalled_ItShouldReturnReadyDiscordEvent() + { + var data = new + { + op = 0, + s = null as int?, + t = null as string, + d = new + { + v = 1, + session_id = "session_id", + resume_gateway_url = "resume_gateway_url" + } + }; + + var json = JsonSerializer.Serialize(data); + + var readyEvent = JsonSerializer.Deserialize(json); + + readyEvent.Should().NotBeNull(); + readyEvent!.OpCode.Should().Be(data.op); + readyEvent.Sequence.Should().Be(data.s); + readyEvent.Type.Should().Be(data.t); + readyEvent.Data.Version.Should().Be(data.d.v); + readyEvent.Data.SessionId.Should().Be(data.d.session_id); + readyEvent.Data.ResumeGatewayUrl.Should().Be(data.d.resume_gateway_url); + } +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Usings.cs b/src/StevesBot.Worker.Tests/Usings.cs index bac0003..7d617df 100644 --- a/src/StevesBot.Worker.Tests/Usings.cs +++ b/src/StevesBot.Worker.Tests/Usings.cs @@ -1,5 +1,6 @@ global using System.Net.WebSockets; global using System.Text; +global using System.Text.Json; global using Microsoft.AspNetCore.Builder; global using Microsoft.AspNetCore.Hosting; @@ -12,4 +13,4 @@ global using StevesBot.Worker.Discord; global using StevesBot.Worker.Discord.Events; global using StevesBot.Worker.Tests.Integration.Infrastructure; global using StevesBot.Worker.Threading; -global using StevesBot.Worker.WebSockets; +global using StevesBot.Worker.WebSockets; \ No newline at end of file