AyCode.Core/AyCode.Services.Server.Tests/SignalRs/SignalRTestHelper.cs

98 lines
3.6 KiB
C#

using AyCode.Core.Extensions;
using AyCode.Services.SignalRs;
using MessagePack;
using MessagePack.Resolvers;
namespace AyCode.Services.Server.Tests.SignalRs;
/// <summary>
/// Helper methods for creating SignalR test messages.
/// Uses the production SignalR types for compatibility with the actual client/server code.
/// </summary>
public static class SignalRTestHelper
{
private static readonly MessagePackSerializerOptions MessagePackOptions = ContractlessStandardResolver.Options;
/// <summary>
/// Creates a MessagePack message for parameters using IdMessage format.
/// Each parameter is serialized directly as JSON (no array wrapping).
/// </summary>
public static byte[] CreatePrimitiveParamsMessage(params object[] values)
{
var idMessage = new IdMessage(values);
var postMessage = new SignalPostJsonDataMessage<IdMessage>(idMessage);
return MessagePackSerializer.Serialize(postMessage, MessagePackOptions);
}
/// <summary>
/// Creates a MessagePack message for a single primitive parameter.
/// </summary>
public static byte[] CreateSinglePrimitiveMessage<T>(T value) where T : notnull
{
return CreatePrimitiveParamsMessage(value);
}
/// <summary>
/// Creates a MessagePack message for a complex object parameter.
/// Uses PostDataJson pattern for single complex objects.
/// </summary>
public static byte[] CreateComplexObjectMessage<T>(T obj)
{
var json = obj.ToJson();
var postMessage = new SignalPostJsonDataMessage<object>(json);
return MessagePackSerializer.Serialize(postMessage, MessagePackOptions);
}
/// <summary>
/// Creates an empty MessagePack message for parameterless methods.
/// </summary>
public static byte[] CreateEmptyMessage()
{
var postMessage = new SignalPostJsonDataMessage<object>();
return MessagePackSerializer.Serialize(postMessage, MessagePackOptions);
}
/// <summary>
/// Deserialize a SignalResponseJsonMessage from the captured SentMessage.
/// </summary>
public static T? GetResponseData<T>(SentMessage sentMessage)
{
if (sentMessage.AsJsonResponse?.ResponseData == null)
return default;
return sentMessage.AsJsonResponse.ResponseData.JsonTo<T>();
}
/// <summary>
/// Assert that a response was successful.
/// </summary>
public static void AssertSuccessResponse(SentMessage sentMessage, int expectedTag)
{
var response = sentMessage.AsJsonResponse;
if (response == null)
throw new AssertFailedException("Response is not a SignalResponseJsonMessage");
if (response.Status != SignalResponseStatus.Success)
throw new AssertFailedException($"Expected Success status but got {response.Status}");
if (sentMessage.MessageTag != expectedTag)
throw new AssertFailedException($"Expected tag {expectedTag} but got {sentMessage.MessageTag}");
}
/// <summary>
/// Assert that a response was an error.
/// </summary>
public static void AssertErrorResponse(SentMessage sentMessage, int expectedTag)
{
var response = sentMessage.AsJsonResponse;
if (response == null)
throw new AssertFailedException("Response is not a SignalResponseJsonMessage");
if (response.Status != SignalResponseStatus.Error)
throw new AssertFailedException($"Expected Error status but got {response.Status}");
if (sentMessage.MessageTag != expectedTag)
throw new AssertFailedException($"Expected tag {expectedTag} but got {sentMessage.MessageTag}");
}
}