import { expect, tap } from '@push.rocks/tapbundle'; import * as typedrequest from '@api.global/typedrequest'; import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces'; import { SmartServe } from '@push.rocks/smartserve'; import * as typedsocket from '../ts/index.js'; interface IRequest_Client_Server extends typedrequestInterfaces.implementsTR< typedrequestInterfaces.ITypedRequest, IRequest_Client_Server > { method: 'sayhi'; request: { greeting: string; }; response: { answer: string; }; } let testSmartServe: SmartServe; let testTypedSocketServer: typedsocket.TypedSocket; let testTypedSocketClient: typedsocket.TypedSocket; const testTypedRouter = new typedrequest.TypedRouter(); const clientTypedRouter = new typedrequest.TypedRouter(); tap.test('should add some handlers', async () => { // Server-side handler testTypedRouter.addTypedHandler( new typedrequest.TypedHandler('sayhi', async (requestData) => { return { answer: `ok, got it : ${requestData.greeting}`, }; }) ); // Client-side handler (for server-to-client messages) clientTypedRouter.addTypedHandler( new typedrequest.TypedHandler('sayhi', async (requestData) => { return { answer: `client got: ${requestData.greeting}`, }; }) ); }); tap.test('should create Server and Client', async (tools) => { // 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 testTypedSocketClient = await typedsocket.TypedSocket.createClient( clientTypedRouter, 'http://localhost:3000' ); console.log('TypedSocket client connected'); console.log('test: waiting 1 second for connection to stabilize'); await tools.delayFor(1000); }); tap.test('should set tags via TypedRequest', async () => { console.log('Setting tag...'); await testTypedSocketClient.setTag('testTag', { userId: 123 }); console.log('Tag set successfully'); }); tap.test('should process messages from client to server', async () => { console.log('Testing client to server...'); const myClientSideTypedRequest = testTypedSocketClient.createTypedRequest('sayhi'); const response = await myClientSideTypedRequest.fire({ greeting: 'that is a greeting from the client', }); 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('sayhi', connections[0]); const response = await myServerSideTypedRequest.fire({ greeting: 'that is a greeting from the server', }); console.log('Server got response:', response); expect(response.answer).toContain('client got'); }); tap.test('should disconnect', async (tools) => { console.log('Stopping client...'); await testTypedSocketClient.stop(); console.log('Stopping server...'); await testSmartServe.stop(); console.log('All stopped'); tools.delayFor(500).then(() => process.exit(0)); }); export default tap.start();