smartuniverse/test/test.ts

135 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-03-13 05:15:40 +00:00
// tslint:disable-next-line:no-implicit-dependencies
2023-07-25 09:33:13 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
import * as smartuniverse from '../ts/index.js';
2018-03-20 07:16:54 +00:00
2018-03-13 05:15:40 +00:00
let testUniverse: smartuniverse.Universe;
2019-04-22 21:11:51 +00:00
let testClientUniverse: smartuniverse.ClientUniverse;
2019-04-28 10:42:08 +00:00
let testClientUniverse2: smartuniverse.ClientUniverse;
let testClientChannel: smartuniverse.ClientUniverseChannel;
2018-03-08 22:42:46 +00:00
2019-04-28 10:42:08 +00:00
const testServerData = {
2020-09-24 18:17:52 +00:00
serverAddress: 'http://localhost:8765',
2019-04-28 10:42:08 +00:00
};
2019-04-11 16:50:43 +00:00
const testChannelData = {
channelName: 'awesomeTestChannel',
2020-09-24 18:17:52 +00:00
channelPass: 'awesomeChannelPass',
2019-04-22 11:06:01 +00:00
};
2019-04-11 16:50:43 +00:00
2018-03-07 21:22:15 +00:00
tap.test('first test', async () => {
2018-03-13 05:15:40 +00:00
testUniverse = new smartuniverse.Universe({
2020-09-24 18:17:52 +00:00
messageExpiryInMilliseconds: 1000,
2018-03-13 05:15:40 +00:00
});
2018-03-07 21:22:15 +00:00
});
2018-03-13 05:15:40 +00:00
tap.test('add a message to the SmartUniverse', async () => {
2019-04-11 16:50:43 +00:00
await testUniverse.start(8765);
2018-03-20 07:16:54 +00:00
});
2018-03-13 05:15:40 +00:00
// testing message handling
tap.test('create smartuniverse client', async () => {
2019-04-22 21:11:51 +00:00
testClientUniverse = new smartuniverse.ClientUniverse({
2019-11-09 11:59:51 +00:00
serverAddress: testServerData.serverAddress,
2020-09-24 18:17:52 +00:00
autoReconnect: true,
2018-03-15 00:05:13 +00:00
});
2023-07-25 09:33:13 +00:00
expect(testClientUniverse).toBeInstanceOf(smartuniverse.ClientUniverse);
});
tap.test('should add a channel to the universe', async () => {
2019-09-01 15:04:25 +00:00
testUniverse.addChannel(testChannelData.channelName, testChannelData.channelPass);
2019-04-22 21:23:36 +00:00
});
tap.test('should add the same channel to the client universe in the same way', async () => {
2019-08-13 16:43:33 +00:00
testClientUniverse.addChannel(testChannelData.channelName, testChannelData.channelPass);
});
2019-08-13 13:48:20 +00:00
tap.test('should start the ClientUniverse', async () => {
await testClientUniverse.start();
2019-09-01 15:04:25 +00:00
});
2019-08-13 13:48:20 +00:00
tap.test('should get a observable correctly', async () => {
2019-08-13 16:41:27 +00:00
testClientChannel = testClientUniverse.getChannel(testChannelData.channelName);
2023-07-25 09:33:13 +00:00
expect(testClientChannel).toBeInstanceOf(smartuniverse.ClientUniverseChannel);
2018-03-20 07:16:54 +00:00
});
2018-03-13 05:15:40 +00:00
2018-03-15 00:16:16 +00:00
tap.test('should send a message correctly', async () => {
2020-09-29 19:39:13 +00:00
await testClientUniverse.getChannel(testChannelData.channelName).postMessage({
2020-09-24 18:17:52 +00:00
messageText: 'hello',
2018-03-20 07:16:54 +00:00
});
});
2019-04-24 16:20:31 +00:00
tap.test('universe should contain the sent message', async () => {
2023-07-25 09:33:13 +00:00
expect(testUniverse.universeCache.messageMap.getArray()[0].messageText).toEqual('hello');
2019-04-24 16:20:31 +00:00
});
2019-04-28 10:42:08 +00:00
tap.test('a second client should be able to subscibe', async () => {
testClientUniverse2 = new smartuniverse.ClientUniverse({
2019-11-09 11:59:51 +00:00
serverAddress: testServerData.serverAddress,
2020-09-24 18:17:52 +00:00
autoReconnect: true,
2019-04-28 10:42:08 +00:00
});
testClientUniverse2.addChannel(testChannelData.channelName, testChannelData.channelPass);
2019-09-10 16:03:46 +00:00
await testClientUniverse2.start();
2019-04-28 10:42:08 +00:00
});
2020-09-24 18:17:52 +00:00
tap.test('should receive a message correctly', async (tools) => {
2019-09-10 16:03:46 +00:00
const done = tools.defer();
const testChannel = testClientUniverse.getChannel(testChannelData.channelName);
const testChannel2 = testClientUniverse2.getChannel(testChannelData.channelName);
2020-09-24 18:17:52 +00:00
const subscription = testChannel2.subscribe((messageArg) => {
2019-09-17 13:40:54 +00:00
if (messageArg.messageText === 'hellothere') {
console.log('Yay##########');
done.resolve();
}
2019-09-10 16:03:46 +00:00
});
2020-09-29 19:39:13 +00:00
await testChannel.postMessage({
2020-09-24 18:17:52 +00:00
messageText: 'hellothere',
2019-09-10 16:03:46 +00:00
});
await done.promise;
});
2018-03-15 00:16:16 +00:00
2019-09-17 11:57:34 +00:00
interface IDemoReqRes {
2019-11-09 12:00:30 +00:00
method: 'demo';
2019-09-17 11:57:34 +00:00
request: {
wowso: string;
};
response: {
hereso: string;
};
}
tap.test('ReactionRequest and ReactionResponse should work', async () => {
const reactionResponse = new smartuniverse.ReactionResponse<IDemoReqRes>({
2019-09-17 12:01:24 +00:00
channels: [testUniverse.getChannel(testChannelData.channelName)],
2020-09-24 18:17:52 +00:00
funcDef: async (reqData) => {
2019-09-17 11:57:34 +00:00
console.log(reqData);
return {
2020-09-24 18:17:52 +00:00
hereso: 'Hello there',
2019-09-17 11:57:34 +00:00
};
},
2020-09-24 18:17:52 +00:00
method: 'demo',
2019-09-17 11:57:34 +00:00
});
const reactionRequest = new smartuniverse.ReactionRequest<IDemoReqRes>({
2020-09-24 18:17:52 +00:00
method: 'demo',
2019-09-17 11:57:34 +00:00
});
2019-11-09 12:00:30 +00:00
const reactionResult = await reactionRequest.fire(
[testClientUniverse2.getChannel(testChannelData.channelName)],
{
2020-09-24 18:17:52 +00:00
wowso: 'wowza',
2019-11-09 12:00:30 +00:00
}
);
2019-09-17 11:57:34 +00:00
const result = await reactionResult.getFirstResult();
console.log(result);
});
2020-09-24 18:17:52 +00:00
tap.test('should disconnect the client correctly', async (tools) => {
2019-09-09 22:39:18 +00:00
await testClientUniverse.stop();
2019-09-17 13:40:54 +00:00
await testClientUniverse2.stop();
2018-04-13 13:45:48 +00:00
});
2020-09-24 18:17:52 +00:00
tap.test('should end the server correctly', async (tools) => {
2018-03-13 05:15:40 +00:00
await testUniverse.stopServer();
2018-03-20 07:16:54 +00:00
});
2018-03-13 05:15:40 +00:00
2018-03-07 21:22:15 +00:00
tap.start();