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.GetRecentByCamera;
|
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 GetRecentByCameraHandlerTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_recent_by_camera_should_return_events_ordered_by_occurred_at_desc()
|
|
|
|
|
{
|
|
|
|
|
var site = new Site("Test", "Paris", "Europe/Paris");
|
|
|
|
|
var camera = new Camera(site.Id, "Cam-1");
|
|
|
|
|
var cameraId = camera.Id;
|
|
|
|
|
var events = new List<CameraEvent>
|
|
|
|
|
{
|
|
|
|
|
new(cameraId, EventType.MotionDetected, 0.9, DateTime.UtcNow.AddHours(-3)),
|
|
|
|
|
new(cameraId, EventType.PersonDetected, 0.8, DateTime.UtcNow.AddHours(-2)),
|
|
|
|
|
new(cameraId, EventType.VehicleDetected, 0.7, DateTime.UtcNow.AddHours(-1)),
|
|
|
|
|
};
|
|
|
|
|
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.HasCameraAccessAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(true);
|
|
|
|
|
var handler = new GetRecentByCameraHandler(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 GetRecentByCameraQuery(cameraId, 10), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldNotBeNull();
|
|
|
|
|
result.Data.Count.ShouldBe(3);
|
|
|
|
|
result.Data[0].OccurredAt.ShouldBe(events[2].OccurredAt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Get_recent_by_camera_with_no_events_should_return_empty()
|
|
|
|
|
{
|
|
|
|
|
var cameraId = Guid.NewGuid();
|
|
|
|
|
var context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
context.CameraEvents.Returns(MockDbSetHelper.CreateFor<CameraEvent>());
|
2026-08-27 20:28:41 +02:00
|
|
|
var cameraAccess = Substitute.For<ICameraAccessService>();
|
|
|
|
|
cameraAccess.HasCameraAccessAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
|
|
|
|
.Returns(true);
|
|
|
|
|
var handler = new GetRecentByCameraHandler(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 GetRecentByCameraQuery(cameraId, 10), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldBeEmpty();
|
|
|
|
|
}
|
|
|
|
|
}
|