using Bunit;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Xunit;
using DeploymentsPage = ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Deployments;
using ZB.MOM.WW.OtOpcUa.AdminUI.Deployments;
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
using ZB.MOM.WW.OtOpcUa.Configuration;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Deployments;
///
/// lmxopcua#903: after a draft change, /deployments sometimes said "In sync" on its first load and only showed
/// the drift after a refresh. The probe (a singleton shared by every circuit) served a verdict cached against a
/// ConfigEdit watermark that the AdminUI's own editors never move. The page must report the drift on the
/// load that follows the change, and must show a neutral "Checking" state — never a verdict — until it knows.
///
[Trait("Category", "Unit")]
public sealed class DeploymentsPageDriftRenderTests : BunitContext
{
private const string Chip = "[data-testid=drift-chip]";
private string Seed()
{
var name = $"deploy-page-{Guid.NewGuid():N}";
using (var db = UnsTreeTestDb.CreateNamed(name))
{
UnsTreeTestDb.Seed(db);
}
string hash;
using (var db = UnsTreeTestDb.CreateNamed(name))
{
hash = ConfigComposer.SnapshotAndFlattenAsync(db).GetAwaiter().GetResult().RevisionHash;
db.Deployments.Add(new Deployment
{
RevisionHash = hash,
Status = DeploymentStatus.Sealed,
CreatedBy = "test",
SealedAtUtc = DateTime.UtcNow,
});
db.SaveChanges();
}
Services.AddSingleton(UnsTreeTestDb.Factory(name));
Services.AddSingleton(new FakeAuth());
Services.AddSingleton(new UnusedAdminOps());
return name;
}
[Fact]
public void A_draft_change_is_reported_on_the_next_page_load()
{
var name = Seed();
Services.AddSingleton(sp => new DeploymentDriftProbe(sp.GetRequiredService>()));
// Load 1: draft == sealed.
var first = Render();
first.WaitForAssertion(() => first.Find(Chip).TextContent.ShouldBe("In sync"));
// Edit the draft the way the AdminUI editors do: directly, with no ConfigEdit audit row.
using (var db = UnsTreeTestDb.CreateNamed(name))
{
db.ServerClusters.Add(new ServerCluster
{
ClusterId = "NEW-1", Name = "New", Enterprise = "zb", Site = "s9",
RedundancyMode = RedundancyMode.None, CreatedBy = "test",
});
db.SaveChanges();
}
// Load 2 (a new circuit, same singleton probe): must say drift, not the earlier verdict.
var second = Render();
second.WaitForAssertion(() => second.Find(Chip).TextContent.ShouldBe("Configuration drift"));
}
[Fact]
public void First_render_shows_checking_until_the_comparison_answers()
{
Seed();
var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
Services.AddSingleton(sp => new DeploymentDriftProbe(
sp.GetRequiredService>(),
async _ => { await gate.Task; return "a-different-hash"; }));
var cut = Render();
cut.Find(Chip).TextContent.ShouldStartWith("Checking");
cut.Markup.ShouldNotContain("In sync");
gate.SetResult();
cut.WaitForAssertion(() => cut.Find(Chip).TextContent.ShouldBe("Configuration drift"));
}
private sealed class FakeAuth : AuthenticationStateProvider
{
public override Task GetAuthenticationStateAsync() =>
Task.FromResult(new AuthenticationState(new System.Security.Claims.ClaimsPrincipal(
new System.Security.Claims.ClaimsIdentity("test"))));
}
private sealed class UnusedAdminOps : IAdminOperationsClient
{
public Task StartDeploymentAsync(string createdBy, CancellationToken ct) =>
throw new NotSupportedException();
public Task AcknowledgeAlarmAsync(string alarmId, string user, string? comment, CancellationToken ct) =>
throw new NotSupportedException();
public Task ShelveAlarmAsync(string alarmId, string user, ShelveKind kind, DateTime? unshelveAtUtc,
string? comment, CancellationToken ct) => throw new NotSupportedException();
public Task AskAsync(object message, CancellationToken ct) => throw new NotSupportedException();
}
}