using ZB.MOM.WW.ScadaBridge.Commons.Messages.Streaming;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Alarms;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages;
public class AlarmStateChangedEnrichmentTests
{
/// Verifies that a default alarm state change is of the computed kind with auto-acknowledge.
[Fact]
public void Defaults_AreComputedKind_WithAutoAck()
{
var m = new AlarmStateChanged("inst", "HiAlarm", AlarmState.Active, 700, DateTimeOffset.UnixEpoch);
Assert.Equal(AlarmKind.Computed, m.Kind);
Assert.True(m.Condition.Acknowledged); // computed = auto-acked
Assert.Equal(700, m.Condition.Severity); // severity defaults to Priority
Assert.True(m.Condition.Active); // derived from State
Assert.Equal("", m.SourceReference);
}
/// Verifies that the computed factory maps priority and state onto the message.
[Fact]
public void Factory_ForComputed_MapsPriorityAndState()
{
var c = AlarmConditionStateFactory.ForComputed(AlarmState.Normal, priority: 250);
Assert.False(c.Active);
Assert.True(c.Acknowledged);
Assert.Equal(250, c.Severity);
}
// ── MES alarm-status API §6.4: additive AckTime ──
/// Verifies that the acknowledge time defaults to null when using the positional constructor.
[Fact]
public void AckTime_DefaultsToNull_OnThePositionalConstructor()
{
// Additive-only evolution: every existing positional construction stays valid and
// reports no ack time. A computed alarm is auto-acked but has no operator ack
// event, so null (not the timestamp) is the honest value.
var m = new AlarmStateChanged("inst", "HiAlarm", AlarmState.Active, 700, DateTimeOffset.UnixEpoch);
Assert.True(m.Condition.Acknowledged); // computed = auto-acked…
Assert.Null(m.AckTime); // …yet still no ack instant
}
/// Verifies that the acknowledge time round-trips through its init property.
[Fact]
public void AckTime_RoundTripsThroughTheInitProperty()
{
var ackedAt = new DateTimeOffset(2026, 8, 1, 12, 0, 0, TimeSpan.Zero);
var m = new AlarmStateChanged("inst", "Tank01.Level.HiHi", AlarmState.Active, 900, DateTimeOffset.UnixEpoch)
{
Kind = AlarmKind.NativeOpcUa,
AckTime = ackedAt
};
Assert.Equal(ackedAt, m.AckTime);
// `with` (used by the mirror to synthesise a return-to-normal) preserves it.
Assert.Equal(ackedAt, (m with { State = AlarmState.Normal }).AckTime);
}
}