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;
///
/// Gitea #950 — a broadcast from central's AdminUI path (VerdictRequested) is answered
/// to its sender with the engine's verdict, non-owners included (NotOwned); every other broadcast stays one-way.
///
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(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(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(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(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(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(new ScriptedAlarmHostActor.ExecuteAlarmCommand(
new AlarmCommand("someone-elses", "Acknowledge", "alice", null, null)), Timeout, Ct);
outcome.Verdict.ShouldBe(AlarmCommandVerdict.NotApplied);
outcome.NotOwned.ShouldBeTrue();
}
}