Files
lmxopcua/tests/ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests/ConnectCommandTests.cs
Joseph Doherty a2883b82d9 Add cross-platform OPC UA client stack: shared library, CLI tool, and Avalonia UI
Implements Client.Shared (IOpcUaClientService with connection lifecycle, failover,
browse, read/write, subscriptions, alarms, history, redundancy), Client.CLI (8 CliFx
commands mirroring tools/opcuacli-dotnet), and Client.UI (Avalonia desktop app with
tree browser, read/write, subscriptions, alarms, and history tabs). All three target
.NET 10 and are covered by 249 unit tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 15:49:42 -04:00

79 lines
2.7 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.LmxOpcUa.Client.CLI.Commands;
using ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests.Fakes;
using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models;
namespace ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests;
public class ConnectCommandTests
{
[Fact]
public async Task Execute_PrintsConnectionInfo()
{
var fakeService = new FakeOpcUaClientService
{
ConnectionInfoResult = new ConnectionInfo(
"opc.tcp://testhost:4840",
"MyServer",
"SignAndEncrypt",
"http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256",
"session-42",
"MySession")
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConnectCommand(factory)
{
Url = "opc.tcp://testhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Connected to: opc.tcp://testhost:4840");
output.ShouldContain("Server: MyServer");
output.ShouldContain("Security Mode: SignAndEncrypt");
output.ShouldContain("Security Policy: http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
output.ShouldContain("Connection successful.");
}
[Fact]
public async Task Execute_CallsConnectAndDisconnect()
{
var fakeService = new FakeOpcUaClientService();
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConnectCommand(factory)
{
Url = "opc.tcp://localhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
fakeService.ConnectCalled.ShouldBeTrue();
fakeService.DisconnectCalled.ShouldBeTrue();
fakeService.DisposeCalled.ShouldBeTrue();
}
[Fact]
public async Task Execute_DisconnectsOnError()
{
var fakeService = new FakeOpcUaClientService
{
ConnectException = new InvalidOperationException("Connection refused")
};
var factory = new FakeOpcUaClientServiceFactory(fakeService);
var command = new ConnectCommand(factory)
{
Url = "opc.tcp://localhost:4840"
};
using var console = TestConsoleHelper.CreateConsole();
// The command should propagate the exception but still clean up.
// Since connect fails, service is null in finally, so no disconnect.
await Should.ThrowAsync<InvalidOperationException>(
async () => await command.ExecuteAsync(console));
}
}