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).
153 lines
5.2 KiB
C#
153 lines
5.2 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 DiscoveriesPageTests : PageTestBase
|
|
{
|
|
private static readonly Guid SiteId = Guid.NewGuid();
|
|
private static readonly Guid DiscoveryId = Guid.NewGuid();
|
|
|
|
private static PendingDiscoveryItem Discovery(
|
|
string ip = "192.168.1.10",
|
|
string? vendor = "Hikvision",
|
|
string? hardware = "DS-2CD2T47",
|
|
string[]? urls = null) => new()
|
|
{
|
|
Id = DiscoveryId,
|
|
SiteId = SiteId,
|
|
IpAddress = ip,
|
|
MacAddress = "3C:EF:8C:12:34:56",
|
|
Vendor = vendor,
|
|
OnvifHardware = hardware,
|
|
IsReachable = true,
|
|
DetectedType = "Standard",
|
|
DiscoveryMethod = "OnvifWsDiscovery",
|
|
OpenPorts = [80, 554, 3702],
|
|
OnvifDeviceServiceUrl = $"http://{ip}/onvif/device_service",
|
|
OnvifName = vendor,
|
|
OnvifPort = 3702,
|
|
ProbedAt = DateTime.UtcNow.AddMinutes(-2),
|
|
StreamUrls = urls ?? [$"rtsp://{ip}:554/stream1"],
|
|
};
|
|
|
|
private static AgentItem Agent() => new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SiteId = SiteId,
|
|
Name = "Agent 1",
|
|
Hostname = "host1",
|
|
Status = "Online",
|
|
};
|
|
|
|
private void RouteDiscoveries(List<PendingDiscoveryItem> discoveries)
|
|
{
|
|
Handler.OnGet("api/discovery/sites/", discoveries);
|
|
Handler.OnGet("api/agents/by-site/", new PagedData<AgentItem>([Agent()], 1, 1, 50));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_AfficheListeDesDecouvertes()
|
|
{
|
|
RouteDiscoveries([Discovery()]);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
cut.Markup.ShouldContain("Caméras détectées");
|
|
cut.Markup.ShouldContain("192.168.1.10");
|
|
cut.Markup.ShouldContain("Hikvision");
|
|
cut.Markup.ShouldContain("DS-2CD2T47");
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_AucuneDecouverte_AfficheMessage()
|
|
{
|
|
RouteDiscoveries([]);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucune caméra détectée"));
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_ScanNow_NotifieAgents()
|
|
{
|
|
RouteDiscoveries([]);
|
|
Handler.On(HttpMethod.Post, "scan-now", new ScanNowResult(2));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Scanner maintenant"));
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Scanner maintenant")).Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("2 agent", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DiscoveriesPage_Confirmer_AjouteCamera()
|
|
{
|
|
RouteDiscoveries([Discovery()]);
|
|
Handler.On(HttpMethod.Post, "confirm", new CameraItem
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
SiteId = SiteId,
|
|
Name = "Hikvision (192.168.1.10)",
|
|
Status = "Online",
|
|
});
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Confirmer la caméra']")[0].Click();
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Ajouter la caméra 192.168.1.10"));
|
|
|
|
// Le nom par défaut est pré-rempli et l'URL de flux sélectionnée → bouton actif.
|
|
// Find+Click dans InvokeAsync (handler périmé sinon, flaky CI).
|
|
await cut.InvokeAsync(() => cut.FindAll("button").Single(b => b.TextContent.Contains("Ajouter la caméra") && b.TextContent.Contains("Ajouter")).Click());
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("ajoutée", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void DiscoveriesPage_Rejeter_IgnoreDetection()
|
|
{
|
|
RouteDiscoveries([Discovery()]);
|
|
Handler.On(HttpMethod.Post, $"api/discovery/{DiscoveryId}/reject", System.Net.HttpStatusCode.OK);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<DiscoveriesPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("192.168.1.10"));
|
|
|
|
cut.FindAll("button[aria-label='Ignorer la détection']")[0].Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
var snackbar = Services.GetRequiredService<ISnackbar>();
|
|
snackbar.ShownSnackbars.Select(m => m.Message?.ToString() ?? "")
|
|
.ShouldContain(m => m.Contains("ignorée", StringComparison.OrdinalIgnoreCase));
|
|
});
|
|
}
|
|
}
|