106 lines
6.1 KiB
C#
106 lines
6.1 KiB
C#
using System.Reflection;
|
|
using Bunit;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
|
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Uns;
|
|
using RawPage=ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Raw.GlobalRaw;
|
|
using UnsPage=ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Uns.GlobalUns;
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
|
|
|
|
[Trait("Category","Unit")]
|
|
public sealed class TreeRecoveryTests:BunitContext
|
|
{
|
|
public TreeRecoveryTests()
|
|
{
|
|
JSInterop.Mode=JSRuntimeMode.Loose;
|
|
ComponentFactories.AddStub(type=>new[]{"CredentialReferenceBanner","RawTree","AreaModal","LineModal","ImportEquipmentModal"}.Contains(type.Name));
|
|
AddAuthorization().SetAuthorized("fixture");
|
|
}
|
|
[Fact]
|
|
public void RawFailure_IsNotEmptyAndRetryDoesNotFetchLazyChildren()
|
|
{
|
|
var proxy=DispatchProxy.Create<IRawTreeService,TreeReads>();var reads=(TreeReads)(object)proxy;
|
|
Services.AddSingleton(proxy);
|
|
var cut=Render<RawPage>();
|
|
cut.WaitForAssertion(()=>cut.Find("[role=alert]").TextContent.ShouldContain("could not be loaded"));
|
|
cut.Markup.ShouldNotContain("No clusters yet.");
|
|
cut.Find("button[data-testid=tree-retry]").Click();
|
|
cut.WaitForAssertion(()=>reads.Calls.ShouldBe(2));cut.Markup.ShouldNotContain("No clusters yet.");
|
|
reads.Fail=false;
|
|
cut.Find("button[data-testid=tree-retry]").Click();
|
|
cut.WaitForAssertion(()=>cut.Markup.ShouldContain("No clusters yet."));
|
|
reads.Calls.ShouldBe(3);reads.OtherCalls.ShouldBe(0);
|
|
}
|
|
[Fact]
|
|
public void UnsFailure_RetryRecoversConfirmedEmpty()
|
|
{
|
|
var proxy=DispatchProxy.Create<IUnsTreeService,TreeReads>();var reads=(TreeReads)(object)proxy;Services.AddSingleton(proxy);
|
|
var cut=Render<UnsPage>();
|
|
cut.WaitForAssertion(()=>cut.Find("[role=alert]").TextContent.ShouldContain("could not be loaded"));
|
|
cut.Markup.ShouldNotContain("No clusters yet.");reads.Fail=false;
|
|
cut.Find("button[data-testid=tree-retry]").Click();
|
|
cut.WaitForAssertion(()=>cut.Markup.ShouldContain("No clusters yet."));reads.Calls.ShouldBe(2);
|
|
}
|
|
[Fact]
|
|
public async Task RawPendingRetry_IsSingleFlightAndDisposalCancelsRead()
|
|
{
|
|
var proxy=DispatchProxy.Create<IRawTreeService,TreeReads>();var reads=(TreeReads)(object)proxy;Services.AddSingleton(proxy);
|
|
var cut=Render<RawPage>();reads.Fail=false;reads.Hold=new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var click=cut.Find("button[data-testid=tree-retry]").ClickAsync(new());
|
|
cut.WaitForAssertion(()=>reads.Calls.ShouldBe(2));
|
|
cut.Find("[role=status]").TextContent.ShouldContain("Loading");
|
|
await cut.InvokeAsync(()=> (Task)typeof(RawPage).GetMethod("LoadRootsAsync",BindingFlags.Instance|BindingFlags.NonPublic)!.Invoke(cut.Instance,null)!);
|
|
reads.Calls.ShouldBe(2);
|
|
await cut.InvokeAsync(cut.Instance.Dispose);reads.Token.IsCancellationRequested.ShouldBeTrue();reads.Hold.SetResult([]);await click;reads.OtherCalls.ShouldBe(0);
|
|
}
|
|
[Fact]
|
|
public void LoadedFilter_HasNoMatchStateAndDoesNotFetchUnloadedChildren()
|
|
{
|
|
var equipment=new UnsNode{Key="eq:1",Kind=UnsNodeKind.Equipment,DisplayName="Press 1",HasLazyChildren=true,Loaded=false};
|
|
var cut=Render<UnsTree>(p=>p.Add(x=>x.Roots,new[]{equipment}).Add(x=>x.Filter,"unloaded-tag"));
|
|
cut.Find("[role=status]").TextContent.ShouldContain("No loaded names match");
|
|
equipment.Loaded.ShouldBeFalse();equipment.Children.ShouldBeEmpty();
|
|
cut.Render(p=>p.Add(x=>x.Filter,"press"));cut.FindAll("[role=status]").ShouldBeEmpty();cut.Markup.ShouldContain("Press 1");
|
|
}
|
|
[Fact]
|
|
public void UnsClearFilter_RetainsLoadedStructureAndScopeHintWithoutAnotherRead()
|
|
{
|
|
var proxy=DispatchProxy.Create<IUnsTreeService,TreeReads>();var reads=(TreeReads)(object)proxy;
|
|
reads.Fail=false;reads.UnsRoots=[new UnsNode{Key="eq:1",Kind=UnsNodeKind.Equipment,DisplayName="Press 1",HasLazyChildren=true}];
|
|
Services.AddSingleton(proxy);var cut=Render<UnsPage>();
|
|
var input=cut.Find("input");var hint=input.GetAttribute("aria-describedby");hint.ShouldNotBeNullOrWhiteSpace();
|
|
cut.Find("#"+hint).TextContent.ShouldContain("Unloaded tag contents are not searched");
|
|
input.Input("not-loaded-tag");cut.Find("[role=status]").TextContent.ShouldContain("No loaded names match");
|
|
cut.FindAll("button").Single(b=>b.TextContent=="Clear filter").Click();
|
|
cut.Find("input").GetAttribute("value").ShouldBeNullOrEmpty();cut.Markup.ShouldContain("Press 1");reads.Calls.ShouldBe(1);reads.OtherCalls.ShouldBe(0);
|
|
}
|
|
[Theory]
|
|
[InlineData(true)]
|
|
[InlineData(false)]
|
|
public async Task ViewReadLifetime_DisposalIsIdempotent(bool raw)
|
|
{
|
|
if(raw)
|
|
{
|
|
Services.AddSingleton(DispatchProxy.Create<IRawTreeService,TreeReads>());var cut=Render<RawPage>();
|
|
await cut.InvokeAsync(()=>{cut.Instance.Dispose();cut.Instance.Dispose();});
|
|
}
|
|
else
|
|
{
|
|
Services.AddSingleton(DispatchProxy.Create<IUnsTreeService,TreeReads>());var cut=Render<UnsPage>();
|
|
await cut.InvokeAsync(()=>{cut.Instance.Dispose();cut.Instance.Dispose();});
|
|
}
|
|
}
|
|
public class TreeReads:DispatchProxy
|
|
{
|
|
public bool Fail=true;public IReadOnlyList<UnsNode> UnsRoots=[];public int Calls,OtherCalls;public CancellationToken Token;public TaskCompletionSource<IReadOnlyList<RawNode>>? Hold;
|
|
protected override object? Invoke(MethodInfo? method,object?[]? args)
|
|
{
|
|
if(method?.Name=="LoadRootsAsync"){Calls++;Token=(CancellationToken)args![0]!;return Fail?Task.FromException<IReadOnlyList<RawNode>>(new InvalidOperationException("synthetic failure")):Hold?.Task??Task.FromResult<IReadOnlyList<RawNode>>([]);}
|
|
if(method?.Name=="LoadStructureAsync"){Calls++;Token=(CancellationToken)args![0]!;return Fail?Task.FromException<IReadOnlyList<UnsNode>>(new InvalidOperationException("synthetic failure")):Task.FromResult(UnsRoots);}
|
|
OtherCalls++;throw new InvalidOperationException("Unexpected tree operation: "+method?.Name);
|
|
}
|
|
}
|
|
}
|