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

@@ -516,4 +516,33 @@ public class AIServiceTests : UnitTestBase
Times.AtLeast(3) // One for each attempt
);
}
[Fact]
public async Task GenerateChatCompletionAsync_ShouldTimeout_WhenRequestExceedsConfiguredTimeout()
{
// Arrange
var messages = TestDataBuilder.ChatMessages.CreateMessageHistory(2);
var model = "llama3.2";
_modelServiceMock.Setup(x => x.GetCurrentModel()).Returns(model);
_systemPromptServiceMock.Setup(x => x.GetSystemPromptAsync()).ReturnsAsync("System prompt");
// Configure very small timeout (not strictly needed for this simulation)
_aiSettings.RequestTimeoutSeconds = 1;
// Emulate timeout from underlying client
_ollamaClientMock
.Setup(x => x.ChatAsync(It.IsAny<OllamaSharp.Models.Chat.ChatRequest>()))
.Throws(new TimeoutException("Request timed out"));
// Act
var result = await _aiService.GenerateChatCompletionAsync(messages);
// Assert
result.Should().Be(AIResponseConstants.DefaultErrorMessage);
_ollamaClientMock.Verify(
x => x.ChatAsync(It.IsAny<OllamaSharp.Models.Chat.ChatRequest>()),
Times.Exactly(3)
);
}
}

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()
{