using Bunit; using System.Net.Http.Json; using System.Text.Json; 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 AgentDetailTests : PageTestBase { private static readonly Guid AgentId = Guid.NewGuid(); private static AgentItem Agent() => new() { Id = AgentId, SiteId = Guid.NewGuid(), SiteName = "Site Principal", Name = "agent-01", Hostname = "agent-01", Version = "1.0.0", Status = "Online", LastHeartbeat = DateTime.UtcNow, PublicIp = "10.0.0.5", CreatedAt = DateTime.UtcNow.AddDays(-5) }; private static AppSettingItem Setting(string key, string value, string category, string valueType, bool agentScoped = true, string? description = null) => new( Guid.NewGuid(), key, value, description, category, valueType, null, DateTime.UtcNow, agentScoped); private static AgentSettingItem Override(string key, string value) => new( Guid.NewGuid(), AgentId, key, value, DateTime.UtcNow); [Fact] public void AgentDetail_AfficheGlobauxHerites_AvecBadge() { // Ordre important : les routes spécifiques /settings AVANT api/agents/{id} // (RoutedHandler matche par Contains) Handler.OnGet($"api/settings/agent/{AgentId}/overrides", new List()); Handler.OnGet("api/settings", new List { Setting("detection.threshold", "0.7", "Detection", "number", true, "Seuil de détection"), Setting("recording.enabled", "true", "Recording", "boolean", true, "Enregistrement activé"), Setting("server.internal.port", "8080", "Server", "number", false, "Port interne (non agent)") }); Handler.OnGet($"api/agents/{AgentId}", Agent()); LoginAs(); var cut = RenderPage(p => p.Add(x => x.Id, AgentId)); cut.WaitForAssertion(() => cut.Markup.ShouldContain("agent-01")); // Les globaux agent-scoped sont affichés avec leurs libellés cut.WaitForAssertion(() => { cut.Markup.ShouldContain("Seuil de détection"); cut.Markup.ShouldContain("Enregistrement activé"); cut.Markup.ShouldContain("Hérité (global)"); }); // Le setting non agent-scoped n'apparaît pas cut.Markup.ShouldNotContain("server.internal.port"); } [Fact] public void AgentDetail_ModifierGlobal_CreeSurcharge() { Handler.OnGet($"api/settings/agent/{AgentId}/overrides", new List()); Handler.On(HttpMethod.Put, $"api/settings/agent/{AgentId}/overrides", System.Net.HttpStatusCode.OK); Handler.OnGet("api/settings", new List { Setting("detection.threshold", "0.7", "Detection", "number", true, "Seuil de détection") }); Handler.OnGet($"api/agents/{AgentId}", Agent()); LoginAs(); var cut = RenderPage(p => p.Add(x => x.Id, AgentId)); cut.WaitForAssertion(() => cut.Markup.ShouldContain("Seuil de détection")); // Modifier la valeur dans la carte → crée un override cut.FindAll(".settings-card .mud-input input")[0].Change("0.9"); cut.WaitForAssertion(() => Handler.Requests.Any(r => r.Method == HttpMethod.Put && r.RequestUri!.AbsolutePath.Contains($"api/settings/agent/{AgentId}/overrides")).ShouldBeTrue()); var put = Handler.Requests.First(r => r.Method == HttpMethod.Put && r.RequestUri!.AbsolutePath.Contains($"api/settings/agent/{AgentId}/overrides")); put.RequestUri!.AbsolutePath.ShouldContain(AgentId.ToString()); } [Fact] public void AgentDetail_SurchargeExistante_AfficheBadgeEtReinitialise() { Handler.OnGet($"api/settings/agent/{AgentId}/overrides", new List { Override("detection.threshold", "0.9") }); Handler.On(HttpMethod.Delete, $"api/settings/agent/{AgentId}/overrides", System.Net.HttpStatusCode.OK); Handler.OnGet("api/settings", new List { Setting("detection.threshold", "0.7", "Detection", "number", true, "Seuil de détection") }); Handler.OnGet($"api/agents/{AgentId}", Agent()); LoginAs(); var cut = RenderPage(p => p.Add(x => x.Id, AgentId)); cut.WaitForAssertion(() => cut.Markup.ShouldContain("Seuil de détection")); // La carte affiche la valeur surchargée et le badge cut.WaitForAssertion(() => { cut.Markup.ShouldContain("Surchargé pour cet agent"); cut.Markup.ShouldContain("0.9"); }); // Le bouton Restore (réinitialiser) déclenche le DELETE cut.FindAll("button.mud-icon-button").First(b => b.QuerySelector("svg") is not null).Click(); cut.WaitForAssertion(() => Handler.Requests.Any(r => r.Method == HttpMethod.Delete && r.RequestUri!.AbsolutePath.Contains($"api/settings/agent/{AgentId}/overrides")).ShouldBeTrue()); } }