96 lines
2.9 KiB
C#
96 lines
2.9 KiB
C#
using ChatBot.Models.Configuration;
|
|
using ChatBot.Services.HealthChecks;
|
|
using ChatBot.Services.Interfaces;
|
|
using ChatBot.Tests.TestUtilities;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
|
|
namespace ChatBot.Tests.Services.HealthChecks;
|
|
|
|
public class OllamaHealthCheckTests : UnitTestBase
|
|
{
|
|
private readonly Mock<ILogger<OllamaHealthCheck>> _loggerMock;
|
|
private readonly Mock<IOllamaClient> _ollamaClientMock;
|
|
private readonly OllamaHealthCheck _healthCheck;
|
|
|
|
public OllamaHealthCheckTests()
|
|
{
|
|
_loggerMock = TestDataBuilder.Mocks.CreateLoggerMock<OllamaHealthCheck>();
|
|
_ollamaClientMock = TestDataBuilder.Mocks.CreateOllamaClientMock();
|
|
|
|
_healthCheck = new OllamaHealthCheck(_ollamaClientMock.Object, _loggerMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CheckHealthAsync_ShouldReturnHealthy_WhenOllamaResponds()
|
|
{
|
|
// Arrange
|
|
_ollamaClientMock
|
|
.Setup(x => x.ListLocalModelsAsync())
|
|
.ReturnsAsync(
|
|
new List<OllamaSharp.Models.Model>
|
|
{
|
|
new OllamaSharp.Models.Model { Name = "llama3.2" },
|
|
}
|
|
);
|
|
|
|
// Act
|
|
var context = new HealthCheckContext();
|
|
var result = await _healthCheck.CheckHealthAsync(context);
|
|
|
|
// Assert
|
|
result.Status.Should().Be(HealthStatus.Healthy);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenOllamaThrowsException()
|
|
{
|
|
// Arrange
|
|
_ollamaClientMock
|
|
.Setup(x => x.ListLocalModelsAsync())
|
|
.ThrowsAsync(new Exception("Ollama unavailable"));
|
|
|
|
// Act
|
|
var context = new HealthCheckContext();
|
|
var result = await _healthCheck.CheckHealthAsync(context);
|
|
|
|
// Assert
|
|
result.Status.Should().Be(HealthStatus.Unhealthy);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenOllamaReturnsEmptyResponse()
|
|
{
|
|
// Arrange
|
|
_ollamaClientMock
|
|
.Setup(x => x.ListLocalModelsAsync())
|
|
.ReturnsAsync(new List<OllamaSharp.Models.Model>());
|
|
|
|
// Act
|
|
var context = new HealthCheckContext();
|
|
var result = await _healthCheck.CheckHealthAsync(context);
|
|
|
|
// Assert
|
|
result.Status.Should().Be(HealthStatus.Unhealthy);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CheckHealthAsync_ShouldReturnUnhealthy_WhenOllamaReturnsNullMessage()
|
|
{
|
|
// Arrange
|
|
_ollamaClientMock
|
|
.Setup(x => x.ListLocalModelsAsync())
|
|
.ReturnsAsync((IEnumerable<OllamaSharp.Models.Model>)null!);
|
|
|
|
// Act
|
|
var context = new HealthCheckContext();
|
|
var result = await _healthCheck.CheckHealthAsync(context);
|
|
|
|
// Assert
|
|
result.Status.Should().Be(HealthStatus.Unhealthy);
|
|
}
|
|
}
|