refactor: extract tests to library test project
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class CreateMessageRequestTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldReturnAnInstance()
|
||||
{
|
||||
var content = "content";
|
||||
var messageReference = new DiscordMessageReference(
|
||||
1,
|
||||
"message_id",
|
||||
"channel_id",
|
||||
"guild_id",
|
||||
false
|
||||
);
|
||||
|
||||
var result = new CreateMessageRequest(content, messageReference);
|
||||
|
||||
result.Content.Should().Be(content);
|
||||
result.MessageReference.Should().BeSameAs(messageReference);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class DiscordClientOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithoutParameters_ItShouldCreateInstance()
|
||||
{
|
||||
var options = new DiscordClientOptions();
|
||||
|
||||
options.Should().NotBeNull();
|
||||
options.Should().BeOfType<DiscordClientOptions>();
|
||||
options.ApiUrl.Should().Be(string.Empty);
|
||||
options.AppToken.Should().Be(string.Empty);
|
||||
options.Intents.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithParameters_ItShouldCreateInstance()
|
||||
{
|
||||
var apiUrl = "https://api.example.com";
|
||||
var appToken = "test-token";
|
||||
var intents = 123;
|
||||
|
||||
var options = new DiscordClientOptions
|
||||
{
|
||||
ApiUrl = apiUrl,
|
||||
AppToken = appToken,
|
||||
Intents = intents
|
||||
};
|
||||
|
||||
options.Should().NotBeNull();
|
||||
options.Should().BeOfType<DiscordClientOptions>();
|
||||
options.ApiUrl.Should().Be(apiUrl);
|
||||
options.AppToken.Should().Be(appToken);
|
||||
options.Intents.Should().Be(intents);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class DiscordMessageReferenceTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldReturnAnInstance()
|
||||
{
|
||||
var type = 1;
|
||||
var messageId = "message_id";
|
||||
var channelId = "channel_id";
|
||||
var guildId = "guild_id";
|
||||
var failIfNotExists = true;
|
||||
|
||||
var result = new DiscordMessageReference(
|
||||
type,
|
||||
messageId,
|
||||
channelId,
|
||||
guildId,
|
||||
failIfNotExists
|
||||
);
|
||||
|
||||
result.Type.Should().Be(type);
|
||||
result.MessageId.Should().Be(messageId);
|
||||
result.ChannelId.Should().Be(channelId);
|
||||
result.GuildId.Should().Be(guildId);
|
||||
result.FailIfNotExists.Should().Be(failIfNotExists);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class DiscordMessageReferenceTypesTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(TestData))]
|
||||
public void Type_WhenCalled_ItShouldReturnExpectedValue(int type, int expected)
|
||||
{
|
||||
type.Should().Be(expected);
|
||||
}
|
||||
|
||||
public static TheoryData<int, int> TestData => new()
|
||||
{
|
||||
{ DiscordMessageReferenceTypes.Default, 0 },
|
||||
{ DiscordMessageReferenceTypes.Forward, 1 },
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
@@ -0,0 +1,34 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class DiscordRestClientExceptionTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithNoParameters_ItShouldCreateInstance()
|
||||
{
|
||||
var exception = new DiscordRestClientException();
|
||||
|
||||
exception.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithMessage_ItShouldCreateInstance()
|
||||
{
|
||||
var message = "Test message";
|
||||
var exception = new DiscordRestClientException(message);
|
||||
|
||||
exception.Should().NotBeNull();
|
||||
exception.Message.Should().Be(message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithMessageAndInnerException_ItShouldCreateInstance()
|
||||
{
|
||||
var message = "Test message";
|
||||
var innerException = new Exception("Inner exception");
|
||||
var exception = new DiscordRestClientException(message, innerException);
|
||||
|
||||
exception.Should().NotBeNull();
|
||||
exception.Message.Should().Be(message);
|
||||
exception.InnerException.Should().Be(innerException);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public sealed class DiscordRestClientTests : IDisposable
|
||||
{
|
||||
private const string BaseUrl = "https://discord.com/api/v10";
|
||||
private static string GatewayEndpoint => $"{BaseUrl}/gateway";
|
||||
private static string ChannelMessagesEndpoint => $"{BaseUrl}/channels/*/messages";
|
||||
|
||||
private readonly Mock<ILogger<DiscordRestClient>> _mockLogger = new();
|
||||
private readonly MockHttpMessageHandler _mockHttpMessageHandler;
|
||||
private readonly DiscordRestClient _discordRestClient;
|
||||
|
||||
public DiscordRestClientTests()
|
||||
{
|
||||
_mockHttpMessageHandler = new MockHttpMessageHandler();
|
||||
var httpClient = _mockHttpMessageHandler.ToHttpClient();
|
||||
httpClient.BaseAddress = new Uri("https://discord.com/api/v10/");
|
||||
_discordRestClient = new DiscordRestClient(_mockLogger.Object, httpClient);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenLoggerIsNull_ItShouldThrowAnException()
|
||||
{
|
||||
var act = () => new DiscordRestClient(null!, _mockHttpMessageHandler.ToHttpClient());
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenHttpClientIsNull_ItShouldThrowAnException()
|
||||
{
|
||||
var act = () => new DiscordRestClient(_mockLogger.Object, null!);
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetGatewayUrlAsync_WhenRequestFails_ItShouldThrowException()
|
||||
{
|
||||
_mockHttpMessageHandler
|
||||
.When(GatewayEndpoint)
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var act = async () => await _discordRestClient.GetGatewayUrlAsync(CancellationToken.None);
|
||||
|
||||
await act.Should().ThrowAsync<DiscordRestClientException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetGatewayUrlAsync_WhenResponseIsNull_ItShouldThrowException()
|
||||
{
|
||||
_mockHttpMessageHandler
|
||||
.When(GatewayEndpoint)
|
||||
.Respond(HttpStatusCode.OK, "application/json", "null");
|
||||
|
||||
var act = async () => await _discordRestClient.GetGatewayUrlAsync(CancellationToken.None);
|
||||
|
||||
await act.Should().ThrowAsync<DiscordRestClientException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetGatewayUrlAsync_WhenRequestIsSuccessful_ItShouldReturnGatewayUrl()
|
||||
{
|
||||
var expectedUrl = "test";
|
||||
var jsonResponse = $@"{{
|
||||
""url"": ""{expectedUrl}""
|
||||
}}";
|
||||
|
||||
_mockHttpMessageHandler
|
||||
.When(GatewayEndpoint)
|
||||
.Respond(HttpStatusCode.OK, "application/json", jsonResponse);
|
||||
|
||||
var result = await _discordRestClient.GetGatewayUrlAsync(CancellationToken.None);
|
||||
|
||||
result.Should().Be(expectedUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateMessageAsync_WhenRequestFails_ItShouldThrowAnException()
|
||||
{
|
||||
_mockHttpMessageHandler
|
||||
.When(ChannelMessagesEndpoint)
|
||||
.Respond(HttpStatusCode.InternalServerError);
|
||||
|
||||
var request = new CreateMessageRequest(
|
||||
"content",
|
||||
new(DiscordMessageReferenceTypes.Default, "message_id", "channel_id", "guild_id", false)
|
||||
);
|
||||
|
||||
var act = async () => await _discordRestClient.CreateMessageAsync("channel_id", request);
|
||||
|
||||
await act.Should().ThrowAsync<DiscordRestClientException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateMessageyAsync_WhenResponseIsNull_ItShouldThrowAnException()
|
||||
{
|
||||
_mockHttpMessageHandler
|
||||
.When(ChannelMessagesEndpoint)
|
||||
.Respond(HttpStatusCode.OK, "application/json", "null");
|
||||
|
||||
var request = new CreateMessageRequest(
|
||||
"content",
|
||||
new(DiscordMessageReferenceTypes.Default, "message_id", "channel_id", "guild_id", false)
|
||||
);
|
||||
|
||||
var act = async () => await _discordRestClient.CreateMessageAsync("channel_id", request);
|
||||
|
||||
await act.Should().ThrowAsync<DiscordRestClientException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateMessageAsync_WhenRequestSucceeds_ItShouldReturnMessage()
|
||||
{
|
||||
var message = new DiscordMessage();
|
||||
var messageResponse = JsonSerializer.Serialize(message);
|
||||
|
||||
_mockHttpMessageHandler
|
||||
.When(ChannelMessagesEndpoint)
|
||||
.Respond(HttpStatusCode.OK, "application/json", messageResponse);
|
||||
|
||||
var request = new CreateMessageRequest(
|
||||
"content",
|
||||
new(DiscordMessageReferenceTypes.Default, "message_id", "channel_id", "guild_id", false)
|
||||
);
|
||||
|
||||
var result = await _discordRestClient.CreateMessageAsync("channel_id", request);
|
||||
|
||||
result.Should().BeEquivalentTo(message);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_mockHttpMessageHandler.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class HostExtensionsTests
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public class SeqOptionsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldReturnInstance()
|
||||
{
|
||||
var result = new SeqOptions();
|
||||
|
||||
result.ServerUrl.Should().Be(string.Empty);
|
||||
result.ApiKeyHeader.Should().Be(string.Empty);
|
||||
result.ApiKey.Should().Be(string.Empty);
|
||||
result.IsEnabled.Should().BeFalse();
|
||||
result.LogEndpoint.Should().Be($"{string.Empty}/ingest/otlp/v1/logs");
|
||||
result.TraceEndpoint.Should().Be($"{string.Empty}/ingest/otlp/v1/traces");
|
||||
result.AuthHeader.Should().Be($"{string.Empty}={string.Empty}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenPropertiesSet_ItShouldReturnCorrectValues()
|
||||
{
|
||||
var serverUrl = "http://example.com";
|
||||
var apiKeyHeader = "ApiKeyHeader";
|
||||
var apiKey = "ApiKeyValue";
|
||||
|
||||
var result = new SeqOptions
|
||||
{
|
||||
ServerUrl = serverUrl,
|
||||
ApiKeyHeader = apiKeyHeader,
|
||||
ApiKey = apiKey
|
||||
};
|
||||
|
||||
result.ServerUrl.Should().Be(serverUrl);
|
||||
result.ApiKeyHeader.Should().Be(apiKeyHeader);
|
||||
result.ApiKey.Should().Be(apiKey);
|
||||
result.IsEnabled.Should().BeTrue();
|
||||
result.LogEndpoint.Should().Be($"{serverUrl}/ingest/otlp/v1/logs");
|
||||
result.TraceEndpoint.Should().Be($"{serverUrl}/ingest/otlp/v1/traces");
|
||||
result.AuthHeader.Should().Be($"{apiKeyHeader}={apiKey}");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null, null, false)]
|
||||
[InlineData("", "", "", false)]
|
||||
[InlineData("http://example.com", null, null, false)]
|
||||
[InlineData("http://example.com", "", "", false)]
|
||||
[InlineData(null, "ApiKeyHeader", null, false)]
|
||||
[InlineData(null, null, "ApiKeyValue", false)]
|
||||
[InlineData("http://example.com", "ApiKeyHeader", "ApiKeyValue", true)]
|
||||
[InlineData("http://example.com", "", "ApiKeyValue", false)]
|
||||
[InlineData("", "ApiKeyHeader", "ApiKeyValue", false)]
|
||||
[InlineData("http://example.com", "ApiKeyHeader", null, false)]
|
||||
[InlineData("", "ApiKeyHeader", null, false)]
|
||||
[InlineData(null, "ApiKeyHeader", "ApiKeyValue", false)]
|
||||
public void IsEnabled_WhenPropertiesAreEmpty_ItShouldReturnFalse(string? serverUrl, string? apiKeyHeader, string? apiKey, bool expected)
|
||||
{
|
||||
var result = new SeqOptions
|
||||
{
|
||||
ServerUrl = serverUrl!,
|
||||
ApiKeyHeader = apiKeyHeader!,
|
||||
ApiKey = apiKey!
|
||||
};
|
||||
|
||||
result.IsEnabled.Should().Be(expected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public sealed class StevesBotWebhookInstrumentationTests : IDisposable
|
||||
{
|
||||
private readonly StevesBotWebhookInstrumentation _instrumentation = new();
|
||||
|
||||
[Fact]
|
||||
public void SourceName_WhenCalled_ItShouldReturnCorrectName()
|
||||
{
|
||||
_instrumentation.SourceName.Should().Be("StevesBot.Webhook");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SourceVersion_WhenCalled_ItShouldReturnCorrectVersion()
|
||||
{
|
||||
_instrumentation.SourceVersion.Should().Be("0.0.0");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldReturnInstance()
|
||||
{
|
||||
using var result = new StevesBotWorkerInstrumentation();
|
||||
using var expectedSource = new ActivitySource(_instrumentation.SourceName, _instrumentation.SourceVersion);
|
||||
|
||||
result.Source.Should().BeEquivalentTo(expectedSource);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_instrumentation.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
namespace StevesBot.Library.Tests.Unit;
|
||||
|
||||
public sealed class StevesBotWorkerInstrumentationTests : IDisposable
|
||||
{
|
||||
private readonly StevesBotWorkerInstrumentation _instrumentation = new();
|
||||
|
||||
[Fact]
|
||||
public void SourceName_WhenCalled_ItShouldReturnCorrectName()
|
||||
{
|
||||
_instrumentation.SourceName.Should().Be("StevesBot.Worker");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SourceVersion_WhenCalled_ItShouldReturnCorrectVersion()
|
||||
{
|
||||
_instrumentation.SourceVersion.Should().Be("0.0.0");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldReturnInstance()
|
||||
{
|
||||
using var result = new StevesBotWorkerInstrumentation();
|
||||
using var expectedSource = new ActivitySource(_instrumentation.SourceName, _instrumentation.SourceVersion);
|
||||
|
||||
result.Source.Should().BeEquivalentTo(expectedSource);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_instrumentation.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,16 @@
|
||||
global using System.Diagnostics;
|
||||
global using System.Net;
|
||||
global using System.Text.Json;
|
||||
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
|
||||
global using Moq;
|
||||
|
||||
global using RichardSzalay.MockHttp;
|
||||
|
||||
global using StevesBot.Library.Discord;
|
||||
global using StevesBot.Library.Discord.Common;
|
||||
global using StevesBot.Library.Discord.Rest;
|
||||
global using StevesBot.Library.Discord.Rest;
|
||||
global using StevesBot.Library.Discord.Rest.Requests;
|
||||
global using StevesBot.Library.Telemetry;
|
||||
|
||||
Reference in New Issue
Block a user