Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests/ScriptContextTests.cs
Joseph Doherty 0a20de728d fix(core-scripting): resolve Low code-review findings (Core.Scripting-005,006,008,009,011)
- Core.Scripting-005: DependencyExtractor.HandleTagCall now recognises
  raw-string literal paths by checking the StringLiteralExpression node
  kind instead of the legacy StringLiteralToken kind.
- Core.Scripting-006: scope CompiledScriptCache failed-compile eviction
  with TryRemove(KeyValuePair) so a racing retry entry is not evicted.
- Core.Scripting-008: document the per-publish assembly accretion as an
  accepted limitation in docs/VirtualTags.md.
- Core.Scripting-009: enumerate the authoritative deny-list (namespace
  prefixes + type-granular denies) in the Phase 7 decision-#6 entry to
  match ForbiddenTypeAnalyzer.
- Core.Scripting-011: pin ScriptSandbox.Build, ScriptContext.Deadband
  boundary semantics, and end-to-end factory + companion-sink
  integration.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 07:23:42 -04:00

71 lines
2.9 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting.Tests;
/// <summary>
/// Locks the boundary semantics of <see cref="ScriptContext.Deadband"/>. The helper
/// is the canonical "ignore small noise" predicate alarm authors compose into bigger
/// expressions; subtle sign / boundary changes here would silently move the
/// active-state edge of every consuming alarm. (Core.Scripting-011.)
/// </summary>
[Trait("Category", "Unit")]
public sealed class ScriptContextTests
{
[Fact]
public void Deadband_returns_false_when_difference_equals_tolerance()
{
// Strict greater-than comparison: a delta exactly equal to tolerance is
// considered "within deadband" and must NOT trip.
ScriptContext.Deadband(current: 10.5, previous: 10.0, tolerance: 0.5).ShouldBeFalse();
}
[Fact]
public void Deadband_returns_true_when_difference_just_exceeds_tolerance()
{
// Any delta strictly greater than tolerance trips the deadband.
ScriptContext.Deadband(current: 10.6, previous: 10.0, tolerance: 0.5).ShouldBeTrue();
}
[Fact]
public void Deadband_returns_false_when_difference_just_below_tolerance()
{
ScriptContext.Deadband(current: 10.4, previous: 10.0, tolerance: 0.5).ShouldBeFalse();
}
[Fact]
public void Deadband_is_symmetric_in_direction_of_change()
{
// Math.Abs ensures the helper trips identically whether the value rose or fell.
ScriptContext.Deadband(current: 9.0, previous: 10.0, tolerance: 0.5).ShouldBeTrue();
ScriptContext.Deadband(current: 11.0, previous: 10.0, tolerance: 0.5).ShouldBeTrue();
}
[Fact]
public void Deadband_returns_false_when_values_are_equal()
{
ScriptContext.Deadband(current: 10.0, previous: 10.0, tolerance: 0.001).ShouldBeFalse();
}
[Fact]
public void Deadband_with_zero_tolerance_returns_true_for_any_difference()
{
// Zero-tolerance is the "trip on any non-equal change" mode. Equality still
// returns false (delta 0 is not strictly greater than 0).
ScriptContext.Deadband(current: 10.0, previous: 10.0, tolerance: 0).ShouldBeFalse();
ScriptContext.Deadband(current: 10.0, previous: 10.000001, tolerance: 0).ShouldBeTrue();
}
[Fact]
public void Deadband_with_negative_tolerance_always_trips_for_unequal_values()
{
// A negative tolerance is nonsensical input but the helper does not guard
// against it — Math.Abs(delta) is always >= 0, so the comparison is
// effectively "any non-equal change". Lock this so a refactor that adds
// (or removes) input validation requires an explicit test update.
ScriptContext.Deadband(current: 10.0, previous: 10.5, tolerance: -1.0).ShouldBeTrue();
ScriptContext.Deadband(current: 10.0, previous: 10.0, tolerance: -1.0).ShouldBeTrue();
}
}