Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/ScriptedAlarms/ScriptedAlarmBroadcastVerdictTests.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

117 lines
5.4 KiB
C#

using Akka.Actor;
using Akka.TestKit;
using Serilog;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
using ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
using ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.ScriptedAlarms;
/// <summary>
/// Gitea #950 — a broadcast <see cref="AlarmCommand"/> from central's AdminUI path (<c>VerdictRequested</c>) is answered
/// to its sender with the engine's verdict, non-owners included (<c>NotOwned</c>); every other broadcast stays one-way.
/// </summary>
public sealed class ScriptedAlarmBroadcastVerdictTests : RuntimeActorTestBase
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30);
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private static EquipmentScriptedAlarmPlan Plan(string id, string source = "return (int)ctx.GetTag(\"M.T\").Value > 90;") =>
new(ScriptedAlarmId: id, EquipmentId: "Plant/Line1/M", Name: id, AlarmType: "AlarmCondition", Severity: 800,
MessageTemplate: "condition", PredicateScriptId: $"{id}-script", PredicateSource: source,
DependencyRefs: ["M.T"], HistorizeToAveva: false, Retain: true, Enabled: true);
private (IActorRef Host, ScriptedAlarmEngine Engine) SpawnLoaded(params EquipmentScriptedAlarmPlan[] plans)
{
var publish = CreateTestProbe();
var mux = CreateTestProbe();
var upstream = new DependencyMuxTagUpstreamSource();
var logger = new LoggerConfiguration().CreateLogger();
var engine = new ScriptedAlarmEngine(upstream, new InMemoryAlarmStateStore(), new ScriptLoggerFactory(logger), logger);
var host = Sys.ActorOf(ScriptedAlarmHostActor.Props(publish.Ref, mux.Ref, () => (upstream, engine),
readiness: new ScriptedAlarmCommandReadinessView()));
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms(plans));
mux.ExpectMsg<DependencyMuxActor.RegisterInterest>(Timeout, cancellationToken: Ct);
return (host, engine);
}
private static AlarmCommand Broadcast(string alarmId, string text, bool verdictRequested) =>
new(alarmId, "AddComment", "alice", text, null, VerdictRequested: verdictRequested);
[Fact]
public void An_owner_answers_its_verdict_to_the_publisher()
{
var (host, engine) = SpawnLoaded(Plan("alm-1"));
var collector = CreateTestProbe();
host.Tell(Broadcast("alm-1", "from the AdminUI", verdictRequested: true), collector.Ref);
var outcome = collector.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct);
outcome.Verdict.ShouldBe(AlarmCommandVerdict.Applied);
outcome.NotOwned.ShouldBeFalse();
engine.GetState("alm-1")!.Comments.Select(c => c.Text).ShouldBe(["from the AdminUI"]);
}
[Fact]
public void A_non_owner_answers_not_owned_and_applies_nothing()
{
var (host, _) = SpawnLoaded(Plan("alm-1"));
var collector = CreateTestProbe();
host.Tell(Broadcast("someone-elses", "x", verdictRequested: true), collector.Ref);
var outcome = collector.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct);
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
outcome.NotOwned.ShouldBeTrue();
}
[Fact]
public void An_owner_refusal_is_answered_as_a_refusal_not_as_not_owned()
{
var (host, _) = SpawnLoaded(Plan("alm-1"));
var collector = CreateTestProbe();
// AddComment without text is refused by the host before the engine runs.
host.Tell(Broadcast("alm-1", " ", verdictRequested: true), collector.Ref);
var outcome = collector.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct);
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
outcome.NotOwned.ShouldBeFalse();
outcome.Reason.ShouldNotBeNull().ShouldContain("comment text");
}
[Fact]
public void A_broadcast_that_did_not_ask_for_a_verdict_is_not_answered()
{
var (host, engine) = SpawnLoaded(Plan("alm-1"));
var publisher = CreateTestProbe();
host.Tell(Broadcast("alm-1", "one-way", verdictRequested: false), publisher.Ref);
host.Tell(Broadcast("someone-elses", "one-way", verdictRequested: false), publisher.Ref);
// Positive control queued behind both: a verdict request IS answered, so the silence above is not a slow mailbox.
host.Tell(Broadcast("alm-1", "asked", verdictRequested: true), publisher.Ref);
publisher.ExpectMsg<AlarmCommandOutcome>(Timeout, cancellationToken: Ct).Verdict.ShouldBe(AlarmCommandVerdict.Applied);
publisher.ExpectNoMsg(TimeSpan.FromMilliseconds(200), Ct);
engine.GetState("alm-1")!.Comments.Select(c => c.Text).ShouldBe(["one-way", "asked"]);
}
[Fact]
public async Task A_direct_ask_for_an_unowned_alarm_is_flagged_not_owned()
{
var (host, _) = SpawnLoaded(Plan("alm-1"));
var outcome = await host.Ask<AlarmCommandOutcome>(new ScriptedAlarmHostActor.ExecuteAlarmCommand(
new AlarmCommand("someone-elses", "Acknowledge", "alice", null, null)), Timeout, Ct);
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
outcome.NotOwned.ShouldBeTrue();
}
}