feat(cli): read --attribute reads one node attribute (e.g. Historizing, AccessLevel) (#921)

This commit is contained in:
Joseph Doherty
2026-11-28 09:28:59 -05:00
parent 05f431f5d8
commit a2a1104f20
7 changed files with 106 additions and 3 deletions
@@ -199,6 +199,17 @@ public sealed class FakeOpcUaClientService : IOpcUaClientService
public TimeSpan? LastMaximumAge { get; private set; }
/// <summary>The (node, attribute id) pairs passed to ReadAttributeAsync.</summary>
public List<(NodeId NodeId, uint AttributeId)> ReadAttributes { get; } = [];
/// <inheritdoc />
public Task<DataValue> ReadAttributeAsync(NodeId nodeId, uint attributeId, CancellationToken ct = default)
{
ReadAttributes.Add((nodeId, attributeId));
if (ReadException != null) throw ReadException;
return Task.FromResult(ReadValueResult);
}
public Task<DataValue> ReadValueAsync(NodeId nodeId, TimeSpan maximumAge, CancellationToken ct = default)
{
LastMaximumAge = maximumAge;
@@ -42,6 +42,41 @@ public class ReadCommandTests
service.ReadNodeIds.ShouldBeEmpty();
}
/// <summary>lmxopcua#921: <c>--attribute Historizing</c> reads that attribute (not the value) and labels the output.</summary>
[Fact]
public async Task Execute_ReadsTheRequestedAttribute()
{
var service = new FakeOpcUaClientService { ReadValueResult = new DataValue(new Variant(false), StatusCodes.Good) };
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(service))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Test", Attribute = "historizing"
};
using var console = TestConsoleHelper.CreateConsole();
await command.ExecuteAsync(console);
service.ReadAttributes.ShouldBe([(new NodeId("Test", 2), Attributes.Historizing)]);
service.ReadNodeIds.ShouldBeEmpty();
var output = TestConsoleHelper.GetOutput(console);
output.ShouldContain("Attribute: historizing");
output.ShouldContain("Value: False");
}
/// <summary>An unknown attribute, or <c>--max-age</c> with a non-Value attribute, is refused before connecting.</summary>
[Theory]
[InlineData("Bogus", 0)]
[InlineData("Historizing", 5000)]
public async Task Execute_RejectsAnUnknownAttributeOrMaxAgeOffTheValue(string attribute, double maxAge)
{
var service = new FakeOpcUaClientService();
var command = new ReadCommand(new FakeOpcUaClientServiceFactory(service))
{
Url = "opc.tcp://localhost:4840", NodeId = "ns=2;s=Test", Attribute = attribute, MaxAgeMilliseconds = maxAge
};
using var console = TestConsoleHelper.CreateConsole();
await Should.ThrowAsync<CliFx.Exceptions.CommandException>(async () => await command.ExecuteAsync(console));
service.ReadAttributes.ShouldBeEmpty();
service.ReadNodeIds.ShouldBeEmpty();
}
/// <summary>Verifies that execute prints the read value.</summary>
[Fact]
public async Task Execute_PrintsReadValue()