using System.Globalization; 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 WriteCommandTests { /// Verifies that an explicit UInt16 write does not require read access. [Fact] public async Task Execute_ExplicitUInt16BypassesReadAndWritesUShort() { var fakeService = new FakeOpcUaClientService { ReadException = new InvalidOperationException("Value read is not authorized.") }; var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService)) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=WriteOnly", Value = "65535", Type = "UInt16" }; using var console = TestConsoleHelper.CreateConsole(); await command.ExecuteAsync(console); fakeService.ReadNodeIds.ShouldBeEmpty(); fakeService.WriteValues.Count.ShouldBe(1); fakeService.WriteValues[0].Value.ShouldBeOfType().ShouldBe(ushort.MaxValue); fakeService.DisconnectCalled.ShouldBeTrue(); fakeService.DisposeCalled.ShouldBeTrue(); } /// Verifies the explicit scalar set and invariant numeric conversion. [Theory] [InlineData("Boolean", "yes", typeof(bool))] [InlineData("Byte", "255", typeof(byte))] [InlineData("SByte", "-128", typeof(sbyte))] [InlineData("Int16", "-32768", typeof(short))] [InlineData("UInt16", "65535", typeof(ushort))] [InlineData("Int32", "-2147483648", typeof(int))] [InlineData("UInt32", "4294967295", typeof(uint))] [InlineData("Int64", "-9223372036854775808", typeof(long))] [InlineData("UInt64", "18446744073709551615", typeof(ulong))] [InlineData("Float", "1.25", typeof(float))] [InlineData("Double", "1.25", typeof(double))] [InlineData("String", "operator text", typeof(string))] [InlineData("uint16", "123", typeof(ushort))] public async Task Execute_ExplicitScalarUsesRequestedClrType(string type, string value, Type expectedType) { var fakeService = new FakeOpcUaClientService { ReadException = new InvalidOperationException("Explicit types must not read.") }; var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService)) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Scalar", Value = value, Type = type }; var originalCulture = CultureInfo.CurrentCulture; try { CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR"); using var console = TestConsoleHelper.CreateConsole(); await command.ExecuteAsync(console); } finally { CultureInfo.CurrentCulture = originalCulture; } fakeService.ReadNodeIds.ShouldBeEmpty(); fakeService.WriteValues.Count.ShouldBe(1); var writtenValue = fakeService.WriteValues[0].Value; writtenValue.GetType().ShouldBe(expectedType); if (expectedType == typeof(bool)) writtenValue.ShouldBe(true); else Convert.ToString(writtenValue, CultureInfo.InvariantCulture).ShouldBe(value); } /// Verifies unsupported type names fail locally before connecting or writing. [Theory] [InlineData("")] [InlineData(" ")] [InlineData("DateTime")] [InlineData("ByteString")] [InlineData("UInt16[]")] [InlineData("System.UInt16")] [InlineData("Unknown")] public async Task Execute_UnsupportedExplicitTypeRejectsBeforeConnect(string type) { var fakeService = new FakeOpcUaClientService(); var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService)) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Scalar", Value = "1", Type = type }; using var console = TestConsoleHelper.CreateConsole(); var error = await Should.ThrowAsync(async () => await command.ExecuteAsync(console)); error.Message.ShouldContain("--type"); fakeService.ConnectCalled.ShouldBeFalse(); fakeService.ReadNodeIds.ShouldBeEmpty(); fakeService.WriteValues.ShouldBeEmpty(); } /// Verifies invalid explicit values fail locally before connecting or writing. [Theory] [InlineData("UInt16", "65536")] [InlineData("UInt16", "-1")] [InlineData("UInt16", "not-a-number")] [InlineData("UInt16", "1.5")] [InlineData("SByte", "128")] [InlineData("Boolean", "maybe")] [InlineData("Double", "1,25")] public async Task Execute_InvalidExplicitValueRejectsBeforeConnect(string type, string value) { var fakeService = new FakeOpcUaClientService(); var command = new WriteCommand(new FakeOpcUaClientServiceFactory(fakeService)) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Scalar", Value = value, Type = type }; using var console = TestConsoleHelper.CreateConsole(); var error = await Should.ThrowAsync(async () => await command.ExecuteAsync(console)); error.Message.ShouldContain("--value"); fakeService.ConnectCalled.ShouldBeFalse(); fakeService.ReadNodeIds.ShouldBeEmpty(); fakeService.WriteValues.ShouldBeEmpty(); } /// Verifies that write command executes successfully. [Fact] public async Task Execute_WritesSuccessfully() { var fakeService = new FakeOpcUaClientService { ReadValueResult = new DataValue(new Variant(42)), WriteStatusCodeResult = StatusCodes.Good }; var factory = new FakeOpcUaClientServiceFactory(fakeService); var command = new WriteCommand(factory) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=MyVar", Value = "100" }; using var console = TestConsoleHelper.CreateConsole(); await command.ExecuteAsync(console); var output = TestConsoleHelper.GetOutput(console); output.ShouldContain("Write successful: ns=2;s=MyVar = 100"); } /// Verifies that write command reports failure. [Fact] public async Task Execute_ReportsFailure() { var fakeService = new FakeOpcUaClientService { ReadValueResult = new DataValue(new Variant("current")), WriteStatusCodeResult = StatusCodes.BadNotWritable }; var factory = new FakeOpcUaClientServiceFactory(fakeService); var command = new WriteCommand(factory) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=ReadOnly", Value = "newvalue" }; using var console = TestConsoleHelper.CreateConsole(); await command.ExecuteAsync(console); var output = TestConsoleHelper.GetOutput(console); output.ShouldContain("Write failed:"); } /// Verifies that write command reads current value before writing. [Fact] public async Task Execute_ReadsCurrentValueThenWrites() { var fakeService = new FakeOpcUaClientService { ReadValueResult = new DataValue(new Variant(3.14)), WriteStatusCodeResult = StatusCodes.Good }; var factory = new FakeOpcUaClientServiceFactory(fakeService); var command = new WriteCommand(factory) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=FloatVar", Value = "2.718" }; using var console = TestConsoleHelper.CreateConsole(); await command.ExecuteAsync(console); // Should read first to get current type, then write fakeService.ReadNodeIds.Count.ShouldBe(1); fakeService.WriteValues.Count.ShouldBe(1); fakeService.WriteValues[0].Value.ShouldBeOfType(); } /// Verifies that write command disconnects in finally block. [Fact] public async Task Execute_DisconnectsInFinally() { var fakeService = new FakeOpcUaClientService { ReadValueResult = new DataValue(new Variant("test")) }; var factory = new FakeOpcUaClientServiceFactory(fakeService); var command = new WriteCommand(factory) { Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Node", Value = "value" }; using var console = TestConsoleHelper.CreateConsole(); await command.ExecuteAsync(console); fakeService.DisconnectCalled.ShouldBeTrue(); fakeService.DisposeCalled.ShouldBeTrue(); } }