using Microsoft.AspNetCore.SignalR.Client;
namespace BuildFlow.IM.Integration.Tests;
///
/// SignalR 客户端构造助手。统一注入 TestServer 的 handler,使客户端连接走内存测试通道而非真实网络。
///
public static class HubConnectionFactory
{
///
/// 构造一个已携带 JWT(query)的 ChatHub 连接(尚未 Start)。
/// 调用方负责 await conn.StartAsync() 与用后 DisposeAsync。
/// 不启用自动重连:测试需要可预期的连接生命周期,断开即断开,便于验证 OnDisconnected 钩子。
///
public static HubConnection Create(ImServerFixture fixture, Guid userId, string userName)
{
var token = fixture.IssueToken(userId, userName);
var handler = fixture.CreateMessageHandler();
return new HubConnectionBuilder()
.WithUrl($"{fixture.BaseAddress}hubs/chat?access_token={token}", options =>
{
// 关键:复用 TestServer handler,绕过真实 TCP,连接走内存管道
options.HttpMessageHandlerFactory = _ => handler;
})
.Build();
}
}