| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | // tslint:disable-next-line:no-implicit-dependencies
 | 
					
						
							|  |  |  | import { expect, tap } from '@pushrocks/tapbundle'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 18:30:56 +02:00
										 |  |  | import * as smarthash from '@pushrocks/smarthash'; | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | import * as smartpromise from '@pushrocks/smartpromise'; | 
					
						
							|  |  |  | import * as smartexpress from '@pushrocks/smartexpress'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import socketIoClient = require('socket.io-client'); | 
					
						
							|  |  |  | import smartsocket = require('../ts/index'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let testSmartsocket: smartsocket.Smartsocket; | 
					
						
							|  |  |  | let testSmartsocketClient: smartsocket.SmartsocketClient; | 
					
						
							|  |  |  | let testSocketRole1: smartsocket.SocketRole; | 
					
						
							|  |  |  | let testSocketFunction1: smartsocket.SocketFunction; | 
					
						
							|  |  |  | let myseServer: smartexpress.Server; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const testConfig = { | 
					
						
							|  |  |  |   port: 3000 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // class smartsocket
 | 
					
						
							|  |  |  | tap.test('should create a new smartsocket', async () => { | 
					
						
							|  |  |  |   testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port }); | 
					
						
							|  |  |  |   expect(testSmartsocket).be.instanceOf(smartsocket.Smartsocket); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('Should accept an smartExpressServer as server', async () => { | 
					
						
							|  |  |  |   myseServer = new smartexpress.Server({ | 
					
						
							|  |  |  |     cors: true, | 
					
						
							|  |  |  |     forceSsl: false, | 
					
						
							|  |  |  |     port: testConfig.port | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   testSmartsocket.setExternalServer('smartexpress', myseServer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await myseServer.start(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // class socketrole
 | 
					
						
							|  |  |  | tap.test('should add a socketrole', async () => { | 
					
						
							|  |  |  |   testSocketRole1 = new smartsocket.SocketRole({ | 
					
						
							|  |  |  |     name: 'testRole1', | 
					
						
							| 
									
										
										
										
											2019-04-24 18:30:56 +02:00
										 |  |  |     passwordHash: smarthash.sha256FromStringSync('testPassword') | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  |   testSmartsocket.addSocketRoles([testSocketRole1]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // class SocketFunction
 | 
					
						
							|  |  |  | tap.test('should register a new Function', async () => { | 
					
						
							|  |  |  |   testSocketFunction1 = new smartsocket.SocketFunction({ | 
					
						
							|  |  |  |     allowedRoles: [testSocketRole1], | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |     funcDef: async (dataArg, socketConnectionArg) => { | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  |       return dataArg; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     funcName: 'testFunction1' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |   testSmartsocket.addSocketFunction(testSocketFunction1); | 
					
						
							|  |  |  |   console.log(testSmartsocket.socketFunctions); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('should start listening when .started is called', async () => { | 
					
						
							|  |  |  |   await testSmartsocket.start(); | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // class SmartsocketClient
 | 
					
						
							|  |  |  | tap.test('should react to a new websocket connection from client', async () => { | 
					
						
							|  |  |  |   testSmartsocketClient = new smartsocket.SmartsocketClient({ | 
					
						
							|  |  |  |     port: testConfig.port, | 
					
						
							|  |  |  |     url: 'http://localhost', | 
					
						
							|  |  |  |     password: 'testPassword', | 
					
						
							|  |  |  |     alias: 'testClient1', | 
					
						
							|  |  |  |     role: 'testRole1' | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |   testSmartsocketClient.addSocketFunction(testSocketFunction1); | 
					
						
							|  |  |  |   console.log(testSmartsocketClient.socketFunctions); | 
					
						
							|  |  |  |   await testSmartsocketClient.connect(); | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | tap.test('client should disconnect and reconnect', async tools => { | 
					
						
							|  |  |  |   await testSmartsocketClient.disconnect(); | 
					
						
							|  |  |  |   await tools.delayFor(100); | 
					
						
							|  |  |  |   await testSmartsocketClient.connect(); | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('2 clients should connect in parallel', async () => { | 
					
						
							|  |  |  |   // TODO: implement parallel test
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('should be able to make a functionCall from client to server', async () => { | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |   const response = await testSmartsocketClient.serverCall('testFunction1', { | 
					
						
							|  |  |  |     value1: 'hello' | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   console.log(response); | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('should be able to make a functionCall from server to client', async () => { | 
					
						
							| 
									
										
										
										
											2019-08-12 22:31:40 +02:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2019-04-24 17:57:16 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // terminate
 | 
					
						
							|  |  |  | tap.test('should close the server', async () => { | 
					
						
							|  |  |  |   await testSmartsocket.stop(); | 
					
						
							|  |  |  |   await myseServer.stop(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.start(); |