add tests
All checks were successful
SonarQube / Build and analyze (push) Successful in 3m54s
Unit Tests / Run Tests (push) Successful in 2m23s

This commit is contained in:
Leonid Pershin
2025-10-20 15:11:42 +03:00
parent f8fd16edb2
commit 1c910d7b7f
8 changed files with 549 additions and 154 deletions

View File

@@ -574,6 +574,74 @@ public class ChatServiceTests : UnitTestBase
);
}
[Fact]
public async Task ProcessMessageAsync_ShouldTrimHistory_WhenCompressionDisabled()
{
// Arrange
var chatId = 99999L;
_aiSettings.EnableHistoryCompression = false;
var session = TestDataBuilder.ChatSessions.CreateBasicSession(chatId);
session.MaxHistoryLength = 5; // force small history limit
_sessionStorageMock
.Setup(x => x.GetOrCreate(chatId, It.IsAny<string>(), It.IsAny<string>()))
.Returns(session);
_sessionStorageMock.Setup(x => x.Get(chatId)).Returns(session);
_aiServiceMock
.Setup(x =>
x.GenerateChatCompletionWithCompressionAsync(
It.IsAny<List<ChatBot.Models.Dto.ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync("ok");
// Act: add many messages to exceed the limit
for (int i = 0; i < 10; i++)
{
await _chatService.ProcessMessageAsync(chatId, "u", $"m{i}");
}
// Assert: history trimmed to MaxHistoryLength
session.GetMessageCount().Should().BeLessThanOrEqualTo(5);
}
[Fact]
public async Task ProcessMessageAsync_ShouldCompressHistory_WhenCompressionEnabledAndThresholdExceeded()
{
// Arrange
var chatId = 88888L;
_aiSettings.EnableHistoryCompression = true;
_aiSettings.CompressionThreshold = 4;
_aiSettings.CompressionTarget = 3;
var session = TestDataBuilder.ChatSessions.CreateBasicSession(chatId);
session.MaxHistoryLength = 50; // avoid trimming impacting compression assertion
_sessionStorageMock
.Setup(x => x.GetOrCreate(chatId, It.IsAny<string>(), It.IsAny<string>()))
.Returns(session);
_sessionStorageMock.Setup(x => x.Get(chatId)).Returns(session);
_aiServiceMock
.Setup(x =>
x.GenerateChatCompletionWithCompressionAsync(
It.IsAny<List<ChatBot.Models.Dto.ChatMessage>>(),
It.IsAny<CancellationToken>()
)
)
.ReturnsAsync("ok");
// Act: add enough messages to exceed threshold
for (int i = 0; i < 6; i++)
{
await _chatService.ProcessMessageAsync(chatId, "u", $"m{i}");
}
// Assert: compressed to target (user+assistant messages should be around target)
session.GetMessageCount().Should().BeLessThanOrEqualTo(_aiSettings.CompressionTarget + 1);
}
[Fact]
public async Task ProcessMessageAsync_ShouldLogEmptyResponseMarker()
{