From 02cff46d1124815a7a93f271e0d92a9341fa0ec2 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Wed, 21 May 2025 21:52:14 -0500 Subject: [PATCH] tests: add unit tests for Discord event types and extensions --- .../Unit/DiscordEventTypes.cs | 1 - .../Unit/DiscordEventTypesTests.cs | 22 ++++++ .../Unit/ExtensionsTests.cs | 70 +++++++++++++++++++ .../Unit/IdentifyDiscordEventTests.cs | 23 ++++++ src/StevesBot.Worker.Tests/Usings.cs | 1 + src/StevesBot.Worker/Discord/Extensions.cs | 2 +- .../Gateway/Events/DiscordEventTypes.cs | 2 +- 7 files changed, 118 insertions(+), 3 deletions(-) delete mode 100644 src/StevesBot.Worker.Tests/Unit/DiscordEventTypes.cs create mode 100644 src/StevesBot.Worker.Tests/Unit/ExtensionsTests.cs create mode 100644 src/StevesBot.Worker.Tests/Unit/IdentifyDiscordEventTests.cs diff --git a/src/StevesBot.Worker.Tests/Unit/DiscordEventTypes.cs b/src/StevesBot.Worker.Tests/Unit/DiscordEventTypes.cs deleted file mode 100644 index 2f73151..0000000 --- a/src/StevesBot.Worker.Tests/Unit/DiscordEventTypes.cs +++ /dev/null @@ -1 +0,0 @@ -namespace StevesBot.Worker.Tests.Unit; \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs b/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs index d337be9..0e9e83e 100644 --- a/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs +++ b/src/StevesBot.Worker.Tests/Unit/DiscordEventTypesTests.cs @@ -12,5 +12,27 @@ public class DiscordEventTypesTests public static TheoryData TestData => new() { { DiscordEventTypes.Ready, "READY" }, + { DiscordEventTypes.GuildMemberAdd, "GUILD_MEMBER_ADD" }, + { DiscordEventTypes.MessageCreate, "MESSAGE_CREATE" }, + }; + + [Theory] + [MemberData(nameof(IsValidEventNameTestData))] + public void IsValidEventName_WhenCalled_ItShouldReturnCorrectValue(string? eventName, bool expected) + { + var result = DiscordEventTypes.IsValidEvent(eventName!); + + result.Should().Be(expected); + } + + public static TheoryData IsValidEventNameTestData => new() + { + { " ", false }, + { "", false }, + { null, false }, + { "I MADE IT UP", false }, + { DiscordEventTypes.Ready, true }, + { DiscordEventTypes.GuildMemberAdd, true }, + { DiscordEventTypes.MessageCreate, true }, }; } \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/ExtensionsTests.cs b/src/StevesBot.Worker.Tests/Unit/ExtensionsTests.cs new file mode 100644 index 0000000..210d5dc --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/ExtensionsTests.cs @@ -0,0 +1,70 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class ExtensionsTests +{ + private readonly DiscordClientOptions _discordClientOptions = new() + { + ApiUrl = "https://test.com", + AppToken = "test_token", + }; + private readonly Mock _mockDiscordRestClient = new(); + private readonly Mock> _mockLogger = new(); + private readonly Mock _mockWebSocketFactory = new(); + private readonly Mock _mockTimeProvider = new(); + private readonly Mock _mockScopeFactory = new(); + + private readonly ServiceCollection _services = new(); + + public ExtensionsTests() + { + _services.AddSingleton(_discordClientOptions); + _services.AddSingleton(_mockLogger.Object); + _services.AddSingleton(_mockWebSocketFactory.Object); + _services.AddSingleton(_mockTimeProvider.Object); + _services.AddSingleton(_mockScopeFactory.Object); + } + + [Fact] + public void AddDiscordRestClient_WhenCalled_ItShouldAddDiscordRestClient() + { + _services.AddDiscordRestClient(); + + var act = () => _services + .BuildServiceProvider() + .GetRequiredService(); + + act.Should().NotThrow(); + } + + [Fact] + public void AddDiscordGatewayClient_WhenCalled_ItShouldAddDiscordGatewayClient() + { + _services.AddSingleton(_mockDiscordRestClient.Object); + + _services.AddDiscordGatewayClient(); + + var act = () => _services + .BuildServiceProvider() + .GetRequiredService(); + + act.Should().NotThrow(); + } + + [Fact] + public void AddDiscordGatewayClient_WhenCalledWithConfigureAction_ItShouldAddDiscordGatewayClientAndConfigureIt() + { + var mockAction = new Mock>(); + + _services.AddSingleton(_mockDiscordRestClient.Object); + + _services.AddDiscordGatewayClient(mockAction.Object); + + var act = () => _services + .BuildServiceProvider() + .GetRequiredService(); + + act.Should().NotThrow(); + + mockAction.Invocations.Count.Should().Be(1); + } +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Unit/IdentifyDiscordEventTests.cs b/src/StevesBot.Worker.Tests/Unit/IdentifyDiscordEventTests.cs new file mode 100644 index 0000000..8d2c505 --- /dev/null +++ b/src/StevesBot.Worker.Tests/Unit/IdentifyDiscordEventTests.cs @@ -0,0 +1,23 @@ +namespace StevesBot.Worker.Tests.Unit; + +public class IdentifyDiscordEventTests +{ + [Fact] + public void Constructor_WhenCalled_ItShouldReturnInstance() + { + var token = "test_token"; + var intents = 123456789; + var presence = new UpdatePresenceData + { + Status = "online", + Activities = [], + }; + + var result = new IdentifyDiscordEvent(token, intents, presence); + + result.OpCode.Should().Be(DiscordOpCodes.Identify); + result.Data.Token.Should().Be(token); + result.Data.Intents.Should().Be(intents); + result.Data.Presence.Should().BeSameAs(presence); + } +} \ No newline at end of file diff --git a/src/StevesBot.Worker.Tests/Usings.cs b/src/StevesBot.Worker.Tests/Usings.cs index 351d711..b5db2a1 100644 --- a/src/StevesBot.Worker.Tests/Usings.cs +++ b/src/StevesBot.Worker.Tests/Usings.cs @@ -16,6 +16,7 @@ global using Moq; global using RichardSzalay.MockHttp; +global using StevesBot.Worker.Discord; global using StevesBot.Worker.Discord.Gateway; global using StevesBot.Worker.Discord.Gateway.Events; global using StevesBot.Worker.Discord.Gateway.Events.Data; diff --git a/src/StevesBot.Worker/Discord/Extensions.cs b/src/StevesBot.Worker/Discord/Extensions.cs index a1eccf2..6ed4d5a 100644 --- a/src/StevesBot.Worker/Discord/Extensions.cs +++ b/src/StevesBot.Worker/Discord/Extensions.cs @@ -17,7 +17,7 @@ internal static class Extensions return services; } - public static IServiceCollection AddDiscordGatewayClient(this IServiceCollection services, Action? configure) + public static IServiceCollection AddDiscordGatewayClient(this IServiceCollection services, Action? configure = null) { services.AddSingleton(sp => { diff --git a/src/StevesBot.Worker/Discord/Gateway/Events/DiscordEventTypes.cs b/src/StevesBot.Worker/Discord/Gateway/Events/DiscordEventTypes.cs index 1553b5a..6bec93f 100644 --- a/src/StevesBot.Worker/Discord/Gateway/Events/DiscordEventTypes.cs +++ b/src/StevesBot.Worker/Discord/Gateway/Events/DiscordEventTypes.cs @@ -8,7 +8,7 @@ internal static class DiscordEventTypes public static bool IsValidEvent(string eventName) { - if (string.IsNullOrEmpty(eventName)) + if (string.IsNullOrWhiteSpace(eventName)) { return false; }