2023-08-04 16:22:39 +02:00
|
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
2023-08-05 12:00:27 +02:00
|
|
|
import * as typedrequest from '@api.global/typedrequest';
|
|
|
|
|
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
2025-12-04 00:00:38 +00:00
|
|
|
import { SmartServe } from '@push.rocks/smartserve';
|
2020-12-26 18:14:19 +00:00
|
|
|
|
2022-03-24 22:46:08 +01:00
|
|
|
import * as typedsocket from '../ts/index.js';
|
2020-12-26 18:14:19 +00:00
|
|
|
|
2022-10-26 10:55:08 +02:00
|
|
|
interface IRequest_Client_Server
|
|
|
|
|
extends typedrequestInterfaces.implementsTR<
|
|
|
|
|
typedrequestInterfaces.ITypedRequest,
|
|
|
|
|
IRequest_Client_Server
|
|
|
|
|
> {
|
2020-12-26 18:14:19 +00:00
|
|
|
method: 'sayhi';
|
|
|
|
|
request: {
|
|
|
|
|
greeting: string;
|
|
|
|
|
};
|
|
|
|
|
response: {
|
2022-10-26 10:55:08 +02:00
|
|
|
answer: string;
|
|
|
|
|
};
|
2020-12-26 18:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 00:00:38 +00:00
|
|
|
let testSmartServe: SmartServe;
|
2020-12-26 18:14:19 +00:00
|
|
|
let testTypedSocketServer: typedsocket.TypedSocket;
|
|
|
|
|
let testTypedSocketClient: typedsocket.TypedSocket;
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
const testTypedRouter = new typedrequest.TypedRouter();
|
2025-12-04 00:00:38 +00:00
|
|
|
const clientTypedRouter = new typedrequest.TypedRouter();
|
2020-12-21 21:01:37 +00:00
|
|
|
|
2020-12-26 18:14:19 +00:00
|
|
|
tap.test('should add some handlers', async () => {
|
2025-12-04 00:00:38 +00:00
|
|
|
// Server-side handler
|
2022-10-26 10:55:08 +02:00
|
|
|
testTypedRouter.addTypedHandler<IRequest_Client_Server>(
|
|
|
|
|
new typedrequest.TypedHandler('sayhi', async (requestData) => {
|
|
|
|
|
return {
|
|
|
|
|
answer: `ok, got it : ${requestData.greeting}`,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
2025-12-04 00:00:38 +00:00
|
|
|
|
|
|
|
|
// Client-side handler (for server-to-client messages)
|
|
|
|
|
clientTypedRouter.addTypedHandler<IRequest_Client_Server>(
|
|
|
|
|
new typedrequest.TypedHandler('sayhi', async (requestData) => {
|
|
|
|
|
return {
|
|
|
|
|
answer: `client got: ${requestData.greeting}`,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
2022-10-26 10:55:08 +02:00
|
|
|
});
|
2020-12-26 18:14:19 +00:00
|
|
|
|
2020-12-26 18:45:17 +00:00
|
|
|
tap.test('should create Server and Client', async (tools) => {
|
2025-12-04 00:00:38 +00:00
|
|
|
// Create SmartServe with TypedRouter for WebSocket
|
|
|
|
|
testSmartServe = new SmartServe({
|
|
|
|
|
port: 3000,
|
|
|
|
|
websocket: {
|
|
|
|
|
typedRouter: testTypedRouter,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await testSmartServe.start();
|
|
|
|
|
console.log('SmartServe started on port 3000');
|
|
|
|
|
|
|
|
|
|
// Create TypedSocket server from SmartServe
|
|
|
|
|
testTypedSocketServer = typedsocket.TypedSocket.fromSmartServe(testSmartServe, testTypedRouter);
|
|
|
|
|
console.log('TypedSocket server created');
|
|
|
|
|
|
|
|
|
|
// Create client
|
2022-10-26 10:55:08 +02:00
|
|
|
testTypedSocketClient = await typedsocket.TypedSocket.createClient(
|
2025-12-04 00:00:38 +00:00
|
|
|
clientTypedRouter,
|
2022-10-26 10:55:08 +02:00
|
|
|
'http://localhost:3000'
|
|
|
|
|
);
|
2025-12-04 00:00:38 +00:00
|
|
|
console.log('TypedSocket client connected');
|
2021-10-23 00:27:46 +02:00
|
|
|
|
2025-12-04 00:00:38 +00:00
|
|
|
console.log('test: waiting 1 second for connection to stabilize');
|
|
|
|
|
await tools.delayFor(1000);
|
|
|
|
|
});
|
2022-10-26 10:55:08 +02:00
|
|
|
|
2025-12-04 00:00:38 +00:00
|
|
|
tap.test('should set tags via TypedRequest', async () => {
|
|
|
|
|
console.log('Setting tag...');
|
|
|
|
|
await testTypedSocketClient.setTag('testTag', { userId: 123 });
|
|
|
|
|
console.log('Tag set successfully');
|
2020-12-21 21:01:37 +00:00
|
|
|
});
|
|
|
|
|
|
2025-12-04 00:00:38 +00:00
|
|
|
tap.test('should process messages from client to server', async () => {
|
|
|
|
|
console.log('Testing client to server...');
|
2022-10-26 10:55:08 +02:00
|
|
|
const myClientSideTypedRequest =
|
|
|
|
|
testTypedSocketClient.createTypedRequest<IRequest_Client_Server>('sayhi');
|
2020-12-26 18:45:17 +00:00
|
|
|
const response = await myClientSideTypedRequest.fire({
|
2022-10-26 10:55:08 +02:00
|
|
|
greeting: 'that is a greeting from the client',
|
2020-12-26 18:45:17 +00:00
|
|
|
});
|
2025-12-04 00:00:38 +00:00
|
|
|
console.log('Client got response:', response);
|
|
|
|
|
expect(response.answer).toContain('ok, got it');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('should find connections by tag', async () => {
|
|
|
|
|
console.log('Finding connections by tag...');
|
|
|
|
|
const connections = await testTypedSocketServer.findAllTargetConnectionsByTag('testTag');
|
|
|
|
|
console.log(`Found ${connections.length} connections with tag`);
|
|
|
|
|
expect(connections.length).toEqual(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
tap.test('should process messages from server to client', async () => {
|
|
|
|
|
console.log('Testing server to client...');
|
|
|
|
|
const connections = await testTypedSocketServer.findAllTargetConnectionsByTag('testTag');
|
|
|
|
|
const myServerSideTypedRequest =
|
|
|
|
|
testTypedSocketServer.createTypedRequest<IRequest_Client_Server>('sayhi', connections[0]);
|
|
|
|
|
const response = await myServerSideTypedRequest.fire({
|
2022-10-26 10:55:08 +02:00
|
|
|
greeting: 'that is a greeting from the server',
|
2020-12-26 18:45:17 +00:00
|
|
|
});
|
2025-12-04 00:00:38 +00:00
|
|
|
console.log('Server got response:', response);
|
|
|
|
|
expect(response.answer).toContain('client got');
|
2022-10-26 10:55:08 +02:00
|
|
|
});
|
2020-12-26 18:45:17 +00:00
|
|
|
|
2022-01-19 19:17:22 +01:00
|
|
|
tap.test('should disconnect', async (tools) => {
|
2025-12-04 00:00:38 +00:00
|
|
|
console.log('Stopping client...');
|
2020-12-26 18:14:19 +00:00
|
|
|
await testTypedSocketClient.stop();
|
2025-12-04 00:00:38 +00:00
|
|
|
console.log('Stopping server...');
|
|
|
|
|
await testSmartServe.stop();
|
|
|
|
|
console.log('All stopped');
|
|
|
|
|
tools.delayFor(500).then(() => process.exit(0));
|
2022-10-26 10:55:08 +02:00
|
|
|
});
|
2020-12-26 18:14:19 +00:00
|
|
|
|
2025-12-04 00:00:38 +00:00
|
|
|
export default tap.start();
|