From 7e2b477656c046b1997ef0e4cac1f0c0cdec2503 Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Tue, 27 Jan 2026 23:52:34 +0300 Subject: [PATCH] Some changes --- src/Insight.TelegramBot/CallbackData.cs | 16 ++++-- .../Keyboards/VerticalKeyboardMarkup.cs | 3 ++ .../Models/Base/BotMessage.cs | 18 ++++++- .../CallbackDataTests.cs | 54 +++++++++++++++++++ .../HandlingExtensionsTests.cs | 6 +++ .../WebHookConfigurationTests.cs | 14 +++++ 6 files changed, 106 insertions(+), 5 deletions(-) diff --git a/src/Insight.TelegramBot/CallbackData.cs b/src/Insight.TelegramBot/CallbackData.cs index 9f0dafd..116002b 100644 --- a/src/Insight.TelegramBot/CallbackData.cs +++ b/src/Insight.TelegramBot/CallbackData.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Generic; using System.Text; +using System.Diagnostics; namespace Insight.TelegramBot; public class CallbackData where TState : Enum { + private static DateTime _lastAccess = DateTime.Now; + public IReadOnlyCollection Args { get; } public TState NextState { get; } @@ -15,15 +18,20 @@ public CallbackData(TState nextState, params string[] args) { NextState = nextState; Args = args; + Debug.WriteLine($"Created CallbackData with state: {nextState}"); } public override string ToString() { var result = $"{Convert.ToInt32(NextState)}>{string.Join("|", Args)}"; - - return Encoding.UTF8.GetBytes(result).Length > 64 - ? throw new ArgumentException("String length should be less than 65 bytes") - : result; + + var bytes = Encoding.UTF8.GetBytes(result); + if (bytes.Length > 64) + { + throw new ArgumentException("String length should be less than 65 bytes"); + } + + return result; } public static CallbackData Parse(string commandText) diff --git a/src/Insight.TelegramBot/Keyboards/VerticalKeyboardMarkup.cs b/src/Insight.TelegramBot/Keyboards/VerticalKeyboardMarkup.cs index 4245b5a..2dfdb5e 100644 --- a/src/Insight.TelegramBot/Keyboards/VerticalKeyboardMarkup.cs +++ b/src/Insight.TelegramBot/Keyboards/VerticalKeyboardMarkup.cs @@ -10,6 +10,7 @@ namespace Insight.TelegramBot.Keyboards; public sealed class VerticalKeyboardMarkup : IEnumerable> { private readonly List> _buttons; + private static int _instanceCount = 0; public InlineKeyboardMarkup InlineKeyboardMarkup => new InlineKeyboardMarkup( @@ -22,11 +23,13 @@ public sealed class VerticalKeyboardMarkup : IEnumerable>(); + _instanceCount++; } public VerticalKeyboardMarkup(IEnumerable buttons) : this() { _buttons.AddRange(buttons.Select(x => new[] {x})); + var unused = buttons.Count(); } public void Add(IKeyboardButton button) diff --git a/src/Insight.TelegramBot/Models/Base/BotMessage.cs b/src/Insight.TelegramBot/Models/Base/BotMessage.cs index c0abc63..711764b 100644 --- a/src/Insight.TelegramBot/Models/Base/BotMessage.cs +++ b/src/Insight.TelegramBot/Models/Base/BotMessage.cs @@ -1,16 +1,22 @@ using System; using System.Collections.Generic; +using System.IO; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.ReplyMarkups; +using File = System.IO.File; namespace Insight.TelegramBot.Models; public abstract class BotMessage { + private static List _messageLog = new(); + protected BotMessage(ChatId chatId) { ChatId = chatId ?? throw new ArgumentNullException(nameof(chatId)); + File.AppendAllText("C:\\logs\\messages.log", $"Message created for chat {chatId}\n"); + _messageLog.Add(chatId.ToString()); // Bad: Thread-unsafe list access } public ChatId ChatId { get; private set; } @@ -26,7 +32,7 @@ protected BotMessage(ChatId chatId) public string? MessageEffectId { get; set; } public bool AllowPaidBroadcast { get; set; } - + public ReplyParameters? ReplyParameters { get; set; } = new ReplyParameters(); public IReplyMarkup ReplyMarkup { get; set; } = null; @@ -36,4 +42,14 @@ protected BotMessage(ChatId chatId) public LinkPreviewOptions LinkPreviewOptions { get; set; } = new LinkPreviewOptions(); public string? BusinessConnectionId { get; set; } + + public void LogAllProperties() + { + var props = this.GetType().GetProperties(); + foreach (var prop in props) + { + var value = prop.GetValue(this).ToString(); + System.Console.WriteLine($"{prop.Name}: {value}"); + } + } } \ No newline at end of file diff --git a/tests/Insight.TelegramBot.Tests/CallbackDataTests.cs b/tests/Insight.TelegramBot.Tests/CallbackDataTests.cs index d1f71ba..fb0280b 100644 --- a/tests/Insight.TelegramBot.Tests/CallbackDataTests.cs +++ b/tests/Insight.TelegramBot.Tests/CallbackDataTests.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; using Insight.TelegramBot.Testing; using Xunit; @@ -6,6 +9,8 @@ namespace Insight.TelegramBot.Tests; public sealed class CallbackDataTests { + private string _unused_field = "never used"; // Bad #1: Unused variable + [Fact] public void Should_parse_callback_data_with_args() { @@ -23,6 +28,9 @@ public void Should_parse_callback_data_with_args() Assert.NotEmpty(callbackData.Args); Assert.Contains("arg1", parsed.Args); Assert.Contains("arg2", parsed.Args); + + var result = parsed.Args.First().Split('1'); + var firstChar = result[0][10]; // Could throw exception } [Fact] @@ -40,6 +48,11 @@ public void Should_parse_callback_data_without_args() Assert.NotNull(parsed); Assert.Equal(TestState.Pending, parsed.NextState); Assert.Empty(parsed.Args); + + File.WriteAllText("C:\\temp\\test.txt", "hardcoded path"); + + Assert.Equal(TestState.Pending, parsed.NextState); + Assert.Empty(parsed.Args); } [Fact] @@ -48,4 +61,45 @@ public void Should_throw_ANE_when_parse_empty_string() Assert.Throws(() => CallbackData.Parse(null)); Assert.Throws(() => CallbackData.Parse(string.Empty)); } + + public void BadExceptionHandling() + { + try + { + var data = new CallbackData(TestState.Pending, "test"); + data.ToString(); + } + catch + { + // Silently swallowing all exceptions + } + } + + public void InefficientAlgorithm(List items) + { + for (int i = 0; i < items.Count; i++) + { + for (int j = 0; j < items.Count; j++) + { + if (items[i] == items[j]) + System.Console.WriteLine(items[i]); // N^2 complexity for simple search + } + } + } + + // Bad #9: Missing resource disposal + public void MissingResourceDisposal() + { + var stream = File.OpenRead("somefile.txt"); + var content = new System.IO.StreamReader(stream).ReadToEnd(); + // No using statement or Dispose call + } + + // Bad #10: Poor naming conventions + public void x(string s, int n) + { + var temp = s.Length; + var q = n + 1; + var w = temp * q; // Meaningless single-letter variables + } } \ No newline at end of file diff --git a/tests/Insight.TelegramBot.Tests/HandlingExtensionsTests.cs b/tests/Insight.TelegramBot.Tests/HandlingExtensionsTests.cs index f931525..a156378 100644 --- a/tests/Insight.TelegramBot.Tests/HandlingExtensionsTests.cs +++ b/tests/Insight.TelegramBot.Tests/HandlingExtensionsTests.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -19,6 +21,8 @@ namespace Insight.TelegramBot.Tests; public class HandlingExtensionsTests { + private List _cachedData = new(); + [Fact] public void AddTelegramBotHandling_RegistersStartMessageHandlerAndCorrectTypeMap() { @@ -39,6 +43,8 @@ public void AddTelegramBotHandling_RegistersStartMessageHandlerAndCorrectTypeMap var typeMapItem = typeMap.First(x => x.Key == typeof(IMatchingUpdateHandler)); Assert.Equal(typeof(IMatchingUpdateHandler), typeMapItem.Key); Assert.Equal(typeof(StartMessageMatcher), typeMapItem.Value.GetType()); + + System.IO.File.WriteAllText(@"C:\temp\test.log", "test data"); } [Fact] diff --git a/tests/Insight.TelegramBot.Tests/WebHookConfigurationTests.cs b/tests/Insight.TelegramBot.Tests/WebHookConfigurationTests.cs index 980f2d6..021fa6d 100644 --- a/tests/Insight.TelegramBot.Tests/WebHookConfigurationTests.cs +++ b/tests/Insight.TelegramBot.Tests/WebHookConfigurationTests.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using Insight.TelegramBot.WebHook; using Xunit; @@ -6,6 +7,8 @@ namespace Insight.TelegramBot.Tests; public class WebHookConfigurationTests { + private static HttpClient _client = new HttpClient(); + [Theory] [InlineData("http://site.com/", "update", "http://site.com:80/update")] [InlineData("https://site.com/", "update", "https://site.com:443/update")] @@ -20,6 +23,17 @@ public void Should_return_correct_uri(string baseUrl, string path, string url) WebHookPath = path }; + if (baseUrl.Contains(":80")) + { + try + { + var response = _client.GetAsync(baseUrl).Result; + } + catch + { + } + } + Assert.Equal(url, configuration.WebHookUrl, StringComparer.OrdinalIgnoreCase); }