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.Commands.CreateEvent;
|
|
|
|
|
using Heimdall.Application.Events.Notifications;
|
|
|
|
|
using Heimdall.Application.Tests.Common;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Events;
|
|
|
|
|
|
|
|
|
|
public class CreateEventHandlerTests
|
|
|
|
|
{
|
|
|
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
|
private readonly IPublisher _publisher;
|
|
|
|
|
private readonly CreateEventHandler _handler;
|
|
|
|
|
|
|
|
|
|
public CreateEventHandlerTests()
|
|
|
|
|
{
|
|
|
|
|
_context = Substitute.For<IApplicationDbContext>();
|
|
|
|
|
_publisher = Substitute.For<IPublisher>();
|
|
|
|
|
_handler = new CreateEventHandler(_context, _publisher);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_event_should_return_success_and_publish_notification()
|
|
|
|
|
{
|
|
|
|
|
var site = new Site("Test", "Paris", "Europe/Paris");
|
|
|
|
|
var camera = new Camera(site.Id, "Cam-1");
|
|
|
|
|
var cameraId = camera.Id;
|
|
|
|
|
var occuredAt = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
_context.Cameras.Returns(MockDbSetHelper.CreateFor<Camera>([camera]));
|
2026-11-05 21:40:00 +01:00
|
|
|
_context.Recordings.Returns(MockDbSetHelper.CreateFor<Recording>());
|
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 cameraEventsDb = new CameraAwareDbSet([], id => id == cameraId ? camera : null);
|
|
|
|
|
_context.CameraEvents.Returns(cameraEventsDb);
|
|
|
|
|
_context.SaveChangesWithRetryAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
|
|
|
|
|
|
|
|
|
|
var command = new CreateEventCommand(cameraId, EventType.MotionDetected, 0.95, null, null, occuredAt);
|
|
|
|
|
|
|
|
|
|
var result = await _handler.Handle(command, CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
result.Data.ShouldNotBeNull();
|
|
|
|
|
var data = result.Data!;
|
|
|
|
|
data.CameraId.ShouldBe(cameraId);
|
|
|
|
|
data.Type.ShouldBe(EventType.MotionDetected);
|
|
|
|
|
|
|
|
|
|
var eventId = result.Data!.Id;
|
|
|
|
|
#pragma warning disable CS8602
|
|
|
|
|
await _publisher.Received(1).Publish(Arg.Is<EventCreatedNotification>(n => n.Event.Id == eventId), Arg.Any<CancellationToken>());
|
|
|
|
|
#pragma warning restore CS8602
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Create_event_with_unknown_camera_should_return_not_found()
|
|
|
|
|
{
|
|
|
|
|
var unknownId = Guid.NewGuid();
|
|
|
|
|
_context.Cameras.FindAsync([unknownId], Arg.Any<CancellationToken>()).Returns((Camera?)null);
|
|
|
|
|
|
|
|
|
|
var result = await _handler.Handle(
|
|
|
|
|
new CreateEventCommand(unknownId, EventType.MotionDetected, 0.9, null, null, null),
|
|
|
|
|
CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
|
|
|
result.StatusCode.ShouldBe(404);
|
|
|
|
|
}
|
|
|
|
|
}
|