Files
arnaud.dartois 636154572b
Heimdall Gitea / Detect CI scope (push) Successful in 6s
Heimdall Gitea / Build and tests (push) Successful in 19m19s
Heimdall Gitea / Publish versioned release (push) Skipped
Heimdall Gitea / Build and scan server image (push) Successful in 6m13s
Heimdall Gitea / Build and scan agent image (push) Successful in 14m44s
Heimdall Gitea / Publish Docker Hub latest tags (push) Successful in 1m48s
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Successful in 19m9s
Heimdall Gitea / CI policy (push) Successful in 10s
Complete remaining Heimdall features and acceptance
Merge pull request #138

Co-authored-by: Arnaud Dartois <admin@tordais.cc>
2026-11-06 11:14:49 +01:00

228 lines
7.9 KiB
C#

using Heimdall.Application.DTOs;
using Heimdall.Domain.Entities;
using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Data.Interceptors;
using Heimdall.Infrastructure.Services;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
namespace Heimdall.Infrastructure.Tests;
public class PersonServiceTests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly IDbContextFactory<AppDbContext> _dbFactory;
private readonly ILogger<PersonService> _logger = Substitute.For<ILogger<PersonService>>();
public PersonServiceTests()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_dbFactory = CreateFactory(_connection);
}
public void Dispose()
{
_connection?.Close();
_connection?.Dispose();
GC.SuppressFinalize(this);
}
private static TestDbContextFactory CreateFactory(SqliteConnection connection)
{
var opts = new DbContextOptionsBuilder<AppDbContext>()
.UseSqlite(connection)
.Options;
using var ctx = new AppDbContext(opts, new AuditInterceptor());
ctx.Database.EnsureCreated();
return new TestDbContextFactory(opts);
}
[Fact]
public async Task CreateAsync_ValidRequest_ReturnsPersonResponse()
{
var service = new PersonService(_dbFactory, _logger);
var result = await service.CreateAsync(
new CreatePersonRequest("John Doe", "family", null), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldNotBeNull();
result.Data.DisplayName.ShouldBe("John Doe");
result.Data.Category.ShouldBe("family");
}
[Fact]
public async Task CreateAsync_SavesToDatabase()
{
var service = new PersonService(_dbFactory, _logger);
await service.CreateAsync(
new CreatePersonRequest("Jane Doe", "employee", null, IsWatchlisted: true), CancellationToken.None);
await service.CreateAsync(
new CreatePersonRequest("Bob Smith", "visitor", null), CancellationToken.None);
var getAll = await service.GetAllAsync(null, CancellationToken.None);
getAll.Data!.Count.ShouldBe(2);
}
[Fact]
public async Task GetByIdAsync_ExistingPerson_ReturnsPerson()
{
var service = new PersonService(_dbFactory, _logger);
var created = await service.CreateAsync(
new CreatePersonRequest("Alice", "family", null), CancellationToken.None);
var result = await service.GetByIdAsync(created.Data!.Id, CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data!.DisplayName.ShouldBe("Alice");
}
[Fact]
public async Task GetByIdAsync_UnknownId_ReturnsNotFound()
{
var service = new PersonService(_dbFactory, _logger);
var result = await service.GetByIdAsync(Guid.NewGuid(), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task GetAllAsync_ReturnsAllPersons()
{
var service = new PersonService(_dbFactory, _logger);
await service.CreateAsync(new("A", null, null), CancellationToken.None);
await service.CreateAsync(new("B", null, null), CancellationToken.None);
await service.CreateAsync(new("C", null, null), CancellationToken.None);
var result = await service.GetAllAsync(null, CancellationToken.None);
result.Data!.Count.ShouldBe(3);
}
[Fact]
public async Task GetAllAsync_FilterByCategory_ReturnsFiltered()
{
var service = new PersonService(_dbFactory, _logger);
await service.CreateAsync(new("Alice", "family", null), CancellationToken.None);
await service.CreateAsync(new("Bob", "employee", null), CancellationToken.None);
await service.CreateAsync(new("Charlie", "family", null), CancellationToken.None);
var result = await service.GetAllAsync("family", CancellationToken.None);
result.Data!.Count.ShouldBe(2);
result.Data.All(p => p.Category == "family").ShouldBeTrue();
}
[Fact]
public async Task GetAllAsync_ReturnsOrderedByDisplayName()
{
var service = new PersonService(_dbFactory, _logger);
await service.CreateAsync(new("Charlie", null, null), CancellationToken.None);
await service.CreateAsync(new("Alice", null, null), CancellationToken.None);
await service.CreateAsync(new("Bob", null, null), CancellationToken.None);
var result = await service.GetAllAsync(null, CancellationToken.None);
result.Data![0].DisplayName.ShouldBe("Alice");
result.Data[1].DisplayName.ShouldBe("Bob");
result.Data[2].DisplayName.ShouldBe("Charlie");
}
[Fact]
public async Task RegisterFaceAsync_RejectsOversizedEmbedding()
{
var service = new PersonService(_dbFactory, _logger);
var result = await service.RegisterFaceAsync(
new RegisterFaceRequest(Guid.NewGuid(), new float[KnownFace.MaxEmbeddingDimensions + 1]),
CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(400);
}
[Fact]
public async Task RegisterFaceAsync_RejectsNonFiniteEmbedding()
{
var service = new PersonService(_dbFactory, _logger);
var result = await service.RegisterFaceAsync(
new RegisterFaceRequest(Guid.NewGuid(), [0.1f, float.NaN]), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(400);
}
[Fact]
public async Task UpdateAsync_ExistingPerson_UpdatesFields()
{
var service = new PersonService(_dbFactory, _logger);
var created = await service.CreateAsync(
new CreatePersonRequest("Old Name", "family", "old notes"), CancellationToken.None);
var updated = await service.UpdateAsync(created.Data!.Id,
new UpdatePersonRequest("New Name", "new notes", "employee", true), CancellationToken.None);
updated.IsSuccess.ShouldBeTrue();
updated.Data!.DisplayName.ShouldBe("New Name");
var fetched = await service.GetByIdAsync(created.Data.Id, CancellationToken.None);
fetched.Data!.DisplayName.ShouldBe("New Name");
}
[Fact]
public async Task UpdateAsync_UnknownId_ReturnsNotFound()
{
var service = new PersonService(_dbFactory, _logger);
var result = await service.UpdateAsync(Guid.NewGuid(),
new UpdatePersonRequest("Name", null, null, false), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
[Fact]
public async Task DeleteAsync_ExistingPerson_RemovesPerson()
{
var service = new PersonService(_dbFactory, _logger);
var created = await service.CreateAsync(
new CreatePersonRequest("Delete Me", 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();
}
[Fact]
public async Task DeleteAsync_UnknownId_ReturnsNotFound()
{
var service = new PersonService(_dbFactory, _logger);
var result = await service.DeleteAsync(Guid.NewGuid(), CancellationToken.None);
result.IsSuccess.ShouldBeFalse();
result.StatusCode.ShouldBe(404);
}
private sealed class TestDbContextFactory(DbContextOptions<AppDbContext> opts)
: IDbContextFactory<AppDbContext>
{
private static readonly AuditInterceptor Audit = new();
public AppDbContext CreateDbContext() => new(opts, Audit);
public Task<AppDbContext> CreateDbContextAsync(CancellationToken ct = default)
=> Task.FromResult(new AppDbContext(opts, Audit));
}
}