Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/AlertsAckPathTests.cs
T
Joseph Doherty 09aaabbeef fix(alarms): AdminUI /alerts ack/shelve reports the scripted engine's verdict (#950)
The /alerts buttons went central->node fire-and-forget and always showed
"Ack sent". AdminOperationsActor now spawns a per-request
AlarmCommandVerdictCollector (7 s window, under the client's 10 s ask) that
sends the command with AlarmCommand.VerdictRequested and gathers each node's
answer, finishing early once every node it sent to has answered.

- gRPC plane: CentralCommunicationActor tells the collector the dialled
  ClusterNode set and one verdict per node. A node handles a
  verdict_requested AlarmCommandMessage by asking its driver host
  (ScriptedAlarmHostActor.ExecuteAlarmCommand, the #937 path, no ask deadline,
  bounded 5 s) and returns it in the ADDITIVE ExecuteAck.alarm_verdict.
  Transport failure / timeout / empty ack (pre-#950 node) => that node unknown.
- DPS: published with the collector as sender; each ScriptedAlarmHostActor
  answers (non-owners NotOwned); expected set = Up driver members.
- Aggregation: any owner applied => Applied (partial noted); else any
  unknown/silent node => Outcome unknown (never "refused"); else owner
  refusal => Refused: <reason>; else no owner => Not applied.
- UI: green Applied / red Refused (reason) / amber Outcome unknown, per-node
  detail in the details pane; a verdict-less reply (validation, server fault,
  pre-#950 singleton) renders as before.

Wire is back-compatible both ways (new proto fields only; VerdictRequested and
NotOwned default false). Real-wire test through the real router, dialer,
interceptor and NodeCommandGrpcService (red without the node change).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-12-01 19:45:17 -05:00

112 lines
5.4 KiB
C#

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;
/// <summary>
/// Gitea #597 — the AdminUI <c>/alerts</c> acknowledge / shelve path, from the page's client call down to
/// the command the cluster receives: <see cref="AdminOperationsClient"/> (what <c>Alerts.razor</c>'s
/// per-row buttons call) Asks the real <see cref="AdminOperationsActor"/> registered under
/// <see cref="AdminOperationsActorKey"/>, which maps the request to an <see cref="AlarmCommand"/> 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.
/// </summary>
/// <remarks>
/// 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 <c>comment: null</c>, both reproduced below exactly as the page
/// passes them.
/// </remarks>
public sealed class AlertsAckPathTests : TestKit
{
private (AdminOperationsClient Client, Akka.TestKit.TestProbe Mesh) Wire(
Func<AlarmCommand, AlarmCommandOutcome>? 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<IDriverProbe>(),
meshRouter: mesh.Ref));
var registry = new ActorRegistry();
registry.Register<AdminOperationsActorKey>(actor);
return (new AdminOperationsClient(registry), mesh);
}
/// <summary>The page's Acknowledge reaches the cluster as an <c>Acknowledge</c> AlarmCommand on the
/// <c>alarm-commands</c> 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).</summary>
[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<MeshCommand>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
sent.Topic.ShouldBe(AlarmCommandsTopic.Name);
var command = sent.Message.ShouldBeOfType<AlarmCommand>();
command.AlarmId.ShouldBe("alarm-42");
command.Operation.ShouldBe("Acknowledge");
command.User.ShouldBe("operator-jo");
command.Comment.ShouldBeNull();
command.UnshelveAtUtc.ShouldBeNull();
}
/// <summary>The page's timed shelve carries its expiry through to the command — the instant the engine
/// projects as the node's <c>UnshelveTime</c>.</summary>
[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<MeshCommand>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken).Message.ShouldBeOfType<AlarmCommand>();
command.Operation.ShouldBe("TimedShelve");
command.UnshelveAtUtc.ShouldBe(until);
}
/// <summary>#950: the engine's refusal reaches the page as a refusal with its reason, not as "sent".</summary>
[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.");
}
}