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.
70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
using Bunit;
|
|
using Microsoft.AspNetCore.Components;
|
|
using ScadaLink.CentralUI.Components.Shared;
|
|
|
|
namespace ScadaLink.CentralUI.Tests.Shared;
|
|
|
|
/// <summary>
|
|
/// Component tests for the OnTrue/WhileTrue mode selector that
|
|
/// <see cref="ScriptTriggerEditor"/> exposes for Conditional and Expression
|
|
/// triggers.
|
|
/// </summary>
|
|
public class ScriptTriggerEditorTests : BunitContext
|
|
{
|
|
private const string ConditionalConfig =
|
|
@"{""attributeName"":""Temp"",""operator"":"">"",""threshold"":50}";
|
|
|
|
private const string ConditionalWhileTrueConfig =
|
|
@"{""attributeName"":""Temp"",""operator"":"">"",""threshold"":50,""mode"":""WhileTrue""}";
|
|
|
|
[Fact]
|
|
public void SelectingWhileTrue_EmitsConfigWithWhileTrueMode()
|
|
{
|
|
ScriptTriggerValue? captured = null;
|
|
var cut = Render<ScriptTriggerEditor>(ps => ps
|
|
.Add(p => p.TriggerType, "Conditional")
|
|
.Add(p => p.TriggerConfig, ConditionalConfig)
|
|
.Add(p => p.Changed,
|
|
EventCallback.Factory.Create<ScriptTriggerValue>(this, v => captured = v)));
|
|
|
|
cut.Find("#script-trigger-mode").Change("WhileTrue");
|
|
|
|
Assert.NotNull(captured);
|
|
Assert.Contains("\"mode\":\"WhileTrue\"", captured!.Config);
|
|
}
|
|
|
|
[Fact]
|
|
public void ModeSelector_DefaultsToOnTrue_WhenConfigHasNoMode()
|
|
{
|
|
ScriptTriggerValue? captured = null;
|
|
var cut = Render<ScriptTriggerEditor>(ps => ps
|
|
.Add(p => p.TriggerType, "Conditional")
|
|
.Add(p => p.TriggerConfig, ConditionalConfig)
|
|
.Add(p => p.Changed,
|
|
EventCallback.Factory.Create<ScriptTriggerValue>(this, v => captured = v)));
|
|
|
|
// Change the threshold to force an emit without touching the mode.
|
|
cut.Find("input[type=number]").Input("75");
|
|
|
|
Assert.NotNull(captured);
|
|
Assert.Contains("\"mode\":\"OnTrue\"", captured!.Config);
|
|
}
|
|
|
|
[Fact]
|
|
public void LoadedWhileTrueMode_IsRetainedAcrossAnUnrelatedEdit()
|
|
{
|
|
ScriptTriggerValue? captured = null;
|
|
var cut = Render<ScriptTriggerEditor>(ps => ps
|
|
.Add(p => p.TriggerType, "Conditional")
|
|
.Add(p => p.TriggerConfig, ConditionalWhileTrueConfig)
|
|
.Add(p => p.Changed,
|
|
EventCallback.Factory.Create<ScriptTriggerValue>(this, v => captured = v)));
|
|
|
|
// Editing the threshold must not silently drop the loaded WhileTrue mode.
|
|
cut.Find("input[type=number]").Input("75");
|
|
|
|
Assert.NotNull(captured);
|
|
Assert.Contains("\"mode\":\"WhileTrue\"", captured!.Config);
|
|
}
|
|
}
|