perf(centralui): load template stubs/analysis projections instead of the full tracked graph (review 2026-10-21 07/P-1, 08/P1)

This commit is contained in:
Joseph Doherty
2026-10-22 08:40:49 -04:00
parent 0ebfafa98b
commit c429fb374b
12 changed files with 103 additions and 28 deletions
@@ -1,3 +1,4 @@
using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates;
using System.Security.Claims;
using Bunit;
using Microsoft.AspNetCore.Authorization;
@@ -61,8 +62,9 @@ public class TransportExportPageTests : BunitContext
// Default empty repos so OnInitializedAsync doesn't throw — individual
// tests override the bits they care about.
_templateRepo.GetAllTemplatesAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<Template>>(new List<Template>()));
_templateRepo.GetTemplateStubsAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<TemplateStub>>(
(new List<Template>()).Select(t => new TemplateStub(t.Id, t.Name, t.ParentTemplateId, t.FolderId, t.IsDerived, t.OwnerCompositionId)).ToList()));
_templateRepo.GetAllFoldersAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<TemplateFolder>>(new List<TemplateFolder>()));
_templateRepo.GetAllSharedScriptsAsync(Arg.Any<CancellationToken>())
@@ -139,8 +141,9 @@ public class TransportExportPageTests : BunitContext
// export page no longer offers a keys selection list, only API methods.
var apiMethod = new ApiMethod("CreateOrder", "// noop") { Id = 70 };
_templateRepo.GetAllTemplatesAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<Template>>(new List<Template> { template }));
_templateRepo.GetTemplateStubsAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<TemplateStub>>(
(new List<Template> { template }).Select(t => new TemplateStub(t.Id, t.Name, t.ParentTemplateId, t.FolderId, t.IsDerived, t.OwnerCompositionId)).ToList()));
_templateRepo.GetAllSharedScriptsAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<SharedScript>>(new List<SharedScript> { script }));
_externalRepo.GetAllExternalSystemsAsync(Arg.Any<CancellationToken>())
@@ -218,8 +221,9 @@ public class TransportExportPageTests : BunitContext
});
var motor = new Template("Motor") { Id = 2 };
_templateRepo.GetAllTemplatesAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<Template>>(new List<Template> { pump, motor }));
_templateRepo.GetTemplateStubsAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<TemplateStub>>(
(new List<Template> { pump, motor }).Select(t => new TemplateStub(t.Id, t.Name, t.ParentTemplateId, t.FolderId, t.IsDerived, t.OwnerCompositionId)).ToList()));
_templateRepo.GetTemplateWithChildrenAsync(1, Arg.Any<CancellationToken>())
.Returns(Task.FromResult<Template?>(pump));
_templateRepo.GetTemplateWithChildrenAsync(2, Arg.Any<CancellationToken>())
@@ -262,8 +266,9 @@ public class TransportExportPageTests : BunitContext
public async Task Step4_triggers_ExportAsync_with_selected_artifacts_and_user_identity()
{
var template = new Template("Pump") { Id = 1 };
_templateRepo.GetAllTemplatesAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<Template>>(new List<Template> { template }));
_templateRepo.GetTemplateStubsAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<TemplateStub>>(
(new List<Template> { template }).Select(t => new TemplateStub(t.Id, t.Name, t.ParentTemplateId, t.FolderId, t.IsDerived, t.OwnerCompositionId)).ToList()));
_templateRepo.GetTemplateWithChildrenAsync(1, Arg.Any<CancellationToken>())
.Returns(Task.FromResult<Template?>(template));
@@ -0,0 +1,41 @@
using System.Security.Claims;
using Bunit;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Security;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Templates;
using Dashboard = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Dashboard;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Pages;
/// <summary>Review 2026-10-21 07/P-1: the landing page must not load the full tracked template graph just to count.</summary>
public class TemplateListLoadTests : BunitContext
{
/// <summary>The Dashboard counts templates from stubs, never the full graph.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Dashboard_CountsTemplatesFromStubs_NotFullGraph()
{
var repo = Substitute.For<ITemplateEngineRepository>();
repo.GetTemplateStubsAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult<IReadOnlyList<TemplateStub>>(
new List<TemplateStub> { new(1, "A", null, null, false, null), new(2, "B", null, null, false, null) }));
var sites = Substitute.For<ISiteRepository>();
sites.GetAllSitesAsync().Returns(new List<Site>());
sites.GetAllDataConnectionsAsync().Returns(new List<DataConnection>());
var keys = Substitute.For<IInboundApiKeyAdmin>();
Services.AddSingleton(repo);
Services.AddSingleton(sites);
Services.AddSingleton(keys);
Services.AddSingleton(Substitute.For<AuthenticationStateProvider>());
AddAuthorization();
var cut = Render<Dashboard>();
await cut.WaitForStateAsync(() => cut.Markup.Contains("Templates"));
await repo.Received(1).GetTemplateStubsAsync(Arg.Any<CancellationToken>());
await repo.DidNotReceive().GetAllTemplatesAsync(Arg.Any<CancellationToken>());
}
}