From 9bc5c1941c05e9df799c4e48ce01bd1156861aa4 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Sat, 10 Oct 2026 17:50:22 +0200 Subject: [PATCH] feat(ui): add PersonDetailPage (/persons/{id}) - Header with photo, name, category, watchlist badge, notes - Face carousel (MudCarousel) with thumbnails - Associated vehicles table - 30-day FaceRecognized event history (client-filtered by name) - Edit/Delete actions with PersonFormDialog - 10 bUnit tests --- .../Components/Pages/PersonDetailPage.razor | 386 ++++++++++++++++++ .../Components/Pages/PersonDetailPageTests.cs | 267 ++++++++++++ 2 files changed, 653 insertions(+) create mode 100644 src/Heimdall.Web/Components/Pages/PersonDetailPage.razor create mode 100644 tests/Heimdall.Web.Tests/Components/Pages/PersonDetailPageTests.cs diff --git a/src/Heimdall.Web/Components/Pages/PersonDetailPage.razor b/src/Heimdall.Web/Components/Pages/PersonDetailPage.razor new file mode 100644 index 0000000..590ab0d --- /dev/null +++ b/src/Heimdall.Web/Components/Pages/PersonDetailPage.razor @@ -0,0 +1,386 @@ +@page "/persons/{Id:guid}" +@attribute [Authorize] +@layout MainLayout +@inject HeimdallApiClient Api +@inject ISnackbar Snackbar +@inject NavigationManager Nav +@using Heimdall.Web.Components.Shared +@using Heimdall.Web.Services + +@if (_loading) +{ + +} +else if (_person is null) +{ + Person not found +} +else +{ +
+
+ @if (!string.IsNullOrEmpty(_person.PhotoUrl)) + { + @_person.DisplayName + } + else + { +
+ +
+ } +
+ @_person.DisplayName + + @FormatCategory(_person.Category) · Created @_person.CreatedAt.ToString("d") + + @if (_person.IsWatchlisted) + { + + Watchlisted + + } +
+
+
+ + Edit + + + Delete + +
+
+ + @if (!string.IsNullOrEmpty(_person.Notes)) + { + + Notes + @_person.Notes + + } + + + @* ─── Faces ─── *@ + + +
+ + + Registered faces (@_faces.Count) + +
+ + @if (_facesLoading) + { + + } + else if (_faces.Count == 0) + { + + No face registered. Add a face to improve recognition. + + } + else + { + + @foreach (var face in _faces) + { + +
+ @if (!string.IsNullOrEmpty(face.ThumbnailUrl)) + { + Face + } + else + { +
+ +
+ } + + @if (!string.IsNullOrEmpty(face.SourceEventId)) + { + Event: @face.SourceEventId.Substring(0, Math.Min(8, face.SourceEventId.Length))… + } + else + { + Face #@face.Id.ToString()[..8] + } + +
+
+ } +
+
+ @for (int i = 0; i < _faces.Count; i++) + { + var idx = i; + + @(i + 1) + + } +
+ } +
+
+ + @* ─── Vehicles ─── *@ + + + + + Associated vehicles (@_vehicles.Count) + + + @if (_vehiclesLoading) + { + + } + else if (_vehicles.Count == 0) + { + + No vehicle associated with this person. + + } + else + { + + + Plate + Make + Model + Color + Events + + + @context.LicensePlate + @context.Make + @context.Model + @context.Color + @context.EventCount + + + } + + +
+ + @* ─── Event history (30 days) ─── *@ + + + + Detection history (last 30 days) + + + @if (_eventsLoading) + { + + } + else if (_events.Count == 0) + { + + No face detection for the last 30 days. + + } + else + { + + + Date + Type + Camera + Confidence + Snapshot + + + @context.OccurredAt.ToLocalTime().ToString("dd/MM HH:mm") + + + Face + + + @context.Label + @((context.Confidence * 100).ToString("0"))% + + @if (!string.IsNullOrEmpty(context.SnapshotUrl)) + { + + } + + + + } + + + + ← Back to persons + +} + +@* ─── Edit form ─── *@ + + +@* ─── Confirm delete ─── *@ + + + Confirm deletion + + Delete the person "@_person?.DisplayName" ? + + This action is irreversible. The @(_person?.FaceCount) registered face(s) will also be deleted. + + + + Cancel + Delete + + + +@* ─── Snapshot dialog ─── *@ + + + Snapshot — @_showSnapshotEvent?.Label + + + Snapshot + + + Close + + + +@code { + [Parameter] public Guid Id { get; set; } + + private PersonClientItem? _person; + private List _faces = []; + private List _vehicles = []; + private List _events = []; + + private bool _loading = true; + private bool _facesLoading = true; + private bool _vehiclesLoading = true; + private bool _eventsLoading = true; + + private bool _showForm; + private bool _confirmDeleteShow; + private bool _showSnapshot; + private int _faceIndex; + private EventItem? _showSnapshotEvent; + + private readonly DialogOptions _dialogOptions = new() + { + FullWidth = true, + MaxWidth = MaxWidth.Large, + CloseButton = true + }; + + private DateTime _eventsSince => DateTime.UtcNow.AddDays(-30); + + protected override async Task OnInitializedAsync() + { + try + { + _person = await Api.GetPersonAsync(Id); + } + catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); } + finally { _loading = false; } + + await LoadFaces(); + await LoadVehicles(); + await LoadEvents(); + } + + private async Task LoadFaces() + { + _facesLoading = true; + try + { + _faces = (await Api.GetFacesAsync(Id)) ?? []; + } + catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); } + finally { _facesLoading = false; } + } + + private async Task LoadVehicles() + { + _vehiclesLoading = true; + try + { + var all = await Api.GetVehiclesAsync(); + _vehicles = (all?.Where(v => v.PersonId == Id).ToList()) ?? []; + } + catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); } + finally { _vehiclesLoading = false; } + } + + private async Task LoadEvents() + { + _eventsLoading = true; + try + { + var result = await Api.GetEventsAsync(null, null, page: 1, pageSize: 50, + type: "FaceRecognized", from: _eventsSince); + var all = result?.Items ?? []; + var name = _person?.DisplayName; + _events = string.IsNullOrEmpty(name) + ? all + : all.Where(e => e.Label?.Contains(name, StringComparison.OrdinalIgnoreCase) == true).ToList(); + } + catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); } + finally { _eventsLoading = false; } + } + + private async Task OnPersonUpdated(PersonClientItem person) + { + _person = person; + await LoadFaces(); + } + + private void ConfirmDelete() => _confirmDeleteShow = true; + + private async Task ExecuteDelete() + { + _confirmDeleteShow = false; + try + { + if (await Api.DeletePersonAsync(Id)) + { + Snackbar.Add($"Person \"{_person?.DisplayName}\" deleted", Severity.Success); + Nav.NavigateTo("/persons"); + } + else + { + Snackbar.Add("Failed to delete person", Severity.Error); + } + } + catch { Snackbar.Add(UiErrors.OperationFailed(), Severity.Error); } + } + + private void ShowSnapshot(EventItem ev) + { + _showSnapshotEvent = ev; + _showSnapshot = true; + } + + private static string FormatCategory(string? category) => category switch + { + "family" => "Family", + "employee" => "Employee", + "watchlist" => "Watchlist", + "unknown" => "Unknown", + _ => "—" + }; +} \ No newline at end of file diff --git a/tests/Heimdall.Web.Tests/Components/Pages/PersonDetailPageTests.cs b/tests/Heimdall.Web.Tests/Components/Pages/PersonDetailPageTests.cs new file mode 100644 index 0000000..75c8ef6 --- /dev/null +++ b/tests/Heimdall.Web.Tests/Components/Pages/PersonDetailPageTests.cs @@ -0,0 +1,267 @@ +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, "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()); + Handler.OnGet("api/vehicles", new List()); + Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50)); + Handler.OnGet("api/persons/", Person()); + LoginAs(); + } + + [Fact] + public void PersonDetail_AfficheInformationsEtStatut() + { + HandlerBase(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + { + cut.Markup.ShouldContain("Alice Dupont"); + cut.Markup.ShouldContain("Family"); + cut.Markup.ShouldContain("Created"); + cut.Markup.ShouldContain("Watchlisted"); + cut.Markup.ShouldContain("Member of the family"); + }); + } + + [Fact] + public void PersonDetail_NotesMasqueesQuandAbsent() + { + Handler.OnGet($"api/persons/{PersonId}/faces", new List()); + Handler.OnGet("api/vehicles", new List()); + Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50)); + var noNotes = Person(); + noNotes.Notes = null; + Handler.OnGet("api/persons/", noNotes); + LoginAs(); + + var cut = RenderPage(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); + Handler.OnGet($"api/persons/{PersonId}/faces", new List()); + Handler.OnGet("api/vehicles", new List()); + Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50)); + LoginAs(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Person not found")); + } + + [Fact] + public void PersonDetail_AfficheVisagesCarousel() + { + Handler.OnGet($"api/persons/{PersonId}/faces", new List { Face() }); + Handler.OnGet("api/vehicles", new List()); + Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50)); + Handler.OnGet("api/persons/", Person()); + LoginAs(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + { + cut.Markup.ShouldContain("https://example.com/face1.jpg"); + cut.Markup.ShouldContain("Registered faces"); + cut.Markup.ShouldContain("evt-1234"); + }); + } + + [Fact] + public void PersonDetail_AucunVisage_AfficheMessage() + { + HandlerBase(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + cut.Markup.ShouldContain("No face registered")); + } + + [Fact] + public void PersonDetail_AfficheVehiculesAssocies() + { + Handler.OnGet($"api/persons/{PersonId}/faces", new List()); + Handler.OnGet("api/vehicles", new List { Vehicle() }); + Handler.OnGet("api/events", new EventPageResult([], 0, 1, 50)); + Handler.OnGet("api/persons/", Person()); + LoginAs(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + { + cut.Markup.ShouldContain("Associated vehicles"); + cut.Markup.ShouldContain("AB-123-CD"); + cut.Markup.ShouldContain("Toyota"); + cut.Markup.ShouldContain("Prius"); + cut.Markup.ShouldContain("Black"); + }); + } + + [Fact] + public void PersonDetail_AucunVehicule_AfficheMessage() + { + HandlerBase(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + cut.Markup.ShouldContain("No vehicle associated")); + } + + [Fact] + public void PersonDetail_AfficheHistoriqueEvenements() + { + Handler.OnGet($"api/persons/{PersonId}/faces", new List()); + Handler.OnGet("api/vehicles", new List()); + Handler.OnGet("api/events", new EventPageResult([Ev()], 1, 1, 50)); + Handler.OnGet("api/persons/", Person()); + LoginAs(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + { + cut.Markup.ShouldContain("Detection history"); + // The table displays the label (person name) + cut.Markup.ShouldContain("Alice Dupont"); + cut.Markup.ShouldContain("92%"); + // The table includes an IconButton with aria-label="View snapshot" if snapshot exists + cut.Markup.ShouldContain("aria-label=\"View snapshot\""); + }); + } + + [Fact] + public void PersonDetail_HistoriqueFiltreParNom() + { + Handler.OnGet($"api/persons/{PersonId}/faces", new List()); + Handler.OnGet("api/vehicles", new List()); + Handler.OnGet("api/events", + new EventPageResult([Ev("Alice Dupont"), Ev("Bob Martin")], 2, 1, 50)); + Handler.OnGet("api/persons/", Person()); + LoginAs(); + + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => + { + cut.Markup.ShouldContain("Alice Dupont"); + cut.Markup.ShouldNotContain("Bob Martin"); + }); + } + + [Fact] + public async Task PersonDetail_SupprimerPersonne_ConfirmeEtSupprime() + { + HandlerBase(); + Handler.On(HttpMethod.Delete, "api/persons/", System.Net.HttpStatusCode.OK); + + var nav = Services.GetRequiredService(); + var cut = RenderPage(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Alice Dupont")); + + cut.FindAll("button").Single(b => b.TextContent.Contains("Delete")).Click(); + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Confirm deletion")); + + // 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() == "Delete").Click()); + + cut.WaitForAssertion(() => + { + var snackbar = Services.GetRequiredService(); + snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "") + .ShouldContain(m => m.Contains("deleted", 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(p => p.Add(x => x.Id, PersonId)); + + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Alice Dupont")); + + cut.FindAll("button").Single(b => b.TextContent.Contains("Edit")).Click(); + cut.WaitForAssertion(() => cut.Markup.ShouldContain("Edit Person")); + + cut.FindAll("input")[0].Input("Alice Renommee"); + await cut.InvokeAsync(() => cut.FindAll("div.mud-dialog-actions button") + .Single(b => b.TextContent.Trim() == "Save").Click()); + + cut.WaitForAssertion(() => + { + Handler.Requests.Count(r => r.Method == HttpMethod.Put + && r.RequestUri!.PathAndQuery.Contains("api/persons")).ShouldBe(1); + var snackbar = Services.GetRequiredService(); + snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "") + .ShouldContain(m => m.Contains("updated", StringComparison.OrdinalIgnoreCase)); + }); + } +} \ No newline at end of file