fix(driver-focas-cli): resolve Low code-review findings (Driver.FOCAS.Cli-001,002,003,004; -005 deferred)

- Driver.FOCAS.Cli-001: WriteCommand.ParseValue now wraps numeric
  FormatException / OverflowException as CliFx CommandException with
  the offending value.
- Driver.FOCAS.Cli-002: SubscribeCommand's OnDataChange handler and the
  banner both take a writeLock so notification-callback and main-thread
  writes can't interleave; handler exceptions are warn-and-swallow.
- Driver.FOCAS.Cli-003: FocasCommandBase.ValidateOptions rejects
  --cnc-port outside 1..65535, non-positive --timeout-ms, and
  non-positive --interval-ms; ExecuteAsync calls it first.
- Driver.FOCAS.Cli-004: 'await using var driver' is the sole driver
  disposal path; dropped the redundant explicit await ShutdownAsync.
- Driver.FOCAS.Cli-005 (Deferred): the fix lives in
  Driver.Cli.Common.SnapshotFormatter — explicitly naming the
  status-code shortlist there benefits every driver CLI. Left as a
  Driver.Cli.Common follow-up.
- Registered the new tests/Drivers/Cli/ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Cli.Tests
  project in ZB.MOM.WW.OtOpcUa.slnx.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-05-23 11:11:55 -04:00
parent 2a941b255f
commit 6923be3aa2
14 changed files with 629 additions and 65 deletions

View File

@@ -0,0 +1,79 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Cli.Tests;
/// <summary>
/// Driver.FOCAS.Cli-003: numeric options that are not range-checked at the CLI
/// boundary surface either as opaque downstream exceptions or as tight-spinning
/// poll loops rather than a clear "value must be positive" message. These tests
/// pin the validation contract for <c>--cnc-port</c>, <c>--timeout-ms</c>, and
/// <c>--interval-ms</c>.
/// </summary>
[Trait("Category", "Unit")]
public sealed class FocasCommandBaseValidationTests
{
// Test-only FocasCommandBase concrete subclass that exposes the protected ValidateOptions
// helper. The [Command] attribute is required by the CliFx analyzer
// (CliFx_CommandMustBeAnnotated) — this command is never registered with the CLI app
// but the analyzer rule fires for every ICommand implementor in the compilation.
[Command("noop-test", Description = "Test-only probe of FocasCommandBase.ValidateOptions.")]
private sealed class Probe : FocasCommandBase
{
public int IntervalMs { get; init; }
public override ValueTask ExecuteAsync(IConsole console) => default;
public void InvokeValidate() => ValidateOptions(IntervalMs);
public void InvokeValidateNoInterval() => ValidateOptions(intervalMs: null);
}
[Fact]
public void Validate_accepts_default_options()
{
var sut = new Probe { CncHost = "host", IntervalMs = 1000 };
Should.NotThrow(() => sut.InvokeValidate());
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
[InlineData(65536)]
public void Validate_rejects_out_of_range_cnc_port(int port)
{
var sut = new Probe { CncHost = "host", CncPort = port, IntervalMs = 1000 };
var ex = Should.Throw<CliFx.Exceptions.CommandException>(() => sut.InvokeValidate());
ex.Message.ShouldContain("cnc-port", Case.Insensitive);
}
[Theory]
[InlineData(0)]
[InlineData(-100)]
public void Validate_rejects_non_positive_timeout_ms(int timeoutMs)
{
var sut = new Probe { CncHost = "host", TimeoutMs = timeoutMs, IntervalMs = 1000 };
var ex = Should.Throw<CliFx.Exceptions.CommandException>(() => sut.InvokeValidate());
ex.Message.ShouldContain("timeout-ms", Case.Insensitive);
}
[Theory]
[InlineData(0)]
[InlineData(-500)]
public void Validate_rejects_non_positive_interval_ms(int intervalMs)
{
var sut = new Probe { CncHost = "host", IntervalMs = intervalMs };
var ex = Should.Throw<CliFx.Exceptions.CommandException>(() => sut.InvokeValidate());
ex.Message.ShouldContain("interval-ms", Case.Insensitive);
}
[Fact]
public void Validate_skips_interval_check_when_command_omits_it()
{
// probe / read / write don't take an --interval-ms option; the validator must
// skip that check when the caller passes null.
var sut = new Probe { CncHost = "host" };
Should.NotThrow(() => sut.InvokeValidateNoInterval());
}
}