using CliFx.Attributes; using CliFx.Infrastructure; using Shouldly; using Xunit; namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Cli.Tests; /// /// 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 --cnc-port, --timeout-ms, and /// --interval-ms. /// [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(() => 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(() => 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(() => 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()); } }