test(application): add CQRS handler tests for Sites, Cameras, Agents, Auth, Events, Settings, Groups, Users, Assignments, Discovery, Persons, Stats, Audit
2026-07-23 14:32:33 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.Events.Queries.GetEvents;
|
2026-08-27 20:28:41 +02:00
|
|
|
using Heimdall.Application.Services;
|
test(application): add CQRS handler tests for Sites, Cameras, Agents, Auth, Events, Settings, Groups, Users, Assignments, Discovery, Persons, Stats, Audit
2026-07-23 14:32:33 +00:00
|
|
|
using Heimdall.Application.Tests.Common;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Events;
|
|
|
|
|
|
|
|
|
|
public class GetEventsHandlerTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_events_should_return_paged_results()
|
|
|
|
|
{
|
|
|
|
|
var site = new Site("Test", "Paris", "Europe/Paris");
|
|
|
|
|
var camera = new Camera(site.Id, "Cam-1");
|
|
|
|
|
var events = new List<CameraEvent>
|
|
|
|
|
{
|
|
|
|
|
new(camera.Id, EventType.MotionDetected, 0.95, DateTime.UtcNow.AddMinutes(-5)),
|
|
|
|
|
new(camera.Id, EventType.PersonDetected, 0.85, DateTime.UtcNow.AddMinutes(-10)),
|
|
|
|
|
};
|
|
|
|
|
foreach (var e in events) e.SetCamera(camera);
|
|
|
|
|
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>(events));
|
2026-08-27 20:28:41 +02:00
|
|
|
var cameraAccess = Substitute.For<ICameraAccessService>();
|
|
|
|
|
cameraAccess.GetUserAccessibleCameraIdsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns([camera.Id]);
|
|
|
|
|
var handler = new GetEventsHandler(context, cameraAccess);
|
test(application): add CQRS handler tests for Sites, Cameras, Agents, Auth, Events, Settings, Groups, Users, Assignments, Discovery, Persons, Stats, Audit
2026-07-23 14:32:33 +00:00
|
|
|
|
|
|
|
|
var result = await handler.Handle(new GetEventsQuery(null, null, null, null, null, null, 1, 50), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldNotBeNull();
|
|
|
|
|
result.Data.Items.Count.ShouldBe(2);
|
|
|
|
|
result.Data.TotalCount.ShouldBe(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_events_should_filter_by_camera_id()
|
|
|
|
|
{
|
|
|
|
|
var site = new Site("Test", "Paris", "Europe/Paris");
|
|
|
|
|
var camera1 = new Camera(site.Id, "Cam-1");
|
|
|
|
|
var camera2 = new Camera(site.Id, "Cam-2");
|
|
|
|
|
var events = new List<CameraEvent>
|
|
|
|
|
{
|
|
|
|
|
new(camera1.Id, EventType.MotionDetected, 0.9, DateTime.UtcNow),
|
|
|
|
|
new(camera2.Id, EventType.PersonDetected, 0.8, DateTime.UtcNow),
|
|
|
|
|
};
|
|
|
|
|
events[0].SetCamera(camera1);
|
|
|
|
|
events[1].SetCamera(camera2);
|
|
|
|
|
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>(events));
|
2026-08-27 20:28:41 +02:00
|
|
|
var cameraAccess = Substitute.For<ICameraAccessService>();
|
|
|
|
|
cameraAccess.GetUserAccessibleCameraIdsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns([camera1.Id, camera2.Id]);
|
|
|
|
|
var handler = new GetEventsHandler(context, cameraAccess);
|
test(application): add CQRS handler tests for Sites, Cameras, Agents, Auth, Events, Settings, Groups, Users, Assignments, Discovery, Persons, Stats, Audit
2026-07-23 14:32:33 +00:00
|
|
|
|
|
|
|
|
var result = await handler.Handle(new GetEventsQuery(camera1.Id, null, null, null, null, null, 1, 50), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data!.Items.Count.ShouldBe(1);
|
|
|
|
|
result.Data.Items[0].CameraId.ShouldBe(camera1.Id);
|
|
|
|
|
}
|
2026-08-10 21:50:09 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_events_should_filter_by_search_label_ignoring_case()
|
|
|
|
|
{
|
|
|
|
|
var site = new Site("Test", "Paris", "Europe/Paris");
|
|
|
|
|
var camera = new Camera(site.Id, "Cam-1");
|
|
|
|
|
var events = new List<CameraEvent>
|
|
|
|
|
{
|
|
|
|
|
new(camera.Id, EventType.PersonDetected, 0.9, DateTime.UtcNow),
|
|
|
|
|
new(camera.Id, EventType.VehicleDetected, 0.7, DateTime.UtcNow),
|
|
|
|
|
};
|
|
|
|
|
events[0].SetCamera(camera);
|
|
|
|
|
events[1].SetCamera(camera);
|
|
|
|
|
events[0].SetMetadata("Jean Dupont", null);
|
|
|
|
|
events[1].SetMetadata("Voiture", null);
|
|
|
|
|
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>(events));
|
2026-08-27 20:28:41 +02:00
|
|
|
var cameraAccess = Substitute.For<ICameraAccessService>();
|
|
|
|
|
cameraAccess.GetUserAccessibleCameraIdsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns([camera.Id]);
|
|
|
|
|
var handler = new GetEventsHandler(context, cameraAccess);
|
2026-08-10 21:50:09 +02:00
|
|
|
|
|
|
|
|
var result = await handler.Handle(new GetEventsQuery(null, null, null, null, null, null, 1, 50, "JEAN"), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data!.Items.Count.ShouldBe(1);
|
|
|
|
|
result.Data.Items[0].Label.ShouldBe("Jean Dupont");
|
|
|
|
|
}
|
2026-10-20 07:32:56 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_events_should_search_camera_name_and_filter_to_snapshots_before_paging()
|
|
|
|
|
{
|
|
|
|
|
var site = new Site("Test", "Paris", "Europe/Paris");
|
|
|
|
|
var camera = new Camera(site.Id, "North Gate");
|
|
|
|
|
var withSnapshot = new CameraEvent(camera.Id, EventType.PersonDetected, 0.9, DateTime.UtcNow);
|
|
|
|
|
withSnapshot.SetSnapshot("snapshot.jpg", "thumb.jpg");
|
|
|
|
|
withSnapshot.SetCamera(camera);
|
|
|
|
|
var withoutSnapshot = new CameraEvent(camera.Id, EventType.MotionDetected, 0.8, DateTime.UtcNow);
|
|
|
|
|
withoutSnapshot.SetCamera(camera);
|
|
|
|
|
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>([withSnapshot, withoutSnapshot]));
|
|
|
|
|
var cameraAccess = Substitute.For<ICameraAccessService>();
|
|
|
|
|
cameraAccess.GetUserAccessibleCameraIdsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns([camera.Id]);
|
|
|
|
|
var handler = new GetEventsHandler(context, cameraAccess);
|
|
|
|
|
|
|
|
|
|
var result = await handler.Handle(new GetEventsQuery(null, null, null, null, null, null,
|
|
|
|
|
Page: 1, PageSize: 50, Search: "NORTH GATE", HasSnapshot: true), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data!.TotalCount.ShouldBe(1);
|
|
|
|
|
result.Data.Items.ShouldHaveSingleItem().SnapshotUrl.ShouldBe("snapshot.jpg");
|
|
|
|
|
}
|
test(application): add CQRS handler tests for Sites, Cameras, Agents, Auth, Events, Settings, Groups, Users, Assignments, Discovery, Persons, Stats, Audit
2026-07-23 14:32:33 +00:00
|
|
|
}
|