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