Files
Heimdall/tests/Heimdall.Agent.Tests/AgentHealthCheckTests.cs
T

73 lines
2.8 KiB
C#
Raw Normal View History

using Heimdall.Agent.Services;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using NSubstitute;
namespace Heimdall.Agent.Tests;
public class AgentHealthCheckTests
{
[Fact]
public async Task CheckHealth_WhenRegistered_ReturnsHealthy()
{
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
var check = new AgentHealthCheck(state, degradationManager);
var result = await check.CheckHealthAsync(new HealthCheckContext());
result.Status.ShouldBe(HealthStatus.Healthy);
}
[Fact]
public async Task CheckHealth_WhenNotRegistered_ReturnsUnhealthy()
{
var state = new AgentState { AgentId = Guid.Empty };
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
var check = new AgentHealthCheck(state, degradationManager);
var result = await check.CheckHealthAsync(new HealthCheckContext());
result.Status.ShouldBe(HealthStatus.Unhealthy);
}
[Fact]
public async Task CheckHealth_WhenNoApiKey_ReturnsDegraded()
{
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = null };
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
var check = new AgentHealthCheck(state, degradationManager);
var result = await check.CheckHealthAsync(new HealthCheckContext());
result.Status.ShouldBe(HealthStatus.Degraded);
}
[Fact]
public async Task CheckHealth_WhenDegraded_ReturnsDegraded()
{
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
// Escalate to SlowExecutor
for (int i = 0; i < 3; i++)
degradationManager.ReportInferenceSlow(600);
var check = new AgentHealthCheck(state, degradationManager);
var result = await check.CheckHealthAsync(new HealthCheckContext());
result.Status.ShouldBe(HealthStatus.Degraded);
}
[Fact]
public async Task CheckHealth_WhenCpuSaturation_ReturnsUnhealthy()
{
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
degradationManager.ReportCpuSaturation(85.0);
var check = new AgentHealthCheck(state, degradationManager);
var result = await check.CheckHealthAsync(new HealthCheckContext());
result.Status.ShouldBe(HealthStatus.Unhealthy);
}
}