Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Pages/NotificationListsPageTests.cs
T
Joseph Doherty 40944a0d04 feat(centralui): row 75 — Approve is caution amber, not destructive red
The Secured Writes page rendered Approve and Reject as IDENTICAL red buttons.
Row 28 fixed the labels but left the styling, and the styling was the worse
half: at the moment of a two-person-control decision the colour carried no
discriminating information at all, and the affirmative half of the control wore
the delete affordance.

Root cause is that `danger` was a two-state flag for a three-state concept, so a
consequential-but-not-destructive action had to choose between looking like a
delete and looking like the safe default. Adds the missing middle rung:

    DialogSeverity { Normal -> btn-primary, Caution -> btn-warning,
                     Destructive -> btn-danger }

DECISION. Approve -> Caution (amber): approving executes a real write to live
plant equipment, so it is not a benign primary action, but it is not destroying
anything either. Reject KEEPS Destructive (red): it is terminal and irreversible
for the request — the Operator must resubmit — and holding it red is what keeps
the two opposite decisions visually distinct now that Approve is amber. Dropping
Reject to neutral was considered and rejected: it would invert the bias, making
declining look like the safe default. Colour is never the only signal — both
buttons carry their verb.

Only Destructive inherits the row-28 "Delete" label fallback; an unlabelled
Caution reads "Confirm", since a caution action is by definition not a delete —
otherwise row 28 would come back through the new door.

ADDITIVE OVERLOAD, deliberately. The boolean form stays the signature for all
~34 existing call sites, which are genuinely two-state, so nothing in src/
outside SecuredWrites.razor changed. Adding a fifth optional parameter to the
boolean form instead would have silently un-matched every existing four-argument
NSubstitute stub — the exact row-28 gotcha (null Task, caller NREs, no compile
error). The overload cannot: the only breakage was six hand-written IDialogService
fakes failing to compile, which is the loud failure mode. That gotcha did land
once, in SecuredWritesTests' own stub, and is now stubbed on both overloads with
a comment saying why.

DialogState's `bool Danger` became `DialogSeverity Severity` rather than gaining
a second field — it is read only by DialogHost, and one field cannot disagree
with itself.

Fail-before, both halves:
  - Approve call site reverted to `danger: true` -> both new Approve pins fail.
  - Caution remapped to btn-danger in DialogHost -> the amber styling pin fails.

