feat(client): AddComment + comment CLI, drop Galaxy-era alarm fallback, serialize CLI tests (#598); Client.Shared off obsolete sync SDK APIs (#621)

Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
Joseph Doherty
2026-10-08 06:53:57 -04:00
parent 3baa332d6b
commit 48bd266e19
18 changed files with 363 additions and 211 deletions
@@ -0,0 +1,6 @@
using Xunit;
// Every command run reassigns the process-global Serilog Log.Logger (CommandBase.ConfigureLogging), and
// LoggerLifecycleTests asserts on that global's dispose lifecycle. A collection attribute on that one class
// cannot serialize it against the ~20 other classes that run commands, so the whole (small) suite runs serially. (#598)
[assembly: CollectionBehavior(DisableTestParallelization = true)]
@@ -0,0 +1,106 @@
using CliFx.Exceptions;
using Opc.Ua;
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 CommentCommandTests
{
private static string HexOf(byte[] bytes) => Convert.ToHexString(bytes);
/// <summary>Verifies that a successful comment prints a success message and passes correct arguments to the service.</summary>
[Fact]
public async Task Execute_Success_PrintsSuccessMessage()
{
var fakeService = new FakeOpcUaClientService
{
AddCommentResult = StatusCodes.Good
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var eventId = new byte[] { 0x0A, 0x1B, 0x2C };
var command = new CommentCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(eventId),
Comment = "operator note"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Comment added");
fakeService.AddCommentCalls.Count.ShouldBe(1);
fakeService.AddCommentCalls[0].ConditionNodeId.ShouldBe("ns=2;s=MyAlarm");
fakeService.AddCommentCalls[0].EventId.ShouldBe(eventId);
fakeService.AddCommentCalls[0].Comment.ShouldBe("operator note");
}
/// <summary>Verifies that a bad StatusCode from the service prints a failure message.</summary>
[Fact]
public async Task Execute_BadStatus_PrintsFailureMessage()
{
var fakeService = new FakeOpcUaClientService
{
AddCommentResult = StatusCodes.BadNotSupported
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new CommentCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(new byte[] { 0xFF }),
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Comment failed");
}
/// <summary>Verifies that an invalid hex event-id throws CommandException.</summary>
[Fact]
public async Task Execute_InvalidEventId_ThrowsCommandException()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new CommentCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = "ZZZ-not-hex",
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
var ex = await Should.ThrowAsync<CommandException>(async () => await command.ExecuteAsync(console));
ex.Message.ShouldContain("event-id", Case.Insensitive);
}
/// <summary>Verifies that the command disconnects in the finally block.</summary>
[Fact]
public async Task Execute_DisconnectsInFinally()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new CommentCommand(factory)
{
Url = "opc.tcp://localhost:4840",
NodeId = "ns=2;s=MyAlarm",
EventId = HexOf(new byte[] { 0x01 }),
Comment = ""
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
}
@@ -264,6 +264,20 @@ public sealed class FakeOpcUaClientService : IOpcUaClientService
return Task.FromResult(ConfirmAlarmResult);
}
/// <summary>Gets the list of (conditionNodeId, eventId, comment) tuples from AddCommentAsync calls.</summary>
public List<(string ConditionNodeId, byte[] EventId, string Comment)> AddCommentCalls { get; } = [];
/// <summary>Gets or sets the status code returned by AddCommentAsync.</summary>
public StatusCode AddCommentResult { get; set; } = StatusCodes.Good;
/// <inheritdoc />
public Task<StatusCode> AddCommentAsync(string conditionNodeId, byte[] eventId, string comment,
CancellationToken ct = default)
{
AddCommentCalls.Add((conditionNodeId, eventId, comment));
return Task.FromResult(AddCommentResult);
}
/// <summary>Gets the list of (conditionNodeId, kind, durationSeconds) tuples from ShelveAlarmAsync calls.</summary>
public List<(string ConditionNodeId, ShelveKind Kind, double DurationSeconds)> ShelveAlarmCalls { get; } = [];