273 lines
8.7 KiB
C#
273 lines
8.7 KiB
C#
using ChatBot.Services;
|
|
using ChatBot.Services.Interfaces;
|
|
using ChatBot.Tests.TestUtilities;
|
|
using FluentAssertions;
|
|
using Moq;
|
|
using Telegram.Bot.Types;
|
|
|
|
namespace ChatBot.Tests.Services.Interfaces;
|
|
|
|
public class ITelegramBotClientWrapperTests : UnitTestBase
|
|
{
|
|
[Fact]
|
|
public void ITelegramBotClientWrapper_ShouldHaveCorrectMethodSignatures()
|
|
{
|
|
// Arrange & Act
|
|
var interfaceType = typeof(ITelegramBotClientWrapper);
|
|
var methods = interfaceType.GetMethods();
|
|
|
|
// Assert
|
|
methods.Should().HaveCount(1);
|
|
|
|
// GetMeAsync method
|
|
var getMeAsyncMethod = methods.FirstOrDefault(m => m.Name == "GetMeAsync");
|
|
getMeAsyncMethod.Should().NotBeNull();
|
|
getMeAsyncMethod!.ReturnType.Should().Be<Task<User>>();
|
|
getMeAsyncMethod.GetParameters().Should().HaveCount(1);
|
|
getMeAsyncMethod.GetParameters()[0].ParameterType.Should().Be<CancellationToken>();
|
|
}
|
|
|
|
[Fact]
|
|
public void ITelegramBotClientWrapper_ShouldBeImplementedByTelegramBotClientWrapper()
|
|
{
|
|
// Arrange & Act
|
|
var telegramBotClientWrapperType = typeof(TelegramBotClientWrapper);
|
|
var interfaceType = typeof(ITelegramBotClientWrapper);
|
|
|
|
// Assert
|
|
interfaceType.IsAssignableFrom(telegramBotClientWrapperType).Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldReturnUser()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var cancellationToken = CancellationToken.None;
|
|
var expectedUser = new User
|
|
{
|
|
Id = 123456789,
|
|
IsBot = true,
|
|
FirstName = "TestBot",
|
|
Username = "test_bot",
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var result = await mock.Object.GetMeAsync(cancellationToken);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedUser);
|
|
mock.Verify(x => x.GetMeAsync(cancellationToken), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldHandleCancellationToken()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var cancellationToken = new CancellationToken(true); // Cancelled token
|
|
var expectedUser = new User
|
|
{
|
|
Id = 123456789,
|
|
IsBot = true,
|
|
FirstName = "TestBot",
|
|
Username = "test_bot",
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var result = await mock.Object.GetMeAsync(cancellationToken);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedUser);
|
|
mock.Verify(x => x.GetMeAsync(cancellationToken), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldUseDefaultCancellationToken()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var expectedUser = new User
|
|
{
|
|
Id = 123456789,
|
|
IsBot = true,
|
|
FirstName = "TestBot",
|
|
Username = "test_bot",
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var result = await mock.Object.GetMeAsync();
|
|
|
|
// Assert
|
|
result.Should().Be(expectedUser);
|
|
mock.Verify(x => x.GetMeAsync(CancellationToken.None), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldHandleUserWithAllProperties()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var cancellationToken = CancellationToken.None;
|
|
var expectedUser = new User
|
|
{
|
|
Id = 987654321,
|
|
IsBot = true,
|
|
FirstName = "AdvancedBot",
|
|
LastName = "Test",
|
|
Username = "advanced_test_bot",
|
|
LanguageCode = "en",
|
|
IsPremium = true,
|
|
AddedToAttachmentMenu = true,
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var result = await mock.Object.GetMeAsync(cancellationToken);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedUser);
|
|
result.Id.Should().Be(987654321);
|
|
result.IsBot.Should().BeTrue();
|
|
result.FirstName.Should().Be("AdvancedBot");
|
|
result.LastName.Should().Be("Test");
|
|
result.Username.Should().Be("advanced_test_bot");
|
|
result.LanguageCode.Should().Be("en");
|
|
result.IsPremium.Should().BeTrue();
|
|
result.AddedToAttachmentMenu.Should().BeTrue();
|
|
mock.Verify(x => x.GetMeAsync(cancellationToken), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldHandleMinimalUser()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var cancellationToken = CancellationToken.None;
|
|
var expectedUser = new User
|
|
{
|
|
Id = 111111111,
|
|
IsBot = false,
|
|
FirstName = "MinimalUser",
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var result = await mock.Object.GetMeAsync(cancellationToken);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedUser);
|
|
result.Id.Should().Be(111111111);
|
|
result.IsBot.Should().BeFalse();
|
|
result.FirstName.Should().Be("MinimalUser");
|
|
result.LastName.Should().BeNull();
|
|
result.Username.Should().BeNull();
|
|
result.LanguageCode.Should().BeNull();
|
|
mock.Verify(x => x.GetMeAsync(cancellationToken), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void ITelegramBotClientWrapper_ShouldBePublicInterface()
|
|
{
|
|
// Arrange & Act
|
|
var interfaceType = typeof(ITelegramBotClientWrapper);
|
|
|
|
// Assert
|
|
interfaceType.IsPublic.Should().BeTrue();
|
|
interfaceType.IsInterface.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void ITelegramBotClientWrapper_ShouldHaveCorrectNamespace()
|
|
{
|
|
// Arrange & Act
|
|
var interfaceType = typeof(ITelegramBotClientWrapper);
|
|
|
|
// Assert
|
|
interfaceType.Namespace.Should().Be("ChatBot.Services.Interfaces");
|
|
}
|
|
|
|
[Fact]
|
|
public void ITelegramBotClientWrapper_ShouldHaveCorrectGenericConstraints()
|
|
{
|
|
// Arrange & Act
|
|
var interfaceType = typeof(ITelegramBotClientWrapper);
|
|
var methods = interfaceType.GetMethods();
|
|
|
|
// Assert
|
|
// All methods should be public
|
|
methods.All(m => m.IsPublic).Should().BeTrue();
|
|
|
|
// GetMeAsync should have default parameter for CancellationToken
|
|
var getMeAsyncMethod = methods.First(m => m.Name == "GetMeAsync");
|
|
getMeAsyncMethod.GetParameters()[0].HasDefaultValue.Should().BeTrue();
|
|
getMeAsyncMethod.GetParameters()[0].DefaultValue.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldHandleMultipleCalls()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var cancellationToken = CancellationToken.None;
|
|
var expectedUser = new User
|
|
{
|
|
Id = 123456789,
|
|
IsBot = true,
|
|
FirstName = "TestBot",
|
|
Username = "test_bot",
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var result1 = await mock.Object.GetMeAsync(cancellationToken);
|
|
var result2 = await mock.Object.GetMeAsync(cancellationToken);
|
|
var result3 = await mock.Object.GetMeAsync(cancellationToken);
|
|
|
|
// Assert
|
|
result1.Should().Be(expectedUser);
|
|
result2.Should().Be(expectedUser);
|
|
result3.Should().Be(expectedUser);
|
|
mock.Verify(x => x.GetMeAsync(cancellationToken), Times.Exactly(3));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ITelegramBotClientWrapper_GetMeAsync_ShouldHandleConcurrentCalls()
|
|
{
|
|
// Arrange
|
|
var mock = new Mock<ITelegramBotClientWrapper>();
|
|
var cancellationToken = CancellationToken.None;
|
|
var expectedUser = new User
|
|
{
|
|
Id = 123456789,
|
|
IsBot = true,
|
|
FirstName = "TestBot",
|
|
Username = "test_bot",
|
|
};
|
|
|
|
mock.Setup(x => x.GetMeAsync(It.IsAny<CancellationToken>())).ReturnsAsync(expectedUser);
|
|
|
|
// Act
|
|
var tasks = new List<Task<User>>();
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
tasks.Add(mock.Object.GetMeAsync(cancellationToken));
|
|
}
|
|
|
|
var results = await Task.WhenAll(tasks);
|
|
|
|
// Assert
|
|
results.Should().AllBeEquivalentTo(expectedUser);
|
|
mock.Verify(x => x.GetMeAsync(cancellationToken), Times.Exactly(10));
|
|
}
|
|
}
|