fix(site-runtime): failed trigger evaluation keeps state, RateOfChange clears when flat, WhileTrue re-fires after seed adoption (SRT-2, SRT-3, SRT-4, WP3 task 3.7)

This commit is contained in:
Joseph Doherty
2026-12-03 10:11:15 -05:00
parent a730bc0558
commit bc7fae2652
6 changed files with 394 additions and 78 deletions
@@ -616,6 +616,75 @@ public class ScriptActorTests : TestKit, IDisposable
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2));
}
/// <summary>
/// SRT-2: a REAL (non-seed) evaluation that faults or times out is "unknown", not false. Pre-fix
/// it stopped the running WhileTrue timer (and the next true evaluation fired a spurious edge run).
/// </summary>
[Fact]
public void ScriptActor_ExpressionWhileTrue_FaultedRealEval_DoesNotStopReFiring()
{
var triggerExpr = CompileTriggerExpression("Attributes[\"Active\"]?.ToString() == \"yes\"");
var (actor, instance) = CreateTriggeredActor(
"ExprRealFault",
"Expression",
"{\"expression\":\"Attributes[\\\"Active\\\"]?.ToString() == \\\"yes\\\"\",\"mode\":\"WhileTrue\"}",
TimeSpan.FromMilliseconds(300),
triggerExpr);
actor.Tell(Change("Active", "yes"));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2)); // edge fire
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2)); // tick 1
actor.Tell(new ScriptActor.ExpressionEvalFailed(new TimeoutException("eval timed out")));
// Nothing real said the condition went false: the timer keeps re-firing (pre-fix: stopped).
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(2));
}
/// <summary>
/// SRT-4: a Conditional WhileTrue trigger whose condition is adopted TRUE from a seed (a
/// persisted static override after failover/restart) re-fires on its cadence — without the
/// immediate edge run a seed never makes. Pre-fix it never fired again.
/// </summary>
[Fact]
public void ScriptActor_ConditionalWhileTrue_SeededTrue_StartsReFiringWithoutAnEdgeRun()
{
var (actor, instance) = CreateTriggeredActor(
"CondSeed",
"Conditional",
"{\"attributeName\":\"Temp\",\"operator\":\">\",\"threshold\":50,\"mode\":\"WhileTrue\"}",
TimeSpan.FromMilliseconds(300));
var sw = System.Diagnostics.Stopwatch.StartNew();
actor.Tell(new ZB.MOM.WW.ScadaBridge.SiteRuntime.Messages.AttributeValueSeed(Change("Temp", "100")));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3)); // pre-fix: never
// The first run is a TIMER tick (≥ one 300 ms interval), not an immediate edge run.
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(250), $"first run after {sw.Elapsed} — seed fired an edge run");
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3)); // and it keeps re-firing
}
/// <summary>SRT-4: the Expression twin — a seed-only evaluation that is true starts the WhileTrue timer.</summary>
[Fact]
public void ScriptActor_ExpressionWhileTrue_SeededTrue_StartsReFiringWithoutAnEdgeRun()
{
var triggerExpr = CompileTriggerExpression("Attributes[\"Active\"]?.ToString() == \"yes\"");
var (actor, instance) = CreateTriggeredActor(
"ExprSeed",
"Expression",
"{\"expression\":\"Attributes[\\\"Active\\\"]?.ToString() == \\\"yes\\\"\",\"mode\":\"WhileTrue\"}",
TimeSpan.FromMilliseconds(300),
triggerExpr);
var sw = System.Diagnostics.Stopwatch.StartNew();
actor.Tell(new ZB.MOM.WW.ScadaBridge.SiteRuntime.Messages.AttributeValueSeed(Change("Active", "yes")));
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3)); // pre-fix: never
Assert.True(sw.Elapsed >= TimeSpan.FromMilliseconds(250), $"first run after {sw.Elapsed} — seed fired an edge run");
instance.ExpectMsg<SetStaticAttributeCommand>(TimeSpan.FromSeconds(3));
}
/// <summary>Verifies that an on-true expression trigger fires once per false-to-true edge.</summary>
[Fact]
public void ScriptActor_ExpressionOnTrue_FiresOncePerFalseToTrueEdge()