2018-03-19 09:00:11 +00:00
|
|
|
// tslint:disable-next-line:no-implicit-dependencies
|
2019-01-30 02:14:02 +00:00
|
|
|
import { expect, tap } from '@pushrocks/tapbundle';
|
2017-07-07 20:02:19 +00:00
|
|
|
|
2018-03-15 01:29:40 +00:00
|
|
|
import smartsocket = require('../ts/index');
|
2020-12-26 17:43:19 +00:00
|
|
|
import * as isohash from '@pushrocks/isohash';
|
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: {
|
2020-09-24 18:04:11 +00:00
|
|
|
hi: string;
|
|
|
|
};
|
2019-09-09 21:58:32 +00:00
|
|
|
}
|
2016-08-12 03:56:40 +00:00
|
|
|
|
2018-03-19 09:00:11 +00:00
|
|
|
const testConfig = {
|
2020-09-24 18:04:11 +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',
|
2020-12-26 17:43:19 +00:00
|
|
|
passwordHash: await isohash.sha256FromString('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
|
|
|
},
|
2020-09-24 18:04:11 +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;
|
|
|
|
},
|
2020-09-24 18:04:11 +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',
|
2020-09-24 18:04:11 +00:00
|
|
|
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
|
|
|
|
2021-01-28 01:39:23 +00:00
|
|
|
tap.test('should be able to tag a connection from client', async (tools) => {
|
2021-01-28 01:30:27 +00:00
|
|
|
await testSmartsocketClient.addTag({
|
|
|
|
id: 'awesome',
|
2021-01-28 01:31:42 +00:00
|
|
|
payload: 'yes',
|
2021-01-28 01:30:27 +00:00
|
|
|
});
|
2021-01-28 01:31:42 +00:00
|
|
|
const tagOnServerSide = await testSmartsocket.socketConnections
|
|
|
|
.find((socketConnection) => {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.getTagById('awesome');
|
2021-01-28 01:30:27 +00:00
|
|
|
expect(tagOnServerSide.payload).to.equal('yes');
|
2021-01-28 01:31:42 +00:00
|
|
|
});
|
2021-01-28 01:30:27 +00:00
|
|
|
|
2021-01-28 01:39:23 +00:00
|
|
|
tap.test('should be able to tag a connection from server', async (tools) => {
|
|
|
|
await testSmartsocket.socketConnections
|
|
|
|
.find((socketConnection) => {
|
|
|
|
return true;
|
|
|
|
}).addTag({
|
|
|
|
id: 'awesome2',
|
|
|
|
payload: 'absolutely',
|
|
|
|
});
|
|
|
|
const tagOnClientSide = await testSmartsocketClient.socketConnection.getTagById('awesome2');
|
|
|
|
expect(tagOnClientSide.payload).to.equal('absolutely');
|
|
|
|
});
|
|
|
|
|
2017-07-07 20:02:19 +00:00
|
|
|
tap.test('2 clients should connect in parallel', async () => {
|
2018-03-18 16:50:37 +00:00
|
|
|
// TODO: implement parallel test
|
2018-03-15 01:29:40 +00:00
|
|
|
});
|
2018-03-18 16:50:37 +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', {
|
2020-09-24 18:04:11 +00:00
|
|
|
value1: 'hello',
|
2019-08-12 20:31:40 +00:00
|
|
|
});
|
|
|
|
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
|
|
|
});
|
2016-08-15 00:36:17 +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
|
|
|
{
|
2020-09-24 18:04:11 +00:00
|
|
|
hi: 'hi there from server',
|
2019-08-13 09:36:31 +00:00
|
|
|
},
|
2020-09-24 18:04:11 +00:00
|
|
|
testSmartsocket.socketConnections.find((socketConnection) => {
|
2019-08-13 09:36:31 +00:00
|
|
|
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
|
|
|
|
2020-09-24 18:04:11 +00:00
|
|
|
tap.test('client should disconnect and reconnect', async (tools) => {
|
2019-08-13 09:36:31 +00:00
|
|
|
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();
|