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( [Agent("Agent-1"), Agent("Agent-2")], 2, 1, 20)); LoginAs(); var cut = RenderPage(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([], 0, 1, 20)); LoginAs(); var cut = RenderPage(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(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([], 0, 1, 20)); LoginAs(); var nav = Services.GetRequiredService(); var cut = RenderPage(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( Enumerable.Range(1, 20).Select(i => Agent($"Agent-{i}")).ToArray(), 25, 1, 20)); LoginAs(); var cut = RenderPage(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()); } }