smartsocket/test/test.ts

134 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-03-19 09:00:11 +00:00
// tslint:disable-next-line:no-implicit-dependencies
import { expect, tap } from '@pushrocks/tapbundle';
2017-07-07 20:02:19 +00:00
2019-04-24 16:30:56 +00:00
import * as smarthash from '@pushrocks/smarthash';
import * as smartpromise from '@pushrocks/smartpromise';
2018-03-19 09:00:11 +00:00
2018-03-15 01:29:40 +00:00
import socketIoClient = require('socket.io-client');
import smartsocket = require('../ts/index');
2016-08-07 12:58:20 +00:00
2018-03-15 01:29:40 +00:00
let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient;
2019-08-13 09:36:31 +00:00
let testSocketConnection: smartsocket.SocketConnection;
2018-03-15 01:29:40 +00:00
let testSocketRole1: smartsocket.SocketRole;
2019-09-09 21:58:32 +00:00
let testSocketFunctionForServer: smartsocket.SocketFunction<any>;
let testSocketFunctionClient: smartsocket.SocketFunction<any>;
export interface IReqResClient {
method: 'testFunction1';
request: {
value1: string;
};
response: {
value1: string;
2019-11-03 17:33:46 +00:00
};
2019-09-09 21:58:32 +00:00
}
export interface IReqResServer {
method: 'testFunction2';
request: {
hi: string;
};
response: {
hi: string
}
}
2018-03-19 09:00:11 +00:00
const testConfig = {
2017-07-07 20:02:19 +00:00
port: 3000
2018-03-15 01:29:40 +00:00
};
2016-08-07 12:58:20 +00:00
2017-07-07 20:02:19 +00:00
// class smartsocket
tap.test('should create a new smartsocket', async () => {
2018-03-15 01:29:40 +00:00
testSmartsocket = new smartsocket.Smartsocket({ port: testConfig.port });
expect(testSmartsocket).be.instanceOf(smartsocket.Smartsocket);
});
2017-07-07 20:02:19 +00:00
// class socketrole
tap.test('should add a socketrole', async () => {
testSocketRole1 = new smartsocket.SocketRole({
name: 'testRole1',
2019-04-24 16:30:56 +00:00
passwordHash: smarthash.sha256FromStringSync('testPassword')
2018-03-15 01:29:40 +00:00
});
testSmartsocket.addSocketRoles([testSocketRole1]);
});
2017-07-07 20:02:19 +00:00
// class SocketFunction
tap.test('should register a new Function', async () => {
2019-08-13 09:36:31 +00:00
testSocketFunctionForServer = new smartsocket.SocketFunction({
2018-03-19 09:00:11 +00:00
allowedRoles: [testSocketRole1],
2019-05-02 09:46:36 +00:00
funcDef: async (dataArg, socketConnectionArg) => {
2018-03-15 01:29:40 +00:00
return dataArg;
2017-07-07 20:02:19 +00:00
},
funcName: 'testFunction1'
2018-03-15 01:29:40 +00:00
});
2019-08-13 09:36:31 +00:00
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
2019-08-13 07:37:23 +00:00
testSocketFunctionClient = new smartsocket.SocketFunction({
allowedRoles: [],
funcDef: async (dataArg, socketConnectionArg) => {
return dataArg;
},
2019-09-09 14:20:43 +00:00
funcName: 'testFunction2'
2019-08-13 07:37:23 +00:00
});
2019-08-13 09:36:31 +00:00
testSmartsocket.addSocketFunction(testSocketFunctionForServer);
2019-08-12 20:31:40 +00:00
console.log(testSmartsocket.socketFunctions);
});
tap.test('should start listening when .started is called', async () => {
await testSmartsocket.start();
2018-03-15 01:29:40 +00:00
});
2016-08-14 20:17:55 +00:00
2017-07-07 20:02:19 +00:00
// class SmartsocketClient
2017-07-07 20:10:03 +00:00
tap.test('should react to a new websocket connection from client', async () => {
2017-07-07 20:02:19 +00:00
testSmartsocketClient = new smartsocket.SmartsocketClient({
port: testConfig.port,
url: 'http://localhost',
password: 'testPassword',
alias: 'testClient1',
role: 'testRole1'
2018-03-15 01:29:40 +00:00
});
2019-08-13 07:37:23 +00:00
testSmartsocketClient.addSocketFunction(testSocketFunctionClient);
2019-08-12 20:31:40 +00:00
console.log(testSmartsocketClient.socketFunctions);
await testSmartsocketClient.connect();
2018-03-15 01:29:40 +00:00
});
2019-08-12 20:31:40 +00:00
2017-07-07 20:02:19 +00:00
tap.test('2 clients should connect in parallel', async () => {
// TODO: implement parallel test
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
tap.test('should be able to make a functionCall from client to server', async () => {
2019-09-09 21:58:32 +00:00
const response = await testSmartsocketClient.serverCall<IReqResClient>('testFunction1', {
2019-08-12 20:31:40 +00:00
value1: 'hello'
});
console.log(response);
2019-09-09 14:20:43 +00:00
expect(response.value1).to.equal('hello');
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
tap.test('should be able to make a functionCall from server to client', async () => {
2019-09-09 21:58:32 +00:00
const response = await testSmartsocket.clientCall<IReqResServer>(
2019-09-09 14:20:43 +00:00
'testFunction2',
2019-08-13 09:36:31 +00:00
{
hi: 'hi there from server'
},
testSmartsocket.socketConnections.find(socketConnection => {
return true;
})
);
2019-08-12 20:45:58 +00:00
console.log(response);
2019-09-09 14:20:43 +00:00
expect(response.hi).to.equal('hi there from server');
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
2019-08-13 09:36:31 +00:00
tap.test('client should disconnect and reconnect', async tools => {
await testSmartsocketClient.disconnect();
await tools.delayFor(100);
await testSmartsocketClient.connect();
});
2017-07-07 20:02:19 +00:00
// terminate
tap.test('should close the server', async () => {
2018-03-19 09:00:11 +00:00
await testSmartsocket.stop();
2018-03-15 01:29:40 +00:00
});
2017-07-07 20:02:19 +00:00
2018-03-15 01:29:40 +00:00
tap.start();