using Google.Protobuf.WellKnownTypes;
using ZB.MOM.WW.MxGateway.Client;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
using CommonsTransitionKind = ZB.MOM.WW.ScadaBridge.Commons.Types.Enums.AlarmTransitionKind;
using ProtoConditionState = ZB.MOM.WW.MxGateway.Contracts.Proto.AlarmConditionState;
using ProtoTransitionKind = ZB.MOM.WW.MxGateway.Contracts.Proto.AlarmTransitionKind;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;
/// Pure MxGateway alarm-feed proto → NativeAlarmTransition mapping.
public class MxGatewayAlarmMapperTests
{
/// Verifies that an acknowledge transition is mapped as acknowledged with the operator.
[Fact]
public void MapTransition_AckTransition_IsAcknowledgedWithOperator()
{
var ev = new OnAlarmTransitionEvent
{
AlarmFullReference = "Tank01.Level.HiHi",
SourceObjectReference = "Tank01",
AlarmTypeName = "AnalogLimitAlarm.HiHi",
TransitionKind = ProtoTransitionKind.Acknowledge,
Severity = 600,
OperatorUser = "operator1",
OperatorComment = "ack",
Category = "Process",
Description = "hi"
};
var t = MxGatewayAlarmMapper.MapTransition(ev);
Assert.Equal(CommonsTransitionKind.Acknowledge, t.Kind);
Assert.True(t.Condition.Active);
Assert.True(t.Condition.Acknowledged);
Assert.Equal(600, t.Condition.Severity);
Assert.Equal("operator1", t.OperatorUser);
Assert.Equal("Tank01", t.SourceObjectReference);
}
/// Verifies that an active, acknowledged condition state maps to active true and acknowledged true.
[Fact]
public void MapConditionState_ActiveAcked_To_ActiveTrue_AckTrue()
{
var c = MxGatewayAlarmMapper.MapConditionState(ProtoConditionState.ActiveAcked, severity: 600);
Assert.True(c.Active);
Assert.True(c.Acknowledged);
Assert.Equal(600, c.Severity);
}
/// Verifies that an active, unacknowledged snapshot maps to the snapshot kind.
[Fact]
public void MapSnapshot_ActiveUnacked_IsSnapshotKind()
{
var snap = new ActiveAlarmSnapshot
{
AlarmFullReference = "Tank01.Level.Hi",
SourceObjectReference = "Tank01",
AlarmTypeName = "AnalogLimitAlarm.Hi",
CurrentState = ProtoConditionState.Active,
Severity = 1500 // out of range — must clamp
};
var t = MxGatewayAlarmMapper.MapSnapshot(snap);
Assert.Equal(CommonsTransitionKind.Snapshot, t.Kind);
Assert.True(t.Condition.Active);
Assert.False(t.Condition.Acknowledged);
Assert.Equal(1000, t.Condition.Severity);
}
/// Verifies that the source reference is object-relative rather than the full provider reference.
[Fact]
public void SourceReference_IsObjectRelative_NotFullProviderReference()
{
// The condition identity surfaced upward is the object-relative reference
// (e.g. "Z28061.HeartbeatTimeoutAlarm"), not the gateway's full provider
// reference ("Galaxy!.