using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;

namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Adapters;

/// <summary>
/// Task 9 (opcua-tag-browser): <see cref="OpcUaDataConnection"/> implements
/// <see cref="IBrowsableDataConnection"/> as a one-line forwarder onto the
/// underlying <see cref="IOpcUaClient"/>. This guards that wiring — the
/// adapter must not add its own browse semantics; all browse behaviour lives
/// in the client (and is covered by RealOpcUaClient's own tests).
/// </summary>
public class OpcUaDataConnectionBrowseTests
{
    [Fact]
    public async Task BrowseChildrenAsync_ForwardsToUnderlyingClient()
    {
        var client = Substitute.For<IOpcUaClient>();
        var factory = Substitute.For<IOpcUaClientFactory>();
        factory.Create().Returns(client);
        client.IsConnected.Returns(true);

        var expected = new BrowseChildrenResult(
            new[] { new BrowseNode("ns=2;s=X", "X", BrowseNodeClass.Variable, false) },
            Truncated: false);
        client.BrowseChildrenAsync("ns=2;s=Parent", Arg.Any<string?>(), Arg.Any<CancellationToken>())
            .Returns(expected);

        var adapter = new OpcUaDataConnection(factory, NullLogger<OpcUaDataConnection>.Instance);
        // ConnectAsync installs the IOpcUaClient instance on the adapter; the
        // forwarder dereferences that field directly.
        await adapter.ConnectAsync(new Dictionary<string, string>());

        var actual = await adapter.BrowseChildrenAsync("ns=2;s=Parent");

        Assert.Same(expected, actual);
        await client.Received(1).BrowseChildrenAsync("ns=2;s=Parent", Arg.Any<string?>(), Arg.Any<CancellationToken>());
    }
}
