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]
|
2026-10-25 21:49:46 +01:00
|
|
|
public async Task CheckHealth_WhenCpuSaturation_ReturnsUnhealthyWithCaptureAndSaturationDiagnostics()
|
2026-08-26 23:28:26 +02:00
|
|
|
{
|
|
|
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
|
|
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
degradationManager.ReportCpuSaturation(85.0);
|
2026-10-25 21:49:46 +01:00
|
|
|
var cameraStore = new CameraConfigStore();
|
|
|
|
|
var cameraId = Guid.NewGuid();
|
|
|
|
|
cameraStore.SetLocalFallback(
|
|
|
|
|
[new AgentCameraConfig(cameraId, "Test camera", "rtsp://user:secret@example.test/stream", null, true)]);
|
|
|
|
|
var check = new AgentHealthCheck(state, degradationManager, cameraStore: cameraStore);
|
2026-08-26 23:28:26 +02:00
|
|
|
|
|
|
|
|
var result = await check.CheckHealthAsync(new HealthCheckContext());
|
|
|
|
|
|
|
|
|
|
result.Status.ShouldBe(HealthStatus.Unhealthy);
|
2026-10-25 21:49:46 +01:00
|
|
|
result.Data["degradationLevel"].ShouldBe((int)DegradationLevel.CpuSaturation);
|
|
|
|
|
result.Data["degradationLevelName"].ShouldBe(nameof(DegradationLevel.CpuSaturation));
|
|
|
|
|
result.Data["cpuPercent"].ShouldBe(85.0);
|
|
|
|
|
result.Data["rejectedFrames"].ShouldBe(0L);
|
|
|
|
|
result.Data["assignedCameraCount"].ShouldBe(1);
|
|
|
|
|
result.Data["pipelineStale"].ShouldBe(true);
|
|
|
|
|
((string[])result.Data["staleCameraIds"]).ShouldContain(cameraId.ToString("D"));
|
|
|
|
|
System.Text.Json.JsonSerializer.Serialize(result.Data).ShouldNotContain("secret");
|
2026-08-26 23:28:26 +02:00
|
|
|
}
|
2026-10-25 20:33:40 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CheckHealth_WhenDetectionModelIsUnavailable_ReturnsUnhealthy()
|
|
|
|
|
{
|
|
|
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
|
|
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
using var analyzer = new FrameAnalyzerService(
|
|
|
|
|
Substitute.For<ILogger<FrameAnalyzerService>>(), 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);
|
2026-10-25 20:39:21 +01:00
|
|
|
result.Description!.ShouldContain("Detection model unavailable");
|
2026-10-25 20:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CheckHealth_WhenAssignedCameraHasNoActiveStream_ReturnsCaptureDiagnostics()
|
|
|
|
|
{
|
|
|
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
|
|
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
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"));
|
|
|
|
|
}
|
2026-10-25 22:14:33 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CheckHealth_ReportsSnapshotAndInferenceBacklog()
|
|
|
|
|
{
|
|
|
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
|
|
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
using var enteredInference = new ManualResetEventSlim();
|
|
|
|
|
using var releaseInference = new ManualResetEventSlim();
|
|
|
|
|
using var analyzer = new FrameAnalyzerService(
|
|
|
|
|
Substitute.For<ILogger<FrameAnalyzerService>>(), null, degradationManager,
|
|
|
|
|
(_, _) =>
|
|
|
|
|
{
|
|
|
|
|
enteredInference.Set();
|
|
|
|
|
releaseInference.Wait(TimeSpan.FromSeconds(10)).ShouldBeTrue();
|
|
|
|
|
return [];
|
|
|
|
|
});
|
|
|
|
|
using var streams = new CameraStreamService(Substitute.For<ILogger<CameraStreamService>>());
|
|
|
|
|
|
|
|
|
|
var activeInference = analyzer.AnalyzeFrameAsync([1]);
|
|
|
|
|
enteredInference.Wait(TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
|
|
|
|
var queuedInference = analyzer.AnalyzeFrameAsync([1]);
|
|
|
|
|
streams.SnapshotChannel.Writer.TryWrite(new Snapshot(Guid.NewGuid(), DateTime.UtcNow, [1, 2, 3])).ShouldBeTrue();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var check = new AgentHealthCheck(state, degradationManager, analyzer, streams: streams);
|
|
|
|
|
var result = await check.CheckHealthAsync(new HealthCheckContext());
|
|
|
|
|
|
|
|
|
|
result.Data["inferenceQueueDepth"].ShouldBe(1);
|
|
|
|
|
result.Data["snapshotQueueDepth"].ShouldBe(1);
|
|
|
|
|
result.Data["droppedSnapshots"].ShouldBe(0L);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
releaseInference.Set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(await Task.WhenAll(activeInference, queuedInference).WaitAsync(TimeSpan.FromSeconds(10)))
|
|
|
|
|
.ShouldAllBe(result => result != null);
|
|
|
|
|
}
|
2026-10-25 22:18:49 +01:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task CheckHealth_ReportsDiskUsageWhenAvailable()
|
|
|
|
|
{
|
|
|
|
|
var state = new AgentState { AgentId = Guid.NewGuid(), ApiKey = "valid-key" };
|
|
|
|
|
var degradationManager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
|
|
|
|
|
var metrics = new SystemMetricsProvider();
|
|
|
|
|
var check = new AgentHealthCheck(state, degradationManager, metrics: metrics);
|
|
|
|
|
|
|
|
|
|
var result = await check.CheckHealthAsync(new HealthCheckContext());
|
|
|
|
|
|
|
|
|
|
if (metrics.GetDiskUsagePercent() is double diskUsagePercent)
|
|
|
|
|
{
|
|
|
|
|
result.Data["diskUsagePercent"].ShouldBe(diskUsagePercent);
|
|
|
|
|
((double)result.Data["diskUsagePercent"]).ShouldBeInRange(0, 100);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.Data.ShouldNotContainKey("diskUsagePercent");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-18 14:16:13 +00:00
|
|
|
}
|