From c16e8754db53d695f0c2925d28f020b1c2ab2cf0 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 9 May 2025 18:28:40 -0500 Subject: [PATCH] tests: add tests --- src/StevesBot.Worker.Tests/.editorconfig | 1 + .../Infrastructure/TestWebSocketServer.cs | 2 + .../Integration/WebSocketTests.cs | 2 - .../Unit/DiscordEventTypesTests.cs | 16 +++++++ .../Unit/DiscordIntentsTests.cs | 37 +++++++++++++++ .../Unit/DiscordOpCodesTests.cs | 19 ++++++++ .../Unit/WebSocketFactoryTests.cs | 17 +++++++ src/StevesBot.Worker.Tests/Usings.cs | 2 + .../Discord/DiscordClientOptions.cs | 2 +- .../Discord/DiscordIntents.cs | 46 +++++++++---------- .../WebSockets/IWebSocketFactory.cs | 6 +++ .../WebSockets/WebSocketFactory.cs | 9 ++++ 12 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs create mode 100644 src/StevesBot.Worker.Tests/Unit/DiscordIntentsTests.cs create mode 100644 src/StevesBot.Worker.Tests/Unit/DiscordOpCodesTests.cs create mode 100644 src/StevesBot.Worker.Tests/Unit/WebSocketFactoryTests.cs diff --git a/src/StevesBot.Worker.Tests/.editorconfig b/src/StevesBot.Worker.Tests/.editorconfig index c14fa32..4d6c5e6 100644 --- a/src/StevesBot.Worker.Tests/.editorconfig +++ b/src/StevesBot.Worker.Tests/.editorconfig @@ -5,3 +5,4 @@ dotnet_diagnostic.CA2201.severity = none dotnet_diagnostic.CA2007.severity = none dotnet_diagnostic.CA1303.severity = none dotnet_diagnostic.CA1031.severity = none +dotnet_diagnostic.CA1034.severity = none diff --git a/src/StevesBot.Worker.Tests/Integration/Infrastructure/TestWebSocketServer.cs b/src/StevesBot.Worker.Tests/Integration/Infrastructure/TestWebSocketServer.cs index cf2ebc0..0f33444 100644 --- a/src/StevesBot.Worker.Tests/Integration/Infrastructure/TestWebSocketServer.cs +++ b/src/StevesBot.Worker.Tests/Integration/Infrastructure/TestWebSocketServer.cs @@ -1,3 +1,5 @@ +using WebSocket = System.Net.WebSockets.WebSocket; + namespace StevesBot.Worker.Tests.Integration.Infrastructure; # pragma warning disable CA1001 diff --git a/src/StevesBot.Worker.Tests/Integration/WebSocketTests.cs b/src/StevesBot.Worker.Tests/Integration/WebSocketTests.cs index f5a89b2..bc79796 100644 --- a/src/StevesBot.Worker.Tests/Integration/WebSocketTests.cs +++ b/src/StevesBot.Worker.Tests/Integration/WebSocketTests.cs @@ -1,5 +1,3 @@ -using System.Text; - using WebSocket = StevesBot.Worker.WebSockets.WebSocket; namespace StevesBot.Worker.Tests.Integration; diff --git a/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs b/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs new file mode 100644 index 0000000..d337be9 --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs @@ -0,0 +1,16 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class DiscordEventTypesTests +{ + [Theory] + [MemberData(nameof(TestData))] + public void Event_WhenCalled_ItShouldReturnCorrectValue(string eventType, string expectedValue) + { + eventType.Should().Be(expectedValue); + } + + public static TheoryData TestData => new() + { + { DiscordEventTypes.Ready, "READY" }, + }; +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/DiscordIntentsTests.cs b/src/StevesBot.Worker.Tests/Unit/DiscordIntentsTests.cs new file mode 100644 index 0000000..bb3a52c --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/DiscordIntentsTests.cs @@ -0,0 +1,37 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class DiscordIntentsTests +{ + [Theory] + [MemberData(nameof(TestData))] + public void Intents_ShouldHaveCorrectValue(long intent, long expectedValue) + { + intent.Should().Be(expectedValue); + } + + public static TheoryData TestData => new() + { + { DiscordIntents.Guilds, 1 }, + { DiscordIntents.GuildMembers, 2 }, + { DiscordIntents.GuildModeration, 4 }, + { DiscordIntents.GuildExpressions, 8 }, + { DiscordIntents.GuildIntegrations, 16 }, + { DiscordIntents.GuildWebhooks, 32 }, + { DiscordIntents.GuildInvites, 64 }, + { DiscordIntents.GuildVoiceStates, 128 }, + { DiscordIntents.GuildPresences, 256 }, + { DiscordIntents.GuildMessages, 512 }, + { DiscordIntents.GuildMessageReactions, 1024 }, + { DiscordIntents.GuildMessageTyping, 2048 }, + { DiscordIntents.DirectMessages, 4096 }, + { DiscordIntents.DirectMessageReactions, 8192 }, + { DiscordIntents.DirectMessageTyping, 16384 }, + { DiscordIntents.MessageContent, 32768 }, + { DiscordIntents.GuildScheduledEvents, 65536 }, + { DiscordIntents.AutoModerationConfiguration, 1048576 }, + { DiscordIntents.AutoModerationExecution, 2097152 }, + { DiscordIntents.GuildMessagePolls, 16777216 }, + { DiscordIntents.DirectMessagePolls, 33554432 }, + { DiscordIntents.All, 53608447 } + }; +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/DiscordOpCodesTests.cs b/src/StevesBot.Worker.Tests/Unit/DiscordOpCodesTests.cs new file mode 100644 index 0000000..0aae7c7 --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/DiscordOpCodesTests.cs @@ -0,0 +1,19 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class DiscordOpCodesTests +{ + [Theory] + [MemberData(nameof(TestData))] + public void OpCode_ShouldHaveCorrectValue(int opCode, int expectedValue) + { + opCode.Should().Be(expectedValue); + } + + public static TheoryData TestData => new() + { + { DiscordOpCodes.Dispatch, 0 }, + { DiscordOpCodes.Heartbeat, 1 }, + { DiscordOpCodes.Hello, 10 }, + { DiscordOpCodes.HeartbeatAck, 11 } + }; +} diff --git a/src/StevesBot.Worker.Tests/Unit/WebSocketFactoryTests.cs b/src/StevesBot.Worker.Tests/Unit/WebSocketFactoryTests.cs new file mode 100644 index 0000000..d0f254e --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/WebSocketFactoryTests.cs @@ -0,0 +1,17 @@ +using WebSocket = StevesBot.Worker.WebSockets.WebSocket; + +namespace StevesBot.Worker.Tests.Unit; + +public class WebSocketFactoryTests +{ + private readonly WebSocketFactory _webSocketFactory = new(); + + [Fact] + public void Create_WhenCalled_ItShouldReturnNewWebSocketInstance() + { + var result = _webSocketFactory.Create(); + + result.Should().NotBeNull(); + result.Should().BeOfType(); + } +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Usings.cs b/src/StevesBot.Worker.Tests/Usings.cs index 22f266f..bac0003 100644 --- a/src/StevesBot.Worker.Tests/Usings.cs +++ b/src/StevesBot.Worker.Tests/Usings.cs @@ -1,4 +1,5 @@ global using System.Net.WebSockets; +global using System.Text; global using Microsoft.AspNetCore.Builder; global using Microsoft.AspNetCore.Hosting; @@ -11,3 +12,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; diff --git a/src/StevesBot.Worker/Discord/DiscordClientOptions.cs b/src/StevesBot.Worker/Discord/DiscordClientOptions.cs index 130390d..3d3d11f 100644 --- a/src/StevesBot.Worker/Discord/DiscordClientOptions.cs +++ b/src/StevesBot.Worker/Discord/DiscordClientOptions.cs @@ -4,5 +4,5 @@ internal sealed class DiscordClientOptions { public string ApiUrl { get; init; } = string.Empty; public string AppToken { get; init; } = string.Empty; - public int Intents { get; init; } + public long Intents { get; init; } } \ No newline at end of file diff --git a/src/StevesBot.Worker/Discord/DiscordIntents.cs b/src/StevesBot.Worker/Discord/DiscordIntents.cs index 5d9e28c..2d58e15 100644 --- a/src/StevesBot.Worker/Discord/DiscordIntents.cs +++ b/src/StevesBot.Worker/Discord/DiscordIntents.cs @@ -2,28 +2,28 @@ namespace StevesBot.Worker.Discord; internal static class DiscordIntents { - public const int Guilds = 1 << 0; - public const int GuildMembers = 1 << 1; - public const int GuildModeration = 1 << 2; - public const int GuildExpressions = 1 << 3; - public const int GuildIntegrations = 1 << 4; - public const int GuildWebhooks = 1 << 5; - public const int GuildInvites = 1 << 6; - public const int GuildVoiceStates = 1 << 7; - public const int GuildPresences = 1 << 8; - public const int GuildMessages = 1 << 9; - public const int GuildMessageReactions = 1 << 10; - public const int GuildMessageTyping = 1 << 11; - public const int DirectMessages = 1 << 12; - public const int DirectMessageReactions = 1 << 13; - public const int DirectMessageTyping = 1 << 14; - public const int MessageContent = 1 << 15; - public const int GuildScheduledEvents = 1 << 16; - public const int AutoModerationConfiguration = 1 << 20; - public const int AutoModerationExecution = 1 << 21; - public const int GuildMessagePolls = 1 << 24; - public const int DirectMessagePolls = 1 << 25; - public const int All = Guilds + public const long Guilds = 1 << 0; + public const long GuildMembers = 1 << 1; + public const long GuildModeration = 1 << 2; + public const long GuildExpressions = 1 << 3; + public const long GuildIntegrations = 1 << 4; + public const long GuildWebhooks = 1 << 5; + public const long GuildInvites = 1 << 6; + public const long GuildVoiceStates = 1 << 7; + public const long GuildPresences = 1 << 8; + public const long GuildMessages = 1 << 9; + public const long GuildMessageReactions = 1 << 10; + public const long GuildMessageTyping = 1 << 11; + public const long DirectMessages = 1 << 12; + public const long DirectMessageReactions = 1 << 13; + public const long DirectMessageTyping = 1 << 14; + public const long MessageContent = 1 << 15; + public const long GuildScheduledEvents = 1 << 16; + public const long AutoModerationConfiguration = 1 << 20; + public const long AutoModerationExecution = 1 << 21; + public const long GuildMessagePolls = 1 << 24; + public const long DirectMessagePolls = 1 << 25; + public const long All = Guilds | GuildMembers | GuildModeration | GuildExpressions @@ -44,4 +44,4 @@ internal static class DiscordIntents | AutoModerationExecution | GuildMessagePolls | DirectMessagePolls; -} +} \ No newline at end of file diff --git a/src/StevesBot.Worker/WebSockets/IWebSocketFactory.cs b/src/StevesBot.Worker/WebSockets/IWebSocketFactory.cs index e69de29..5fb9f9a 100644 --- a/src/StevesBot.Worker/WebSockets/IWebSocketFactory.cs +++ b/src/StevesBot.Worker/WebSockets/IWebSocketFactory.cs @@ -0,0 +1,6 @@ +namespace StevesBot.Worker.WebSockets; + +internal interface IWebSocketFactory +{ + IWebSocket Create(); +} \ No newline at end of file diff --git a/src/StevesBot.Worker/WebSockets/WebSocketFactory.cs b/src/StevesBot.Worker/WebSockets/WebSocketFactory.cs index e69de29..3fb0795 100644 --- a/src/StevesBot.Worker/WebSockets/WebSocketFactory.cs +++ b/src/StevesBot.Worker/WebSockets/WebSocketFactory.cs @@ -0,0 +1,9 @@ +namespace StevesBot.Worker.WebSockets; + +internal class WebSocketFactory : IWebSocketFactory +{ + public IWebSocket Create() + { + return new WebSocket(); + } +} \ No newline at end of file