2018-03-15 01:29:40 +00:00
|
|
|
import { expect, tap } from 'tapbundle';
|
2017-07-07 20:02:19 +00:00
|
|
|
|
2018-03-15 01:29:40 +00:00
|
|
|
import socketIoClient = require('socket.io-client');
|
|
|
|
import smartsocket = require('../ts/index');
|
|
|
|
import smartq = require('smartq');
|
|
|
|
import nodehash = require('nodehash');
|
2016-08-07 12:58:20 +00:00
|
|
|
|
2018-03-15 01:29:40 +00:00
|
|
|
let testSmartsocket: smartsocket.Smartsocket;
|
|
|
|
let testSmartsocketClient: smartsocket.SmartsocketClient;
|
|
|
|
let testSocketRole1: smartsocket.SocketRole;
|
|
|
|
let testSocketFunction1: smartsocket.SocketFunction;
|
2016-08-12 03:56:40 +00:00
|
|
|
|
|
|
|
let 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
|
|
|
|
|
|
|
tap.test('should start listening when .started is called', async () => {
|
2018-03-15 01:29:40 +00:00
|
|
|
testSmartsocket.startServer();
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
|
|
|
|
// class socketrole
|
|
|
|
tap.test('should add a socketrole', async () => {
|
|
|
|
testSocketRole1 = new smartsocket.SocketRole({
|
|
|
|
name: 'testRole1',
|
|
|
|
passwordHash: nodehash.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 () => {
|
|
|
|
testSocketFunction1 = new smartsocket.SocketFunction({
|
|
|
|
funcName: 'testFunction1',
|
2018-03-15 01:29:40 +00:00
|
|
|
funcDef: async dataArg => {
|
|
|
|
return dataArg;
|
2017-07-07 20:02:19 +00:00
|
|
|
},
|
2018-03-15 01:29:40 +00:00
|
|
|
allowedRoles: [testSocketRole1]
|
|
|
|
});
|
|
|
|
});
|
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 () => {
|
2018-03-15 01:29:40 +00:00
|
|
|
let done = smartq.defer();
|
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
|
|
|
});
|
|
|
|
testSmartsocketClient.connect().then(() => {
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
await done.promise;
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
tap.test('client should disconnect and reconnect', async () => {
|
2018-03-15 01:29:40 +00:00
|
|
|
let done = smartq.defer();
|
|
|
|
testSmartsocketClient
|
|
|
|
.disconnect()
|
2017-07-07 20:02:19 +00:00
|
|
|
.then(() => {
|
2018-03-15 01:29:40 +00:00
|
|
|
let done = smartq.defer();
|
2017-07-07 20:02:19 +00:00
|
|
|
setTimeout(() => {
|
2018-03-15 01:29:40 +00:00
|
|
|
testSmartsocketClient.connect().then(done.resolve);
|
|
|
|
}, 0);
|
|
|
|
return done.promise;
|
2017-07-07 20:02:19 +00:00
|
|
|
})
|
|
|
|
.then(() => {
|
2018-03-15 01:29:40 +00:00
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
await done.promise;
|
|
|
|
});
|
2018-03-18 16:50:37 +00:00
|
|
|
|
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 () => {
|
2018-03-15 01:29:40 +00:00
|
|
|
let done = smartq.defer();
|
|
|
|
testSmartsocketClient
|
|
|
|
.serverCall('testFunction1', {
|
|
|
|
value1: 'hello'
|
|
|
|
})
|
|
|
|
.then(dataArg => {
|
|
|
|
console.log(dataArg);
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
await done.promise;
|
|
|
|
});
|
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 () => {
|
2018-03-15 01:29:40 +00:00
|
|
|
let done = smartq.defer();
|
2017-07-07 20:02:19 +00:00
|
|
|
let targetSocket = (() => {
|
2018-03-15 01:29:40 +00:00
|
|
|
return smartsocket.allSocketConnections.find(socketConnectionArg => {
|
|
|
|
return socketConnectionArg.alias === 'testClient1';
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
testSmartsocket
|
|
|
|
.clientCall(
|
|
|
|
'testFunction1',
|
|
|
|
{
|
|
|
|
value1: 'helloFromServer'
|
|
|
|
},
|
|
|
|
targetSocket
|
|
|
|
)
|
|
|
|
.then(dataArg => {
|
|
|
|
console.log(dataArg);
|
|
|
|
done.resolve();
|
|
|
|
});
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
|
|
|
|
// terminate
|
|
|
|
tap.test('should close the server', async () => {
|
2018-03-15 01:29:40 +00:00
|
|
|
await testSmartsocket.closeServer();
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
|
2018-03-15 01:29:40 +00:00
|
|
|
tap.start();
|