add tests
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user