Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Deployment/DeploymentsReloadDebounceTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

141 lines
6.1 KiB
C#

using System.Security.Claims;
using Bunit;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.DeploymentManager;
using DeploymentsPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Deployment.Deployments;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Deployment;
/// <summary>
/// Regression tests for the Deployment Status push coalescing. The
/// notifier fires per status write, so a multi-instance deploy produced a stampede of
/// reloads per circuit. The reload is leading-edge debounced: the first push after an
/// idle gap is still immediate, and a burst behind it collapses into one trailing reload.
///
/// <para>
/// Server-side paging (residual R3) shrank what one reload costs but not how many
/// arrive, so the coalescing still has to hold. The assertions now count
/// <c>QueryDeploymentListPageAsync</c> calls instead of the deleted
/// <c>GetAllDeploymentRecordsAsync</c>.
/// </para>
/// </summary>
public class DeploymentsReloadDebounceTests : BunitContext
{
private IDeploymentManagerRepository _deployRepo = null!;
private DeploymentStatusNotifier _notifier = null!;
private void RegisterServices()
{
_deployRepo = Substitute.For<IDeploymentManagerRepository>();
_notifier = new DeploymentStatusNotifier(NullLogger<DeploymentStatusNotifier>.Instance);
_deployRepo.QueryDeploymentListPageAsync(
Arg.Any<DeploymentListFilter>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(DeploymentListPage.Empty);
_deployRepo.GetDeploymentStatusCountsAsync(
Arg.Any<DeploymentListFilter>(), Arg.Any<CancellationToken>())
.Returns(DeploymentStatusCounts.Empty);
Services.AddSingleton(_deployRepo);
Services.AddSingleton<IDeploymentStatusNotifier>(_notifier);
var identity = new ClaimsIdentity(
new[] { new Claim(ClaimTypes.Name, "deployer") }, "TestCookie");
var stubAuth = new StubAuthStateProvider(
new AuthenticationState(new ClaimsPrincipal(identity)));
Services.AddSingleton<AuthenticationStateProvider>(stubAuth);
Services.AddScoped(_ => new SiteScopeService(stubAuth));
}
private sealed class StubAuthStateProvider : AuthenticationStateProvider
{
private readonly AuthenticationState _state;
/// <summary>Initializes a new instance of the <see cref="StubAuthStateProvider"/> class with a fixed authentication state.</summary>
/// <param name="state">The authentication state to return.</param>
public StubAuthStateProvider(AuthenticationState state) => _state = state;
/// <inheritdoc />
public override Task<AuthenticationState> GetAuthenticationStateAsync()
=> Task.FromResult(_state);
}
private void AssertReloaded(IRenderedComponent<DeploymentsPage> cut) =>
cut.WaitForAssertion(() =>
_deployRepo.Received().QueryDeploymentListPageAsync(
Arg.Any<DeploymentListFilter>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>()));
/// <summary>Verifies that a burst of status writes collapses into far fewer page reloads.</summary>
[Fact]
public void BurstOfStatusWrites_CollapsesIntoFarFewerReloads()
{
RegisterServices();
var cut = Render<DeploymentsPage>();
AssertReloaded(cut);
_deployRepo.ClearReceivedCalls();
// A 40-instance site deploy: every status write raises the notifier.
for (var i = 0; i < 40; i++)
{
_notifier.NotifyStatusChanged(
new DeploymentStatusChange($"dep-{i}", 1, DeploymentStatus.InProgress));
}
// Leading edge fires at once; the rest ride one trailing reload.
AssertReloaded(cut);
// Let the trailing window close before counting.
Thread.Sleep(900);
var reloads = _deployRepo.ReceivedCalls()
.Count(c => c.GetMethodInfo().Name == nameof(IDeploymentManagerRepository.QueryDeploymentListPageAsync));
Assert.InRange(reloads, 1, 4);
}
/// <summary>Verifies that the first push after an idle period reloads the page immediately.</summary>
[Fact]
public void FirstPushAfterIdle_ReloadsImmediately()
{
RegisterServices();
var cut = Render<DeploymentsPage>();
AssertReloaded(cut);
_deployRepo.ClearReceivedCalls();
// Idle since the initial load — the leading edge must not wait out the window.
Thread.Sleep(600);
_notifier.NotifyStatusChanged(
new DeploymentStatusChange("dep-1", 1, DeploymentStatus.Success));
cut.WaitForAssertion(
() => _deployRepo.Received().QueryDeploymentListPageAsync(
Arg.Any<DeploymentListFilter>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>()),
TimeSpan.FromMilliseconds(400));
}
/// <summary>Verifies that disposing the page during a coalesce window does not trigger a reload.</summary>
[Fact]
public void DisposeDuringACoalesceWindow_DoesNotReload()
{
RegisterServices();
var cut = Render<DeploymentsPage>();
AssertReloaded(cut);
// Two pushes: the first takes the leading edge, the second arms the trailing timer.
_notifier.NotifyStatusChanged(new DeploymentStatusChange("dep-1", 1, DeploymentStatus.InProgress));
_notifier.NotifyStatusChanged(new DeploymentStatusChange("dep-2", 1, DeploymentStatus.InProgress));
cut.Instance.Dispose();
_deployRepo.ClearReceivedCalls();
// The armed timer must be disposed with the component, not fire against it.
Thread.Sleep(900);
_deployRepo.DidNotReceive().QueryDeploymentListPageAsync(
Arg.Any<DeploymentListFilter>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>());
}
}