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 { [$"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()); 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(() => store.ReplaceCentral(new Dictionary { [$"camera.{id}.profile"] = JsonSerializer.Serialize(new DetectionProfileDto { Version = 2 }) })); store.Cameras.Single().RtspUrl.ShouldBe("rtsp://old"); } [Fact] public void UnsupportedFeatures_AndInvalidThreshold_AreRejected() { Should.Throw(() => new DetectionProfileDto { MinConfidence = float.NaN }.Validate()); Should.Throw(() => new DetectionProfileDto { DetectPackage = true }.Validate()); Should.Throw(() => new DetectionProfileDto { ZoneX = .8, ZoneWidth = .5 }.Validate()); } }