2026-08-10 13:10:37 +02:00
|
|
|
using Bunit;
|
|
|
|
|
using Heimdall.Web.Components.Pages;
|
|
|
|
|
using Heimdall.Web.Services;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MudBlazor;
|
|
|
|
|
using Shouldly;
|
2026-10-20 21:00:38 +02:00
|
|
|
using System.Net;
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Components.Pages;
|
|
|
|
|
|
|
|
|
|
public class SnapshotsPageTests : PageTestBase
|
|
|
|
|
{
|
|
|
|
|
private static readonly Guid CameraId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
private static EventItem SnapshotEvent(
|
|
|
|
|
string type = "PersonDetected",
|
|
|
|
|
double confidence = 0.87,
|
|
|
|
|
string? label = "Personne",
|
2026-10-20 21:00:38 +02:00
|
|
|
string? snapshotUrl = "/snapshots/1.jpg",
|
|
|
|
|
string? thumbUrl = null) =>
|
2026-10-22 20:56:49 +02:00
|
|
|
new(Guid.NewGuid(), CameraId, null, type, confidence, label, snapshotUrl, thumbUrl,
|
2026-08-10 13:10:37 +02:00
|
|
|
DateTime.UtcNow.AddMinutes(-5), DateTime.UtcNow);
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SnapshotsPage_AfficheSnapshots()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/events", new EventPageResult(
|
|
|
|
|
[SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg"),
|
2026-10-20 21:00:38 +02:00
|
|
|
SnapshotEvent("VehicleDetected", 0.66, "Voiture", "/snapshots/2.jpg")],
|
|
|
|
|
2, 1, 48));
|
2026-08-10 13:10:37 +02:00
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.Markup.ShouldContain("2 capture(s) trouvée(s)");
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.Markup.ShouldContain("Personne");
|
|
|
|
|
cut.Markup.ShouldContain("/snapshots/1.jpg");
|
|
|
|
|
cut.Markup.ShouldContain("/snapshots/2.jpg");
|
|
|
|
|
});
|
2026-10-20 21:00:38 +02:00
|
|
|
var request = Handler.Requests.Single().RequestUri!;
|
|
|
|
|
request.Query.ShouldContain("hasSnapshot=true");
|
|
|
|
|
request.Query.ShouldContain("page=1");
|
|
|
|
|
request.Query.ShouldContain("pageSize=48");
|
2026-08-10 13:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SnapshotsPage_AucunSnapshot_AfficheMessage()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/events", new EventPageResult(
|
2026-10-20 21:00:38 +02:00
|
|
|
[], 0, 1, 48));
|
2026-08-10 13:10:37 +02:00
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
|
|
|
|
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucune capture trouvée."));
|
2026-08-10 13:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2026-08-11 16:56:19 +02:00
|
|
|
public async Task SnapshotsPage_Recherche_FiltreLesSnapshots()
|
2026-08-10 13:10:37 +02:00
|
|
|
{
|
2026-10-20 21:00:38 +02:00
|
|
|
var events = new[]
|
|
|
|
|
{
|
|
|
|
|
SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg"),
|
|
|
|
|
SnapshotEvent("VehicleDetected", 0.66, "Voiture", "/snapshots/2.jpg")
|
|
|
|
|
};
|
|
|
|
|
Handler.On(HttpMethod.Get, "api/events", () =>
|
|
|
|
|
{
|
|
|
|
|
var query = Handler.Requests.Last().RequestUri!.Query;
|
|
|
|
|
var filtered = query.Contains("q=Personne", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
? events.Where(e => e.Label == "Personne").ToList()
|
|
|
|
|
: events.ToList();
|
|
|
|
|
return RoutedHandler.JsonResponse(HttpStatusCode.OK,
|
|
|
|
|
new EventPageResult(filtered, filtered.Count, 1, 48));
|
|
|
|
|
});
|
2026-08-10 13:10:37 +02:00
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("2 capture(s) trouvée(s)"));
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
// Champ recherche sans Immediate → Change
|
|
|
|
|
cut.FindAll("input")[0].Change("Personne");
|
2026-08-11 16:56:19 +02:00
|
|
|
// Find+Click dans InvokeAsync (handler périmé sinon, flaky CI).
|
|
|
|
|
await cut.InvokeAsync(() => cut.FindAll("button").Single(b => b.TextContent.Contains("Charger")).Click());
|
2026-08-10 13:10:37 +02:00
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.Markup.ShouldContain("1 capture(s) trouvée(s)");
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.Markup.ShouldContain("Personne");
|
|
|
|
|
cut.Markup.ShouldNotContain("Voiture");
|
|
|
|
|
});
|
2026-10-20 21:00:38 +02:00
|
|
|
Handler.Requests.Last().RequestUri!.Query.ShouldContain("q=Personne");
|
|
|
|
|
Handler.Requests.Last().RequestUri!.Query.ShouldContain("hasSnapshot=true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task SnapshotsPage_PagineEtUtiliseImageOriginaleSiMiniatureAbsente()
|
|
|
|
|
{
|
|
|
|
|
var firstPageEvent = SnapshotEvent("PersonDetected", 0.87, "Première page", "/snapshots/original.jpg");
|
|
|
|
|
var secondPageEvent = SnapshotEvent("VehicleDetected", 0.66, "Deuxième page", "/snapshots/second.jpg");
|
|
|
|
|
Handler.On(HttpMethod.Get, "api/events", () =>
|
|
|
|
|
{
|
|
|
|
|
var query = Handler.Requests.Last().RequestUri!.Query;
|
|
|
|
|
var page = query.Contains("page=2", StringComparison.Ordinal) ? 2 : 1;
|
|
|
|
|
var item = page == 1 ? firstPageEvent : secondPageEvent;
|
|
|
|
|
return RoutedHandler.JsonResponse(HttpStatusCode.OK,
|
|
|
|
|
new EventPageResult([item], 49, page, 48));
|
|
|
|
|
});
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Page 1 / 2 · 49 captures"));
|
|
|
|
|
cut.Find("img[alt='Capture d’événement']").GetAttribute("src").ShouldBe("/snapshots/original.jpg");
|
2026-10-20 21:00:38 +02:00
|
|
|
|
|
|
|
|
await cut.InvokeAsync(() => cut.FindAll("button").Single(b => b.TextContent.Contains("Suivant")).Click());
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.Markup.ShouldContain("Page 2 / 2 · 49 captures");
|
|
|
|
|
cut.Find("img[alt='Capture d’événement']").GetAttribute("src").ShouldBe("/snapshots/second.jpg");
|
2026-10-20 21:00:38 +02:00
|
|
|
});
|
|
|
|
|
Handler.Requests.Last().RequestUri!.Query.ShouldContain("page=2");
|
|
|
|
|
Handler.Requests.Last().RequestUri!.Query.ShouldContain("pageSize=48");
|
|
|
|
|
Handler.Requests.Last().RequestUri!.Query.ShouldContain("hasSnapshot=true");
|
2026-08-10 13:10:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SnapshotsPage_OuvrirLightbox_AfficheDialog()
|
|
|
|
|
{
|
2026-08-10 13:17:27 +02:00
|
|
|
// Le clic sur une carte ouvre la lightbox (MudDialog avec le snapshot).
|
2026-08-10 13:10:37 +02:00
|
|
|
Handler.OnGet("api/events", new EventPageResult(
|
|
|
|
|
[SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg")], 1, 1, 100));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Personne");
|
|
|
|
|
cut.Markup.ShouldContain("87");
|
|
|
|
|
});
|
2026-08-10 13:17:27 +02:00
|
|
|
|
|
|
|
|
// La carte est un div cliquable (role=button)
|
|
|
|
|
var cards = cut.FindAll("div[role=button]");
|
|
|
|
|
cards.Count.ShouldBeGreaterThan(0);
|
|
|
|
|
cards[0].Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Voir la caméra"));
|
2026-08-10 13:10:37 +02:00
|
|
|
}
|
2026-08-15 02:45:39 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SnapshotsPage_FermerLightbox_LeDialogSeFerme()
|
|
|
|
|
{
|
|
|
|
|
// Régression issue #94 : le MudDialog était enveloppé dans un @if →
|
|
|
|
|
// Visible=false détruisait le composant au lieu de le cacher, le dialog
|
|
|
|
|
// restait orphelin et le bouton Fermer ne répondait plus.
|
|
|
|
|
Handler.OnGet("api/events", new EventPageResult(
|
|
|
|
|
[SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg")], 1, 1, 100));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Personne");
|
|
|
|
|
cut.Markup.ShouldContain("87");
|
|
|
|
|
});
|
2026-08-15 02:45:39 +02:00
|
|
|
|
|
|
|
|
// Ouvrir la lightbox.
|
|
|
|
|
cut.FindAll("div[role=button]")[0].Click();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Voir la caméra"));
|
|
|
|
|
|
|
|
|
|
// Cliquer Fermer → le dialog disparaît du markup (le composant reste
|
|
|
|
|
// rendu mais Visible=false, le MudDialogProvider ne l'affiche plus).
|
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Fermer")).Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldNotContain("Voir la caméra"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SnapshotsPage_LeDialogResteRenduApresFermeture()
|
|
|
|
|
{
|
|
|
|
|
// Cœur de la régression issue #94 : le MudDialog ne doit jamais être
|
|
|
|
|
// détruit par un @if — il reste rendu, Visible piloté par @bind-Visible.
|
|
|
|
|
// Avec l'ancien pattern, FindComponents<MudDialog>() retournait 0 hors
|
|
|
|
|
// ouverture (le @if (_lightboxEvent is not null) détruisait le composant).
|
|
|
|
|
Handler.OnGet("api/events", new EventPageResult(
|
|
|
|
|
[SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg")], 1, 1, 100));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<SnapshotsPage>();
|
2026-10-25 06:35:00 +01:00
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Personne");
|
|
|
|
|
cut.Markup.ShouldContain("87");
|
|
|
|
|
});
|
2026-08-15 02:45:39 +02:00
|
|
|
|
|
|
|
|
// Dialog présent dans l'arbre dès le rendu initial, alors que le
|
|
|
|
|
// lightbox est fermé (Visible=false).
|
|
|
|
|
cut.FindComponents<MudDialog>().Count.ShouldBe(1);
|
|
|
|
|
|
|
|
|
|
// Ouvrir puis fermer : le composant MudDialog reste dans l'arbre.
|
|
|
|
|
cut.FindAll("div[role=button]")[0].Click();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Voir la caméra"));
|
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Fermer")).Click();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldNotContain("Voir la caméra"));
|
|
|
|
|
|
|
|
|
|
cut.FindComponents<MudDialog>().Count.ShouldBe(1);
|
|
|
|
|
}
|
2026-08-10 13:10:37 +02:00
|
|
|
}
|