Files
Heimdall/tests/Heimdall.Agent.Tests/DetectionProfileTests.cs

54 lines
2.2 KiB
C#

using System.Text.Json;
using Heimdall.Agent.Services;
using Heimdall.Application.DTOs;
namespace Heimdall.Agent.Tests;
public class DetectionProfileTests
{
[Fact]
public void FullResync_RemovesOldCamera_AndAppliesProfileInOneNotification()
{
var store = new CameraConfigStore();
var oldId = Guid.NewGuid();
var newId = Guid.NewGuid();
store.ApplyCameraSetting($"camera.{oldId}.rtsp", "rtsp://old");
var changes = 0;
store.Changed += (_, _) => changes++;
store.ReplaceCentral(new Dictionary<string, string>
{
[$"camera.{newId}.rtsp"] = "rtsp://new",
[$"camera.{newId}.enabled"] = "false",
[$"camera.{newId}.profile"] = JsonSerializer.Serialize(new DetectionProfileDto { MinConfidence = .8f, DetectVehicle = false })
});
changes.ShouldBe(1);
var camera = store.Cameras.Single();
camera.CameraId.ShouldBe(newId);
camera.Enabled.ShouldBeFalse();
camera.DetectionProfile!.MinConfidence.ShouldBe(.8f);
camera.DetectionProfile.AllowsClass(2).ShouldBeFalse();
camera.DetectionProfile.AllowsClass(0).ShouldBeTrue();
store.ReplaceCentral(new Dictionary<string, string>());
store.Cameras.ShouldBeEmpty();
}
[Fact]
public void InvalidSnapshot_KeepsPreviousConfiguration()
{
var store = new CameraConfigStore();
var id = Guid.NewGuid();
store.ApplyCameraSetting($"camera.{id}.rtsp", "rtsp://old");
Should.Throw<ArgumentException>(() => store.ReplaceCentral(new Dictionary<string, string>
{ [$"camera.{id}.profile"] = JsonSerializer.Serialize(new DetectionProfileDto { Version = 2 }) }));
store.Cameras.Single().RtspUrl.ShouldBe("rtsp://old");
}
[Fact]
public void UnsupportedFeatures_AndInvalidThreshold_AreRejected()
{
Should.Throw<ArgumentException>(() => new DetectionProfileDto { MinConfidence = float.NaN }.Validate());
Should.Throw<ArgumentException>(() => new DetectionProfileDto { DetectPackage = true }.Validate());
Should.Throw<ArgumentException>(() => new DetectionProfileDto { ZoneX = .8, ZoneWidth = .5 }.Validate());
}
}