104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
using ChatBot.Services;
|
|
using ChatBot.Services.Telegram.Commands;
|
|
using ChatBot.Services.Telegram.Interfaces;
|
|
using ChatBot.Tests.TestUtilities;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
|
|
namespace ChatBot.Tests.Telegram.Commands;
|
|
|
|
public class CommandRegistryTests : UnitTestBase
|
|
{
|
|
private readonly Mock<ILogger<CommandRegistry>> _loggerMock;
|
|
private readonly CommandRegistry _commandRegistry;
|
|
|
|
public CommandRegistryTests()
|
|
{
|
|
_loggerMock = TestDataBuilder.Mocks.CreateLoggerMock<CommandRegistry>();
|
|
_commandRegistry = new CommandRegistry(_loggerMock.Object, new List<ITelegramCommand>());
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_ShouldRegisterCommands()
|
|
{
|
|
// Arrange
|
|
var command = new Mock<ITelegramCommand>();
|
|
command.Setup(x => x.CommandName).Returns("test");
|
|
|
|
// Act
|
|
var registry = new CommandRegistry(_loggerMock.Object, new[] { command.Object });
|
|
|
|
// Assert
|
|
var registeredCommand = registry.GetCommand("test");
|
|
registeredCommand.Should().Be(command.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetCommand_ShouldReturnCommand_WhenCommandExists()
|
|
{
|
|
// Arrange
|
|
var command = new Mock<ITelegramCommand>();
|
|
command.Setup(x => x.CommandName).Returns("test");
|
|
var registry = new CommandRegistry(_loggerMock.Object, new[] { command.Object });
|
|
|
|
// Act
|
|
var result = registry.GetCommand("test");
|
|
|
|
// Assert
|
|
result.Should().Be(command.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetCommand_ShouldReturnNull_WhenCommandDoesNotExist()
|
|
{
|
|
// Act
|
|
var result = _commandRegistry.GetCommand("nonexistent");
|
|
|
|
// Assert
|
|
result.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAllCommands_ShouldReturnAllRegisteredCommands()
|
|
{
|
|
// Arrange
|
|
var command1 = new Mock<ITelegramCommand>();
|
|
command1.Setup(x => x.CommandName).Returns("test1");
|
|
var command2 = new Mock<ITelegramCommand>();
|
|
command2.Setup(x => x.CommandName).Returns("test2");
|
|
var registry = new CommandRegistry(
|
|
_loggerMock.Object,
|
|
new[] { command1.Object, command2.Object }
|
|
);
|
|
|
|
// Act
|
|
var result = registry.GetAllCommands();
|
|
|
|
// Assert
|
|
result.Should().HaveCount(2);
|
|
result.Should().Contain(command1.Object);
|
|
result.Should().Contain(command2.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_ShouldNotOverwriteExistingCommand_WhenCommandWithSameNameExists()
|
|
{
|
|
// Arrange
|
|
var command1 = new Mock<ITelegramCommand>();
|
|
command1.Setup(x => x.CommandName).Returns("test");
|
|
var command2 = new Mock<ITelegramCommand>();
|
|
command2.Setup(x => x.CommandName).Returns("test");
|
|
|
|
// Act
|
|
var registry = new CommandRegistry(
|
|
_loggerMock.Object,
|
|
new[] { command1.Object, command2.Object }
|
|
);
|
|
|
|
// Assert
|
|
var result = registry.GetCommand("test");
|
|
result.Should().Be(command1.Object); // First command should be kept
|
|
}
|
|
}
|