tests: add unit tests for Discord event types and extensions

This commit is contained in:
Stevan Freeborn
2025-05-21 21:52:14 -05:00
parent 6ae1cd413b
commit 02cff46d11
7 changed files with 118 additions and 3 deletions
@@ -1 +0,0 @@
namespace StevesBot.Worker.Tests.Unit;
@@ -12,5 +12,27 @@ public class DiscordEventTypesTests
public static TheoryData<string, string> 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<string?, bool> IsValidEventNameTestData => new()
{
{ " ", false },
{ "", false },
{ null, false },
{ "I MADE IT UP", false },
{ DiscordEventTypes.Ready, true },
{ DiscordEventTypes.GuildMemberAdd, true },
{ DiscordEventTypes.MessageCreate, true },
};
}
@@ -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<IDiscordRestClient> _mockDiscordRestClient = new();
private readonly Mock<ILogger<DiscordGatewayClient>> _mockLogger = new();
private readonly Mock<IWebSocketFactory> _mockWebSocketFactory = new();
private readonly Mock<TimeProvider> _mockTimeProvider = new();
private readonly Mock<IServiceScopeFactory> _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<IDiscordRestClient>();
act.Should().NotThrow();
}
[Fact]
public void AddDiscordGatewayClient_WhenCalled_ItShouldAddDiscordGatewayClient()
{
_services.AddSingleton(_mockDiscordRestClient.Object);
_services.AddDiscordGatewayClient();
var act = () => _services
.BuildServiceProvider()
.GetRequiredService<IDiscordGatewayClient>();
act.Should().NotThrow();
}
[Fact]
public void AddDiscordGatewayClient_WhenCalledWithConfigureAction_ItShouldAddDiscordGatewayClientAndConfigureIt()
{
var mockAction = new Mock<Action<IDiscordGatewayClient>>();
_services.AddSingleton(_mockDiscordRestClient.Object);
_services.AddDiscordGatewayClient(mockAction.Object);
var act = () => _services
.BuildServiceProvider()
.GetRequiredService<IDiscordGatewayClient>();
act.Should().NotThrow();
mockAction.Invocations.Count.Should().Be(1);
}
}
@@ -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);
}
}
+1
View File
@@ -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;
+1 -1
View File
@@ -17,7 +17,7 @@ internal static class Extensions
return services;
}
public static IServiceCollection AddDiscordGatewayClient(this IServiceCollection services, Action<IDiscordGatewayClient>? configure)
public static IServiceCollection AddDiscordGatewayClient(this IServiceCollection services, Action<IDiscordGatewayClient>? configure = null)
{
services.AddSingleton<IDiscordGatewayClient>(sp =>
{
@@ -8,7 +8,7 @@ internal static class DiscordEventTypes
public static bool IsValidEvent(string eventName)
{
if (string.IsNullOrEmpty(eventName))
if (string.IsNullOrWhiteSpace(eventName))
{
return false;
}