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); } [Fact] public async Task CheckHealth_WhenDetectionModelIsUnavailable_ReturnsUnhealthy() { var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" }; var degradationManager = new PipelineDegradationManager(Substitute.For>()); using var analyzer = new FrameAnalyzerService( Substitute.For>(), null, degradationManager, null, Path.Combine(Path.GetTempPath(), $"missing-model-{Guid.NewGuid():N}.onnx")); var check = new AgentHealthCheck(state, degradationManager, analyzer); var result = await check.CheckHealthAsync(new HealthCheckContext()); result.Status.ShouldBe(HealthStatus.Unhealthy); result.Description!.ShouldContain("Detection model unavailable"); } [Fact] public async Task CheckHealth_WhenAssignedCameraHasNoActiveStream_ReturnsCaptureDiagnostics() { var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" }; var degradationManager = new PipelineDegradationManager(Substitute.For>()); var cameraStore = new CameraConfigStore(); var cameraId = Guid.NewGuid(); cameraStore.SetLocalFallback( [new AgentCameraConfig(cameraId, "Test camera", "rtsp://camera/stream", null, true)]); var check = new AgentHealthCheck(state, degradationManager, cameraStore: cameraStore); var result = await check.CheckHealthAsync(new HealthCheckContext()); result.Status.ShouldBe(HealthStatus.Degraded); result.Data["assignedCameraCount"].ShouldBe(1); result.Data["activeStreamCount"].ShouldBe(0); result.Data["pipelineStale"].ShouldBe(true); ((string[])result.Data["staleCameraIds"]).ShouldContain(cameraId.ToString("D")); } }