using System.Security.Claims;
using AyCode.Core.Extensions;
using AyCode.Core.Helpers;
using AyCode.Core.Serializers.Jsons;
using AyCode.Core.Tests.TestModels;
using AyCode.Models.Server.DynamicMethods;
using AyCode.Services.Server.SignalRs;
using AyCode.Services.SignalRs;
using Microsoft.Extensions.Configuration;
namespace AyCode.Services.Server.Tests.SignalRs;
///
/// Testable SignalR hub that overrides infrastructure dependencies.
/// Enables unit testing without SignalR server or mocks.
///
public class TestableSignalRHub2 : AcWebSignalRHubBase
{
private IAcSignalRHubItemServer _callerClient;
#region Test Configuration
///
/// Simulated connection ID
///
public string TestConnectionId { get; set; } = "test-connection-id";
///
/// Simulated user identifier
///
public string? TestUserIdentifier { get; set; } = "test-user-id";
///
/// Simulated connection aborted state
///
public bool TestIsConnectionAborted { get; set; } = false;
///
/// Simulated ClaimsPrincipal (optional)
///
public ClaimsPrincipal? TestUser { get; set; }
#endregion
public TestableSignalRHub2()
: base(new ConfigurationBuilder().Build(), new TestLogger())
{
}
public TestableSignalRHub2(IConfiguration configuration, TestLogger logger)
: base(configuration, logger)
{
}
#region Public Test Entry Points
///
/// Register a service with SignalR-attributed methods
///
public void RegisterService(object service, IAcSignalRHubItemServer callerClient)
{
_callerClient = callerClient;
DynamicMethodCallModels.Add(new AcDynamicMethodCallModel(service));
}
///
/// Sets the serializer type for testing Binary vs JSON serialization.
///
public void SetSerializerType(AcSerializerOptions acSerializerOptions)
{
SerializerOptions = acSerializerOptions;
}
#endregion
#region Overridden Context Accessors
protected override string GetConnectionId() => TestConnectionId;
protected override bool IsConnectionAborted() => TestIsConnectionAborted;
protected override string? GetUserIdentifier() => TestUserIdentifier;
protected override ClaimsPrincipal? GetUser() => TestUser;
#endregion
#region Overridden Response Methods (capture messages for testing)
protected override Task ResponseToCaller(int messageTag, ISignalRMessage message, int? requestId)
=> SendMessageToClient(_callerClient, messageTag, message, requestId);
#endregion
}