Files
Heimdall/tests/Heimdall.Agent.Tests/AgentHealthCheckTests.cs
T
Arnaud Dartois 435f8c4a05
Heimdall Gitea / Detect CI scope (pull_request) Successful in 7s
Heimdall Gitea / Build and tests (pull_request) Failing after 5m5s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 2s
test(health): assert model failure description
2026-10-25 20:39:21 +01:00

110 lines
4.6 KiB
C#

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);
}
[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);
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<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"));
}
}