2026-08-10 13:10:37 +02:00
|
|
|
using Bunit;
|
|
|
|
|
using Bunit.TestDoubles;
|
|
|
|
|
using Heimdall.Web.Components.Pages.Admin;
|
|
|
|
|
using Heimdall.Web.Services;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using MudBlazor;
|
|
|
|
|
using Shouldly;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Web.Tests.Components.Pages;
|
|
|
|
|
|
|
|
|
|
public class AllAgentsPageTests : PageTestBase
|
|
|
|
|
{
|
|
|
|
|
private static readonly Guid SiteId = Guid.NewGuid();
|
|
|
|
|
private static readonly Guid AgentId = Guid.NewGuid();
|
|
|
|
|
|
|
|
|
|
private static AgentItem Agent(string name = "Agent-1", string siteName = "Site Alpha") => new()
|
|
|
|
|
{
|
2026-08-10 23:26:32 +02:00
|
|
|
Id = Guid.NewGuid(),
|
2026-08-10 13:10:37 +02:00
|
|
|
SiteId = SiteId,
|
|
|
|
|
SiteName = siteName,
|
|
|
|
|
Name = name,
|
|
|
|
|
Hostname = "agent-1",
|
|
|
|
|
Version = "1.2.3",
|
|
|
|
|
Status = "Online",
|
|
|
|
|
LastHeartbeat = DateTime.UtcNow.AddMinutes(-1),
|
|
|
|
|
PublicIp = "10.0.0.5",
|
|
|
|
|
CreatedAt = DateTime.UtcNow.AddDays(-3)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static List<AgentStatItem> Stats() =>
|
|
|
|
|
[
|
|
|
|
|
new("total", "Agents", 4, "#4caf50"),
|
|
|
|
|
new("online", "En ligne", 3, "#2196f3")
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_AfficheAgentsEtStats()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>(
|
|
|
|
|
[Agent("Agent-1"), Agent("Agent-2", "Site Beta")], 2, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Agent-1");
|
|
|
|
|
cut.Markup.ShouldContain("Agent-2");
|
2026-08-10 23:26:32 +02:00
|
|
|
cut.Markup.ShouldContain("agent-1");
|
|
|
|
|
cut.Markup.ShouldContain("10.0.0.5");
|
|
|
|
|
// Statut traduit sur la carte (StatusChip)
|
|
|
|
|
cut.Markup.ShouldContain("En ligne");
|
|
|
|
|
// KPI cards
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.Markup.ShouldContain("Agents");
|
|
|
|
|
cut.Markup.ShouldContain("En ligne");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_AucunAgent_AfficheEtatVide()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>([], 0, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Aucun agent déployé");
|
2026-11-08 21:19:00 +01:00
|
|
|
cut.Markup.ShouldContain("Aucun agent n'est actuellement enregistré sur un site.");
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.Markup.ShouldContain("Voir les sites");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_ErreurChargement_AfficheAlerte()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", System.Net.HttpStatusCode.InternalServerError);
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
cut.Markup.ShouldContain("Erreur lors du chargement des agents."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_Recherche_RechargeLaListe()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>(
|
|
|
|
|
[Agent("Agent-1"), Agent("Agent-2")], 2, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agent-1"));
|
|
|
|
|
|
2026-08-10 13:17:27 +02:00
|
|
|
// Le FilterBar invoque SearchChanged à la frappe (fix) → la page
|
|
|
|
|
// recharge via LoadData sans submit
|
2026-08-10 13:10:37 +02:00
|
|
|
cut.FindAll("input")[0].Input("Agent-2");
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
Handler.Requests.Count(r => r.Method == HttpMethod.Get
|
|
|
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/agents")).ShouldBe(2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_VoirLesSites_Navigue()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>([], 0, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var nav = Services.GetRequiredService<BunitNavigationManager>();
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Voir les sites"));
|
|
|
|
|
|
|
|
|
|
cut.FindAll("button").Single(b => b.TextContent.Contains("Voir les sites")).Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => nav.Uri.ShouldBe("http://localhost/sites"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_Pagination_Suivant_ChargePage2()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>(
|
|
|
|
|
Enumerable.Range(1, 20).Select(i => Agent($"Agent-{i}")).ToArray(), 25, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
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")
|
|
|
|
|
&& r.RequestUri.Query.Contains("page=2")).ShouldBeTrue());
|
|
|
|
|
}
|
2026-08-30 09:45:37 +02:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_Supprimer_BoutonPresent()
|
|
|
|
|
{
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>(
|
|
|
|
|
[Agent("Agent-1"), Agent("Agent-2", "Site Beta")], 2, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agent-1"));
|
|
|
|
|
// Les boutons supprimer sont présents (2 agents = 2 boutons)
|
|
|
|
|
var deleteButtons = cut.FindAll("[aria-label=\"Supprimer l'agent\"]");
|
|
|
|
|
deleteButtons.Count.ShouldBe(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_Supprimer_OuvreDialogConfirmation()
|
|
|
|
|
{
|
|
|
|
|
var agent = Agent("Agent-Test");
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>([agent], 1, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agent-Test"));
|
|
|
|
|
|
|
|
|
|
// Cliquer sur le bouton supprimer ouvre le dialog
|
|
|
|
|
cut.Find("[aria-label=\"Supprimer l'agent\"]").Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
cut.Markup.ShouldContain("Confirmer la suppression");
|
|
|
|
|
cut.Markup.ShouldContain("Agent-Test");
|
|
|
|
|
cut.Markup.ShouldContain("désassignées");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AllAgentsPage_Supprimer_ConfirmationEnvoieDeleteEtRecharge()
|
|
|
|
|
{
|
|
|
|
|
var agent = Agent("Agent-Del");
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>([agent], 1, 1, 20));
|
|
|
|
|
Handler.OnGet("api/stats/agent-stats", Stats());
|
|
|
|
|
Handler.On(HttpMethod.Delete, "api/agents", System.Net.HttpStatusCode.OK);
|
|
|
|
|
// Après suppression, la liste est rechargée vide
|
|
|
|
|
Handler.OnGet("api/agents", new PagedData<AgentItem>([], 0, 1, 20));
|
|
|
|
|
LoginAs();
|
|
|
|
|
|
|
|
|
|
var cut = RenderPage<AllAgentsPage>();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Agent-Del"));
|
|
|
|
|
|
|
|
|
|
// Ouvrir le dialog
|
|
|
|
|
cut.Find("[aria-label=\"Supprimer l'agent\"]").Click();
|
|
|
|
|
cut.WaitForAssertion(() => cut.Markup.ShouldContain("Confirmer la suppression"));
|
|
|
|
|
|
|
|
|
|
// Cliquer "Supprimer" dans le dialog (dernier bouton avec ce texte)
|
|
|
|
|
var dialogButtons = cut.FindAll("button");
|
|
|
|
|
dialogButtons.Where(b => b.TextContent.Trim() == "Supprimer").Last().Click();
|
|
|
|
|
|
|
|
|
|
cut.WaitForAssertion(() =>
|
|
|
|
|
{
|
|
|
|
|
// Le DELETE a bien été envoyé
|
|
|
|
|
Handler.Requests.Any(r => r.Method == HttpMethod.Delete
|
|
|
|
|
&& r.RequestUri!.PathAndQuery.Contains("api/agents")).ShouldBeTrue();
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-08-10 13:10:37 +02:00
|
|
|
}
|