using AyCode.Core.Enums;
using AyCode.Core.Loggers;
using AyCode.Core.Serializers.Binaries;
using AyCode.Services.SignalRs;
using FruitBank.Common;
using FruitBank.Common.Loggers;
using FruitBankHybrid.Shared.Services.Loggers;
using FruitBankHybrid.Shared.Services.SignalRs;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
namespace FruitBankHybrid.Shared.Tests;
///
/// Test-only factory for . Builds a HubConnectionBuilder
/// with the same connection settings a production Program.cs would use, wires a logger factory
/// backed by a single SignaRClientLogItemWriter (test-unit AppType, Detail level),
/// and uses for the protocol.
///
internal static class TestSignalRClientFactory
{
public static FruitBankSignalRClient Create(string testCategoryName)
{
var logWriters = new List
{
new SignaRClientLogItemWriter(AppType.TestUnit, LogLevel.Detail, testCategoryName)
};
Func loggerFactory =
categoryName => new LoggerClient(categoryName, logWriters.ToArray());
var connectionOptions = new AcHubConnectionOptions
{
Url = $"{FruitBankConstClient.BaseUrl}/{FruitBankConstClient.DefaultHubName}",
TransportMaxBufferSize = 30_000_000,
ApplicationMaxBufferSize = 30_000_000,
CloseTimeout = TimeSpan.FromSeconds(10),
KeepAliveInterval = TimeSpan.FromSeconds(60),
ServerTimeout = TimeSpan.FromSeconds(180),
SkipNegotiation = true,
Transports = HttpTransportType.WebSockets,
UseAutomaticReconnect = true,
UseStatefulReconnect = true
};
var logger = loggerFactory(nameof(FruitBankSignalRClient));
var hubBuilder = new HubConnectionBuilder().AddFruitBankDefaults(logger, connectionOptions);
hubBuilder.AddAcBinaryProtocol(opts => opts.ProtocolMode = BinaryProtocolMode.AsyncSegment);
return new FruitBankSignalRClient(hubBuilder, loggerFactory);
}
}