feat(triggers): add WhileTrue fire mode for Conditional/Expression script triggers
Conditional and Expression script triggers gain an optional `mode` field in their TriggerConfiguration JSON: - OnTrue (default): unchanged edge/per-change firing. An absent mode field parses as OnTrue, so every existing trigger config behaves identically. - WhileTrue: fires on the false->true edge, then re-fires on a periodic timer while the condition holds; stops on the true->false edge. The re-fire cadence is the script's MinTimeBetweenRuns; with none configured the trigger degrades to a single edge fire and logs a warning. ScriptActor tracks condition truth state and manages a dedicated "whiletrue-trigger" timer. ScriptTriggerConfigCodec and ScriptTriggerEditor round-trip the mode and expose an OnTrue/WhileTrue selector for the two trigger kinds. Design: docs/plans/2026-05-18-whiletrue-trigger-mode-design.md Tests: 7 ScriptActor runtime tests (edge fire, timer re-fire, stop, re-arm, no-MinTimeBetweenRuns degrade, OnTrue regressions) + 14 codec / editor tests. SiteRuntime suite 206 green, CentralUI suite 295 green.
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
using ScadaLink.CentralUI.Components.Shared;
|
||||
|
||||
namespace ScadaLink.CentralUI.Tests.Shared;
|
||||
|
||||
/// <summary>
|
||||
/// Round-trip coverage for the WhileTrue/OnTrue <c>mode</c> field on the
|
||||
/// Conditional and Expression script triggers.
|
||||
/// </summary>
|
||||
public class ScriptTriggerConfigCodecTests
|
||||
{
|
||||
// ── Parse: mode field ──────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Parse_Conditional_WithoutMode_DefaultsToOnTrue()
|
||||
{
|
||||
const string json = @"{""attributeName"":""Temp"",""operator"":"">"",""threshold"":80}";
|
||||
|
||||
var model = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Conditional);
|
||||
|
||||
Assert.Equal(ScriptTriggerMode.OnTrue, model.Mode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_Conditional_WhileTrue_IsRead()
|
||||
{
|
||||
const string json =
|
||||
@"{""attributeName"":""Temp"",""operator"":"">"",""threshold"":80,""mode"":""WhileTrue""}";
|
||||
|
||||
var model = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Conditional);
|
||||
|
||||
Assert.Equal(ScriptTriggerMode.WhileTrue, model.Mode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_Expression_WithoutMode_DefaultsToOnTrue()
|
||||
{
|
||||
const string json = @"{""expression"":""Attributes[\""T\""] > 1""}";
|
||||
|
||||
var model = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Expression);
|
||||
|
||||
Assert.Equal(ScriptTriggerMode.OnTrue, model.Mode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_Expression_WhileTrue_IsRead()
|
||||
{
|
||||
const string json =
|
||||
@"{""expression"":""Attributes[\""T\""] > 1"",""mode"":""WhileTrue""}";
|
||||
|
||||
var model = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Expression);
|
||||
|
||||
Assert.Equal(ScriptTriggerMode.WhileTrue, model.Mode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_UnrecognizedMode_DefaultsToOnTrue()
|
||||
{
|
||||
const string json =
|
||||
@"{""attributeName"":""Temp"",""operator"":"">"",""threshold"":80,""mode"":""Sometimes""}";
|
||||
|
||||
var model = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Conditional);
|
||||
|
||||
Assert.Equal(ScriptTriggerMode.OnTrue, model.Mode);
|
||||
}
|
||||
|
||||
// ── Serialize: mode field ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Serialize_Conditional_WhileTrue_WritesMode()
|
||||
{
|
||||
var model = new ScriptTriggerModel
|
||||
{
|
||||
AttributeName = "Temp",
|
||||
Operator = ">",
|
||||
Threshold = 80,
|
||||
Mode = ScriptTriggerMode.WhileTrue
|
||||
};
|
||||
|
||||
var json = ScriptTriggerConfigCodec.Serialize(model, ScriptTriggerKind.Conditional);
|
||||
|
||||
Assert.Contains("\"mode\":\"WhileTrue\"", json);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Serialize_Expression_WhileTrue_WritesMode()
|
||||
{
|
||||
var model = new ScriptTriggerModel
|
||||
{
|
||||
Expression = "Attributes[\"T\"] > 1",
|
||||
Mode = ScriptTriggerMode.WhileTrue
|
||||
};
|
||||
|
||||
var json = ScriptTriggerConfigCodec.Serialize(model, ScriptTriggerKind.Expression);
|
||||
|
||||
Assert.Contains("\"mode\":\"WhileTrue\"", json);
|
||||
}
|
||||
|
||||
// ── Round-trip ─────────────────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void RoundTrip_Conditional_PreservesMode(bool whileTrue)
|
||||
{
|
||||
var mode = whileTrue ? ScriptTriggerMode.WhileTrue : ScriptTriggerMode.OnTrue;
|
||||
var original = new ScriptTriggerModel
|
||||
{
|
||||
AttributeName = "Temp",
|
||||
Operator = ">=",
|
||||
Threshold = 12.5,
|
||||
Mode = mode
|
||||
};
|
||||
|
||||
var json = ScriptTriggerConfigCodec.Serialize(original, ScriptTriggerKind.Conditional);
|
||||
var reparsed = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Conditional);
|
||||
|
||||
Assert.Equal(mode, reparsed.Mode);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public void RoundTrip_Expression_PreservesMode(bool whileTrue)
|
||||
{
|
||||
var mode = whileTrue ? ScriptTriggerMode.WhileTrue : ScriptTriggerMode.OnTrue;
|
||||
var original = new ScriptTriggerModel
|
||||
{
|
||||
Expression = "Attributes[\"T\"] > 1",
|
||||
Mode = mode
|
||||
};
|
||||
|
||||
var json = ScriptTriggerConfigCodec.Serialize(original, ScriptTriggerKind.Expression);
|
||||
var reparsed = ScriptTriggerConfigCodec.Parse(json, ScriptTriggerKind.Expression);
|
||||
|
||||
Assert.Equal(mode, reparsed.Mode);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user