using System.Data.Common; using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.AdminUI.Hosts; using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs; using ZB.MOM.WW.OtOpcUa.Configuration; using HostsPage=ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Hosts; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages; /// A later query failure cannot partially install an earlier query's data. [Collection(nameof(AdminUiSqlServerCollection))] public sealed class HostsPartialReadSqlServerTests(AdminUiSqlServerFixture sql) { [SqlServerFact] public async Task SecondConfigQueryFailure_PreservesBothCachedListsAndSkipsEviction() { var reads=new FailDriverInstancesRead(); var options=new DbContextOptionsBuilder().UseSqlServer(sql.ConnectionString).AddInterceptors(reads).Options; var factory=new ReadFactory(options); var page=new HostsPage(); SetProperty(page,"DbFactory",factory); SetProperty(page,"Logger",NullLogger.Instance); SetProperty(page,"DriverStore",new InMemoryDriverStatusSnapshotStore()); // These deliberate cached values differ from the owned SQL fixture's actual rows. var nodes=new List{new("cached", "cached-node", "cached-host",4840)}; var instances=new List{new("cached-driver","cached","Cached name","Modbus",true)}; SetField(page,"_nodes",nodes);SetField(page,"_instances",instances); var acquired=new DateTime(2026,1,1,0,0,0,DateTimeKind.Utc);SetField(page,"_lastConfigUtc",acquired); // Deployed/resilience store intentionally absent: reaching eviction would fail before the scripted // second-query exception, and the exact query observations below would no longer hold. var changed=await (Task)typeof(HostsPage).GetMethod("LoadConfigAsync",BindingFlags.Instance|BindingFlags.NonPublic)!.Invoke(page,null)!; changed.ShouldBeFalse();reads.NodesRead.ShouldBeTrue();reads.InstancesRejected.ShouldBeTrue(); GetField(page,"_nodes").ShouldBeSameAs(nodes);GetField(page,"_instances").ShouldBeSameAs(instances); GetField(page,"_lastConfigUtc").ShouldBe(acquired);GetField(page,"_configFailedAtUtc").ShouldNotBeNull(); await page.DisposeAsync(); } private static void SetProperty(object target,string name,object value)=>target.GetType().GetProperty(name,BindingFlags.Instance|BindingFlags.NonPublic)!.SetValue(target,value); private static void SetField(object target,string name,object value)=>target.GetType().GetField(name,BindingFlags.Instance|BindingFlags.NonPublic)!.SetValue(target,value); private static object? GetField(object target,string name)=>target.GetType().GetField(name,BindingFlags.Instance|BindingFlags.NonPublic)!.GetValue(target); private sealed class ReadFactory(DbContextOptions options):IDbContextFactory { public OtOpcUaConfigDbContext CreateDbContext()=>new(options); public Task CreateDbContextAsync(CancellationToken ct=default)=>Task.FromResult(CreateDbContext()); } private sealed class FailDriverInstancesRead:DbCommandInterceptor { public bool NodesRead {get;private set;} public bool InstancesRejected {get;private set;} public override ValueTask> ReaderExecutingAsync(DbCommand command,CommandEventData data,InterceptionResult result,CancellationToken ct=default) { if(command.CommandText.Contains("[ClusterNode]",StringComparison.Ordinal)) NodesRead=true; if(command.CommandText.Contains("[DriverInstance]",StringComparison.Ordinal)) {InstancesRejected=true;throw new IOException("Synthetic second-query failure");} return ValueTask.FromResult(result); } } }