using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
///
/// Pins the AVEVA priority → (AlarmSeverity, OPC UA numeric) table the live Galaxy alarm feed uses (lmxopcua#887).
/// mxaccessgw sends the AVEVA priority (1 = most urgent … 999 = least) as the alarm's raw severity; the table is
/// AVEVA's default four ranges, most urgent first.
///
public sealed class MxAccessSeverityMapperTests
{
/// Verifies the band table at every edge, plus the out-of-range clamps.
/// The raw value the gateway sends: the AVEVA priority.
/// The expected AlarmSeverity bucket.
/// The expected OPC UA numeric severity value.
[Theory]
[InlineData(1, AlarmSeverity.Critical, 1000)] // AVEVA Critical 1–250
[InlineData(250, AlarmSeverity.Critical, 1000)]
[InlineData(251, AlarmSeverity.High, 750)] // AVEVA High 251–500
[InlineData(500, AlarmSeverity.High, 750)]
[InlineData(501, AlarmSeverity.Medium, 500)] // AVEVA Medium 501–750
[InlineData(750, AlarmSeverity.Medium, 500)]
[InlineData(751, AlarmSeverity.Low, 250)] // AVEVA Low 751–999
[InlineData(999, AlarmSeverity.Low, 250)]
[InlineData(0, AlarmSeverity.Critical, 1000)] // below range: clamped to priority 1
[InlineData(-100, AlarmSeverity.Critical, 1000)]
[InlineData(int.MinValue, AlarmSeverity.Critical, 1000)]
[InlineData(1000, AlarmSeverity.Low, 250)] // above range: clamped to priority 999
[InlineData(int.MaxValue, AlarmSeverity.Low, 250)]
public void Map_assigns_the_AVEVA_priority_band(int rawPriority, AlarmSeverity expectedBucket, int expectedOpcUaSeverity)
{
var (bucket, opcUa) = MxAccessSeverityMapper.Map(rawPriority);
bucket.ShouldBe(expectedBucket);
opcUa.ShouldBe(expectedOpcUaSeverity);
}
///
/// Verifies the mapping is monotonic decreasing over the whole AVEVA range: a more urgent (lower) priority
/// never yields a lower OPC UA severity, and the two ends differ (so a constant mapping fails too).
///
[Fact]
public void Map_never_ranks_a_less_urgent_priority_above_a_more_urgent_one()
{
var previous = MxAccessSeverityMapper.Map(1).OpcUaSeverity;
for (var priority = 2; priority <= 999; priority++)
{
var current = MxAccessSeverityMapper.Map(priority).OpcUaSeverity;
current.ShouldBeLessThanOrEqualTo(previous, $"priority {priority} must not be more severe than {priority - 1}");
previous = current;
}
MxAccessSeverityMapper.Map(1).OpcUaSeverity.ShouldBeGreaterThan(MxAccessSeverityMapper.Map(999).OpcUaSeverity);
}
/// Verifies the live mapper is the shared table history uses (Gitea #886), value for value.
[Fact]
public void Map_is_the_shared_GalaxyAlarmPriority_table()
{
for (var priority = -2; priority <= 1001; priority++)
MxAccessSeverityMapper.Map(priority).ShouldBe(GalaxyAlarmPriority.Map(priority));
}
}