using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Http;
using NSubstitute;
using ScadaLink.Commons.Interfaces.Repositories;
namespace ScadaLink.ExternalSystemGateway.Tests;
///
/// ExternalSystemGateway-013: configuration options must actually influence the
/// registered HTTP client — an operator setting them must not be silently ignored.
///
public class ServiceWiringTests
{
[Fact]
public void MaxConcurrentConnectionsPerSystem_IsAppliedToTheNamedHttpClientPrimaryHandler()
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary
{
["ScadaLink:ExternalSystemGateway:MaxConcurrentConnectionsPerSystem"] = "4",
})
.Build();
var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton(config);
services.AddSingleton(Substitute.For());
services.AddExternalSystemGateway();
using var provider = services.BuildServiceProvider();
// Resolve the per-system named client's message-handler chain and walk to the
// primary handler — the option must be reflected in MaxConnectionsPerServer.
var handlerFactory = provider.GetRequiredService();
var handler = handlerFactory.CreateHandler("ExternalSystem_AnySystem");
var primary = FindPrimaryHandler(handler);
var sockets = Assert.IsType(primary);
Assert.Equal(4, sockets.MaxConnectionsPerServer);
}
private static HttpMessageHandler FindPrimaryHandler(HttpMessageHandler handler)
{
var current = handler;
while (current is DelegatingHandler delegating && delegating.InnerHandler != null)
{
current = delegating.InnerHandler;
}
return current;
}
}