From e80ab1a31619d92ca3b584aee70cef89972692ae Mon Sep 17 00:00:00 2001 From: nenoNaninu Date: Wed, 10 Jan 2024 22:44:35 +0900 Subject: [PATCH] add tests --- .../TypeScriptTests/src/json/Inherit.test.ts | 63 +++++++++++++++++++ tests/TypeScriptTests/src/json/unary.test.ts | 2 +- .../src/msgpack/Inherit.test.ts | 62 ++++++++++++++++++ .../TypeScriptTests/src/msgpack/unary.test.ts | 3 - .../Hubs/InheritHub.cs | 27 ++++++++ .../Hubs/UnaryHub.cs | 19 ++++++ .../Program.cs | 1 + .../IInheritHub.cs | 50 +++++++++++++++ 8 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 tests/TypeScriptTests/src/json/Inherit.test.ts create mode 100644 tests/TypeScriptTests/src/msgpack/Inherit.test.ts create mode 100644 tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/InheritHub.cs create mode 100644 tests/TypedSignalR.Client.TypeScript.Tests.Shared/IInheritHub.cs diff --git a/tests/TypeScriptTests/src/json/Inherit.test.ts b/tests/TypeScriptTests/src/json/Inherit.test.ts new file mode 100644 index 0000000..f713dbf --- /dev/null +++ b/tests/TypeScriptTests/src/json/Inherit.test.ts @@ -0,0 +1,63 @@ +import { HubConnectionBuilder } from '@microsoft/signalr' +import { getHubProxyFactory } from '../generated/json/TypedSignalR.Client' +import { MyEnum, MyRequestItem, MyRequestItem2, UserDefinedType } from '../generated/json/TypedSignalR.Client.TypeScript.Tests.Shared'; +import crypto from 'crypto' + +const getRandomInt = (max: number) => { + return Math.floor(Math.random() * max); +} + +const toUTCString = (date: string | Date): string => { + if (typeof date === 'string') { + const d = new Date(date); + return d.toUTCString(); + } + + return date.toUTCString(); +} + +const testMethod = async () => { + const connection = new HubConnectionBuilder() + .withUrl("http://localhost:5000/hubs/InheritHub") + .build(); + + const hubProxy = getHubProxyFactory("IInheritHub") + .createHubProxy(connection); + + try { + await connection.start(); + + const r1 = await hubProxy.get(); + expect(r1).toEqual("TypedSignalR.Client.TypeScript"); + + const x = getRandomInt(1000); + const y = getRandomInt(1000); + + const r2 = await hubProxy.add(x, y); + expect(r2).toEqual(x + y); + + const s1 = "revue"; + const s2 = "starlight"; + + const r3 = await hubProxy.cat(s1, s2);; + + expect(r3).toEqual(s1 + s2); + + const instance: UserDefinedType = { + dateTime: new Date(), + guid: crypto.randomUUID() + } + + const r4 = await hubProxy.echo(instance); + + instance.dateTime = toUTCString(instance.dateTime) + r4.dateTime = toUTCString(r4.dateTime) + + expect(r4).toEqual(instance) + } + finally { + await connection.stop(); + } +} + +test('unary.test', testMethod); diff --git a/tests/TypeScriptTests/src/json/unary.test.ts b/tests/TypeScriptTests/src/json/unary.test.ts index e8bbf05..8f1f153 100644 --- a/tests/TypeScriptTests/src/json/unary.test.ts +++ b/tests/TypeScriptTests/src/json/unary.test.ts @@ -50,7 +50,7 @@ const testMethod = async () => { const r4 = await hubProxy.echo(instance); - instance.dateTime = toUTCString(r4.dateTime) + instance.dateTime = toUTCString(instance.dateTime) r4.dateTime = toUTCString(r4.dateTime) expect(r4).toEqual(instance) diff --git a/tests/TypeScriptTests/src/msgpack/Inherit.test.ts b/tests/TypeScriptTests/src/msgpack/Inherit.test.ts new file mode 100644 index 0000000..36baa60 --- /dev/null +++ b/tests/TypeScriptTests/src/msgpack/Inherit.test.ts @@ -0,0 +1,62 @@ +import { HubConnectionBuilder } from '@microsoft/signalr' +import { getHubProxyFactory } from '../generated/msgpack/TypedSignalR.Client' +import { UserDefinedType } from '../generated/msgpack/TypedSignalR.Client.TypeScript.Tests.Shared'; +import crypto from 'crypto' +import { MessagePackHubProtocol } from '@microsoft/signalr-protocol-msgpack'; + +const getRandomInt = (max: number) => { + return Math.floor(Math.random() * max); +} + +const toUTCString = (date: string | Date): string => { + if (typeof date === 'string') { + const d = new Date(date); + return d.toUTCString(); + } + + return date.toUTCString(); +} + +const testMethod = async () => { + const connection = new HubConnectionBuilder() + .withUrl("http://localhost:5000/hubs/InheritHub") + .withHubProtocol(new MessagePackHubProtocol()) + .build(); + + const hubProxy = getHubProxyFactory("IInheritHub") + .createHubProxy(connection); + + try { + await connection.start(); + + const r1 = await hubProxy.get(); + expect(r1).toEqual("TypedSignalR.Client.TypeScript"); + + const x = getRandomInt(1000); + const y = getRandomInt(1000); + + const r2 = await hubProxy.add(x, y); + expect(r2).toEqual(x + y); + + const s1 = "revue"; + const s2 = "starlight"; + + const r3 = await hubProxy.cat(s1, s2);; + + expect(r3).toEqual(s1 + s2); + + const instance: UserDefinedType = { + DateTime: new Date(), + Guid: crypto.randomUUID() + } + + const r4 = await hubProxy.echo(instance); + + expect(r4).toEqual(instance) + } + finally { + await connection.stop(); + } +} + +test('unary.test', testMethod); diff --git a/tests/TypeScriptTests/src/msgpack/unary.test.ts b/tests/TypeScriptTests/src/msgpack/unary.test.ts index cc31961..882742c 100644 --- a/tests/TypeScriptTests/src/msgpack/unary.test.ts +++ b/tests/TypeScriptTests/src/msgpack/unary.test.ts @@ -44,9 +44,6 @@ const testMethod = async () => { const r4 = await hubProxy.echo(instance); - instance.DateTime = r4.DateTime - r4.DateTime = r4.DateTime - expect(r4).toEqual(instance) const r5 = await hubProxy.echoMyEnum(MyEnum.Four); diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/InheritHub.cs b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/InheritHub.cs new file mode 100644 index 0000000..7523c82 --- /dev/null +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/InheritHub.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.SignalR; +using TypedSignalR.Client.TypeScript.Tests.Shared; + +namespace TypedSignalR.Client.TypeScript.Tests.Server.Hubs; + +public class InheritHub : Hub, IInheritHub +{ + public Task Add(int x, int y) + { + return Task.FromResult(x + y); + } + + public Task Cat(string x, string y) + { + return Task.FromResult(x + y); + } + + public Task Echo(UserDefinedType instance) + { + return Task.FromResult(instance); + } + + public Task Get() + { + return Task.FromResult("TypedSignalR.Client.TypeScript"); + } +} diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs index b2661c8..160ab9b 100644 --- a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Hubs/UnaryHub.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; using TypedSignalR.Client.TypeScript.Tests.Shared; namespace TypedSignalR.Client.TypeScript.Tests.Server.Hubs; @@ -75,4 +76,22 @@ public Task> RequestList(List list) return Task.FromResult(buffer); } + + public override Task OnConnectedAsync() + { + _logger.Log(LogLevel.Information, "UnaryHub.OnConnectedAsync"); + + return base.OnConnectedAsync(); + } + + public override Task OnDisconnectedAsync(Exception? exception) + { + //_logger.Log(LogLevel.Information, "UnaryHub.OnDisconnectedAsync"); + if (exception is not null) + { + _logger.LogError(exception, "UnaryHub.OnDisconnectedAsync"); + } + + return base.OnDisconnectedAsync(exception); + } } diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Program.cs b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Program.cs index 148d1bb..f83949d 100644 --- a/tests/TypedSignalR.Client.TypeScript.Tests.Server/Program.cs +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Server/Program.cs @@ -44,5 +44,6 @@ app.MapHub("/hubs/StreamingHub"); app.MapHub("/hubs/ClientResultsTestHub"); app.MapHub("/hubs/NestedTypeHub"); +app.MapHub("/hubs/InheritHub"); app.Run(); diff --git a/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IInheritHub.cs b/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IInheritHub.cs new file mode 100644 index 0000000..e2fba3f --- /dev/null +++ b/tests/TypedSignalR.Client.TypeScript.Tests.Shared/IInheritHub.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TypedSignalR.Client.TypeScript.Tests.Shared; + +public interface IHubBaseBase +{ + Task Get(); +} + +public interface IHubBase1 : IHubBaseBase +{ + Task Add(int x, int y); +} + +public interface IHubBase2 : IHubBaseBase +{ + Task Cat(string x, string y); +} + +[Hub] +public interface IInheritHub : IHubBase1, IHubBase2 +{ + Task Echo(UserDefinedType instance); +} + + +public interface IReceiverBaseBase +{ + Task ReceiveMessage(string message, int value); +} + +public interface IReceiverBase1 : IReceiverBaseBase +{ + Task ReceiveCustomMessage(UserDefinedType userDefined); +} + +public interface IReceiverBase2 : IReceiverBaseBase +{ + Task Notify(); +} + +[Receiver] +public interface IInheritHubReceiver : IReceiverBase1, IReceiverBase2 +{ + Task ReceiveMessage2(string message, int value); +}