Files
Heimdall/tests/Heimdall.Web.Tests/Components/Pages/PersonDetailPageTests.cs
T

269 lines
9.5 KiB
C#
Raw Normal View History

using Bunit;
using Bunit.TestDoubles;
using Heimdall.Web.Components.Pages;
using Heimdall.Web.Services;
using Microsoft.Extensions.DependencyInjection;
using MudBlazor;
using Shouldly;
namespace Heimdall.Web.Tests.Components.Pages;
public class PersonDetailPageTests : PageTestBase
{
private static readonly Guid PersonId = Guid.NewGuid();
private static readonly Guid CameraId = Guid.NewGuid();
private static PersonClientItem Person() => new()
{
Id = PersonId,
DisplayName = "Alice Dupont",
Category = "family",
IsWatchlisted = true,
Notes = "Member of the family",
FaceCount = 2,
CreatedAt = DateTime.UtcNow.AddMonths(-1)
};
private static KnownFaceClientItem Face() => new()
{
Id = Guid.NewGuid(),
PersonId = PersonId,
ThumbnailUrl = "https://example.com/face1.jpg",
SourceEventId = "evt-12345678"
};
private static VehicleClientItem Vehicle() => new()
{
Id = Guid.NewGuid(),
LicensePlate = "AB-123-CD",
Make = "Toyota",
Model = "Prius",
Color = "Black",
PersonId = PersonId,
EventCount = 5
};
private static EventItem Ev(string label = "Alice Dupont") =>
new(Guid.NewGuid(), CameraId, PersonId, "FaceRecognized", 0.92, label,
"https://example.com/snap.jpg", null,
DateTime.UtcNow.AddHours(-2), DateTime.UtcNow.AddHours(-2));
private void HandlerBase()
{
Handler.OnGet($"api/persons/{PersonId}/faces", new List<KnownFaceClientItem>());
Handler.OnGet("api/vehicles", new List<VehicleClientItem>());
Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50));
Handler.OnGet("api/persons/", Person());
LoginAs();
}
[Fact]
public void PersonDetail_AfficheInformationsEtStatut()
{
HandlerBase();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Alice Dupont");
cut.Markup.ShouldContain("Famille");
cut.Markup.ShouldContain("Créée le");
cut.Markup.ShouldContain("Surveillée");
cut.Markup.ShouldContain("Member of the family");
});
}
[Fact]
public void PersonDetail_NotesMasqueesQuandAbsent()
{
Handler.OnGet($"api/persons/{PersonId}/faces", new List<KnownFaceClientItem>());
Handler.OnGet("api/vehicles", new List<VehicleClientItem>());
Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50));
var noNotes = Person();
noNotes.Notes = null;
Handler.OnGet("api/persons/", noNotes);
LoginAs();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Alice Dupont");
cut.Markup.ShouldNotContain("Member of the family");
});
}
[Fact]
public void PersonDetail_PersonneIntrouvable_AfficheAlerte()
{
Handler.OnGet("api/persons/", System.Net.HttpStatusCode.NotFound);
LoginAs();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Personne introuvable");
Services.GetRequiredService<ISnackbar>().ShownSnackbars.ShouldBeEmpty();
Handler.Requests.ShouldHaveSingleItem();
});
}
[Fact]
public void PersonDetail_AfficheVisagesCarousel()
{
Handler.OnGet($"api/persons/{PersonId}/faces", new List<KnownFaceClientItem> { Face() });
Handler.OnGet("api/vehicles", new List<VehicleClientItem>());
Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50));
Handler.OnGet("api/persons/", Person());
LoginAs();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("https://example.com/face1.jpg");
cut.Markup.ShouldContain("visage(s) enregistré(s)");
cut.Markup.ShouldContain("evt-1234");
});
}
[Fact]
public void PersonDetail_AucunVisage_AfficheMessage()
{
HandlerBase();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Aucun visage enregistré"));
}
[Fact]
public void PersonDetail_AfficheVehiculesAssocies()
{
Handler.OnGet($"api/persons/{PersonId}/faces", new List<KnownFaceClientItem>());
Handler.OnGet("api/vehicles", new List<VehicleClientItem> { Vehicle() });
Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50));
Handler.OnGet("api/persons/", Person());
LoginAs();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
2026-10-25 06:19:40 +01:00
cut.Markup.ShouldContain("véhicule(s) associé(s)");
cut.Markup.ShouldContain("<code>AB-123-CD</code>");
cut.Markup.ShouldContain("Toyota");
cut.Markup.ShouldContain("Prius");
cut.Markup.ShouldContain("Black");
});
}
[Fact]
public void PersonDetail_AucunVehicule_AfficheMessage()
{
HandlerBase();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
cut.Markup.ShouldContain("Aucun véhicule associé à cette personne."));
}
[Fact]
public void PersonDetail_AfficheHistoriqueEvenements()
{
Handler.OnGet($"api/persons/{PersonId}/faces", new List<KnownFaceClientItem>());
Handler.OnGet("api/vehicles", new List<VehicleClientItem>());
Handler.OnGet("api/events", new EventPageResult([Ev()], 1, 1, 50));
Handler.OnGet("api/persons/", Person());
LoginAs();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Historique de détection");
// The table displays the label (person name)
cut.Markup.ShouldContain("Alice Dupont");
2026-10-25 06:35:00 +01:00
cut.Markup.ShouldContain("92");
2026-10-25 06:41:02 +01:00
// The table includes a localized snapshot button when a snapshot exists.
cut.Markup.ShouldContain("aria-label=\"Voir la capture\"");
});
}
[Fact]
public void PersonDetail_HistoriqueFiltreParPersonId()
{
Handler.OnGet($"api/persons/{PersonId}/faces", new List<KnownFaceClientItem>());
Handler.OnGet("api/vehicles", new List<VehicleClientItem>());
Handler.OnGet("api/events", new EventPageResult([Ev("Alice Dupont")], 1, 1, 50));
Handler.OnGet("api/persons/", Person());
LoginAs();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("Alice Dupont");
cut.Markup.ShouldNotContain("Bob Martin");
Handler.Requests.ShouldContain(r => r.RequestUri!.PathAndQuery.Contains($"personId={PersonId}", StringComparison.OrdinalIgnoreCase));
});
}
[Fact]
public async Task PersonDetail_SupprimerPersonne_ConfirmeEtSupprime()
{
HandlerBase();
Handler.On(HttpMethod.Delete, "api/persons/", System.Net.HttpStatusCode.OK);
var nav = Services.GetRequiredService<BunitNavigationManager>();
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Alice Dupont"));
cut.FindAll("button").Single(b => b.TextContent.Contains("Supprimer")).Click();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Confirmer la suppression"));
// The confirm dialog's Delete button is inside the dialog actions.
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button")
.Single(b => b.TextContent.Trim() == "Supprimer").Click());
cut.WaitForAssertion(() =>
{
var snackbar = Services.GetRequiredService<ISnackbar>();
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
.ShouldContain(m => m.Contains("supprimée", StringComparison.OrdinalIgnoreCase));
nav.Uri.ShouldBe("http://localhost/persons");
});
}
[Fact]
public async Task PersonDetail_ModifierPersonne_AppellePut()
{
HandlerBase();
Handler.On(HttpMethod.Put, "api/persons/", System.Net.HttpStatusCode.OK);
var cut = RenderPage<PersonDetailPage>(p => p.Add(x => x.Id, PersonId));
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Alice Dupont"));
cut.FindAll("button").Single(b => b.TextContent.Contains("Modifier")).Click();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Modifier la personne"));
cut.FindAll("input")[0].Input("Alice Renommee");
await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button")
.Single(b => b.TextContent.Trim() == "Enregistrer").Click());
cut.WaitForAssertion(() =>
{
Handler.Requests.Count(r => r.Method == HttpMethod.Put
&& r.RequestUri!.PathAndQuery.Contains("api/persons")).ShouldBe(1);
var snackbar = Services.GetRequiredService<ISnackbar>();
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
.ShouldContain(m => m.Contains("mise à jour", StringComparison.OrdinalIgnoreCase));
});
}
}