diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/ScriptedAlarms/ScriptedAlarmHostActorTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/ScriptedAlarms/ScriptedAlarmHostActorTests.cs index 455299d3e..6d4be94e7 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/ScriptedAlarms/ScriptedAlarmHostActorTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/ScriptedAlarms/ScriptedAlarmHostActorTests.cs @@ -975,4 +975,66 @@ public sealed class ScriptedAlarmHostActorTests : RuntimeActorTestBase publish.ExpectNoMsg(TimeSpan.FromMilliseconds(500), TestContext.Current.CancellationToken); } + + // --------------------------------------------------------------------------------------------- + // #935 — a redeploy is a restatement. The production store (LocalDb) has no Active column, so the + // engine used to re-seed a still-active alarm Inactive on every ApplyScriptedAlarms and re-activate it: + // Acked reset, an Activated row on `alerts`, and a re-assert stamped with the deploy instant. + // --------------------------------------------------------------------------------------------- + + /// Mirrors LocalDbAlarmConditionStateStore: a loaded row is always Inactive. + private sealed class ActiveDroppingStore : IAlarmStateStore + { + private readonly InMemoryAlarmStateStore _inner = new(); + + public async Task LoadAsync(string alarmId, CancellationToken ct) + => await _inner.LoadAsync(alarmId, ct) is { } row + ? row with { Active = AlarmActiveState.Inactive, LastActiveUtc = null, LastClearedUtc = null } + : null; + + public Task> LoadAllAsync(CancellationToken ct) => _inner.LoadAllAsync(ct); + public Task SaveAsync(AlarmConditionState state, CancellationToken ct) => _inner.SaveAsync(state, ct); + public Task RemoveAsync(string alarmId, CancellationToken ct) => _inner.RemoveAsync(alarmId, ct); + } + + /// #935: an acknowledged active alarm, redeployed, is re-asserted still Active + Acknowledged with the + /// timestamp of its last real transition (the ack), and nothing reaches alerts. + [Fact] + public void A_redeploy_restates_an_acked_active_alarm_without_reactivating_it() + { + var publish = CreateTestProbe(); + var mux = CreateTestProbe(); + var alerts = CreateTestProbe(); + SubscribeToAlerts(alerts); + var ct = TestContext.Current.CancellationToken; + + var (host, _) = Spawn(publish, mux, store: new ActiveDroppingStore()); + host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms(new[] { Plan() })); + mux.ExpectMsg(Timeout, cancellationToken: ct); + + host.Tell(new VirtualTagActor.DependencyValueChanged("M.T", 99, DateTime.UtcNow)); + publish.FishForMessage( + m => m.State.Active && !m.State.Acknowledged, Timeout, cancellationToken: ct); + alerts.FishForMessage(e => e.TransitionKind == "Activated", Timeout, cancellationToken: ct); + + host.Tell(new AlarmCommand(AlarmId: "alm-1", Operation: "Acknowledge", User: "alice", Comment: null, UnshelveAtUtc: null)); + publish.FishForMessage(m => m.State.Acknowledged, Timeout, cancellationToken: ct); + alerts.FishForMessage(e => e.TransitionKind == "Acknowledged", Timeout, cancellationToken: ct); + + // The deploy. + var deployAt = DateTime.UtcNow; + host.Tell(new ScriptedAlarmHostActor.ApplyScriptedAlarms(new[] { Plan() })); + mux.ExpectMsg(Timeout, cancellationToken: ct); + + var reasserted = publish.ExpectMsg(Timeout, cancellationToken: ct); + reasserted.State.Active.ShouldBeTrue(); + reasserted.State.Acknowledged.ShouldBeTrue("a deploy must not un-acknowledge an operator-acked alarm"); + // The re-assert carries LastTransitionUtc — the ack instant, before the deploy (it used to be the load instant). + reasserted.TimestampUtc.ShouldBeLessThan(deployAt, "condition Time stays at its last real transition, not the deploy"); + // Known-state semantics unchanged (A1): the reload's held established-flip lands AFTER the re-assert… + publish.ExpectMsg(Timeout, cancellationToken: ct).Known.ShouldBeTrue(); + // …and nothing else follows: no re-activation projection, no alerts row. + publish.ExpectNoMsg(TimeSpan.FromMilliseconds(500), ct); + alerts.ExpectNoMsg(TimeSpan.FromMilliseconds(500), ct); + } }