Files
Heimdall/tests/Heimdall.Agent.Tests/AnalysisJobPollingServiceTests.cs
arnaud.dartois cae09e3698
Heimdall Gitea / Detect CI scope (push) Successful in 4s
Heimdall Gitea / Build and tests (push) Successful in 22m59s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 6m5s
Heimdall Gitea / Build and scan agent image (push) Successful in 9m5s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m34s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 16m16s
Heimdall Gitea / CI policy (push) Successful in 2s
fix(agent): cap analysis results at Central limit (#279)
Refs #270

Caps successful analysis result payloads to Central’s 500-detection limit after type filtering, preserving input/model order for ObjectDetection, PersonDetection, and VehicleClassification. Adds >500 boundary/order tests for all three types and updates the analysis pipeline skill.

Validation: `dotnet test tests/Heimdall.Agent.Tests/Heimdall.Agent.Tests.csproj --no-restore --filter FullyQualifiedName~AnalysisJobPollingServiceTests` passed 16/16; `git diff --check` passed.

Compose acceptance remains incomplete. Source-built disposable stacks were started with unique projects, generated secrets/ports/volumes and unique Central image tags. Two runs did not reach healthy Postgres; a later run reached healthy Central but the isolated bootstrap-admin returned HTTP 400 because the generated test password did not meet Identity complexity. Therefore no 501 rejection / 500 persistence assertions were made. For each project, Compose teardown completed; label queries confirmed no project containers, volumes, or networks; each exact local image tag was inspected absent. One incomplete trap's two known JSON files were individually removed with `unlink` and its empty directory with `rmdir`. A later trap had harmless unlink attempts for not-yet-created paths and reported a cleanup error; independent checks confirmed its temp directory, project resources, and exact image tag were absent.

Do not treat the Compose acceptance as passed. The server controller and readback shape were inspected in source only. Post-deployment GlitchTip validation also remains open.
Reviewed-on: #279
Co-authored-by: Arnaud Dartois <admin@tordais.cc>
2026-11-07 13:42:01 +01:00

148 lines
5.6 KiB
C#

using System.Text.Json;
using Heimdall.Agent.Services;
using Heimdall.Domain.Enums;
using Microsoft.Extensions.Logging.Abstractions;
namespace Heimdall.Agent.Tests;
public sealed class AnalysisJobPollingServiceTests
{
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);
}
[Fact]
public void SelectDetections_ObjectDetection_EnforcesCentralLimitInStableOrder()
{
var detections = Enumerable.Range(0, AnalysisJobPollingService.MaxDetectionsPerResult + 1)
.Select(i => new DetectionResult($"object-{i}", 0.9f, 0, 0, 0.1f, 0.1f, i))
.ToArray();
var result = AnalysisJobPollingService.SelectDetections(AnalysisJobType.ObjectDetection, detections);
result.Count.ShouldBe(AnalysisJobPollingService.MaxDetectionsPerResult);
result[0].Label.ShouldBe("object-0");
result[^1].Label.ShouldBe("object-499");
}
[Fact]
public void SelectDetections_PersonDetection_EnforcesCentralLimitAfterFilteringInStableOrder()
{
var detections = Enumerable.Range(0, AnalysisJobPollingService.MaxDetectionsPerResult + 1)
.Select(i => new DetectionResult($"person-{i}", 0.9f, 0, 0, 0.1f, 0.1f, 0))
.SelectMany((d, i) => new[]
{
new DetectionResult($"other-{i}", 0.8f, 0, 0, 0.1f, 0.1f, 1), d,
})
.ToArray();
var result = AnalysisJobPollingService.SelectDetections(AnalysisJobType.PersonDetection, detections);
result.Count.ShouldBe(AnalysisJobPollingService.MaxDetectionsPerResult);
result[0].Label.ShouldBe("person-0");
result[^1].Label.ShouldBe("person-499");
}
[Fact]
public void SelectDetections_VehicleClassification_EnforcesCentralLimitAfterFilteringInStableOrder()
{
var detections = Enumerable.Range(0, AnalysisJobPollingService.MaxDetectionsPerResult + 1)
.Select(i => new DetectionResult($"car-{i}", 0.9f, 0, 0, 0.1f, 0.1f, 2))
.SelectMany((d, i) => new[]
{
new DetectionResult($"other-{i}", 0.8f, 0, 0, 0.1f, 0.1f, 1), d,
})
.ToArray();
var result = AnalysisJobPollingService.SelectDetections(AnalysisJobType.VehicleClassification, detections);
result.Count.ShouldBe(AnalysisJobPollingService.MaxDetectionsPerResult);
result[0].Label.ShouldBe("car-0");
result[^1].Label.ShouldBe("car-499");
}
[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);
}
private sealed class StubHttpMessageHandler(
Func<HttpRequestMessage, HttpResponseMessage> responseFactory) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
=> Task.FromResult(responseFactory(request));
}
}