fix(adminui): stale-open guards, validation summaries, key-delete confirm, error boundary (#604)
Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Raw;
|
||||
|
||||
/// <summary>
|
||||
/// Issue #604 source-scan guards (Razor has no bUnit): an <c>EditForm</c> with a validator but nothing
|
||||
/// to show its messages is a silent no-op Save, the layout must keep its error boundary, and the
|
||||
/// API-key Delete must not fire straight from the row button.
|
||||
/// </summary>
|
||||
public sealed class AdminUiFormSweepGuardTests
|
||||
{
|
||||
/// <summary>EditForm pages whose validation messages are rendered by a child/section component.</summary>
|
||||
private static readonly HashSet<string> AllowList = new(StringComparer.Ordinal);
|
||||
|
||||
[Fact]
|
||||
public void Every_razor_with_an_EditForm_and_a_DataAnnotationsValidator_renders_validation_messages()
|
||||
{
|
||||
var offenders = new List<string>();
|
||||
foreach (var file in Directory.EnumerateFiles(ComponentsDir(), "*.razor", SearchOption.AllDirectories))
|
||||
{
|
||||
var text = File.ReadAllText(file);
|
||||
if (!text.Contains("<EditForm") || !text.Contains("<DataAnnotationsValidator")) { continue; }
|
||||
if (text.Contains("<ValidationSummary") || text.Contains("<ValidationMessage")) { continue; }
|
||||
var name = Path.GetRelativePath(ComponentsDir(), file).Replace('\\', '/');
|
||||
if (!AllowList.Contains(name)) { offenders.Add(name); }
|
||||
}
|
||||
|
||||
offenders.ShouldBeEmpty("these forms validate but never show a message: " + string.Join(", ", offenders));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MainLayout_wraps_the_body_in_an_ErrorBoundary()
|
||||
{
|
||||
var text = File.ReadAllText(Path.Combine(ComponentsDir(), "Layout", "MainLayout.razor"));
|
||||
var boundary = text.IndexOf("<ErrorBoundary", StringComparison.Ordinal);
|
||||
boundary.ShouldBeGreaterThan(-1);
|
||||
text.IndexOf("@Body", StringComparison.Ordinal).ShouldBeGreaterThan(boundary);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ApiKeys_delete_button_goes_through_the_confirm_dialog()
|
||||
{
|
||||
var text = File.ReadAllText(Path.Combine(ComponentsDir(), "Pages", "ApiKeys.razor"));
|
||||
text.ShouldContain("<RawConfirmDialog");
|
||||
text.ShouldNotContain("DeleteAsync(k.ApiKeyId)");
|
||||
}
|
||||
|
||||
private static string ComponentsDir()
|
||||
{
|
||||
var dir = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (dir is not null)
|
||||
{
|
||||
var candidate = Path.Combine(dir.FullName, "src", "Server", "ZB.MOM.WW.OtOpcUa.AdminUI", "Components");
|
||||
if (Directory.Exists(candidate)) { return candidate; }
|
||||
dir = dir.Parent;
|
||||
}
|
||||
throw new DirectoryNotFoundException("AdminUI Components directory not found above the test binaries.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.RenderTree;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Raw;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Raw;
|
||||
|
||||
/// <summary>
|
||||
/// Issue #604: the <c>_openGeneration</c> stale-open guard (already on <c>RawBrowseModal</c> /
|
||||
/// <c>RawManualTagEntryModal</c>) on the five modals that lacked it. Each test opens the modal onto
|
||||
/// target A with the async resolve held open, closes, reopens onto target B, then lets A's resolve
|
||||
/// finish: A's continuation must not write A's state under B's displayed modal.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// AdminUI has no bUnit, so components are driven as plain classes (reflection for the injected
|
||||
/// properties, <c>[Parameter]</c>s and private state — same harness as
|
||||
/// <see cref="RawBrowseModalStaleOpenTests"/>). Every test releases the stale resolve LAST and asserts
|
||||
/// the live open's value survived; reverting the guard makes the stale value win.
|
||||
/// </remarks>
|
||||
#pragma warning disable BL0006
|
||||
public sealed class ModalStaleOpenGuardTests : IDisposable
|
||||
{
|
||||
private readonly List<InertRenderer> _renderers = new();
|
||||
|
||||
[Fact]
|
||||
public async Task CsvImport_stale_group_resolve_never_retargets_the_live_open()
|
||||
{
|
||||
var dbName = SeedGroup("TG-A", "dev-A", "GroupA");
|
||||
var gate = new TaskCompletionSource();
|
||||
var modal = Make<RawCsvImportModal>(
|
||||
("ExportReader", new RawTagCsvExportReader(new GatedFactory(dbName, gate.Task))));
|
||||
|
||||
SetParams(modal, ("Visible", true), ("DeviceId", ""), ("TagGroupId", "TG-A"));
|
||||
var stale = Open(modal);
|
||||
|
||||
SetParams(modal, ("Visible", false));
|
||||
await Open(modal);
|
||||
SetParams(modal, ("Visible", true), ("DeviceId", ""), ("TagGroupId", null));
|
||||
await Open(modal);
|
||||
|
||||
gate.SetResult();
|
||||
await stale;
|
||||
|
||||
Field<string?>(modal, "_groupPrefix").ShouldBeNull();
|
||||
Field<string>(modal, "_effectiveDeviceId").ShouldBe("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CsvExport_stale_group_resolve_never_retargets_the_live_open()
|
||||
{
|
||||
var dbName = SeedGroup("TG-A", "dev-A", "GroupA");
|
||||
var gate = new TaskCompletionSource();
|
||||
var modal = Make<RawCsvExportModal>(
|
||||
("ExportReader", new RawTagCsvExportReader(new GatedFactory(dbName, gate.Task))));
|
||||
|
||||
SetParams(modal, ("Visible", true), ("DeviceId", ""), ("TagGroupId", "TG-A"));
|
||||
var stale = Open(modal);
|
||||
|
||||
SetParams(modal, ("Visible", false));
|
||||
await Open(modal);
|
||||
SetParams(modal, ("Visible", true), ("DeviceId", ""), ("TagGroupId", null));
|
||||
await Open(modal);
|
||||
|
||||
gate.SetResult();
|
||||
await stale;
|
||||
|
||||
Field<string?>(modal, "_groupFilter").ShouldBeNull();
|
||||
Field<int>(modal, "_rowCount").ShouldBe(0);
|
||||
Field<string>(modal, "_dataUri").ShouldBe("");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeviceModal_stale_load_never_installs_the_old_devices_fields()
|
||||
{
|
||||
var gate = new TaskCompletionSource();
|
||||
var svc = Stub<IRawTreeService>(async (method, args) =>
|
||||
{
|
||||
if (method.Name != nameof(IRawTreeService.LoadDeviceForEditAsync)) { return null; }
|
||||
if ((string)args[0]! == "dev-A")
|
||||
{
|
||||
await gate.Task;
|
||||
return new RawDeviceEditDto("dev-A", "drv", "NameA", "{}", true, "Modbus", []);
|
||||
}
|
||||
return new RawDeviceEditDto("dev-B", "", "NameB", "{}", true, "Modbus", []);
|
||||
});
|
||||
var modal = Make<DeviceModal>(("Svc", svc));
|
||||
|
||||
SetParams(modal, ("Visible", true), ("DeviceId", "dev-A"));
|
||||
var stale = Open(modal);
|
||||
|
||||
SetParams(modal, ("Visible", false));
|
||||
await Open(modal);
|
||||
SetParams(modal, ("Visible", true), ("DeviceId", "dev-B"));
|
||||
await Open(modal);
|
||||
|
||||
gate.SetResult();
|
||||
await stale;
|
||||
|
||||
Field<string>(modal, "_name").ShouldBe("NameB");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DriverConfigModal_stale_load_never_installs_the_old_drivers_fields()
|
||||
{
|
||||
var gate = new TaskCompletionSource();
|
||||
var svc = Stub<IRawTreeService>(async (method, args) =>
|
||||
{
|
||||
if (method.Name != nameof(IRawTreeService.LoadDriverForEditAsync)) { return null; }
|
||||
if ((string)args[0]! == "drv-A")
|
||||
{
|
||||
await gate.Task;
|
||||
return new RawDriverEditDto("drv-A", "NameA", "Modbus", """{"a":1}""", null, []);
|
||||
}
|
||||
return new RawDriverEditDto("drv-B", "NameB", "Modbus", """{"b":2}""", null, []);
|
||||
});
|
||||
var modal = Make<DriverConfigModal>(("Svc", svc));
|
||||
|
||||
SetParams(modal, ("Visible", true), ("DriverInstanceId", "drv-A"));
|
||||
var stale = Open(modal);
|
||||
|
||||
SetParams(modal, ("Visible", false));
|
||||
await Open(modal);
|
||||
SetParams(modal, ("Visible", true), ("DriverInstanceId", "drv-B"));
|
||||
await Open(modal);
|
||||
|
||||
gate.SetResult();
|
||||
await stale;
|
||||
|
||||
Field<string>(modal, "_name").ShouldBe("NameB");
|
||||
Field<string>(modal, "_driverConfigJson").ShouldBe("""{"b":2}""");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddReferenceModal_stale_root_load_never_replaces_the_live_opens_tree()
|
||||
{
|
||||
var gate = new TaskCompletionSource();
|
||||
var svc = Stub<IUnsTreeService>(async (method, args) =>
|
||||
{
|
||||
if (method.Name != nameof(IUnsTreeService.LoadReferencePickerRootAsync)) { return null; }
|
||||
if ((string)args[0]! == "eq-A")
|
||||
{
|
||||
await gate.Task;
|
||||
return Root("root-A");
|
||||
}
|
||||
return Root("root-B");
|
||||
});
|
||||
var modal = Make<AddReferenceModal>(("Svc", svc));
|
||||
|
||||
SetParams(modal, ("Visible", true), ("EquipmentId", "eq-A"));
|
||||
var stale = Open(modal);
|
||||
|
||||
SetParams(modal, ("Visible", false));
|
||||
await Open(modal);
|
||||
SetParams(modal, ("Visible", true), ("EquipmentId", "eq-B"));
|
||||
await Open(modal);
|
||||
|
||||
gate.SetResult();
|
||||
await stale;
|
||||
|
||||
Field<IReadOnlyList<RawNode>>(modal, "_roots").Single().Key.ShouldBe("root-B");
|
||||
}
|
||||
|
||||
// ---- harness ----
|
||||
|
||||
private static RawNode Root(string key)
|
||||
=> new() { Kind = RawNodeKind.Cluster, Key = key, DisplayName = key };
|
||||
|
||||
private static string SeedGroup(string tagGroupId, string deviceId, string name)
|
||||
{
|
||||
var dbName = $"modalstale-{Guid.NewGuid():N}";
|
||||
using var db = RawTreeTestDb.CreateNamed(dbName);
|
||||
db.TagGroups.Add(new TagGroup { TagGroupId = tagGroupId, DeviceId = deviceId, Name = name });
|
||||
db.SaveChanges();
|
||||
return dbName;
|
||||
}
|
||||
|
||||
private T Make<T>(params (string Name, object Value)[] injected) where T : class, IComponent, new()
|
||||
{
|
||||
var component = new T();
|
||||
foreach (var (name, value) in injected)
|
||||
{
|
||||
var property = typeof(T).GetProperty(name, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
property.ShouldNotBeNull($"the injected {name} property was renamed or removed.");
|
||||
property!.SetValue(component, value);
|
||||
}
|
||||
var renderer = new InertRenderer();
|
||||
_renderers.Add(renderer);
|
||||
renderer.Attach(component);
|
||||
return component;
|
||||
}
|
||||
|
||||
private static void SetParams(IComponent modal, params (string Name, object? Value)[] values)
|
||||
{
|
||||
foreach (var (name, value) in values)
|
||||
{
|
||||
var property = modal.GetType().GetProperty(name);
|
||||
property.ShouldNotBeNull($"the [Parameter] {name} was renamed or removed.");
|
||||
property!.SetValue(modal, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task Open(IComponent modal)
|
||||
{
|
||||
var method = modal.GetType().GetMethod(
|
||||
"OnParametersSetAsync", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
method.ShouldNotBeNull("the open path moved off OnParametersSetAsync.");
|
||||
await (Task)method!.Invoke(modal, null)!;
|
||||
}
|
||||
|
||||
private static T Field<T>(object modal, string name)
|
||||
{
|
||||
var field = modal.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
field.ShouldNotBeNull($"the modal's {name} field was renamed or removed.");
|
||||
return (T)field!.GetValue(modal)!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A throwaway implementation of a service interface: every call goes to <paramref name="handler"/>,
|
||||
/// whose result is wrapped as the method's <c>Task<T></c>. A null result is a null payload.
|
||||
/// </summary>
|
||||
private static TService Stub<TService>(Func<MethodInfo, object?[], Task<object?>> handler)
|
||||
where TService : class
|
||||
{
|
||||
var proxy = DispatchProxy.Create<TService, StubProxy>();
|
||||
((StubProxy)(object)proxy).Handler = handler;
|
||||
return proxy;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var renderer in _renderers) { renderer.Dispose(); }
|
||||
}
|
||||
|
||||
/// <summary>DispatchProxy body for <see cref="Stub{TService}"/>.</summary>
|
||||
public class StubProxy : DispatchProxy
|
||||
{
|
||||
/// <summary>Receives every call; set right after construction.</summary>
|
||||
public Func<MethodInfo, object?[], Task<object?>> Handler { get; set; } = default!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
|
||||
{
|
||||
var inner = targetMethod!.ReturnType.GetGenericArguments()[0];
|
||||
var task = Handler(targetMethod, args ?? []);
|
||||
var tcs = typeof(TaskCompletionSource<>).MakeGenericType(inner);
|
||||
var source = Activator.CreateInstance(tcs)!;
|
||||
task.ContinueWith(t =>
|
||||
{
|
||||
if (t.IsFaulted) { tcs.GetMethod("SetException", [typeof(Exception)])!.Invoke(source, [t.Exception!.GetBaseException()]); }
|
||||
else { tcs.GetMethod("SetResult")!.Invoke(source, [t.Result]); }
|
||||
});
|
||||
return tcs.GetProperty("Task")!.GetValue(source);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class GatedFactory(string dbName, Task gate) : IDbContextFactory<OtOpcUaConfigDbContext>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public OtOpcUaConfigDbContext CreateDbContext() => RawTreeTestDb.CreateNamed(dbName);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken ct = default)
|
||||
{
|
||||
await gate;
|
||||
return CreateDbContext();
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class InertRenderer()
|
||||
: Renderer(new ServiceCollection().BuildServiceProvider(), NullLoggerFactory.Instance)
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override Dispatcher Dispatcher { get; } = new InlineDispatcher();
|
||||
|
||||
/// <summary>Assigns the component a render handle, as a root component would receive.</summary>
|
||||
public void Attach(IComponent component) => AssignRootComponentId(component);
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void HandleException(Exception exception)
|
||||
=> ExceptionDispatchInfo.Capture(exception).Throw();
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
private sealed class InlineDispatcher : Dispatcher
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public override bool CheckAccess() => true;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Task InvokeAsync(Action workItem)
|
||||
{
|
||||
workItem();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Task InvokeAsync(Func<Task> workItem) => workItem();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Task<TResult> InvokeAsync<TResult>(Func<TResult> workItem)
|
||||
=> Task.FromResult(workItem());
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> workItem) => workItem();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user