Tests: DialogSeverityStylingTests renders the REAL DialogService behind a real
DialogHost (FailoverConfirmLabelTests' harness), so it pins what the user sees,
not what an argument was — all three severities, both label fallbacks, and the
boolean adapter. SecuredWritesTests pins that the page asks for Caution, with a
negative control that it did NOT also go through the boolean form (without which
the assertion would pass on a call site left at danger:true). Reject's red is
pinned too, so a later "make this page less alarming" pass cannot flatten both
to one colour.

No Playwright selectors needed updating — the suite has no Secured Writes
coverage, and no `.btn-danger` selector elsewhere targets this page.

Docs: Component-CentralUI.md gains the severity contract under Technology (next
to the row-28 label contract) plus the decision on the Secured Writes section.

CentralUI.Tests: 1055/1055 green.
2026-08-15 21:33:08 -04:00

214 lines
8.4 KiB
C#

using System.Security.Claims;
using ZB.MOM.WW.ScadaBridge.Security;
using Bunit;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using NotificationListsPage = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Pages.Notifications.NotificationLists;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Pages;
/// <summary>
/// bUnit rendering tests for the standalone Notification Lists page (Task 7).
/// </summary>
public class NotificationListsPageTests : BunitContext
{
private void WireAuthAndDialog()
{
Services.AddSingleton<IDialogService>(new AlwaysConfirmDialogService());
var claims = new[]
{
new Claim(JwtTokenService.UsernameClaimType, "tester"),
new Claim(JwtTokenService.RoleClaimType, "Designer"),
};
var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth"));
Services.AddSingleton<AuthenticationStateProvider>(new TestAuthStateProvider(user));
Services.AddAuthorizationCore();
}
[Fact]
public void RendersNotificationListRows()
{
var repo = Substitute.For<INotificationRepository>();
repo.GetAllNotificationListsAsync()
.Returns(Task.FromResult<IReadOnlyList<NotificationList>>(
new List<NotificationList> { new("Ops On-Call") { Id = 1 } }));
repo.GetRecipientsByListIdAsync(1)
.Returns(Task.FromResult<IReadOnlyList<NotificationRecipient>>(
new List<NotificationRecipient> { new("Jane", "jane@example.com") }));
Services.AddSingleton(repo);
WireAuthAndDialog();
var cut = Render<NotificationListsPage>();
cut.WaitForAssertion(() =>
{
Assert.Contains("Ops On-Call", cut.Markup);
Assert.Contains("jane@example.com", cut.Markup);
});
}
[Fact]
public void ShowsEmptyState_WhenNoLists()
{
var repo = Substitute.For<INotificationRepository>();
repo.GetAllNotificationListsAsync()
.Returns(Task.FromResult<IReadOnlyList<NotificationList>>(
new List<NotificationList>()));
Services.AddSingleton(repo);
WireAuthAndDialog();
var cut = Render<NotificationListsPage>();
cut.WaitForAssertion(() =>
Assert.Contains("No notification lists", cut.Markup));
}
[Fact]
public void DeleteList_ConfirmsThenDeletesAndReloads()
{
var repo = Substitute.For<INotificationRepository>();
repo.GetAllNotificationListsAsync()
.Returns(Task.FromResult<IReadOnlyList<NotificationList>>(
new List<NotificationList> { new("Ops On-Call") { Id = 1 } }));
repo.GetRecipientsByListIdAsync(1)
.Returns(Task.FromResult<IReadOnlyList<NotificationRecipient>>(
new List<NotificationRecipient>()));
Services.AddSingleton(repo);
WireAuthAndDialog();
var cut = Render<NotificationListsPage>();
cut.WaitForState(() => cut.Markup.Contains("Ops On-Call"));
var deleteButton = cut.FindAll("tbody tr button")
.First(b => b.TextContent.Contains("Delete"));
deleteButton.Click();
cut.WaitForAssertion(() =>
{
repo.Received().DeleteNotificationListAsync(1);
repo.Received().SaveChangesAsync();
// Reload re-invokes the list query (once on init, once after delete).
repo.Received(2).GetAllNotificationListsAsync();
});
}
[Fact]
public void RendersTypeColumn_Sms()
{
var repo = Substitute.For<INotificationRepository>();
repo.GetAllNotificationListsAsync()
.Returns(Task.FromResult<IReadOnlyList<NotificationList>>(
new List<NotificationList>
{
new("SMS Alerts") { Id = 2, Type = NotificationType.Sms }
}));
repo.GetRecipientsByListIdAsync(2)
.Returns(Task.FromResult<IReadOnlyList<NotificationRecipient>>(
new List<NotificationRecipient>()));
Services.AddSingleton(repo);
WireAuthAndDialog();
var cut = Render<NotificationListsPage>();
cut.WaitForAssertion(() =>
{
Assert.Contains("SMS Alerts", cut.Markup);
Assert.Contains("Sms", cut.Markup);
});
}
[Fact]
public void SmsListRecipientBadge_ShowsPhoneNumber_NotEmptyAngleBrackets()
{
// CentralUI-NNN regression: an SMS recipient carries a PhoneNumber and a null
// EmailAddress. The badge must render the phone, not the email-shaped "Name <>".
var repo = Substitute.For<INotificationRepository>();
repo.GetAllNotificationListsAsync()
.Returns(Task.FromResult<IReadOnlyList<NotificationList>>(
new List<NotificationList>
{
new("SMS Alerts") { Id = 7, Type = NotificationType.Sms }
}));
repo.GetRecipientsByListIdAsync(7)
.Returns(Task.FromResult<IReadOnlyList<NotificationRecipient>>(
new List<NotificationRecipient> { NotificationRecipient.ForSms("Jane", "+15551234567") }));
Services.AddSingleton(repo);
WireAuthAndDialog();
var cut = Render<NotificationListsPage>();
cut.WaitForAssertion(() =>
{
Assert.Contains("+15551234567", cut.Markup);
// The bug rendered an empty contact field ("Jane <>") for SMS recipients.
Assert.DoesNotContain("Jane &lt;&gt;", cut.Markup);
Assert.DoesNotContain("Jane <>", cut.Markup);
});
}
[Fact]
public void RendersTypeColumn_Email()
{
// The list name deliberately contains no channel word so that the only "Email"
// in the rendered markup originates from the Type column cell, not the name cell.
// This guards against false-passes where the name itself satisfies the assertion.
var repo = Substitute.For<INotificationRepository>();
repo.GetAllNotificationListsAsync()
.Returns(Task.FromResult<IReadOnlyList<NotificationList>>(
new List<NotificationList>
{
new("Ops Alerts") { Id = 3, Type = NotificationType.Email }
}));
repo.GetRecipientsByListIdAsync(3)
.Returns(Task.FromResult<IReadOnlyList<NotificationRecipient>>(
new List<NotificationRecipient>()));
Services.AddSingleton(repo);
WireAuthAndDialog();
var cut = Render<NotificationListsPage>();
cut.WaitForAssertion(() =>
{
// Verify the row name rendered.
Assert.Contains("Ops Alerts", cut.Markup);
// Locate the Type <td> in the rendered table row and assert its text content
// is exactly "Email". If the Type column were removed the <td> would not
// exist and the assertion would throw, catching the regression.
var typeCell = cut.FindAll("tbody tr td")
.FirstOrDefault(td => td.TextContent.Trim() == "Email");
Assert.NotNull(typeCell);
});
}
/// <summary>A dialog service that auto-confirms, so action paths run end-to-end.</summary>
private sealed class AlwaysConfirmDialogService : IDialogService
{
public Task<bool> ConfirmAsync(string title, string message, bool danger = false, string? confirmLabel = null)
=> Task.FromResult(true);
// Register row 75 added a DialogSeverity overload. This stub auto-confirms
// regardless of severity — it exists to let action paths run end to end, not to
// assert anything about styling.
public Task<bool> ConfirmAsync(string title, string message, DialogSeverity severity, string? confirmLabel = null)
=> Task.FromResult(true);
public Task<string?> PromptAsync(
string title, string label, string initialValue = "", string? placeholder = null)
=> Task.FromResult<string?>(null);
public Task<TResult?> ShowAsync<TResult>(
string title,
Microsoft.AspNetCore.Components.RenderFragment<DialogContext<TResult>> body,
string? size = null)
=> Task.FromResult<TResult?>(default);
}
}