Files
lmxopcua/tests/Client/ZB.MOM.WW.OtOpcUa.Client.CLI.Tests/AlarmsCommandTests.cs
T
Joseph Doherty c6ab783cb4 fix(cli): alarms prints ConditionNodeId + EventId so ack/confirm are usable
The ack command requires both and nothing in the CLI surfaced them (found in the hw2 FOCAS live gate).

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
2026-10-08 21:25:56 -04:00

180 lines
6.5 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Commands;
using ZB.MOM.WW.OtOpcUa.Client.CLI.Tests.Fakes;
namespace ZB.MOM.WW.OtOpcUa.Client.CLI.Tests;
public class AlarmsCommandTests
{
/// <summary>Verifies that Execute subscribes to alarms.</summary>
[Fact]
public async Task Execute_SubscribesToAlarms()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
Interval = 2000
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
console.RequestCancellation();
await task;
fakeService.SubscribeAlarmsCalls.Count.ShouldBe(1);
fakeService.SubscribeAlarmsCalls[0].IntervalMs.ShouldBe(2000);
fakeService.SubscribeAlarmsCalls[0].SourceNodeId.ShouldBeNull();
}
/// <summary>Verifies that Execute with node passes source node ID.</summary>
[Fact]
public async Task Execute_WithNode_PassesSourceNodeId()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=AlarmSource"
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
console.RequestCancellation();
await task;
fakeService.SubscribeAlarmsCalls.Count.ShouldBe(1);
fakeService.SubscribeAlarmsCalls[0].SourceNodeId.ShouldNotBeNull();
fakeService.SubscribeAlarmsCalls[0].SourceNodeId!.Identifier.ShouldBe("AlarmSource");
}
/// <summary>Verifies that Execute with refresh requests condition refresh.</summary>
[Fact]
public async Task Execute_WithRefresh_RequestsConditionRefresh()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
Refresh = true
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
console.RequestCancellation();
await task;
fakeService.RequestConditionRefreshCalled.ShouldBeTrue();
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Condition refresh requested.");
}
/// <summary>The printed alarm carries the ConditionNodeId and EventId that `ack` requires (hex round-trips).</summary>
[Fact]
public async Task Execute_PrintsConditionNodeIdAndEventId_ForAck()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory) { Url = "opc.tcp://localhost:4840" };
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
fakeService.RaiseAlarmEvent(new Client.Shared.Models.AlarmEventArgs(
"src", "cond", 500, "msg", true, true, false, DateTime.UtcNow,
eventId: [0xAB, 0x01, 0xFF], conditionNodeId: "ns=2;s=Plant/cond"));
await Task.Delay(500);
console.RequestCancellation();
await task;
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("ConditionNodeId: ns=2;s=Plant/cond");
output.ShouldContain("EventId: AB01FF");
}
/// <summary>Verifies that refresh failure prints error.</summary>
[Fact]
public async Task Execute_RefreshFailure_PrintsError()
{
var fakeService = new FakeOpcUaClientService
{
ConditionRefreshException = new NotSupportedException("Not supported")
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840",
Refresh = true
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
console.RequestCancellation();
await task;
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Condition refresh not supported:");
}
/// <summary>Verifies that Execute unsubscribes on cancellation.</summary>
[Fact]
public async Task Execute_UnsubscribesOnCancellation()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
console.RequestCancellation();
await task;
fakeService.UnsubscribeAlarmsCalled.ShouldBeTrue();
}
/// <summary>Verifies that Execute disconnects in finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new AlarmsCommand(factory)
{
Url = "opc.tcp://localhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
var task = Task.Run(async () => { await command.ExecuteAsync(console); });
await fakeService.SubscribeAlarmsInvoked.WaitAsync(TimeSpan.FromSeconds(10));
console.RequestCancellation();
await task;
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}