Files
Heimdall/tests/Heimdall.Agent.Tests/DetectionReporterServiceTests.cs
T
Arnaud Dartois bce39a1034 feat(agent): remontée des détections agent → central (Fixes #57)
- DetectionReporterService : POST /api/events (API Key auth), anti-spam par
  cooldown (caméra, type) — motion 30 s, autres 10 s — échec réseau drop +
  compteur (jamais bloquant), mapping YOLO/COCO → EventType
- SnapshotPipelineService : événement MotionDetected sur la transition
  idle → active (front montant), événements typés PersonDetected/
  VehicleDetected pour chaque détection de l'analyseur (avec bbox en metadata)
- Tests : 11 (mapping COCO, agent non enregistré → pas d'appel, pas de throw)
2026-08-12 09:18:42 +02:00

56 lines
1.9 KiB
C#

using Heimdall.Agent.Services;
using Heimdall.Domain.Enums;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Agent.Tests;
public class DetectionReporterServiceTests
{
[Theory]
[InlineData("person", EventType.PersonDetected)]
[InlineData("car", EventType.VehicleDetected)]
[InlineData("truck", EventType.VehicleDetected)]
[InlineData("bus", EventType.VehicleDetected)]
[InlineData("motorcycle", EventType.VehicleDetected)]
[InlineData("cat", null)]
[InlineData("dog", null)]
[InlineData("bottle", EventType.MotionDetected)]
[InlineData("Person", EventType.PersonDetected)]
public void MapDetectionToEventType_MapsKnownLabels(string label, EventType? expected)
{
DetectionReporterService.MapDetectionToEventType(label).ShouldBe(expected);
}
[Fact]
public async Task ReportAsync_UnregisteredAgent_ReturnsFalse()
{
var state = new AgentState { AgentId = Guid.Empty, ApiKey = "" };
var reporter = new DetectionReporterService(
state,
Substitute.For<IHttpClientFactory>(),
Substitute.For<ILogger<DetectionReporterService>>());
var sent = await reporter.ReportAsync(
Guid.NewGuid(), EventType.MotionDetected, 0.9, "motion");
sent.ShouldBeFalse();
reporter.FailedReports.ShouldBe(0);
}
[Fact]
public void ReportAsync_Unregistered_DoesNotThrow()
{
var state = new AgentState { AgentId = Guid.Empty, ApiKey = "" };
var reporter = new DetectionReporterService(
state,
Substitute.For<IHttpClientFactory>(),
Substitute.For<ILogger<DetectionReporterService>>());
// Pas d'appel réseau possible sans agent → ne doit pas crasher.
var task = reporter.ReportAsync(Guid.NewGuid(), EventType.PersonDetected, 0.8, "person");
task.IsCompleted.ShouldBeTrue();
}
}