using System.Reflection; using Bunit; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers; using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs; using ZB.MOM.WW.OtOpcUa.Commons.Interfaces; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin; using ZB.MOM.WW.OtOpcUa.Security.Auth; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages; /// Driver control errors remain complete and accessible without hover. [Trait("Category", "Unit")] public sealed class DriverStatusDiagnosticsTests : BunitContext { private static string FullError => "Synthetic local failure " + new string('x',2000) + " END-ERROR"; private void Configure(bool storeFails) { var auth = AddAuthorization(); auth.SetAuthorized("fixture"); auth.SetPolicies(AdminUiPolicies.DriverOperator); var status = DispatchProxy.Create(); ((StatusProxy)(object)status).Error = storeFails ? FullError : null; Services.AddSingleton(status); Services.AddSingleton(new InMemoryDriverResilienceStatusStore()); Services.AddSingleton(DispatchProxy.Create()); } [Fact] public void StatusReadError_ExposesCompleteEscapedDiagnostic() { Configure(true); var cut = Render(p => p.Add(x => x.DriverInstanceId,"driver-local")); cut.WaitForAssertion(() => cut.Find("details pre").TextContent.ShouldBe(FullError)); cut.FindAll("details script").ShouldBeEmpty(); cut.Find("summary").TextContent.ShouldContain("Status read error"); } [Fact] public void FailedOperation_ExposesFullReasonAlongsideExistingOutcome() { Configure(false); var cut = Render(p => p.Add(x => x.DriverInstanceId,"driver-local")); cut.FindAll("button").Single(x => x.TextContent.Trim()=="Reconnect").Click(); cut.WaitForAssertion(() => cut.Find("details pre").TextContent.ShouldBe(FullError)); cut.Find("summary").TextContent.ShouldBe("Operation details"); cut.FindAll("details script").ShouldBeEmpty(); } public class StatusProxy : DispatchProxy { public string? Error { get; set; } private readonly InMemoryDriverStatusSnapshotStore _store = new(); protected override object? Invoke(MethodInfo? method, object?[]? args) { if (method?.Name==nameof(IDriverStatusSnapshotStore.GetAllWithReceiveTime) && Error is not null) throw new IOException(Error); return method!.Invoke(_store,args); } } public class OperationsProxy : DispatchProxy { protected override object? Invoke(MethodInfo? method, object?[]? args) => method?.Name==nameof(IAdminOperationsClient.AskAsync) && method.GetGenericArguments().Single()==typeof(ReconnectDriverResult) ? Task.FromResult(new ReconnectDriverResult(false,FullError,Guid.Empty)) : throw new NotSupportedException("Synthetic fixture rejects other operations."); } }