feat(m9/T24b): move-data-connection UI dialog + action

This commit is contained in:
Joseph Doherty
2026-06-18 11:45:53 -04:00
parent dbe51e5f25
commit 16cb078cd2
7 changed files with 530 additions and 0 deletions
@@ -26,11 +26,17 @@ public class DataConnectionsPageTests : BunitContext
// keeps every existing test green (no badge / unknown state when no report).
private readonly IConnectionHealthQueryService _healthQuery =
Substitute.For<IConnectionHealthQueryService>();
// M9-T24b: the page now opens a Move-to-Site dialog that dispatches the move
// through IDataConnectionMoveService (the guard-running ManagementActor path).
// Substituted so the page renders without a real ManagementActor in scope.
private readonly IDataConnectionMoveService _moveService =
Substitute.For<IDataConnectionMoveService>();
public DataConnectionsPageTests()
{
Services.AddSingleton(_siteRepo);
Services.AddSingleton(_healthQuery);
Services.AddSingleton(_moveService);
// Satisfy the page's [Inject] IDialogService — the host that actually
// renders the dialog lives in MainLayout, not in bUnit's render scope.
Services.AddScoped<IDialogService, DialogService>();
@@ -209,4 +215,52 @@ public class DataConnectionsPageTests : BunitContext
Assert.Contains("bg-danger", disconnectedBadge.GetAttribute("class"));
Assert.Contains("Disconnected", disconnectedBadge.TextContent);
}
[Fact]
public void ConnectionNode_Has_MoveToSiteAction()
{
// M9-T24b: each connection node exposes a "Move to Site…" action alongside
// Edit/Delete.
SeedRepos(
sites: new[]
{
new Site("Plant-A", "plant-a") { Id = 1 },
new Site("Plant-B", "plant-b") { Id = 2 }
},
connections: new[] { new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 } });
var cut = Render<DataConnectionsPage>();
FindToggleForLabel(cut, "Plant-A")!.Click();
Assert.Contains(cut.FindAll("button"),
b => b.TextContent.Contains("Move to Site"));
}
[Fact]
public void MoveToSite_OpensDialog_ExcludingCurrentSite()
{
// M9-T24b: clicking "Move to Site…" opens the move dialog whose target-site
// picker omits the connection's current site (Plant-A) and lists the others.
SeedRepos(
sites: new[]
{
new Site("Plant-A", "plant-a") { Id = 1 },
new Site("Plant-B", "plant-b") { Id = 2 },
new Site("Plant-C", "plant-c") { Id = 3 }
},
connections: new[] { new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 } });
var cut = Render<DataConnectionsPage>();
FindToggleForLabel(cut, "Plant-A")!.Click();
cut.FindAll("button").First(b => b.TextContent.Contains("Move to Site")).Click();
// The dialog renders with a site picker; the current site is excluded.
var dialog = cut.Find(".modal.show");
var optionLabels = dialog.QuerySelectorAll("select option")
.Select(o => o.TextContent).ToList();
Assert.Contains(optionLabels, l => l.Contains("Plant-B"));
Assert.Contains(optionLabels, l => l.Contains("Plant-C"));
Assert.DoesNotContain(optionLabels, l => l.Contains("Plant-A"));
}
}
@@ -0,0 +1,133 @@
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared;
/// <summary>
/// bUnit tests for the <see cref="MoveDataConnectionDialog"/> (M9-T24b). The dialog
/// surfaces a target-site picker (excluding the connection's current site) and, on
/// confirm, dispatches a <c>MoveDataConnectionCommand</c> through the management
/// path via <see cref="IDataConnectionMoveService"/> — the SAME guard-running seam
/// the server uses. A guard error must be shown inline and the dialog must stay open;
/// a success must close the dialog and raise the refresh signal.
///
/// The service is substituted so the tests capture the dispatched (connectionId,
/// targetSiteId) and simulate both a success and a guard-error response without a
/// real ManagementActor in scope.
/// </summary>
public class MoveDataConnectionDialogTests : BunitContext
{
private readonly IDataConnectionMoveService _service = Substitute.For<IDataConnectionMoveService>();
public MoveDataConnectionDialogTests()
{
Services.AddSingleton(_service);
}
private IRenderedComponent<MoveDataConnectionDialog> RenderDialog(
int connectionId = 100,
string connectionName = "PLC-1",
bool visible = true,
IEnumerable<(int Id, string Label)>? siteOptions = null,
EventCallback? onMoved = null,
EventCallback<bool>? visibleChanged = null)
{
siteOptions ??= new[] { (2, "Plant-B"), (3, "Plant-C") };
return Render<MoveDataConnectionDialog>(p => p
.Add(d => d.IsVisible, visible)
.Add(d => d.ConnectionId, connectionId)
.Add(d => d.ConnectionName, connectionName)
.Add(d => d.SiteOptions, siteOptions)
.Add(d => d.OnMoved, onMoved ?? default)
.Add(d => d.IsVisibleChanged, visibleChanged ?? default));
}
[Fact]
public void Visible_RendersTargetSitePicker_WithSuppliedOptions()
{
// (a) The dialog opens with a site picker whose options are exactly the
// supplied target sites (the page excludes the connection's current site).
var cut = RenderDialog(siteOptions: new[] { (2, "Plant-B"), (3, "Plant-C") });
var options = cut.FindAll("select option");
Assert.Contains(options, o => o.TextContent.Contains("Plant-B"));
Assert.Contains(options, o => o.TextContent.Contains("Plant-C"));
// Picker reflects the connection name in the header.
Assert.Contains("PLC-1", cut.Markup);
}
[Fact]
public void Hidden_RendersNothing()
{
var cut = RenderDialog(visible: false);
Assert.Empty(cut.Markup.Trim());
}
[Fact]
public void Confirm_DispatchesMoveCommand_WithConnectionAndTargetSiteIds()
{
// (b) Confirming dispatches the move through the management-dispatch service
// with the connection id + the selected target site id.
_service.MoveAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(DataConnectionMoveResult.Ok()));
var cut = RenderDialog(connectionId: 100, siteOptions: new[] { (2, "Plant-B"), (3, "Plant-C") });
// Pick Plant-C (id 3) and confirm.
cut.Find("select").Change("3");
cut.FindAll("button").First(b => b.TextContent.Contains("Move")).Click();
_service.Received(1).MoveAsync(100, 3, Arg.Any<CancellationToken>());
}
[Fact]
public void GuardError_IsShownInline_AndDialogStaysOpen()
{
// (c) A guard-error response is rendered inline and the dialog does NOT close
// (IsVisibleChanged is not raised with false; OnMoved is not raised).
const string guardError =
"Cannot move data connection 'PLC-1' (ID 100): it is referenced by 1 instance binding(s).";
_service.MoveAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(DataConnectionMoveResult.Fail(guardError)));
var closed = false;
var moved = false;
var cut = RenderDialog(
connectionId: 100,
siteOptions: new[] { (2, "Plant-B") },
onMoved: EventCallback.Factory.Create(this, () => moved = true),
visibleChanged: EventCallback.Factory.Create<bool>(this, v => { if (!v) closed = true; }));
cut.Find("select").Change("2");
cut.FindAll("button").First(b => b.TextContent.Contains("Move")).Click();
Assert.Contains(guardError, cut.Markup);
Assert.False(closed, "Dialog must stay open on a guard error.");
Assert.False(moved, "OnMoved must not fire on a guard error.");
}
[Fact]
public void Success_ClosesDialog_AndRaisesRefreshSignal()
{
_service.MoveAsync(Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(DataConnectionMoveResult.Ok()));
var closed = false;
var moved = false;
var cut = RenderDialog(
connectionId: 100,
siteOptions: new[] { (2, "Plant-B") },
onMoved: EventCallback.Factory.Create(this, () => moved = true),
visibleChanged: EventCallback.Factory.Create<bool>(this, v => { if (!v) closed = true; }));
cut.Find("select").Change("2");
cut.FindAll("button").First(b => b.TextContent.Contains("Move")).Click();
Assert.True(closed, "Dialog must close on success.");
Assert.True(moved, "OnMoved must fire on success to refresh the tree.");
}
}