Files
Heimdall/tests/Heimdall.Web.Tests/Components/Pages/SnapshotsPageTests.cs
T
Arnaud Dartois 5db7d84d9c fix(test): immunise les tests bUnit contre le flaky UnknownEventHandlerIdException
Après .Input()/.Change() (MudTextField Immediate), le dialog re-rend de
manière asynchrone : un FindAll() puis .Click()/.Submit() hors InvokeAsync
peut référencer un event handler périmé → bUnit
UnknownEventHandlerIdException en CI (flaky, non reproductible en local).

Workaround officiel bUnit : wrapper le Find+action dans
cut.InvokeAsync(...). Appliqué à tous les patterns Input/Change → Click/Submit
des tests de pages (Sites, Cameras, Groups, Users, Settings, Snapshots,
Register, Profile, AuditLogs, Login, SiteDetail, Discoveries).
2026-08-11 16:58:54 +02:00

99 lines
3.5 KiB
C#

using Bunit;
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 SnapshotsPageTests : PageTestBase
{
private static readonly Guid CameraId = Guid.NewGuid();
private static EventItem SnapshotEvent(
string type = "PersonDetected",
double confidence = 0.87,
string? label = "Personne",
string? snapshotUrl = "/snapshots/1.jpg") =>
new(Guid.NewGuid(), CameraId, type, confidence, label, snapshotUrl, snapshotUrl,
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"),
SnapshotEvent("VehicleDetected", 0.66, "Voiture", "/snapshots/2.jpg"),
SnapshotEvent("MotionDetected", 0.5, null, null)], // sans snapshot → filtré
3, 1, 100));
LoginAs();
var cut = RenderPage<SnapshotsPage>();
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("2 snapshot(s) trouvé(s)");
cut.Markup.ShouldContain("Personne");
cut.Markup.ShouldContain("/snapshots/1.jpg");
cut.Markup.ShouldContain("/snapshots/2.jpg");
});
}
[Fact]
public void SnapshotsPage_AucunSnapshot_AfficheMessage()
{
Handler.OnGet("api/events", new EventPageResult(
[SnapshotEvent("MotionDetected", 0.5, null, null)], 1, 1, 100));
LoginAs();
var cut = RenderPage<SnapshotsPage>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucun snapshot trouvé."));
}
[Fact]
public async Task SnapshotsPage_Recherche_FiltreLesSnapshots()
{
Handler.OnGet("api/events", new EventPageResult(
[SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg"),
SnapshotEvent("VehicleDetected", 0.66, "Voiture", "/snapshots/2.jpg")],
2, 1, 100));
LoginAs();
var cut = RenderPage<SnapshotsPage>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("2 snapshot(s) trouvé(s)"));
// Champ recherche sans Immediate → Change
cut.FindAll("input")[0].Change("Personne");
// Find+Click dans InvokeAsync (handler périmé sinon, flaky CI).
await cut.InvokeAsync(() => cut.FindAll("button").Single(b => b.TextContent.Contains("Charger")).Click());
cut.WaitForAssertion(() =>
{
cut.Markup.ShouldContain("1 snapshot(s) trouvé(s)");
cut.Markup.ShouldContain("Personne");
cut.Markup.ShouldNotContain("Voiture");
});
}
[Fact]
public void SnapshotsPage_OuvrirLightbox_AfficheDialog()
{
// Le clic sur une carte ouvre la lightbox (MudDialog avec le snapshot).
Handler.OnGet("api/events", new EventPageResult(
[SnapshotEvent("PersonDetected", 0.87, "Personne", "/snapshots/1.jpg")], 1, 1, 100));
LoginAs();
var cut = RenderPage<SnapshotsPage>();
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Personne · 87%"));
// 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"));
}
}