Files
ChatBot/ChatBot.Tests/Telegram/Commands/TelegramCommandContextTests.cs
Leonid Pershin c9eac74e35
Some checks failed
SonarQube / Build and analyze (push) Failing after 3m2s
Unit Tests / Run Tests (push) Failing after 2m23s
Add more tests
2025-10-20 08:20:55 +03:00

769 lines
18 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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");
}
}