Files
Heimdall/tests/Heimdall.Infrastructure.Tests/CameraEventServiceTests.cs
T

192 lines
7.5 KiB
C#
Raw Normal View History

using Heimdall.Application.DTOs;
using Heimdall.Application.Services;
using Heimdall.Domain.Entities;
using Heimdall.Domain.Enums;
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class CameraEventServiceTests
{
private readonly IDbContextFactory<AppDbContext> _dbFactory;
private readonly ILogger<CameraEventService> _logger = Substitute.For<ILogger<CameraEventService>>();
public CameraEventServiceTests()
{
_dbFactory = CreateFactory();
}
private static TestDbContextFactory CreateFactory()
{
var opts = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase($"EventTest-{Guid.NewGuid()}")
.Options;
return new TestDbContextFactory(opts);
}
private async Task<(Guid siteId, Guid cameraId)> SeedCameraAsync()
{
await using var db = await _dbFactory.CreateDbContextAsync();
var site = new Site("Event Test Site", "test", "UTC");
db.Sites.Add(site);
await db.SaveChangesAsync();
var camera = new Camera(site.Id, "Event-Cam", "rtsp://192.168.1.100:554/stream1");
camera.SetDiscoveryInfo(CameraDiscoveryMethod.Manual, DetectedCameraType.Standard, null, "192.168.1.100");
db.Cameras.Add(camera);
await db.SaveChangesAsync();
return (site.Id, camera.Id);
}
[Fact]
public async Task CreateAsync_ValidRequest_ReturnsEvent()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
var result = await service.CreateAsync(new CreateCameraEventRequest(
cameraId, EventType.MotionDetected, 0.95, "motion-zone-1", """{"box":[100,200,300,400]}""", null),
CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.Type.ShouldBe(EventType.MotionDetected);
result.Data.Confidence.ShouldBe(0.95);
}
[Fact]
public async Task CreateAsync_UnknownCamera_ReturnsNotFound()
{
var service = new CameraEventService(_dbFactory, _logger);
var result = await service.CreateAsync(new CreateCameraEventRequest(
Guid.NewGuid(), EventType.MotionDetected, 0.9, null, null, null),
CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task GetByIdAsync_ExistingId_ReturnsEvent()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
var created = await service.CreateAsync(new CreateCameraEventRequest(
cameraId, EventType.PersonDetected, 0.88, "person-1", null, null),
CancellationToken.None);
var fetched = await service.GetByIdAsync(created.Data!.Id, CancellationToken.None);
fetched.IsSuccess.ShouldBeTrue();
fetched.Data!.Type.ShouldBe(EventType.PersonDetected);
}
[Fact]
public async Task GetByIdAsync_UnknownId_ReturnsNotFound()
{
var service = new CameraEventService(_dbFactory, _logger);
var result = await service.GetByIdAsync(Guid.NewGuid(), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task QueryAsync_FilterByType_ReturnsFiltered()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
await service.CreateAsync(new(cameraId, EventType.MotionDetected, 0.9, null, null, null), CancellationToken.None);
await service.CreateAsync(new(cameraId, EventType.PersonDetected, 0.85, null, null, null), CancellationToken.None);
await service.CreateAsync(new(cameraId, EventType.VehicleDetected, 0.7, null, null, null), CancellationToken.None);
var personEvents = await service.QueryAsync(new CameraEventFilterRequest(
null, null, EventType.PersonDetected, null, null, null, 1, 50),
CancellationToken.None);
personEvents.IsSuccess.ShouldBeTrue();
personEvents.Data!.Items.Count.ShouldBe(1);
personEvents.Data.TotalCount.ShouldBe(1);
}
[Fact]
public async Task QueryAsync_FilterByMinConfidence_ReturnsFiltered()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
await service.CreateAsync(new(cameraId, EventType.MotionDetected, 0.95, null, null, null), CancellationToken.None);
await service.CreateAsync(new(cameraId, EventType.MotionDetected, 0.50, null, null, null), CancellationToken.None);
var highConfEvents = await service.QueryAsync(new CameraEventFilterRequest(
null, null, null, null, null, 0.9, 1, 50),
CancellationToken.None);
highConfEvents.IsSuccess.ShouldBeTrue();
highConfEvents.Data!.Items.Count.ShouldBe(1);
}
[Fact]
public async Task DeleteAsync_ExistingEvent_RemovesIt()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
var created = await service.CreateAsync(new(cameraId, EventType.MotionDetected, 0.9, null, null, null), CancellationToken.None);
var deleteResult = await service.DeleteAsync(created.Data!.Id, CancellationToken.None);
deleteResult.IsSuccess.ShouldBeTrue();
var fetched = await service.GetByIdAsync(created.Data.Id, CancellationToken.None);
fetched.IsSuccess.ShouldBeFalse();
fetched.StatusCode.ShouldBe(404);
}
[Fact]
public async Task AttachSnapshotAsync_ExistingEvent_UpdatesSnapshot()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
var created = await service.CreateAsync(new(cameraId, EventType.MotionDetected, 0.9, null, null, null), CancellationToken.None);
var attachResult = await service.AttachSnapshotAsync(
created.Data!.Id,
"https://s3.example.com/snapshots/cam1/evt1.jpg",
"https://s3.example.com/snapshots/cam1/evt1_thumb.jpg",
CancellationToken.None);
attachResult.IsSuccess.ShouldBeTrue();
var fetched = await service.GetByIdAsync(created.Data.Id, CancellationToken.None);
fetched.Data!.SnapshotUrl.ShouldBe("https://s3.example.com/snapshots/cam1/evt1.jpg");
fetched.Data.SnapshotThumbUrl.ShouldBe("https://s3.example.com/snapshots/cam1/evt1_thumb.jpg");
}
[Fact]
public async Task GetRecentByCameraAsync_ReturnsLatest()
{
var service = new CameraEventService(_dbFactory, _logger);
var (_, cameraId) = await SeedCameraAsync();
for (int i = 0; i < 5; i++)
await service.CreateAsync(new(cameraId, EventType.MotionDetected, 0.9, null, null, null), CancellationToken.None);
var recent = await service.GetRecentByCameraAsync(cameraId, 3, CancellationToken.None);
recent.IsSuccess.ShouldBeTrue();
recent.Data!.Count.ShouldBe(3);
}
private sealed class TestDbContextFactory(DbContextOptions<AppDbContext> opts)
: IDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext() => new(opts);
public Task<AppDbContext> CreateDbContextAsync(CancellationToken ct = default)
=> Task.FromResult(new AppDbContext(opts));
}
}