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>()); 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>()); 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>()); 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>()); // 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>()); degradationManager.ReportCpuSaturation(85.0); var check = new AgentHealthCheck(state, degradationManager); var result = await check.CheckHealthAsync(new HealthCheckContext()); result.Status.ShouldBe(HealthStatus.Unhealthy); } }