using Opc.Ua; using ZB.MOM.WW.LmxOpcUa.Client.Shared; using ZB.MOM.WW.LmxOpcUa.Client.Shared.Models; using BrowseResult = ZB.MOM.WW.LmxOpcUa.Client.Shared.Models.BrowseResult; namespace ZB.MOM.WW.LmxOpcUa.Client.CLI.Tests.Fakes; /// /// Fake implementation of for unit testing commands. /// Records all method calls and returns configurable results. /// public sealed class FakeOpcUaClientService : IOpcUaClientService { // Track calls public bool ConnectCalled { get; private set; } public ConnectionSettings? LastConnectionSettings { get; private set; } public bool DisconnectCalled { get; private set; } public bool DisposeCalled { get; private set; } public List ReadNodeIds { get; } = []; public List<(NodeId NodeId, object Value)> WriteValues { get; } = []; public List BrowseNodeIds { get; } = []; public List<(NodeId NodeId, int IntervalMs)> SubscribeCalls { get; } = []; public List UnsubscribeCalls { get; } = []; public List<(NodeId? SourceNodeId, int IntervalMs)> SubscribeAlarmsCalls { get; } = []; public bool UnsubscribeAlarmsCalled { get; private set; } public bool RequestConditionRefreshCalled { get; private set; } public List<(NodeId NodeId, DateTime Start, DateTime End, int MaxValues)> HistoryReadRawCalls { get; } = []; public List<(NodeId NodeId, DateTime Start, DateTime End, AggregateType Aggregate, double IntervalMs)> HistoryReadAggregateCalls { get; } = []; public bool GetRedundancyInfoCalled { get; private set; } // Configurable results public ConnectionInfo ConnectionInfoResult { get; set; } = new( "opc.tcp://localhost:4840", "TestServer", "None", "http://opcfoundation.org/UA/SecurityPolicy#None", "session-1", "TestSession"); public DataValue ReadValueResult { get; set; } = new( new Variant(42), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow); public StatusCode WriteStatusCodeResult { get; set; } = StatusCodes.Good; public IReadOnlyList BrowseResults { get; set; } = new List { new("ns=2;s=Node1", "Node1", "Object", true), new("ns=2;s=Node2", "Node2", "Variable", false) }; public IReadOnlyList HistoryReadResult { get; set; } = new List { new(new Variant(10.0), StatusCodes.Good, DateTime.UtcNow.AddHours(-1), DateTime.UtcNow), new(new Variant(20.0), StatusCodes.Good, DateTime.UtcNow, DateTime.UtcNow) }; public RedundancyInfo RedundancyInfoResult { get; set; } = new( "Warm", 200, ["urn:server1", "urn:server2"], "urn:app:test"); public Exception? ConnectException { get; set; } public Exception? ReadException { get; set; } public Exception? WriteException { get; set; } public Exception? ConditionRefreshException { get; set; } /// public bool IsConnected => ConnectCalled && !DisconnectCalled; /// public ConnectionInfo? CurrentConnectionInfo => ConnectCalled ? ConnectionInfoResult : null; /// public event EventHandler? DataChanged; /// public event EventHandler? AlarmEvent; /// public event EventHandler? ConnectionStateChanged; /// public Task ConnectAsync(ConnectionSettings settings, CancellationToken ct = default) { ConnectCalled = true; LastConnectionSettings = settings; if (ConnectException != null) throw ConnectException; return Task.FromResult(ConnectionInfoResult); } /// public Task DisconnectAsync(CancellationToken ct = default) { DisconnectCalled = true; return Task.CompletedTask; } /// public Task ReadValueAsync(NodeId nodeId, CancellationToken ct = default) { ReadNodeIds.Add(nodeId); if (ReadException != null) throw ReadException; return Task.FromResult(ReadValueResult); } /// public Task WriteValueAsync(NodeId nodeId, object value, CancellationToken ct = default) { WriteValues.Add((nodeId, value)); if (WriteException != null) throw WriteException; return Task.FromResult(WriteStatusCodeResult); } /// public Task> BrowseAsync(NodeId? parentNodeId = null, CancellationToken ct = default) { BrowseNodeIds.Add(parentNodeId); return Task.FromResult(BrowseResults); } /// public Task SubscribeAsync(NodeId nodeId, int intervalMs = 1000, CancellationToken ct = default) { SubscribeCalls.Add((nodeId, intervalMs)); return Task.CompletedTask; } /// public Task UnsubscribeAsync(NodeId nodeId, CancellationToken ct = default) { UnsubscribeCalls.Add(nodeId); return Task.CompletedTask; } /// public Task SubscribeAlarmsAsync(NodeId? sourceNodeId = null, int intervalMs = 1000, CancellationToken ct = default) { SubscribeAlarmsCalls.Add((sourceNodeId, intervalMs)); return Task.CompletedTask; } /// public Task UnsubscribeAlarmsAsync(CancellationToken ct = default) { UnsubscribeAlarmsCalled = true; return Task.CompletedTask; } /// public Task RequestConditionRefreshAsync(CancellationToken ct = default) { RequestConditionRefreshCalled = true; if (ConditionRefreshException != null) throw ConditionRefreshException; return Task.CompletedTask; } /// public Task AcknowledgeAlarmAsync(string conditionNodeId, byte[] eventId, string comment, CancellationToken ct = default) { return Task.FromResult(new StatusCode(StatusCodes.Good)); } /// public Task> HistoryReadRawAsync( NodeId nodeId, DateTime startTime, DateTime endTime, int maxValues = 1000, CancellationToken ct = default) { HistoryReadRawCalls.Add((nodeId, startTime, endTime, maxValues)); return Task.FromResult(HistoryReadResult); } /// public Task> HistoryReadAggregateAsync( NodeId nodeId, DateTime startTime, DateTime endTime, AggregateType aggregate, double intervalMs = 3600000, CancellationToken ct = default) { HistoryReadAggregateCalls.Add((nodeId, startTime, endTime, aggregate, intervalMs)); return Task.FromResult(HistoryReadResult); } /// public Task GetRedundancyInfoAsync(CancellationToken ct = default) { GetRedundancyInfoCalled = true; return Task.FromResult(RedundancyInfoResult); } /// /// Marks the fake client as disposed so CLI command tests can assert cleanup behavior. /// public void Dispose() { DisposeCalled = true; } /// Raises the DataChanged event for testing subscribe commands. public void RaiseDataChanged(string nodeId, DataValue value) { DataChanged?.Invoke(this, new DataChangedEventArgs(nodeId, value)); } /// Raises the AlarmEvent for testing alarm commands. public void RaiseAlarmEvent(AlarmEventArgs args) { AlarmEvent?.Invoke(this, args); } /// Raises the ConnectionStateChanged event for testing. public void RaiseConnectionStateChanged(ConnectionState oldState, ConnectionState newState, string endpointUrl) { ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(oldState, newState, endpointUrl)); } }