using Akka.Actor;
using Akka.Hosting;
using Akka.TestKit.Xunit;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Clients;
using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Acls;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Mesh;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.ControlPlane;
using ZB.MOM.WW.OtOpcUa.ControlPlane.AdminOperations;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests;
///
/// Gitea #597 — the AdminUI /alerts acknowledge / shelve path, from the page's client call down to
/// the command the cluster receives: (what Alerts.razor's
/// per-row buttons call) Asks the real registered under
/// , which maps the request to an and hands
/// it to the mesh transport. Until now only the actor half had a test; the client seam the page goes
/// through had none.
///
///
/// The razor click handler itself is not driven here — this repo has no bUnit. What it adds on top of the
/// client call is the operator name and comment: null, both reproduced below exactly as the page
/// passes them.
///
public sealed class AlertsAckPathTests : TestKit
{
private (AdminOperationsClient Client, Akka.TestKit.TestProbe Mesh) Wire(
Func? answer = null)
{
var mesh = CreateTestProbe("mesh-router");
// #950: the router's one node answers each alarm command (Applied by default), as the gRPC fan reports it.
answer ??= cmd => new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.Applied);
mesh.SetAutoPilot(new Akka.TestKit.DelegateAutoPilot((_, msg) =>
{
if (msg is MeshCommand { VerdictReplyTo: { } collector, Message: AlarmCommand cmd })
{
collector.Tell(new AlarmVerdictFanOut(["node-1"]));
collector.Tell(new AlarmNodeVerdict("node-1", answer(cmd)));
}
return Akka.TestKit.AutoPilot.KeepRunning;
}));
var actor = Sys.ActorOf(AdminOperationsActor.Props(
AclScopeTestDb.Factory($"alerts-ack-{Guid.NewGuid():N}"),
coordinator: CreateTestProbe("coordinator").Ref,
probes: Enumerable.Empty(),
meshRouter: mesh.Ref));
var registry = new ActorRegistry();
registry.Register(actor);
return (new AdminOperationsClient(registry), mesh);
}
/// The page's Acknowledge reaches the cluster as an Acknowledge AlarmCommand on the
/// alarm-commands topic carrying the alarm id and the operator, and the page is told the engine applied it
/// (#950 — it used to be told only that it was sent).
[Fact]
public async Task Acknowledge_from_the_alerts_page_dispatches_an_Acknowledge_command_and_reports_applied()
{
var (client, mesh) = Wire();
var result = await client.AcknowledgeAlarmAsync("alarm-42", "operator-jo", comment: null, CancellationToken.None);
result.Ok.ShouldBeTrue();
result.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
result.Summary.ShouldBe("Applied");
var sent = mesh.ExpectMsg(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
sent.Topic.ShouldBe(AlarmCommandsTopic.Name);
var command = sent.Message.ShouldBeOfType();
command.AlarmId.ShouldBe("alarm-42");
command.Operation.ShouldBe("Acknowledge");
command.User.ShouldBe("operator-jo");
command.Comment.ShouldBeNull();
command.UnshelveAtUtc.ShouldBeNull();
}
/// The page's timed shelve carries its expiry through to the command — the instant the engine
/// projects as the node's UnshelveTime.
[Fact]
public async Task Timed_shelve_from_the_alerts_page_carries_its_expiry_to_the_command()
{
var (client, mesh) = Wire();
var until = new DateTime(2026, 10, 8, 12, 30, 0, DateTimeKind.Utc);
var result = await client.ShelveAlarmAsync(
"alarm-7", "operator-kim", ShelveKind.Timed, unshelveAtUtc: until, comment: null, CancellationToken.None);
result.Ok.ShouldBeTrue();
var command = mesh.ExpectMsg(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken).Message.ShouldBeOfType();
command.Operation.ShouldBe("TimedShelve");
command.UnshelveAtUtc.ShouldBe(until);
}
/// #950: the engine's refusal reaches the page as a refusal with its reason, not as "sent".
[Fact]
public async Task A_refused_acknowledge_reaches_the_page_with_the_engines_reason()
{
var (client, _) = Wire(cmd => new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied,
"The last configuration load failed; the command was not applied."));
var result = await client.AcknowledgeAlarmAsync("alarm-42", "operator-jo", comment: null, CancellationToken.None);
result.Ok.ShouldBeFalse();
result.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
result.Summary.ShouldBe("Refused: The last configuration load failed; the command was not applied.");
}
}