using System.Security.Claims; using Bunit; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using NSubstitute; using ScadaLink.CentralUI.Components.Shared; using ScadaLink.Commons.Entities.Sites; using ScadaLink.Commons.Interfaces.Repositories; using ScadaLink.Commons.Interfaces.Services; using ScadaLink.Communication; using ScadaLink.DeploymentManager; using SitesPage = ScadaLink.CentralUI.Components.Pages.Admin.Sites; namespace ScadaLink.CentralUI.Tests.Admin; /// /// Regression tests for CentralUI-012. The Sites page loaded all sites and then /// issued GetDataConnectionsBySiteIdAsync once per site (N+1 database /// round-trips on every load and post-delete refresh). The fix fetches all /// data connections in a single GetAllDataConnectionsAsync call and /// groups them client-side. /// public class SitesPageTests : BunitContext { private readonly ISiteRepository _siteRepo = Substitute.For(); private void RegisterServices() { Services.AddSingleton(_siteRepo); var comms = new CommunicationService( Options.Create(new CommunicationOptions()), NullLogger.Instance); Services.AddSingleton(comms); var artifactSvc = new ArtifactDeploymentService( _siteRepo, Substitute.For(), Substitute.For(), Substitute.For(), Substitute.For(), comms, Substitute.For(), Options.Create(new DeploymentManagerOptions()), NullLogger.Instance); Services.AddSingleton(artifactSvc); Services.AddSingleton(Substitute.For()); var identity = new ClaimsIdentity( new[] { new Claim(ClaimTypes.Name, "admin") }, "TestCookie"); var authState = new AuthenticationState(new ClaimsPrincipal(identity)); Services.AddSingleton( new StubAuthStateProvider(authState)); } private sealed class StubAuthStateProvider : AuthenticationStateProvider { private readonly AuthenticationState _state; public StubAuthStateProvider(AuthenticationState state) => _state = state; public override Task GetAuthenticationStateAsync() => Task.FromResult(_state); } private static List Sites(params int[] ids) => ids.Select(id => new Site($"Site{id}", $"SITE-{id}") { Id = id }).ToList(); private static DataConnection Conn(int siteId, string name) => new(name, "OpcUa", siteId); [Fact] public void LoadData_FetchesAllConnectionsInOneQuery_NoPerSiteQueries() { RegisterServices(); _siteRepo.GetAllSitesAsync().Returns(Sites(1, 2, 3)); _siteRepo.GetAllDataConnectionsAsync().Returns(new List { Conn(1, "c1"), Conn(2, "c2"), Conn(3, "c3"), }); Render(); // Regression: exactly one bulk query, and zero per-site queries. _siteRepo.Received(1).GetAllDataConnectionsAsync(); _siteRepo.DidNotReceive().GetDataConnectionsBySiteIdAsync(Arg.Any()); } [Fact] public void LoadData_GroupsConnectionsBySite_AndRendersThem() { RegisterServices(); _siteRepo.GetAllSitesAsync().Returns(Sites(1, 2)); _siteRepo.GetAllDataConnectionsAsync().Returns(new List { Conn(1, "alpha-conn"), Conn(2, "beta-conn"), }); var cut = Render(); Assert.Contains("alpha-conn", cut.Markup); Assert.Contains("beta-conn", cut.Markup); } }