55 lines
1.9 KiB
C#
55 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();
|
||
|
|
}
|
||
|
|
}
|