Add more tests
Some checks failed
SonarQube / Build and analyze (push) Failing after 3m2s
Unit Tests / Run Tests (push) Failing after 2m23s

This commit is contained in:
Leonid Pershin
2025-10-20 08:20:55 +03:00
parent 1647fe19d3
commit c9eac74e35
12 changed files with 3873 additions and 11 deletions

View File

@@ -0,0 +1,446 @@
using ChatBot.Services.Telegram.Commands;
using FluentAssertions;
using Xunit;
namespace ChatBot.Tests.Telegram.Commands;
public class CommandAttributeTests
{
[Fact]
public void CommandAttribute_ShouldHaveCorrectAttributeUsage()
{
// Arrange
var attributeType = typeof(CommandAttribute);
var attributeUsage = attributeType
.GetCustomAttributes(typeof(AttributeUsageAttribute), false)
.Cast<AttributeUsageAttribute>()
.FirstOrDefault();
// Assert
attributeUsage.Should().NotBeNull();
attributeUsage!.ValidOn.Should().Be(AttributeTargets.Class);
attributeUsage.AllowMultiple.Should().BeFalse();
}
[Fact]
public void CommandAttribute_ShouldSetCommandNameAndDescription()
{
// Arrange
var commandName = "/test";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.Description.Should().Be(description);
attribute.Priority.Should().Be(0); // Default value
}
[Fact]
public void CommandAttribute_ShouldAllowSettingPriority()
{
// Arrange
var commandName = "/test";
var description = "Test command";
var priority = 5;
// Act
var attribute = new CommandAttribute(commandName, description) { Priority = priority };
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.Description.Should().Be(description);
attribute.Priority.Should().Be(priority);
}
[Theory]
[InlineData("/start", "Start the bot")]
[InlineData("/help", "Show help")]
[InlineData("/status", "Show status")]
[InlineData("/clear", "Clear chat history")]
[InlineData("/settings", "Show settings")]
public void CommandAttribute_ShouldAcceptValidCommandNames(
string commandName,
string description
)
{
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.Description.Should().Be(description);
}
[Theory]
[InlineData("")]
[InlineData("start")]
[InlineData("help")]
[InlineData("status")]
[InlineData("clear")]
[InlineData("settings")]
public void CommandAttribute_ShouldAcceptCommandNamesWithoutSlash(string commandName)
{
// Arrange
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.Description.Should().Be(description);
}
[Theory]
[InlineData("")]
[InlineData("A simple description")]
[InlineData(
"A very long description that contains multiple words and explains what the command does in detail"
)]
[InlineData("Описание на русском языке")]
[InlineData("Description with special characters: !@#$%^&*()_+-=[]{}|;':\",./<>?")]
[InlineData("Description with unicode: 用户 ユーザー مستخدم")]
public void CommandAttribute_ShouldAcceptVariousDescriptions(string description)
{
// Arrange
var commandName = "/test";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.Description.Should().Be(description);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(10)]
[InlineData(100)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
[InlineData(-1)]
[InlineData(-100)]
public void CommandAttribute_ShouldAcceptVariousPriorities(int priority)
{
// Arrange
var commandName = "/test";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description) { Priority = priority };
// Assert
attribute.Priority.Should().Be(priority);
}
[Fact]
public void CommandAttribute_ShouldAllowNullCommandName()
{
// Arrange
string? commandName = null;
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName!, description);
// Assert
attribute.CommandName.Should().BeNull();
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldAllowNullDescription()
{
// Arrange
var commandName = "/test";
string? description = null;
// Act
var attribute = new CommandAttribute(commandName, description!);
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.Description.Should().BeNull();
}
[Fact]
public void CommandAttribute_ShouldAllowBothNullValues()
{
// Arrange
string? commandName = null;
string? description = null;
// Act
var attribute = new CommandAttribute(commandName!, description!);
// Assert
attribute.CommandName.Should().BeNull();
attribute.Description.Should().BeNull();
}
[Fact]
public void CommandAttribute_ShouldBeImmutableAfterConstruction()
{
// Arrange
var commandName = "/test";
var description = "Test command";
var attribute = new CommandAttribute(commandName, description);
// Act & Assert
// CommandName and Description should be read-only
var commandNameProperty = typeof(CommandAttribute).GetProperty(
nameof(CommandAttribute.CommandName)
);
var descriptionProperty = typeof(CommandAttribute).GetProperty(
nameof(CommandAttribute.Description)
);
commandNameProperty!.CanWrite.Should().BeFalse();
descriptionProperty!.CanWrite.Should().BeFalse();
}
[Fact]
public void CommandAttribute_ShouldAllowPriorityModification()
{
// Arrange
var commandName = "/test";
var description = "Test command";
var attribute = new CommandAttribute(commandName, description);
// Act
attribute.Priority = 42;
// Assert
attribute.Priority.Should().Be(42);
}
[Fact]
public void CommandAttribute_ShouldInheritFromAttribute()
{
// Arrange & Act
var attribute = new CommandAttribute("/test", "Test command");
// Assert
attribute.Should().BeAssignableTo<Attribute>();
}
[Fact]
public void CommandAttribute_ShouldBeSerializable()
{
// Arrange
var commandName = "/test";
var description = "Test command";
var priority = 5;
var attribute = new CommandAttribute(commandName, description) { Priority = priority };
// Act & Assert
// Check if the attribute can be serialized (basic check)
attribute.Should().NotBeNull();
// Note: In .NET 5+, IsSerializable is obsolete and returns false for most types
// This test verifies the attribute can be created and used
attribute.GetType().Should().NotBeNull();
}
[Fact]
public void CommandAttribute_ShouldHandleVeryLongCommandName()
{
// Arrange
var commandName = new string('a', 1000); // Very long command name
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
attribute.CommandName.Should().HaveLength(1000);
}
[Fact]
public void CommandAttribute_ShouldHandleVeryLongDescription()
{
// Arrange
var commandName = "/test";
var description = new string('a', 10000); // Very long description
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
attribute.Description.Should().HaveLength(10000);
}
[Fact]
public void CommandAttribute_ShouldHandleCommandNameWithSpecialCharacters()
{
// Arrange
var commandName = "/test-command_with.special@chars#123";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
}
[Fact]
public void CommandAttribute_ShouldHandleDescriptionWithNewlines()
{
// Arrange
var commandName = "/test";
var description = "Line 1\nLine 2\nLine 3";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldHandleDescriptionWithTabs()
{
// Arrange
var commandName = "/test";
var description = "Column1\tColumn2\tColumn3";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldHandleDescriptionWithCarriageReturns()
{
// Arrange
var commandName = "/test";
var description = "Line 1\r\nLine 2\r\nLine 3";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldHandleWhitespaceOnlyCommandName()
{
// Arrange
var commandName = " ";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
}
[Fact]
public void CommandAttribute_ShouldHandleWhitespaceOnlyDescription()
{
// Arrange
var commandName = "/test";
var description = " ";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldHandleCommandNameWithUnicode()
{
// Arrange
var commandName = "/команда_命令_コマンド_أمر";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
}
[Fact]
public void CommandAttribute_ShouldHandleDescriptionWithUnicode()
{
// Arrange
var commandName = "/test";
var description = "Описание команды 命令描述 コマンドの説明 وصف الأمر";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldHandleCommandNameWithSpaces()
{
// Arrange
var commandName = "/test command with spaces";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
}
[Fact]
public void CommandAttribute_ShouldHandleDescriptionWithSpaces()
{
// Arrange
var commandName = "/test";
var description = "This is a test command with multiple spaces";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
[Fact]
public void CommandAttribute_ShouldHandleCommandNameWithNumbers()
{
// Arrange
var commandName = "/test123";
var description = "Test command";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.CommandName.Should().Be(commandName);
}
[Fact]
public void CommandAttribute_ShouldHandleDescriptionWithNumbers()
{
// Arrange
var commandName = "/test";
var description = "Test command version 1.0.0 build 123";
// Act
var attribute = new CommandAttribute(commandName, description);
// Assert
attribute.Description.Should().Be(description);
}
}

View File

@@ -0,0 +1,441 @@
using ChatBot.Services.Telegram.Commands;
using FluentAssertions;
using Xunit;
namespace ChatBot.Tests.Telegram.Commands;
public class ReplyInfoTests
{
[Fact]
public void ReplyInfo_ShouldHaveCorrectProperties()
{
// Arrange & Act
var replyInfo = new ReplyInfo
{
MessageId = 123,
UserId = 456L,
Username = "testuser",
};
// Assert
replyInfo.MessageId.Should().Be(123);
replyInfo.UserId.Should().Be(456L);
replyInfo.Username.Should().Be("testuser");
}
[Fact]
public void ReplyInfo_ShouldAllowNullUsername()
{
// Arrange & Act
var replyInfo = new ReplyInfo
{
MessageId = 123,
UserId = 456L,
Username = null,
};
// Assert
replyInfo.MessageId.Should().Be(123);
replyInfo.UserId.Should().Be(456L);
replyInfo.Username.Should().BeNull();
}
[Fact]
public void ReplyInfo_ShouldAllowEmptyUsername()
{
// Arrange & Act
var replyInfo = new ReplyInfo
{
MessageId = 123,
UserId = 456L,
Username = string.Empty,
};
// Assert
replyInfo.MessageId.Should().Be(123);
replyInfo.UserId.Should().Be(456L);
replyInfo.Username.Should().BeEmpty();
}
[Fact]
public void Create_ShouldReturnReplyInfo_WhenValidParameters()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldReturnReplyInfo_WhenUsernameIsNull()
{
// Arrange
var messageId = 123;
var userId = 456L;
string? username = null;
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().BeNull();
}
[Fact]
public void Create_ShouldReturnReplyInfo_WhenUsernameIsEmpty()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = string.Empty;
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().BeEmpty();
}
[Fact]
public void Create_ShouldReturnNull_WhenMessageIdIsNull()
{
// Arrange
int? messageId = null;
var userId = 456L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().BeNull();
}
[Fact]
public void Create_ShouldReturnNull_WhenUserIdIsNull()
{
// Arrange
var messageId = 123;
long? userId = null;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().BeNull();
}
[Fact]
public void Create_ShouldReturnNull_WhenBothMessageIdAndUserIdAreNull()
{
// Arrange
int? messageId = null;
long? userId = null;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().BeNull();
}
[Fact]
public void Create_ShouldReturnNull_WhenMessageIdIsZero()
{
// Arrange
var messageId = 0;
var userId = 456L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull(); // 0 is a valid message ID
result!.MessageId.Should().Be(0);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldReturnNull_WhenUserIdIsZero()
{
// Arrange
var messageId = 123;
var userId = 0L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull(); // 0 is a valid user ID
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(0L);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldReturnNull_WhenMessageIdIsNegative()
{
// Arrange
var messageId = -1;
var userId = 456L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull(); // Negative values are still valid
result!.MessageId.Should().Be(-1);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldReturnNull_WhenUserIdIsNegative()
{
// Arrange
var messageId = 123;
var userId = -1L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull(); // Negative values are still valid
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(-1L);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldHandleVeryLongUsername()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = new string('a', 1000); // Very long username
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
result.Username.Should().HaveLength(1000);
}
[Fact]
public void Create_ShouldHandleUsernameWithSpecialCharacters()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = "user@domain.com!@#$%^&*()_+-=[]{}|;':\",./<>?";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldHandleUsernameWithUnicodeCharacters()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = "пользователь_用户_ユーザー_مستخدم";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldHandleUsernameWithWhitespace()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = " user with spaces ";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username); // Username is preserved as-is
}
[Fact]
public void Create_ShouldHandleUsernameWithNewlines()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = "user\nwith\nnewlines";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void Create_ShouldHandleUsernameWithTabs()
{
// Arrange
var messageId = 123;
var userId = 456L;
var username = "user\twith\ttabs";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(100)]
[InlineData(int.MaxValue)]
[InlineData(int.MinValue)]
public void Create_ShouldHandleVariousMessageIds(int messageId)
{
// Arrange
var userId = 456L;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Theory]
[InlineData(0L)]
[InlineData(1L)]
[InlineData(100L)]
[InlineData(long.MaxValue)]
[InlineData(long.MinValue)]
public void Create_ShouldHandleVariousUserIds(long userId)
{
// Arrange
var messageId = 123;
var username = "testuser";
// Act
var result = ReplyInfo.Create(messageId, userId, username);
// Assert
result.Should().NotBeNull();
result!.MessageId.Should().Be(messageId);
result.UserId.Should().Be(userId);
result.Username.Should().Be(username);
}
[Fact]
public void ReplyInfo_ShouldBeMutable()
{
// Arrange
var replyInfo = new ReplyInfo
{
MessageId = 123,
UserId = 456L,
Username = "testuser",
};
// Act
replyInfo.MessageId = 789;
replyInfo.UserId = 101112L;
replyInfo.Username = "newuser";
// Assert
replyInfo.MessageId.Should().Be(789);
replyInfo.UserId.Should().Be(101112L);
replyInfo.Username.Should().Be("newuser");
}
[Fact]
public void ReplyInfo_ShouldAllowSettingUsernameToNull()
{
// Arrange
var replyInfo = new ReplyInfo
{
MessageId = 123,
UserId = 456L,
Username = "testuser",
};
// Act
replyInfo.Username = null;
// Assert
replyInfo.Username.Should().BeNull();
}
[Fact]
public void ReplyInfo_ShouldAllowSettingUsernameToEmpty()
{
// Arrange
var replyInfo = new ReplyInfo
{
MessageId = 123,
UserId = 456L,
Username = "testuser",
};
// Act
replyInfo.Username = string.Empty;
// Assert
replyInfo.Username.Should().BeEmpty();
}
}

View File

@@ -0,0 +1,768 @@
using ChatBot.Services.Telegram.Commands;
using FluentAssertions;
namespace ChatBot.Tests.Telegram.Commands;
public class TelegramCommandContextTests
{
[Fact]
public void Create_ShouldCreateContextWithBasicProperties()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "Hello bot";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Should().NotBeNull();
context.ChatId.Should().Be(chatId);
context.Username.Should().Be(username);
context.MessageText.Should().Be(messageText);
context.ChatType.Should().Be(chatType);
context.ChatTitle.Should().Be(chatTitle);
context.Arguments.Should().Be("bot"); // "Hello bot" split by space gives ["Hello", "bot"]
context.ReplyInfo.Should().BeNull();
}
[Fact]
public void Create_ShouldExtractArgumentsFromMessage()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1 arg2 arg3";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1 arg2 arg3");
}
[Fact]
public void Create_ShouldHandleEmptyArguments()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleMessageWithoutCommand()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "Hello bot";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("bot"); // "Hello bot" split by space gives ["Hello", "bot"]
}
[Fact]
public void Create_ShouldRemoveBotUsernameFromCommand()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@mybot arg1 arg2";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1 arg2");
}
[Fact]
public void Create_ShouldHandleMultipleBotUsernames()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@bot1@bot2 arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1");
}
[Fact]
public void Create_ShouldHandleAtSymbolWithoutBotName()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@ arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1");
}
[Fact]
public void Create_ShouldHandleEmptyBotUsername()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@ arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1");
}
[Fact]
public void Create_ShouldTrimArguments()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1 arg2 ";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1 arg2");
}
[Fact]
public void Create_ShouldHandleReplyInfo()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "Test Chat";
var replyInfo = new ReplyInfo
{
MessageId = 1,
UserId = 123,
Username = "otheruser",
};
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle,
replyInfo
);
// Assert
context.ReplyInfo.Should().Be(replyInfo);
context.ReplyInfo!.MessageId.Should().Be(1);
context.ReplyInfo.UserId.Should().Be(123);
context.ReplyInfo.Username.Should().Be("otheruser");
}
[Fact]
public void Create_ShouldHandleNullReplyInfo()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "Test Chat";
ReplyInfo? replyInfo = null;
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle,
replyInfo
);
// Assert
context.ReplyInfo.Should().BeNull();
}
[Fact]
public void Create_ShouldHandleEmptyMessage()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.MessageText.Should().BeEmpty();
context.Arguments.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleWhitespaceOnlyMessage()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = " ";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.MessageText.Should().Be(" ");
context.Arguments.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleVeryLongMessage()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start " + new string('A', 10000);
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().HaveLength(10000);
context.Arguments.Should().StartWith("AAAA");
}
[Fact]
public void Create_ShouldHandleSpecialCharactersInArguments()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start !@#$%^&*()_+-=[]{}|;':\",./<>?";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("!@#$%^&*()_+-=[]{}|;':\",./<>?");
}
[Fact]
public void Create_ShouldHandleUnicodeCharacters()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start привет мир 🌍";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("привет мир 🌍");
}
[Fact]
public void Create_ShouldHandleNegativeChatId()
{
// Arrange
var chatId = -12345L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.ChatId.Should().Be(chatId);
}
[Fact]
public void Create_ShouldHandleZeroChatId()
{
// Arrange
var chatId = 0L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.ChatId.Should().Be(chatId);
}
[Fact]
public void Create_ShouldHandleEmptyUsername()
{
// Arrange
var chatId = 12345L;
var username = "";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Username.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleEmptyChatType()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.ChatType.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleEmptyChatTitle()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.ChatTitle.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleVeryLongUsername()
{
// Arrange
var chatId = 12345L;
var username = new string('A', 1000);
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Username.Should().HaveLength(1000);
context.Username.Should().StartWith("AAAA");
}
[Fact]
public void Create_ShouldHandleVeryLongChatTitle()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1";
var chatType = "private";
var chatTitle = new string('B', 1000);
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.ChatTitle.Should().HaveLength(1000);
context.ChatTitle.Should().StartWith("BBBB");
}
[Fact]
public void Create_ShouldHandleMessageWithOnlySpaces()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = " ";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.MessageText.Should().Be(" ");
context.Arguments.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleMessageWithTabsAndNewlines()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start\targ1\narg2\r\narg3";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().BeEmpty(); // Split by space only, so tabs and newlines are not split
}
[Fact]
public void Create_ShouldHandleMessageWithMultipleSpacesInArguments()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start arg1 arg2 arg3";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1 arg2 arg3"); // Trim() removes leading spaces
}
[Fact]
public void Create_ShouldHandleMessageWithOnlyCommandAndSpaces()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start ";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleMessageWithCommandAndOnlySpacesAsArguments()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start ";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().BeEmpty();
}
[Fact]
public void Create_ShouldHandleMessageWithCommandAndMixedWhitespace()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start\t \n arg1 \t arg2 \r\n ";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1 \t arg2"); // Split by space and trim removes leading/trailing spaces
}
[Fact]
public void Create_ShouldHandleMessageWithVeryLongBotUsername()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@verylongbotname arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1");
}
[Fact]
public void Create_ShouldHandleMessageWithSpecialCharactersInBotUsername()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@bot_name-123 arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1");
}
[Fact]
public void Create_ShouldHandleMessageWithUnicodeInBotUsername()
{
// Arrange
var chatId = 12345L;
var username = "testuser";
var messageText = "/start@бот123 arg1";
var chatType = "private";
var chatTitle = "Test Chat";
// Act
var context = TelegramCommandContext.Create(
chatId,
username,
messageText,
chatType,
chatTitle
);
// Assert
context.Arguments.Should().Be("arg1");
}
}