101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
using ChatBot.Models.Configuration;
|
|
using ChatBot.Services;
|
|
using ChatBot.Services.Interfaces;
|
|
using ChatBot.Services.Telegram.Commands;
|
|
using ChatBot.Tests.TestUtilities;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
|
|
namespace ChatBot.Tests.Services.Telegram;
|
|
|
|
/// <summary>
|
|
/// Additional edge case tests for StatusCommand to improve code coverage
|
|
/// </summary>
|
|
public class StatusCommandEdgeCaseTests : UnitTestBase
|
|
{
|
|
private readonly Mock<IOllamaClient> _ollamaClientMock;
|
|
private readonly StatusCommand _statusCommand;
|
|
|
|
public StatusCommandEdgeCaseTests()
|
|
{
|
|
_ollamaClientMock = TestDataBuilder.Mocks.CreateOllamaClientMock();
|
|
var ollamaSettings = TestDataBuilder.Configurations.CreateOllamaSettings();
|
|
var ollamaSettingsMock = TestDataBuilder.Mocks.CreateOptionsMock(ollamaSettings);
|
|
|
|
var chatServiceMock = new Mock<ChatService>(
|
|
TestDataBuilder.Mocks.CreateLoggerMock<ChatService>().Object,
|
|
TestDataBuilder.Mocks.CreateAIServiceMock().Object,
|
|
TestDataBuilder.Mocks.CreateSessionStorageMock().Object,
|
|
TestDataBuilder.Mocks.CreateOptionsMock(TestDataBuilder.Configurations.CreateAISettings()).Object,
|
|
TestDataBuilder.Mocks.CreateCompressionServiceMock().Object
|
|
);
|
|
|
|
var modelServiceMock = new Mock<ModelService>(
|
|
TestDataBuilder.Mocks.CreateLoggerMock<ModelService>().Object,
|
|
ollamaSettingsMock.Object
|
|
);
|
|
|
|
var aiSettingsMock = TestDataBuilder.Mocks.CreateOptionsMock(new AISettings());
|
|
|
|
_statusCommand = new StatusCommand(
|
|
chatServiceMock.Object,
|
|
modelServiceMock.Object,
|
|
aiSettingsMock.Object,
|
|
_ollamaClientMock.Object
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_WhenOllamaThrowsHttpRequestException_ShouldHandleGracefully()
|
|
{
|
|
// Arrange
|
|
var context = new TelegramCommandContext
|
|
{
|
|
ChatId = 12345,
|
|
Username = "testuser",
|
|
MessageText = "/status",
|
|
ChatType = "private",
|
|
ChatTitle = "Test Chat"
|
|
};
|
|
|
|
_ollamaClientMock
|
|
.Setup(x => x.ListLocalModelsAsync())
|
|
.ThrowsAsync(new HttpRequestException("502 Bad Gateway"));
|
|
|
|
// Act
|
|
var result = await _statusCommand.ExecuteAsync(context);
|
|
|
|
// Assert
|
|
result.Should().NotBeNullOrEmpty();
|
|
result.Should().Contain("Статус системы");
|
|
// StatusCommand handles exceptions internally and returns formatted status
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ExecuteAsync_WhenOllamaThrowsTaskCanceledException_ShouldHandleGracefully()
|
|
{
|
|
// Arrange
|
|
var context = new TelegramCommandContext
|
|
{
|
|
ChatId = 12345,
|
|
Username = "testuser",
|
|
MessageText = "/status",
|
|
ChatType = "private",
|
|
ChatTitle = "Test Chat"
|
|
};
|
|
|
|
_ollamaClientMock
|
|
.Setup(x => x.ListLocalModelsAsync())
|
|
.ThrowsAsync(new TaskCanceledException("Operation timed out"));
|
|
|
|
// Act
|
|
var result = await _statusCommand.ExecuteAsync(context);
|
|
|
|
// Assert
|
|
result.Should().NotBeNullOrEmpty();
|
|
result.Should().Contain("Статус системы");
|
|
// StatusCommand handles timeouts internally and returns formatted status
|
|
}
|
|
}
|