- UiErrors : messages métier centralisés (LoadFailed/SaveFailed/DeleteFailed/ OperationFailed) — plus aucun ex.Message brut dans l'UI (48 remplacés sur 19 pages), les détails techniques restent dans les logs. - UiFormat : formatage centralisé des statuts (Online → En ligne, Offline → Hors ligne, Disabled → Désactivée…) et types d'événements ; appliqué aux pages Agents (liste + détail) qui affichaient encore les valeurs brutes. - catch (Exception ex) devenus inutiles → catch simple (46 nettoyés). - Boutons = verbes, validation métier déjà en place (Le nom est requis…) couverte par les tests existants. - Tests : adaptés au message métier (Register) et au statut traduit (Agents). 182 tests Web verts.
107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
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 AgentsPageTests : PageTestBase
|
|
{
|
|
private static readonly Guid SiteId = Guid.NewGuid();
|
|
private static readonly Guid AgentId = Guid.NewGuid();
|
|
|
|
private static AgentItem Agent(string name = "Agent-1", string status = "Online") => new()
|
|
{
|
|
Id = AgentId,
|
|
SiteId = SiteId,
|
|
SiteName = "Site Alpha",
|
|
Name = name,
|
|
Hostname = "agent-1",
|
|
Version = "1.2.3",
|
|
Status = status,
|
|
LastHeartbeat = DateTime.UtcNow.AddMinutes(-1),
|
|
PublicIp = "10.0.0.5",
|
|
CreatedAt = DateTime.UtcNow.AddDays(-3)
|
|
};
|
|
|
|
[Fact]
|
|
public void AgentsPage_AfficheAgentsDuSite()
|
|
{
|
|
Handler.OnGet("api/agents/by-site", new PagedData<AgentItem>(
|
|
[Agent("Agent-1"), Agent("Agent-2")], 2, 1, 20));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<AgentsPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() =>
|
|
{
|
|
cut.Markup.ShouldContain("Agent-1");
|
|
cut.Markup.ShouldContain("Agent-2");
|
|
cut.Markup.ShouldContain("agent-1");
|
|
cut.Markup.ShouldContain("1.2.3");
|
|
cut.Markup.ShouldContain("En ligne");
|
|
cut.Markup.ShouldContain("10.0.0.5");
|
|
cut.Markup.ShouldContain($"/agents/{AgentId}");
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void AgentsPage_AucunAgent_AfficheEtatVide()
|
|
{
|
|
Handler.OnGet("api/agents/by-site", new PagedData<AgentItem>([], 0, 1, 20));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<AgentsPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Aucun agent enregistré"));
|
|
}
|
|
|
|
[Fact]
|
|
public void AgentsPage_ErreurChargement_AfficheAlerte()
|
|
{
|
|
Handler.OnGet("api/agents/by-site", System.Net.HttpStatusCode.InternalServerError);
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<AgentsPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
|
|
cut.WaitForAssertion(() =>
|
|
cut.Markup.ShouldContain("Erreur lors du chargement des agents."));
|
|
}
|
|
|
|
[Fact]
|
|
public void AgentsPage_RetourAuxSites_Navigue()
|
|
{
|
|
Handler.OnGet("api/agents/by-site", new PagedData<AgentItem>([], 0, 1, 20));
|
|
LoginAs();
|
|
|
|
var nav = Services.GetRequiredService<BunitNavigationManager>();
|
|
var cut = RenderPage<AgentsPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Retour aux sites"));
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Retour aux sites")).Click();
|
|
|
|
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/sites"));
|
|
}
|
|
|
|
[Fact]
|
|
public void AgentsPage_Pagination_Suivant_ChargePage2()
|
|
{
|
|
Handler.OnGet("api/agents/by-site", new PagedData<AgentItem>(
|
|
Enumerable.Range(1, 20).Select(i => Agent($"Agent-{i}")).ToArray(), 25, 1, 20));
|
|
LoginAs();
|
|
|
|
var cut = RenderPage<AgentsPage>(p => p.Add(x => x.SiteId, SiteId));
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Page 1 sur 2"));
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Suivant")).Click();
|
|
|
|
cut.WaitForAssertion(() =>
|
|
Handler.Requests.Any(r => r.Method == HttpMethod.Get
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/agents/by-site")
|
|
&& r.RequestUri.Query.Contains("page=2")).ShouldBeTrue());
|
|
}
|
|
}
|