2026-07-18 14:16:13 +00:00
|
|
|
using Heimdall.Agent.Services;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2026-08-26 23:28:26 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
2026-07-18 14:16:13 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
|
|
|
|
|
|
public class AgentHealthCheckTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CheckHealth_WhenRegistered_ReturnsHealthy()
|
|
|
|
|
{
|
|
|
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
|
2026-08-26 23:28:26 +02:00
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
var check = new AgentHealthCheck(state, degradationManager);
|
2026-07-18 14:16:13 +00:00
|
|
|
|
|
|
|
|
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 };
|
2026-08-26 23:28:26 +02:00
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
var check = new AgentHealthCheck(state, degradationManager);
|
2026-07-18 14:16:13 +00:00
|
|
|
|
|
|
|
|
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 };
|
2026-08-26 23:28:26 +02:00
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
var check = new AgentHealthCheck(state, degradationManager);
|
2026-07-18 14:16:13 +00:00
|
|
|
|
|
|
|
|
var result = await check.CheckHealthAsync(new HealthCheckContext());
|
|
|
|
|
|
|
|
|
|
result.Status.ShouldBe(HealthStatus.Degraded);
|
|
|
|
|
}
|
2026-08-26 23:28:26 +02:00
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
}
|
2026-07-18 14:16:13 +00:00
|
|
|
}
|