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');
|
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-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 () => {
|
2022-01-19 14:34:52 +00:00
|
|
|
testSmartsocket = new smartsocket.Smartsocket({ alias: 'testserver2', port: testConfig.port });
|
2018-03-15 01:29:40 +00:00
|
|
|
expect(testSmartsocket).be.instanceOf(smartsocket.Smartsocket);
|
|
|
|
});
|
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({
|
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({
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
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',
|
|
|
|
alias: 'testClient1',
|
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
|
|
|
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
|
2022-01-18 16:10:46 +00:00
|
|
|
.findSync((socketConnection) => {
|
2021-01-28 01:31:42 +00:00
|
|
|
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
|
2022-01-19 06:01:58 +00:00
|
|
|
.findSync((socketConnection) => {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.addTag({
|
|
|
|
id: 'awesome2',
|
|
|
|
payload: 'absolutely',
|
|
|
|
});
|
2021-01-28 01:39:23 +00:00
|
|
|
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
|
|
|
},
|
2022-01-18 16:10:46 +00:00
|
|
|
testSmartsocket.socketConnections.findSync((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 testSmartsocketClient.connect();
|
|
|
|
});
|
|
|
|
|
2022-01-19 06:01:58 +00:00
|
|
|
tap.test('should be able to locate a connection tag after reconnect', async (tools) => {
|
|
|
|
console.log(testSmartsocket.socketConnections.getArray().length);
|
|
|
|
const tagOnServerSide = await testSmartsocket.socketConnections
|
|
|
|
.findSync((socketConnection) => {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.getTagById('awesome');
|
|
|
|
expect(tagOnServerSide.payload).to.equal('yes');
|
|
|
|
});
|
|
|
|
|
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();
|