using Bunit;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Acls;
using ZB.MOM.WW.OtOpcUa.AdminUI.Browsing;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Acls;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
using ZB.MOM.WW.OtOpcUa.Commons.Browsing;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
/// Tree names/states identify the actual scope or driver node without changing lazy loading.
[Trait("Category","Unit")]
public sealed class CommonTreeAccessibilityTests : BunitContext
{
[Fact]
public void BrowseTree_ExpandedFilterAndLeafHaveContextualNames()
{
Services.AddSingleton(DispatchProxy.Create());
var cut=Render(p=>p.Add(x=>x.SessionToken,Guid.NewGuid()).Add(x=>x.MultiSelect,true));
var expand=cut.Find("button.tree-twist");
expand.GetAttribute("aria-label").ShouldBe("Expand Fixture folder");
expand.GetAttribute("aria-expanded").ShouldBe("false");expand.Click();
cut.Find("button.tree-twist").GetAttribute("aria-expanded").ShouldBe("true");
cut.Find("input[type=text]").GetAttribute("aria-label").ShouldBe("Filter children of Fixture folder");
cut.Find("input[type=checkbox]").GetAttribute("aria-label").ShouldBe("Select Fixture leaf");
}
[Fact]
public void ScopeTree_ExpandAndSelectionExposeVisibleNodeState()
{
var node=new AclScopeNode {Key="one",DisplayName="Fixture scope",PathLabel="Fixture scope",Icon="x",ScopeKind=NodeAclScopeKind.Cluster,HasLazyChildren=true};
var cut=Render(p=>p.Add(x=>x.Nodes,new[]{node}).Add(x=>x.SelectedKey,"one"));
cut.Find("button.tree-twist").GetAttribute("aria-label").ShouldBe("Expand Fixture scope");
cut.Find("button[aria-pressed]").GetAttribute("aria-pressed").ShouldBe("true");
cut.Find("button.tree-twist").Click();
cut.Find("button.tree-twist").GetAttribute("aria-expanded").ShouldBe("true");
}
/// Only synthetic root/child reads are supported; all connection and mutation calls fail.
public class SyntheticBrowser : DispatchProxy
{
protected override object? Invoke(MethodInfo? method,object?[]? args)=>method?.Name switch
{
nameof(IBrowserSessionService.RootAsync)=>Task.FromResult>([new("folder","Fixture folder",BrowseNodeKind.Folder,true)]),
nameof(IBrowserSessionService.ExpandAsync)=>Task.FromResult>([new("leaf","Fixture leaf",BrowseNodeKind.Leaf,false)]),
_=>throw new NotSupportedException("Synthetic tree refuses "+method?.Name)
};
}
}