b61f3e0560
v2-ci / unit-tests (push) Has been cancelled
GalaxyAlarmPriority, the one table live Galaxy alarms (MxAccessSeverityMapper) and AVEVA A&E history (EventMapper, #886) map the AVEVA priority through, ranked a higher number as more severe. AVEVA ranks priority 1 as the most urgent, so a priority-1 alarm read 250 and a priority-999 system event 1000. It now uses AVEVA's default priority ranges, most urgent first: AVEVA priority AVEVA severity bucket OPC UA Severity 1-250 1 Critical Critical 1000 251-500 2 High High 750 501-750 3 Medium Medium 500 751-999 4 Low Low 250 (history only) 0 not an alarm - 1 Out-of-range priorities clamp (<= 0 Critical, > 999 Low). Evidence, vd03 Runtime.dbo.Events read-only: priority 1/100 alarms carry AVEVA severity 1, 400/500 severity 2, 600/700 severity 3, and every priority-999 System.* / User.* row severity 0 with IsAlarm 0. - Live and history now use the same number. GalaxyDriver passes the table's exact value as AlarmEventArgs.OpcUaSeverity. Before, it passed only the bucket, which NativeAlarmProjector re-mapped through its generic 200/500/700/900 ladder, so live and history disagreed even before the direction fix. - History: a row whose AVEVA severity is 0 (System.Start/Stop/Deploy, User.Write...) is not an alarm and reads Severity 1, below every alarm band. For alarms AVEVA's 1-4 severity is still never the value: the live feed carries only the priority, so it cannot use it, and history must agree. - Docs: docs/Historian.md gains "Galaxy alarm priority -> Severity (lmxopcua#887)"; docs/AlarmTracking.md updated. - Live re-run of the #801/#886 harness against vd03 HistorianGateway ea9f21c (read-only, network-only): 224 of 233 cases identical to the #886 run, the 9 others differ only in Severity (priority-700 alarm rows 750 -> 500, 19 system/user events 1000 -> 1). Record: aveva-history-801.md "#887 re-run", report aveva-gate-sdk2-887.json.gz. Client-visible: every OPC UA client that ranks or filters Galaxy alarms by Severity sees the order reversed (live and HistoryReadEvents). ScadaBridge has no code that depends on the old values; its own mixed severity directions are ScadaBridge#129. Tests updated because they pinned the inverted table: - Driver.Galaxy.Tests MxAccessSeverityMapperTests (rewritten: edges 1/250/251/ 500/501/750/751/999, 0/-100/int.MinValue/1000/int.MaxValue clamps, monotonic-decreasing sweep, equals GalaxyAlarmPriority for -2..1001) - Driver.Galaxy.Tests GatewayGalaxyAlarmFeedTests (750 and 600 -> Medium) - Driver.Historian.Gateway.Tests EventMapperTests (band table, priority 999 -> 250, 1 -> 1000; system events -> 1) - OpcUaServer.IntegrationTests GalaxyEquipmentEventHistoryWireTests (700 -> 500, 100 -> 1000, System.Stop -> 1; the fake now stores AVEVA's real 0-4 severity) New tests: - Runtime.Tests NativeAlarmRestatementEndToEndTests .Galaxy_live_condition_severity_is_the_AVEVA_priority_band (8 rows: the real feed -> real GalaxyDriver -> host -> projector; the same rows EventMapperTests pins on the history path) - EventMapperTests.Aveva_severity_zero_marks_an_event_that_is_not_an_alarm (5 rows, incl. the priority-999 alarm positive control) - GalaxyDriverAlarmSourceTests: OpcUaSeverity passes through Mutation check, 8 mutants, all killed: the pre-#887 inverted table; the driver dropping OpcUaSeverity; no not-an-alarm rule; each of the three band edges off by one; history ignoring priority; a missing AVEVA severity read as 0. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
267 lines
11 KiB
C#
267 lines
11 KiB
C#
using System.Runtime.CompilerServices;
|
||
using Google.Protobuf.WellKnownTypes;
|
||
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
||
using Shouldly;
|
||
using Xunit;
|
||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
|
||
|
||
// Core.Abstractions now also defines an AlarmTransitionKind (the AlarmEventArgs surface
|
||
// kind). This file drives the gateway proto, so pin the bare token to the proto enum to
|
||
// preserve the existing references unchanged.
|
||
using AlarmTransitionKind = ZB.MOM.WW.MxGateway.Contracts.Proto.AlarmTransitionKind;
|
||
|
||
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Runtime;
|
||
|
||
/// <summary>
|
||
/// Pins <see cref="GatewayGalaxyAlarmFeed"/> — the session-less consumer of the
|
||
/// gateway's <c>StreamAlarms</c> feed. Synthetic <see cref="AlarmFeedMessage"/>s go
|
||
/// in through the stream-factory seam; the feed fires <c>OnAlarmTransition</c> with
|
||
/// decoded payloads and mapped severity buckets, drops malformed messages, and
|
||
/// re-opens the stream after a transport fault.
|
||
/// </summary>
|
||
public sealed class GatewayGalaxyAlarmFeedTests
|
||
{
|
||
/// <summary>Verifies that the feed decodes active alarm snapshots and live transitions.</summary>
|
||
[Fact]
|
||
public async Task Decodes_active_alarm_snapshot_then_live_transition()
|
||
{
|
||
var raise = new DateTime(2026, 5, 1, 12, 0, 0, DateTimeKind.Utc);
|
||
var messages = new[]
|
||
{
|
||
SnapshotMessage("Tank01.Level.HiHi", AlarmConditionState.Active, severity: 750,
|
||
lastTransition: raise),
|
||
SnapshotMessage("Tank02.Level.HiHi", AlarmConditionState.ActiveAcked, severity: 500,
|
||
lastTransition: raise, operatorUser: "alice", operatorComment: "investigating"),
|
||
new AlarmFeedMessage { SnapshotComplete = true },
|
||
TransitionMessage("Tank01.Level.HiHi", AlarmTransitionKind.Clear, severity: 750,
|
||
transitionTime: raise.AddMinutes(5), originalRaise: raise),
|
||
};
|
||
|
||
var observed = new List<GalaxyAlarmTransition>();
|
||
var got3 = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||
|
||
await using var feed = new GatewayGalaxyAlarmFeed(
|
||
(_, ct) => OpenStream(messages, ct), clientName: "FeedTest");
|
||
feed.OnAlarmTransition += (_, t) =>
|
||
{
|
||
lock (observed)
|
||
{
|
||
observed.Add(t);
|
||
if (observed.Count == 3) got3.TrySetResult(true);
|
||
}
|
||
};
|
||
feed.Start();
|
||
|
||
await TestWait.ForAsync(got3.Task, "snapshot + transition should dispatch",
|
||
() => { lock (observed) return $"dispatched={observed.Count}/3"; });
|
||
|
||
observed.Count.ShouldBe(3);
|
||
|
||
// Active (unacked) snapshot entry → Retrigger (#647 review: forces the upstream's unack on the host).
|
||
observed[0].AlarmFullReference.ShouldBe("Tank01.Level.HiHi");
|
||
observed[0].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.Retrigger);
|
||
observed[0].SeverityBucket.ShouldBe(AlarmSeverity.Medium); // priority 750: AVEVA Medium 501–750 (#887)
|
||
observed[0].OpcUaSeverity.ShouldBe(500);
|
||
observed[0].RawMxAccessSeverity.ShouldBe(750);
|
||
|
||
// Acknowledged snapshot entry → ActiveAcknowledged (#656 — an Acknowledge kept the host's prior Active, so a
|
||
// cold host projected it INACTIVE), operator fields preserved.
|
||
observed[1].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.ActiveAcknowledged);
|
||
observed[1].OperatorUser.ShouldBe("alice");
|
||
observed[1].OperatorComment.ShouldBe("investigating");
|
||
|
||
// #656: snapshot entries re-state upstream state; the live transition is unflagged.
|
||
observed[0].IsRestatement.ShouldBeTrue();
|
||
observed[1].IsRestatement.ShouldBeTrue();
|
||
|
||
// Live transition after snapshot_complete.
|
||
observed[2].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.Clear);
|
||
observed[2].OriginalRaiseTimestampUtc.ShouldBe(raise);
|
||
observed[2].IsRestatement.ShouldBeFalse();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gitea #656 — an Inactive snapshot entry is a re-stated Clear; a LIVE Retrigger (a genuine MXAccess
|
||
/// re-occurrence) is never flagged, so the host keeps its history row.
|
||
/// </summary>
|
||
[Fact]
|
||
public async Task Inactive_snapshot_entry_is_a_restated_clear_and_a_live_retrigger_is_unflagged()
|
||
{
|
||
var at = new DateTime(2026, 5, 1, 12, 0, 0, DateTimeKind.Utc);
|
||
var messages = new[]
|
||
{
|
||
SnapshotMessage("Tank01.Level.HiHi", AlarmConditionState.Inactive, severity: 750, lastTransition: at),
|
||
new AlarmFeedMessage { SnapshotComplete = true },
|
||
TransitionMessage("Tank01.Level.HiHi", AlarmTransitionKind.Retrigger, severity: 750,
|
||
transitionTime: at.AddMinutes(1)),
|
||
};
|
||
|
||
var observed = new List<GalaxyAlarmTransition>();
|
||
var got2 = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||
await using var feed = new GatewayGalaxyAlarmFeed((_, ct) => OpenStream(messages, ct), clientName: "FeedTest656");
|
||
feed.OnAlarmTransition += (_, t) =>
|
||
{
|
||
lock (observed)
|
||
{
|
||
observed.Add(t);
|
||
if (observed.Count == 2) got2.TrySetResult(true);
|
||
}
|
||
};
|
||
feed.Start();
|
||
|
||
await TestWait.ForAsync(got2.Task, "restated snapshot Clear + live Retrigger should dispatch",
|
||
() => { lock (observed) return $"dispatched={observed.Count}/2"; });
|
||
|
||
observed[0].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.Clear);
|
||
observed[0].IsRestatement.ShouldBeTrue();
|
||
observed[1].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.Retrigger);
|
||
observed[1].IsRestatement.ShouldBeFalse();
|
||
}
|
||
|
||
/// <summary>Verifies that the feed drops transitions with unspecified kind and empty messages.</summary>
|
||
[Fact]
|
||
public async Task Drops_transition_with_unspecified_kind_and_empty_message()
|
||
{
|
||
var messages = new[]
|
||
{
|
||
TransitionMessage("Tank01.Level.HiHi", AlarmTransitionKind.Unspecified, severity: 100,
|
||
transitionTime: DateTime.UtcNow),
|
||
new AlarmFeedMessage(), // empty oneof — version skew
|
||
TransitionMessage("Tank01.Level.HiHi", AlarmTransitionKind.Raise, severity: 600,
|
||
transitionTime: DateTime.UtcNow),
|
||
};
|
||
|
||
var observed = new List<GalaxyAlarmTransition>();
|
||
var gotOne = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||
|
||
await using var feed = new GatewayGalaxyAlarmFeed(
|
||
(_, ct) => OpenStream(messages, ct), clientName: "FeedTest");
|
||
feed.OnAlarmTransition += (_, t) =>
|
||
{
|
||
lock (observed)
|
||
{
|
||
observed.Add(t);
|
||
gotOne.TrySetResult(true);
|
||
}
|
||
};
|
||
feed.Start();
|
||
|
||
await TestWait.ForAsync(gotOne.Task, "the well-formed Raise should dispatch",
|
||
() => { lock (observed) return $"dispatched={observed.Count}"; });
|
||
|
||
// Only the well-formed Raise survives; the Unspecified + empty messages drop.
|
||
observed.ShouldHaveSingleItem();
|
||
observed[0].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.Raise);
|
||
observed[0].SeverityBucket.ShouldBe(AlarmSeverity.Medium); // priority 600: AVEVA Medium 501–750 (#887)
|
||
}
|
||
|
||
/// <summary>Verifies that the feed reopens the stream after a transport fault.</summary>
|
||
[Fact]
|
||
public async Task Reopens_stream_after_a_transport_fault()
|
||
{
|
||
var calls = 0;
|
||
var liveTransition = new[]
|
||
{
|
||
TransitionMessage("Tank01.Level.HiHi", AlarmTransitionKind.Raise, severity: 750,
|
||
transitionTime: DateTime.UtcNow),
|
||
};
|
||
|
||
var observed = new List<GalaxyAlarmTransition>();
|
||
var gotOne = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||
|
||
await using var feed = new GatewayGalaxyAlarmFeed(
|
||
(_, ct) =>
|
||
{
|
||
// First open faults; the feed must reconnect and succeed on the retry.
|
||
if (Interlocked.Increment(ref calls) == 1)
|
||
{
|
||
throw new InvalidOperationException("synthetic stream fault");
|
||
}
|
||
return OpenStream(liveTransition, ct);
|
||
},
|
||
clientName: "ReconnectTest",
|
||
reconnectDelay: TimeSpan.FromMilliseconds(20));
|
||
feed.OnAlarmTransition += (_, t) =>
|
||
{
|
||
observed.Add(t);
|
||
gotOne.TrySetResult(true);
|
||
};
|
||
feed.Start();
|
||
|
||
await TestWait.ForAsync(gotOne.Task, "the feed should reopen the stream and deliver after a fault",
|
||
() => $"streamOpens={Volatile.Read(ref calls)}");
|
||
|
||
calls.ShouldBeGreaterThanOrEqualTo(2);
|
||
observed.ShouldHaveSingleItem();
|
||
observed[0].TransitionKind.ShouldBe(GalaxyAlarmTransitionKind.Raise);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Yields each message in order, then holds the stream open until the feed is
|
||
/// disposed — mirrors a live server-streaming RPC that does not complete on its
|
||
/// own.
|
||
/// </summary>
|
||
private static async IAsyncEnumerable<AlarmFeedMessage> OpenStream(
|
||
IEnumerable<AlarmFeedMessage> messages,
|
||
[EnumeratorCancellation] CancellationToken ct = default)
|
||
{
|
||
foreach (var message in messages)
|
||
{
|
||
ct.ThrowIfCancellationRequested();
|
||
yield return message;
|
||
await Task.Yield();
|
||
}
|
||
await Task.Delay(Timeout.Infinite, ct);
|
||
}
|
||
|
||
private static AlarmFeedMessage SnapshotMessage(
|
||
string fullReference,
|
||
AlarmConditionState state,
|
||
int severity,
|
||
DateTime lastTransition,
|
||
string operatorUser = "",
|
||
string operatorComment = "")
|
||
=> new()
|
||
{
|
||
ActiveAlarm = new ActiveAlarmSnapshot
|
||
{
|
||
AlarmFullReference = fullReference,
|
||
SourceObjectReference = fullReference.Split('.')[0],
|
||
AlarmTypeName = "AnalogLimitAlarm.HiHi",
|
||
Severity = severity,
|
||
CurrentState = state,
|
||
Category = "Process",
|
||
Description = "Tank high-high level",
|
||
LastTransitionTimestamp = Timestamp.FromDateTime(lastTransition),
|
||
OperatorUser = operatorUser,
|
||
OperatorComment = operatorComment,
|
||
},
|
||
};
|
||
|
||
private static AlarmFeedMessage TransitionMessage(
|
||
string fullReference,
|
||
AlarmTransitionKind kind,
|
||
int severity,
|
||
DateTime transitionTime,
|
||
DateTime? originalRaise = null)
|
||
{
|
||
var body = new OnAlarmTransitionEvent
|
||
{
|
||
AlarmFullReference = fullReference,
|
||
SourceObjectReference = fullReference.Split('.')[0],
|
||
AlarmTypeName = "AnalogLimitAlarm.HiHi",
|
||
TransitionKind = kind,
|
||
Severity = severity,
|
||
TransitionTimestamp = Timestamp.FromDateTime(transitionTime),
|
||
Category = "Process",
|
||
Description = "Tank high-high level",
|
||
};
|
||
if (originalRaise is { } orts)
|
||
{
|
||
body.OriginalRaiseTimestamp = Timestamp.FromDateTime(orts);
|
||
}
|
||
return new AlarmFeedMessage { Transition = body };
|
||
}
|
||
}
|