diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/DataConnections.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/DataConnections.razor index b5749142..7e23f80c 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/DataConnections.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/DataConnections.razor @@ -42,12 +42,6 @@ - - @if (_loading) { @@ -425,37 +419,37 @@ } } - // ── M9-T24b: Move connection to another site ── - // The dialog dispatches MoveDataConnectionCommand through the guard-running - // ManagementActor path (IDataConnectionMoveService) — NOT a direct repository - // write — so the server enforces the Designer gate and every move guard. The - // page only opens the dialog, supplies the candidate target sites (the current - // site excluded), and reloads the tree once the move succeeds. - private bool _showMoveDialog; - private int _moveConnectionId; - private int _moveConnectionSiteId; - private string _moveConnectionName = string.Empty; - - private void OpenMoveDialog(DataConnection conn) + // ── M9-T24b / T33b: Move connection to another site ── + // Opened via IDialogService.ShowAsync. The body dispatches MoveDataConnectionCommand + // through the guard-running ManagementActor path (IDataConnectionMoveService) — NOT + // a direct repository write — so the server enforces the Designer gate and every + // move guard, showing any guard error inline and staying open. On success the body + // closes with true; the page then toasts and reloads the tree. + private async Task OpenMoveDialog(DataConnection conn) { - _moveConnectionId = conn.Id; - _moveConnectionSiteId = conn.SiteId; - _moveConnectionName = conn.Name; - _showMoveDialog = true; + var connId = conn.Id; + var connName = conn.Name; + var options = MoveTargetSiteOptions(conn.SiteId); + var moved = await Dialog.ShowAsync( + $"Move '{connName}' to site…", + ctx => @); + if (moved) + { + _toast.ShowSuccess($"Connection '{connName}' moved."); + await LoadDataAsync(); + } } // Candidate target sites for the move: every site EXCEPT the connection's // current one. Sourced from the already-loaded tree roots (each root is a site). - private IEnumerable<(int Id, string Label)> MoveTargetSiteOptions() => + private IEnumerable<(int Id, string Label)> MoveTargetSiteOptions(int currentSiteId) => _treeRoots - .Where(r => r.SiteId is int sid && sid != _moveConnectionSiteId) - .Select(r => (r.SiteId!.Value, r.Label)); - - private async Task OnConnectionMoved() - { - _toast.ShowSuccess($"Connection '{_moveConnectionName}' moved."); - await LoadDataAsync(); - } + .Where(r => r.SiteId is int sid && sid != currentSiteId) + .Select(r => (r.SiteId!.Value, r.Label)) + .ToList(); // M9-T25: enum → Bootstrap badge class. Mirrors the Health dashboard's // GetConnectionHealthBadge (Components/Pages/Monitoring/Health.razor) so the diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/Templates.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/Templates.razor index 8c0d4a2e..b91fa4f6 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/Templates.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Design/Templates.razor @@ -17,33 +17,6 @@
- - - - - - - - @if (_loading) { @@ -314,34 +287,26 @@ } } - // Move-template dialog state - private bool _showMoveTemplateDialog; - private int _moveTemplateId; - private string _moveTemplateName = string.Empty; - private string? _moveTemplateError; - - private void OpenMoveTemplateDialog(int templateId, string label) + // Move-template dialog: opened via IDialogService.ShowAsync. The body returns + // the picked folder id (null = Root) on Move, or null on Cancel; validation runs + // here after the dialog closes and surfaces server guard failures via a toast. + private async Task OpenMoveTemplateDialog(int templateId, string label) { - _moveTemplateId = templateId; - _moveTemplateName = label; - _moveTemplateError = null; - _showMoveTemplateDialog = true; - } + var result = await Dialog.ShowAsync( + $"Move '{label}' to…", + ctx => @); + if (result is null) return; - private async Task SubmitMoveTemplate((int TemplateId, int? NewFolderId) req) - { - _moveTemplateError = null; var user = await GetCurrentUserAsync(); - var result = await TemplateService.MoveTemplateAsync(req.TemplateId, req.NewFolderId, user); - if (result.IsSuccess) + var moved = await TemplateService.MoveTemplateAsync(templateId, result.NewFolderId, user); + if (moved.IsSuccess) { - _showMoveTemplateDialog = false; - _toast.ShowSuccess($"Template '{_moveTemplateName}' moved."); + _toast.ShowSuccess($"Template '{label}' moved."); await LoadTemplatesAsync(); } else { - _moveTemplateError = result.Error; + _toast.ShowError(moved.Error); } } @@ -373,34 +338,27 @@ } } - // Move-folder dialog state - private bool _showMoveFolderDialog; - private int _moveFolderId; - private string _moveFolderName = string.Empty; - private string? _moveFolderError; - - private void OpenMoveFolderDialog(int folderId, string label) + // Move-folder dialog: opened via IDialogService.ShowAsync. The picker prunes the + // folder + its descendants (server still validates cycles). The body returns the + // picked parent id (null = Root) on Move, or null on Cancel; server guard failures + // surface via a toast. + private async Task OpenMoveFolderDialog(int folderId, string label) { - _moveFolderId = folderId; - _moveFolderName = label; - _moveFolderError = null; - _showMoveFolderDialog = true; - } + var result = await Dialog.ShowAsync( + $"Move '{label}' to…", + ctx => @); + if (result is null) return; - private async Task SubmitMoveFolder((int FolderId, int? NewParentId) req) - { - _moveFolderError = null; var user = await GetCurrentUserAsync(); - var result = await TemplateFolderService.MoveFolderAsync(req.FolderId, req.NewParentId, user); - if (result.IsSuccess) + var moved = await TemplateFolderService.MoveFolderAsync(folderId, result.NewParentId, user); + if (moved.IsSuccess) { - _showMoveFolderDialog = false; - _toast.ShowSuccess($"Folder '{_moveFolderName}' moved."); + _toast.ShowSuccess($"Folder '{label}' moved."); await LoadTemplatesAsync(); } else { - _moveFolderError = result.Error; + _toast.ShowError(moved.Error); } } @@ -467,34 +425,27 @@ private void DismissRootContextMenu() => _showRootMenu = false; - // Rename folder dialog state - private bool _showRenameFolderDialog; - private int _renameFolderId; - private string _renameFolderInitialName = string.Empty; - private string? _renameFolderError; - - private void OpenRenameFolderDialog(int folderId, string currentName) + // Rename folder dialog: opened via IDialogService.ShowAsync. The body seeds the + // input from the current name and returns the trimmed new name on Save, or null on + // Cancel; server guard failures surface via a toast. + private async Task OpenRenameFolderDialog(int folderId, string currentName) { - _renameFolderId = folderId; - _renameFolderInitialName = currentName; - _renameFolderError = null; - _showRenameFolderDialog = true; - } + var newName = await Dialog.ShowAsync( + "Rename Folder", + ctx => @, + size: "modal-sm"); + if (string.IsNullOrWhiteSpace(newName)) return; - private async Task SubmitRenameFolder((int FolderId, string NewName) req) - { - _renameFolderError = null; var user = await GetCurrentUserAsync(); - var result = await TemplateFolderService.RenameFolderAsync(req.FolderId, req.NewName, user); + var result = await TemplateFolderService.RenameFolderAsync(folderId, newName, user); if (result.IsSuccess) { - _showRenameFolderDialog = false; _toast.ShowSuccess("Folder renamed."); await LoadTemplatesAsync(); } else { - _renameFolderError = result.Error; + _toast.ShowError(result.Error); } } @@ -545,17 +496,31 @@ } // ---- Compose-into dialog ---- - private bool _showComposeDialog; - private int _composeSourceId; - private string _composeSourceName = string.Empty; - private string? _composeError; - - private void OpenComposeDialog(Template source) + // Opened via IDialogService.ShowAsync. The body returns the chosen parent template + // + slot name on Compose (its own client-side guard keeps the button disabled until + // both are set), or null on Cancel; server guard failures surface via a toast. + private async Task OpenComposeDialog(Template source) { - _composeSourceId = source.Id; - _composeSourceName = source.Name; - _composeError = null; - _showComposeDialog = true; + var sourceId = source.Id; + var sourceName = source.Name; + var result = await Dialog.ShowAsync( + $"Compose '{sourceName}' into…", + ctx => @); + if (result is null) return; + + var user = await GetCurrentUserAsync(); + var composed = await TemplateService.AddCompositionAsync(result.ParentTemplateId, sourceId, result.SlotName, user); + if (composed.IsSuccess) + { + _toast.ShowSuccess($"Composed '{sourceName}' as '{result.SlotName}'."); + await LoadTemplatesAsync(); + } + else + { + _toast.ShowError(composed.Error); + } } // Possible parents for a compose: every non-derived template except the source itself. @@ -568,23 +533,6 @@ .Select(t => (t.Id, t.Name)); } - private async Task SubmitCompose((int SourceTemplateId, int ParentTemplateId, string SlotName) req) - { - _composeError = null; - var user = await GetCurrentUserAsync(); - var result = await TemplateService.AddCompositionAsync(req.ParentTemplateId, req.SourceTemplateId, req.SlotName, user); - if (result.IsSuccess) - { - _showComposeDialog = false; - _toast.ShowSuccess($"Composed '{_composeSourceName}' as '{req.SlotName}'."); - await LoadTemplatesAsync(); - } - else - { - _composeError = result.Error; - } - } - // ---- Composition leaf: rename + delete ---- private async Task RenameComposition(TemplateComposition composition) { diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/ComposeIntoDialog.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/ComposeIntoDialog.razor index 3e6bb1fe..88df7df5 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/ComposeIntoDialog.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/ComposeIntoDialog.razor @@ -1,68 +1,57 @@ -@if (IsVisible) -{ - -} +@* + T33b: body component for the "Compose into" dialog hosted by + IDialogService.ShowAsync. Renders ONLY the parent/slot fields + action buttons + inside the host's .modal-body; the host owns the backdrop, header, and focus + trap. Closes with a ComposeResult (parent template id + slot name); Cancel + resolves to null. The Compose button stays disabled until both a parent and a + non-blank slot are chosen (client-side guard, preserved from the original). + Server-side compose failures are surfaced by the parent via a toast. +*@ +
+ + +
+
+ + +
+ + @code { - [Parameter] public bool IsVisible { get; set; } - [Parameter] public EventCallback IsVisibleChanged { get; set; } - [Parameter] public int SourceTemplateId { get; set; } + /// Host-supplied context used to close (with the chosen parent + slot) or cancel. + [Parameter] public DialogContext Context { get; set; } = default!; [Parameter] public string SourceName { get; set; } = string.Empty; [Parameter] public IEnumerable<(int Id, string Label)> ParentOptions { get; set; } = Array.Empty<(int, string)>(); - [Parameter] public string? ErrorMessage { get; set; } - [Parameter] public EventCallback<(int SourceTemplateId, int ParentTemplateId, string SlotName)> OnSubmit { get; set; } - private bool _wasVisible; private int _parentTemplateId; private string _slotName = string.Empty; - protected override void OnParametersSet() - { - if (IsVisible && !_wasVisible) - { - _parentTemplateId = 0; - _slotName = SourceName; - } - _wasVisible = IsVisible; - } + protected override void OnInitialized() => _slotName = SourceName; - private async Task Close() => await IsVisibleChanged.InvokeAsync(false); - - private async Task Submit() + private void Submit() { if (_parentTemplateId == 0 || string.IsNullOrWhiteSpace(_slotName)) return; - await OnSubmit.InvokeAsync((SourceTemplateId, _parentTemplateId, _slotName.Trim())); + Context.Close(new ComposeResult(_parentTemplateId, _slotName.Trim())); } + + /// + /// Result of the compose picker: the parent template to compose into and the + /// slot name. ShowAsync resolves to this record on Compose, or null on Cancel. + /// + /// The template the source is composed into. + /// The trimmed slot (composition instance) name. + public sealed record ComposeResult(int ParentTemplateId, string SlotName); } diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveDataConnectionDialog.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveDataConnectionDialog.razor index dba97fa4..ddf94b80 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveDataConnectionDialog.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveDataConnectionDialog.razor @@ -2,84 +2,55 @@ @inject IDataConnectionMoveService MoveService @* - M9-T24b: Move a data connection to another site. The picker lists the candidate - target sites (the page excludes the connection's current site). On confirm the - dialog dispatches MoveDataConnectionCommand through IDataConnectionMoveService — - the guard-running ManagementActor path, NOT a direct repository write — so the - server's Designer gate and every move guard (target exists, no name collision, no - instance binding, no native-alarm-source name reference) run. A guard error is - shown inline and the dialog stays open; success closes the dialog and raises - OnMoved so the page reloads the tree. Mirrors the MoveFolderDialog idiom. + M9-T24b / T33b: body component for the "Move data connection" dialog hosted by + IDialogService.ShowAsync. Renders ONLY the site picker + action buttons inside + the host's .modal-body; the host owns the backdrop, header, and focus trap. + + The body still injects IDataConnectionMoveService and dispatches the + MoveDataConnectionCommand through the guard-running ManagementActor path (NOT a + direct repository write) so the server's Designer gate and every move guard run. + A guard error is shown inline and the dialog STAYS OPEN; success closes the + dialog with Close(true) so the parent reloads the tree. Cancel resolves to false. *@ -@if (IsVisible) +@if (SiteOptions.Any()) { - + +} +else +{ +
No other site is available to move this connection to.
+} +@if (!string.IsNullOrEmpty(_error)) +{ +
@_error
} + + @code { - [Parameter] public bool IsVisible { get; set; } - [Parameter] public EventCallback IsVisibleChanged { get; set; } + /// Host-supplied context: Close(true) on a successful move, Cancel on dismiss. + [Parameter] public DialogContext Context { get; set; } = default!; [Parameter] public int ConnectionId { get; set; } [Parameter] public string ConnectionName { get; set; } = string.Empty; [Parameter] public IEnumerable<(int Id, string Label)> SiteOptions { get; set; } = Array.Empty<(int, string)>(); - /// Raised after a successful move so the page can reload the tree. - [Parameter] public EventCallback OnMoved { get; set; } - - private bool _wasVisible; private int? _targetSiteId; private string? _error; private bool _busy; - protected override void OnParametersSet() + protected override void OnInitialized() { - // Reset internal state on transition from hidden -> visible: default the - // picker to the first candidate site and clear any prior error. - if (IsVisible && !_wasVisible) - { - _targetSiteId = SiteOptions.Select(o => (int?)o.Id).FirstOrDefault(); - _error = null; - _busy = false; - } - _wasVisible = IsVisible; - } - - private async Task Close() - { - await IsVisibleChanged.InvokeAsync(false); + // Default the picker to the first candidate site. + _targetSiteId = SiteOptions.Select(o => (int?)o.Id).FirstOrDefault(); } private async Task Submit() @@ -94,8 +65,9 @@ if (result.Success) { - await IsVisibleChanged.InvokeAsync(false); - await OnMoved.InvokeAsync(); + // Success closes the dialog; the parent's post-ShowAsync block toasts + // and reloads the tree. + Context.Close(true); } else { diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveFolderDialog.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveFolderDialog.razor index cb2d7a88..7cb5eb0d 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveFolderDialog.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveFolderDialog.razor @@ -1,58 +1,38 @@ -@if (IsVisible) -{ - -} +@* + T33b: body component for the "Move folder" dialog hosted by + IDialogService.ShowAsync. Renders ONLY the picker + action buttons inside the + host's .modal-body; the host owns the backdrop, header, and focus trap. The + body closes with a MoveFolderResult carrying the picked parent id (null = Root, + which is why a result record is used: Close(null) would otherwise be + indistinguishable from a Cancel). The parent validates after the dialog closes + and toasts any server guard failure. +*@ + + + @code { - [Parameter] public bool IsVisible { get; set; } - [Parameter] public EventCallback IsVisibleChanged { get; set; } - [Parameter] public int FolderId { get; set; } - [Parameter] public string FolderName { get; set; } = string.Empty; + /// Host-supplied context used to close (with the picked parent id) or cancel. + [Parameter] public DialogContext Context { get; set; } = default!; [Parameter] public IEnumerable<(int? Id, string Label)> FolderOptions { get; set; } = Array.Empty<(int?, string)>(); - [Parameter] public string? ErrorMessage { get; set; } - [Parameter] public EventCallback<(int FolderId, int? NewParentId)> OnSubmit { get; set; } - private bool _wasVisible; private int? _targetParentId; - protected override void OnParametersSet() - { - if (IsVisible && !_wasVisible) - { - _targetParentId = null; - } - _wasVisible = IsVisible; - } + private void Submit() => Context.Close(new MoveFolderResult(_targetParentId)); - private async Task Close() - { - await IsVisibleChanged.InvokeAsync(false); - } - - private async Task Submit() - { - await OnSubmit.InvokeAsync((FolderId, _targetParentId)); - } + /// + /// Result of the move-folder picker. A reference type so a non-null close is + /// distinguishable from a cancel even when the picked parent is null (Root): + /// ShowAsync resolves to this record on Move and to null on Cancel. + /// + /// The chosen parent folder id, or null for Root. + public sealed record MoveFolderResult(int? NewParentId); } diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveTemplateDialog.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveTemplateDialog.razor index 5980ceb2..5224e474 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveTemplateDialog.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/MoveTemplateDialog.razor @@ -1,59 +1,36 @@ -@if (IsVisible) -{ - -} +@* + T33b: body component for the "Move template" dialog hosted by + IDialogService.ShowAsync. Renders ONLY the picker + action buttons inside the + host's .modal-body; the host owns the backdrop, header, and focus trap. Closes + with a MoveTemplateResult carrying the picked folder id (null = Root) — a result + record so Close(null) for the Root choice stays distinguishable from a Cancel. +*@ + + + @code { - [Parameter] public bool IsVisible { get; set; } - [Parameter] public EventCallback IsVisibleChanged { get; set; } - [Parameter] public int TemplateId { get; set; } - [Parameter] public string TemplateName { get; set; } = string.Empty; + /// Host-supplied context used to close (with the picked folder id) or cancel. + [Parameter] public DialogContext Context { get; set; } = default!; [Parameter] public IEnumerable<(int? Id, string Label)> FolderOptions { get; set; } = Array.Empty<(int?, string)>(); - [Parameter] public string? ErrorMessage { get; set; } - [Parameter] public EventCallback<(int TemplateId, int? NewFolderId)> OnSubmit { get; set; } - private bool _wasVisible; private int? _targetFolderId; - protected override void OnParametersSet() - { - // Reset internal state on transition from hidden -> visible. - if (IsVisible && !_wasVisible) - { - _targetFolderId = null; - } - _wasVisible = IsVisible; - } + private void Submit() => Context.Close(new MoveTemplateResult(_targetFolderId)); - private async Task Close() - { - await IsVisibleChanged.InvokeAsync(false); - } - - private async Task Submit() - { - await OnSubmit.InvokeAsync((TemplateId, _targetFolderId)); - } + /// + /// Result of the move-template picker. A reference type so a non-null close is + /// distinguishable from a cancel even when the picked folder is null (Root): + /// ShowAsync resolves to this record on Move and to null on Cancel. + /// + /// The chosen destination folder id, or null for Root. + public sealed record MoveTemplateResult(int? NewFolderId); } diff --git a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/RenameFolderDialog.razor b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/RenameFolderDialog.razor index f2603c43..e74cc4bb 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/RenameFolderDialog.razor +++ b/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Shared/RenameFolderDialog.razor @@ -1,53 +1,31 @@ -@if (IsVisible) -{ - -} +@* + T33b: body component for the "Rename folder" dialog hosted by + IDialogService.ShowAsync. Renders ONLY the input + action buttons inside the + host's .modal-body; the host owns the backdrop, header, and focus trap. Closes + with the trimmed new name (string); Cancel resolves to null. Save is disabled + while the field is blank (client-side guard); server-side rename failures are + surfaced by the parent via a toast. +*@ + + + @code { - [Parameter] public bool IsVisible { get; set; } - [Parameter] public EventCallback IsVisibleChanged { get; set; } - [Parameter] public int FolderId { get; set; } + /// Host-supplied context used to close (with the trimmed name) or cancel. + [Parameter] public DialogContext Context { get; set; } = default!; [Parameter] public string InitialName { get; set; } = string.Empty; - [Parameter] public string? ErrorMessage { get; set; } - [Parameter] public EventCallback<(int FolderId, string NewName)> OnSubmit { get; set; } - private bool _wasVisible; private string _name = string.Empty; - protected override void OnParametersSet() - { - // Reset internal state on transition from hidden -> visible. - if (IsVisible && !_wasVisible) - { - _name = InitialName; - } - _wasVisible = IsVisible; - } + protected override void OnInitialized() => _name = InitialName; - private async Task Close() + private void Submit() { - await IsVisibleChanged.InvokeAsync(false); - } - - private async Task Submit() - { - await OnSubmit.InvokeAsync((FolderId, _name.Trim())); + if (string.IsNullOrWhiteSpace(_name)) return; + Context.Close(_name.Trim()); } } diff --git a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/DataConnectionsPageTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/DataConnectionsPageTests.cs index ab55eadd..b6b51192 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/DataConnectionsPageTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/DataConnectionsPageTests.cs @@ -250,13 +250,21 @@ public class DataConnectionsPageTests : BunitContext }, connections: new[] { new DataConnection("PLC-1", "OpcUa", 1) { Id = 100 } }); + // T33b: the move dialog is now opened via IDialogService.ShowAsync and rendered + // by the shared DialogHost (which lives in MainLayout in production). Render a + // host in the same DI scope so the dialog the page opens is displayed; the host's + // focus-trap JS interop runs as no-ops under loose mode. + JSInterop.Mode = JSRuntimeMode.Loose; + var host = Render(); + var cut = Render(); FindToggleForLabel(cut, "Plant-A")!.Click(); cut.FindAll("button").First(b => b.TextContent.Contains("Move to Site")).Click(); + host.Render(); // The dialog renders with a site picker; the current site is excluded. - var dialog = cut.Find(".modal.show"); + var dialog = host.Find(".modal.show"); var optionLabels = dialog.QuerySelectorAll("select option") .Select(o => o.TextContent).ToList(); Assert.Contains(optionLabels, l => l.Contains("Plant-B")); diff --git a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Shared/MoveDataConnectionDialogTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Shared/MoveDataConnectionDialogTests.cs index 888643db..0631d923 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Shared/MoveDataConnectionDialogTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CentralUI.Tests/Shared/MoveDataConnectionDialogTests.cs @@ -8,65 +8,87 @@ using ZB.MOM.WW.ScadaBridge.CentralUI.Services; namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared; /// -/// bUnit tests for the (M9-T24b). The dialog -/// surfaces a target-site picker (excluding the connection's current site) and, on -/// confirm, dispatches a MoveDataConnectionCommand through the management -/// path via — 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. +/// bUnit tests for the body component +/// (M9-T24b, migrated to the DialogService.ShowAsync host in T33b). The dialog is +/// now opened through : a single +/// DialogHost owns the backdrop/header/focus-trap, and the body renders only +/// the target-site picker (excluding the connection's current site) plus its own +/// action buttons. On confirm the body dispatches a MoveDataConnectionCommand +/// through the management path via — the +/// SAME guard-running seam the server uses. A guard error must be shown inline and +/// the dialog must stay open (the awaited ShowAsync task must NOT complete); a +/// success must close the dialog and resolve the task with true so the page +/// reloads the tree. /// -/// 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. +/// The tests render a real behind a live +/// DialogHost and open the body via ShowAsync<bool> so the +/// host-driven flow is exercised end to end. The move 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. bUnit +/// cannot run real JS interop, so the host's sbDialog.* focus calls run in +/// mode where they become no-ops. /// public class MoveDataConnectionDialogTests : BunitContext { private readonly IDataConnectionMoveService _service = Substitute.For(); + private readonly DialogService _dialog = new(); public MoveDataConnectionDialogTests() { Services.AddSingleton(_service); + // Register the concrete service as the interface so DialogHost (which injects + // IDialogService and casts to DialogService) resolves the same instance the + // test drives. + Services.AddSingleton(_dialog); + JSInterop.Mode = JSRuntimeMode.Loose; } - private IRenderedComponent RenderDialog( + /// + /// Renders a live DialogHost and opens the move dialog via + /// ShowAsync<bool>, returning the rendered host and the pending task + /// the page would await. + /// + private (IRenderedComponent Host, Task Pending) OpenDialog( + IRenderedComponent host, int connectionId = 100, string connectionName = "PLC-1", - bool visible = true, - IEnumerable<(int Id, string Label)>? siteOptions = null, - EventCallback? onMoved = null, - EventCallback? visibleChanged = null) + IEnumerable<(int Id, string Label)>? siteOptions = null) { siteOptions ??= new[] { (2, "Plant-B"), (3, "Plant-C") }; - return Render(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)); + Task pending = null!; + host.InvokeAsync(() => + { + pending = _dialog.ShowAsync( + $"Move '{connectionName}' to site…", + ctx => builder => + { + builder.OpenComponent(0); + builder.AddAttribute(1, nameof(MoveDataConnectionDialog.Context), ctx); + builder.AddAttribute(2, nameof(MoveDataConnectionDialog.ConnectionId), connectionId); + builder.AddAttribute(3, nameof(MoveDataConnectionDialog.ConnectionName), connectionName); + builder.AddAttribute(4, nameof(MoveDataConnectionDialog.SiteOptions), siteOptions); + builder.CloseComponent(); + }); + }).GetAwaiter().GetResult(); + host.Render(); + return (host, pending); } [Fact] - public void Visible_RendersTargetSitePicker_WithSuppliedOptions() + public void ShownViaHost_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") }); + // (a) When opened via ShowAsync, the body renders a site picker whose options + // are exactly the supplied target sites (the page excludes the current site), + // and the connection name appears in the host header title. + var host = Render(); + var (cut, _) = OpenDialog(host, 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() { @@ -75,7 +97,8 @@ public class MoveDataConnectionDialogTests : BunitContext _service.MoveAsync(Arg.Any(), Arg.Any(), Arg.Any()) .Returns(Task.FromResult(DataConnectionMoveResult.Ok())); - var cut = RenderDialog(connectionId: 100, siteOptions: new[] { (2, "Plant-B"), (3, "Plant-C") }); + var host = Render(); + var (cut, _) = OpenDialog(host, connectionId: 100, siteOptions: new[] { (2, "Plant-B"), (3, "Plant-C") }); // Pick Plant-C (id 3) and confirm. cut.Find("select").Change("3"); @@ -87,47 +110,41 @@ public class MoveDataConnectionDialogTests : BunitContext [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). + // (c) A guard-error response is rendered inline and the dialog does NOT close: + // the awaited ShowAsync task stays incomplete (no result resolved). const string guardError = "Cannot move data connection 'PLC-1' (ID 100): it is referenced by 1 instance binding(s)."; _service.MoveAsync(Arg.Any(), Arg.Any(), Arg.Any()) .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(this, v => { if (!v) closed = true; })); + var host = Render(); + var (cut, pending) = OpenDialog(host, connectionId: 100, siteOptions: new[] { (2, "Plant-B") }); 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."); + Assert.False(pending.IsCompleted, "Dialog must stay open on a guard error."); + // The body is still rendered inside the host (not torn down). + Assert.NotNull(cut.Find("[data-test='move-connection-error']")); } [Fact] - public void Success_ClosesDialog_AndRaisesRefreshSignal() + public async Task Success_ClosesDialog_AndResolvesTrue() { + // (d) A successful move closes the dialog and resolves the awaited task with + // true so the page reloads the tree. _service.MoveAsync(Arg.Any(), Arg.Any(), Arg.Any()) .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(this, v => { if (!v) closed = true; })); + var host = Render(); + var (cut, pending) = OpenDialog(host, connectionId: 100, siteOptions: new[] { (2, "Plant-B") }); 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."); + var completed = await Task.WhenAny(pending, Task.Delay(TimeSpan.FromSeconds(2))); + Assert.Same(pending, completed); + Assert.True(await pending, "Dialog must resolve true on success so the page refreshes."); } }