Files
Heimdall/tests/Heimdall.Agent.Tests/AnalysisJobPollingServiceTests.cs
T

97 lines
3.5 KiB
C#
Raw Normal View History

using System.Text.Json;
using Heimdall.Agent.Services;
using Heimdall.Domain.Enums;
2026-11-06 07:34:38 +01:00
using Microsoft.Extensions.Logging.Abstractions;
namespace Heimdall.Agent.Tests;
public sealed class AnalysisJobPollingServiceTests
{
2026-11-06 07:34:38 +01:00
private static readonly JsonSerializerOptions WebJson = new(JsonSerializerDefaults.Web);
private static readonly DetectionResult[] CocoDetections =
[
new("person", 0.91f, 0.1f, 0.2f, 0.2f, 0.5f, 0),
new("bicycle", 0.82f, 0.2f, 0.2f, 0.3f, 0.4f, 1),
new("car", 0.88f, 0.4f, 0.3f, 0.4f, 0.3f, 2),
new("bus", 0.77f, 0.5f, 0.2f, 0.3f, 0.4f, 5),
];
[Fact]
public void SelectDetections_PersonDetection_ReturnsOnlyPersonClass()
{
var result = AnalysisJobPollingService.SelectDetections(AnalysisJobType.PersonDetection, CocoDetections);
result.Select(d => d.Label).ShouldBe(["person"]);
}
[Fact]
public void SelectDetections_VehicleClassification_ReturnsCocoMotorVehicles()
{
var result = AnalysisJobPollingService.SelectDetections(AnalysisJobType.VehicleClassification, CocoDetections);
result.Select(d => d.Label).ShouldBe(["car", "bus"]);
}
[Fact]
public void SelectDetections_ObjectDetection_PreservesAllModelClasses()
{
var result = AnalysisJobPollingService.SelectDetections(AnalysisJobType.ObjectDetection, CocoDetections);
result.ShouldBe(CocoDetections);
}
2026-11-06 07:34:38 +01:00
[Fact]
public void DetectionResult_UsesCentralJsonContract()
{
var detection = new DetectionResult("person", 0.91f, 0.1f, 0.2f, 0.2f, 0.5f, 0);
var json = JsonSerializer.Serialize(detection, WebJson);
using var document = JsonDocument.Parse(json);
document.RootElement.GetProperty("classIndex").GetInt32().ShouldBe(0);
document.RootElement.TryGetProperty("ClassIndex", out _).ShouldBeFalse();
}
[Fact]
public async Task SendResultAsync_DoesNotRetryPermanentClientRejection()
{
var requests = 0;
using var client = new HttpClient(new StubHttpMessageHandler(_ =>
{
requests++;
return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
})) { BaseAddress = new Uri("http://localhost/") };
var service = new AnalysisJobPollingService(null!, null!, null!, null!, null!,
NullLogger<AnalysisJobPollingService>.Instance);
await service.SendResultAsync(client, Guid.NewGuid(), Guid.NewGuid(),
new AnalysisJobPollingService.AnalysisResult(Guid.NewGuid(), true, null, []),
CancellationToken.None);
requests.ShouldBe(1);
}
[Theory]
[InlineData("\"PersonDetection\"", true)]
[InlineData("\"vehicLeClassification\"", true)]
[InlineData("\"MotionDetection\"", true)]
[InlineData("3", true)]
[InlineData("5", true)]
[InlineData("0", true)]
[InlineData("\"FaceRecognition\"", false)]
[InlineData("\"not-a-job\"", false)]
public void SupportedJobTypes_AcceptsOnlyImplementedContracts(string json, bool expected)
{
using var document = JsonDocument.Parse(json);
var actual = AnalysisJobPollingService.TryGetSupportedJobType(document.RootElement, out _);
actual.ShouldBe(expected);
}
2026-11-06 07:34:38 +01:00
private sealed class StubHttpMessageHandler(
Func<HttpRequestMessage, HttpResponseMessage> responseFactory) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
=> Task.FromResult(responseFactory(request));
}
}