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.OpcUa;
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;
///
/// A1 (grpc-data plan §5.1 T2/F2): reaches the publish actor as
/// on the same chain as the host's
/// s — after the projection it summarises, and after the post-load
/// re-assert when it was raised by a (re)load.
///
[Trait("Feature", "GrpcData")]
[Trait("Phase", "A1")]
[Trait("Category", "Unit")]
public sealed class ScriptedAlarmHostInputsEstablishedTests : RuntimeActorTestBase
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30);
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private static EquipmentScriptedAlarmPlan Plan() => new(
ScriptedAlarmId: "alm-1", EquipmentId: "Plant/Line1/M", Name: "HighTemp", AlarmType: "AlarmCondition",
Severity: 800, MessageTemplate: "condition", PredicateScriptId: "alm-1-script",
PredicateSource: "return (int)ctx.GetTag(\"M.T\").Value > 90;", DependencyRefs: ["M.T"],
HistorizeToAveva: false, Retain: true, Enabled: true);
private IActorRef Spawn(TestProbe publish, TestProbe mux)
{
var upstream = new DependencyMuxTagUpstreamSource();
var logger = new LoggerConfiguration().CreateLogger();
var engine = new ScriptedAlarmEngine(upstream, new InMemoryAlarmStateStore(), new ScriptLoggerFactory(logger), logger);
return Sys.ActorOf(ScriptedAlarmHostActor.Props(publish.Ref, mux.Ref, upstream, engine, null));
}
/// T2 (i) at the host: the input arrives late ⇒ no mark before the first real evaluation; then the Active
/// projection, then the mark.
[Fact]
public void A_late_input_marks_the_condition_known_after_its_Active_projection()
{
var publish = CreateTestProbe();
var mux = CreateTestProbe();
var host = Spawn(publish, mux);
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan()]));
mux.ExpectMsg(Timeout, cancellationToken: Ct);
publish.ExpectNoMsg(TimeSpan.FromMilliseconds(300), Ct); // never-pushed input: no known-inactive
host.Tell(new VirtualTagActor.DependencyValueChanged("M.T", 99, DateTime.UtcNow));
publish.ExpectMsg(Timeout, cancellationToken: Ct).State.Active.ShouldBeTrue();
var mark = publish.ExpectMsg(Timeout, cancellationToken: Ct);
mark.ConditionIds.ShouldBe(["alm-1"]);
mark.Realm.ShouldBe(AddressSpaceRealm.Uns);
mark.Source.ShouldBe(OpcUaPublishActor.ScriptedSource);
mark.Known.ShouldBeTrue();
}
/// T2 (iv), scripted: a reload (what follows a rebuild that re-created the condition unknown) re-raises the
/// flip from inside LoadAsync — before the load pipes back — but the mark is held until the post-load re-assert of
/// the still-active condition has been sent, so it never lands on the freshly materialised (inactive) node.
[Fact]
public void A_reload_holds_the_mark_until_after_the_reassert()
{
var publish = CreateTestProbe();
var mux = CreateTestProbe();
var host = Spawn(publish, mux);
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan()]));
mux.ExpectMsg(Timeout, cancellationToken: Ct);
host.Tell(new VirtualTagActor.DependencyValueChanged("M.T", 99, DateTime.UtcNow));
publish.ExpectMsg(Timeout, cancellationToken: Ct);
publish.ExpectMsg(Timeout, cancellationToken: Ct);
host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms([Plan()]));
var reassert = publish.ExpectMsg(Timeout, cancellationToken: Ct);
reassert.State.Active.ShouldBeTrue();
publish.ExpectMsg(Timeout, cancellationToken: Ct).ConditionIds.ShouldBe(["alm-1"]);
}
}