nope-js-node
Version:
NoPE Runtime for Nodejs. For Browser-Support please use nope-browser
827 lines (826 loc) • 32.9 kB
JavaScript
"use strict";
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-11-13 08:17:19
* @modify date 2021-11-13 09:44:51
* @desc [description]
*/
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const mocha_1 = require("mocha");
const nopeEventEmitter_1 = require("../eventEmitter/nopeEventEmitter");
const nopeObservable_1 = require("../observables/nopeObservable");
const nopeDataPubSubSystem_1 = require("./nopeDataPubSubSystem");
const nopePubSubSystem_1 = require("./nopePubSubSystem");
(0, mocha_1.describe)("PubSubSystemBase", function () {
// Describe the required Test:
let pubSubSystem = new nopePubSubSystem_1.PubSubSystemBase({
generateEmitterType: function () {
return new nopeEventEmitter_1.NopeEventEmitter();
},
});
// Create a Publisher and Subscriber
let publisher = new nopeEventEmitter_1.NopeEventEmitter();
let subscriber = new nopeEventEmitter_1.NopeEventEmitter();
(0, mocha_1.describe)("Publish and Subscribe", () => {
(0, mocha_1.beforeEach)(() => {
// Create a new Observer
pubSubSystem = new nopePubSubSystem_1.PubSubSystemBase({
generateEmitterType: function () {
return new nopeEventEmitter_1.NopeEventEmitter();
},
});
publisher = new nopeEventEmitter_1.NopeEventEmitter();
subscriber = new nopeEventEmitter_1.NopeEventEmitter();
});
(0, mocha_1.it)("Ommitting last published data", (done) => {
publisher.emit("This should not be visible");
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with smaller pattern length", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
subscriber = pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with smaller topic", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
subscriber = pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, { a: { test: "Hello World!" } }, "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with same pattern length", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
subscriber = pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/+/test",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with same pattern length and multiple wildcards", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/+/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscriptions", (done) => {
const subscriber02 = new nopeObservable_1.NopeObservable();
publisher.emit("This should not be visible");
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
pubSubSystem.register(subscriber02, {
mode: "subscribe",
schema: {},
topic: "this/is/a",
});
const items = [];
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, { a: { test: "Hello World!" } }, "Message of Subscriber should be equal");
items.push("subscriber");
if (items.length == 2) {
chai_1.assert.include(items, "subscriber02", "Should have subscriber");
chai_1.assert.include(items, "subscriber", "Should have subscriber");
done();
}
}
catch (e) {
done(e);
}
});
subscriber02.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, { test: "Hello World!" }, "Message of Subscriber02 should be equal");
items.push("subscriber02");
if (items.length == 2) {
chai_1.assert.include(items, "subscriber02", "Should have subscriber");
chai_1.assert.include(items, "subscriber", "Should have subscriber");
done();
}
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Testing Emitters", (done) => {
pubSubSystem.subscriptions.onChange.subscribe((data) => {
if (data.added.includes("this/#")) {
done();
}
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
});
(0, mocha_1.it)("Testing Emitters", (done) => {
pubSubSystem.subscriptions.data.subscribe((data) => {
if (data.includes("this/#")) {
done();
}
}, { skipCurrent: true });
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
});
(0, mocha_1.it)("throw Error on multi registering", (done) => {
const error = new Error("Error not thrown");
try {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
throw error;
}
catch (e) {
if (e === error) {
done(e);
}
else {
done();
}
}
});
});
(0, mocha_1.describe)("Publish and Subscribe - without child data", () => {
(0, mocha_1.beforeEach)(() => {
// Create a new Observer
pubSubSystem = new nopePubSubSystem_1.PubSubSystemBase({
generateEmitterType: function () {
return new nopeEventEmitter_1.NopeEventEmitter();
},
forwardChildData: false,
});
publisher = new nopeEventEmitter_1.NopeEventEmitter();
subscriber = new nopeEventEmitter_1.NopeEventEmitter();
});
(0, mocha_1.it)("Ommitting last published data", (done) => {
publisher.emit("This should not be visible");
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with smaller pattern length", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
subscriber = pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with smaller topic", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
subscriber = pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
subscriber.subscribe((data) => {
done(new Error("should not be called!"));
});
publisher.emit("Hello World!");
done();
});
(0, mocha_1.it)("Forwading data for subscription with same pattern length", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
subscriber = pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/+/test",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with same pattern length and multiple wildcards", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/+/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscriptions", (done) => {
const subscriber02 = new nopeObservable_1.NopeObservable();
publisher.emit("This should not be visible");
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
pubSubSystem.register(subscriber02, {
mode: "subscribe",
schema: {},
topic: "this/is/a",
});
const items = [];
subscriber.subscribe((data) => {
done(new Error("should not be called!"));
});
subscriber02.subscribe((data) => {
done(new Error("should not be called!"));
});
publisher.emit("Hello World!");
done();
});
(0, mocha_1.it)("Testing Emitters", (done) => {
pubSubSystem.subscriptions.onChange.subscribe((data) => {
if (data.added.includes("this/#")) {
done();
}
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
});
(0, mocha_1.it)("Testing Emitters", (done) => {
pubSubSystem.subscriptions.data.subscribe((data) => {
if (data.includes("this/#")) {
done();
}
}, { skipCurrent: true });
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
});
(0, mocha_1.it)("throw Error on multi registering", (done) => {
const error = new Error("Error not thrown");
try {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
throw error;
}
catch (e) {
if (e === error) {
done(e);
}
else {
done();
}
}
});
});
});
(0, mocha_1.describe)("DataPubSubSystemBase", function () {
// Describe the required Test:
let pubSubSystem = new nopeDataPubSubSystem_1.DataPubSubSystem({
generateEmitterType: function () {
return new nopeObservable_1.NopeObservable();
},
});
// Create a Publisher and Subscriber
let publisher = new nopeObservable_1.NopeObservable();
let subscriber = new nopeObservable_1.NopeObservable();
(0, mocha_1.describe)("Data Handling", () => {
(0, mocha_1.beforeEach)(() => {
// Create a new Observer
pubSubSystem = new nopeDataPubSubSystem_1.DataPubSubSystem({
generateEmitterType: function () {
return new nopeObservable_1.NopeObservable();
},
});
publisher = new nopeObservable_1.NopeObservable();
subscriber = new nopeObservable_1.NopeObservable();
});
(0, mocha_1.it)("writing to root name", () => {
pubSubSystem.pushData("", { root: "data" });
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({ root: "data" });
});
(0, mocha_1.it)("writing to path", () => {
pubSubSystem.pushData("root", "data");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({ root: "data" });
});
(0, mocha_1.it)("reading with patternbasedPullData", () => {
pubSubSystem.pushData("test", { a: 1337, b: 1337 });
const result = pubSubSystem.patternbasedPullData("test/+");
chai_1.assert.isArray(result, "Expecting an arry");
chai_1.assert.deepInclude(result, { path: "test/a", data: 1337 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.deepInclude(result, { path: "test/b", data: 1337 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
});
(0, mocha_1.it)("reading with patternbasedPullData and deep data", () => {
pubSubSystem.pushData("test", { a: 1337, b: { c: 1337 } });
let result = pubSubSystem.patternbasedPullData("test/+");
chai_1.assert.isArray(result, "Expecting an arry");
chai_1.assert.deepInclude(result, { path: "test/a", data: 1337 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.deepInclude(result, { path: "test/b", data: { c: 1337 } }, `Expecting Element in array. array=${JSON.stringify(result)}`);
result = pubSubSystem.patternbasedPullData("+/a");
chai_1.assert.isArray(result, "Expecting an arry");
chai_1.assert.deepInclude(result, { path: "test/a", data: 1337 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.isTrue(result.length === 1, "Should contain only 1 item.");
});
(0, mocha_1.it)("reading with patternbasedPullData and deep data and multilevel wildcard", () => {
pubSubSystem.pushData("test", { a: 1337, b: { c: 1337 } });
const result = pubSubSystem.patternbasedPullData("test/#");
chai_1.assert.isArray(result, "Expecting an arry");
chai_1.assert.deepInclude(result, { path: "test/a", data: 1337 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.deepInclude(result, { path: "test/b", data: { c: 1337 } }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.deepInclude(result, { path: "test/b/c", data: 1337 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.isTrue(result.length === 3, "Should contain only 3 item.");
});
(0, mocha_1.it)("writing with pattern and deep data", () => {
pubSubSystem.pushData("test", { a: 1337, b: { c: 1337 } });
pubSubSystem.patternBasedPush("test/+", 1338);
let result = pubSubSystem.patternbasedPullData("test/+");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({
test: { a: 1338, b: 1338 },
});
chai_1.assert.isArray(result, "Expecting an arry");
chai_1.assert.deepInclude(result, { path: "test/a", data: 1338 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
chai_1.assert.deepInclude(result, { path: "test/b", data: 1338 }, `Expecting Element in array. array=${JSON.stringify(result)}`);
pubSubSystem.patternBasedPush("+/b", 1339);
result = pubSubSystem.patternbasedPullData("test/+");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({
test: { a: 1338, b: 1339 },
});
pubSubSystem.patternBasedPush("+/c", 1339);
result = pubSubSystem.patternbasedPullData("test/+");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({
test: { a: 1338, b: 1339 },
});
pubSubSystem.patternBasedPush("+/b/c", 1340);
result = pubSubSystem.patternbasedPullData("test/+");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({
test: { a: 1338, b: 1339 },
});
});
(0, mocha_1.it)("throw error pattern in pull", (done) => {
const error = new Error("Error not thrown");
try {
pubSubSystem.pullData("+/#");
throw error;
}
catch (e) {
if (e === error) {
done(e);
}
else {
done();
}
}
});
(0, mocha_1.it)("throw error pattern in pull", (done) => {
const error = new Error("Error not thrown");
try {
pubSubSystem.pullData("test/+");
throw error;
}
catch (e) {
if (e === error) {
done(e);
}
else {
done();
}
}
});
(0, mocha_1.it)("throw error pattern in pull", (done) => {
const error = new Error("Error not thrown");
try {
pubSubSystem.pullData("test/#");
throw error;
}
catch (e) {
if (e === error) {
done(e);
}
else {
done();
}
}
});
});
(0, mocha_1.describe)("Publish and Subscribe", () => {
(0, mocha_1.beforeEach)(() => {
// Create a new Observer
pubSubSystem = new nopeDataPubSubSystem_1.DataPubSubSystem({
generateEmitterType: function () {
return new nopeObservable_1.NopeObservable();
},
});
publisher = new nopeObservable_1.NopeObservable();
subscriber = new nopeObservable_1.NopeObservable();
});
(0, mocha_1.it)("emitting content via emitter", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
publisher.setContent("Hello World!");
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, { a: { test: "Hello World!" } }, "Message should be equal");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({
this: { is: { a: { test: "Hello World!" } } },
});
done();
}
catch (e) {
done(e);
}
});
});
(0, mocha_1.it)("receiving content via pub-sub-system", (done) => {
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "a test", "Message should be equal");
(0, chai_1.expect)(pubSubSystem.data).to.be.deep.equal({
this: { is: "a test" },
});
done();
}
catch (e) {
done(e);
}
}, {
skipCurrent: true,
});
pubSubSystem.pushData("this/is", "a test");
});
});
(0, mocha_1.describe)("Publish and Subscribe - without child data", () => {
(0, mocha_1.beforeEach)(() => {
// Create a new Observer
pubSubSystem = new nopeDataPubSubSystem_1.DataPubSubSystem({
generateEmitterType: function () {
return new nopeObservable_1.NopeObservable();
},
forwardChildData: false,
});
publisher = new nopeObservable_1.NopeObservable();
subscriber = new nopeObservable_1.NopeObservable();
});
(0, mocha_1.it)("Ommitting last published data", (done) => {
publisher.emit("This should not be visible");
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
}, {
skipCurrent: true,
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with smaller pattern length", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.deepEqual(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with smaller topic", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
subscriber.subscribe((data) => {
done(new Error("Should not be called"));
});
publisher.emit("Hello World!");
done();
});
(0, mocha_1.it)("Forwading data for subscription with same pattern length", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/+/test",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscription with same pattern length and multiple wildcards", (done) => {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is/+/#",
});
subscriber.subscribe((data) => {
try {
chai_1.assert.equal(data, "Hello World!", "Message should be equal");
done();
}
catch (e) {
done(e);
}
});
publisher.emit("Hello World!");
});
(0, mocha_1.it)("Forwading data for subscriptions", (done) => {
const subscriber02 = new nopeObservable_1.NopeObservable();
publisher.emit("This should not be visible");
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/is",
});
pubSubSystem.register(subscriber02, {
mode: "subscribe",
schema: {},
topic: "this/is/a",
});
const items = [];
subscriber.subscribe((data) => {
done(new Error("Should not be called!"));
});
subscriber02.subscribe((data) => {
done(new Error("Should not be called!"));
});
publisher.emit("Hello World!");
done();
});
(0, mocha_1.it)("Testing Emitters", (done) => {
pubSubSystem.subscriptions.onChange.subscribe((data) => {
if (data.added.includes("this/#")) {
done();
}
});
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
});
(0, mocha_1.it)("Testing Emitters", (done) => {
pubSubSystem.subscriptions.data.subscribe((data) => {
if (data.includes("this/#")) {
done();
}
}, { skipCurrent: true });
pubSubSystem.register(subscriber, {
mode: "subscribe",
schema: {},
topic: "this/#",
});
});
(0, mocha_1.it)("throw Error on multi registering", (done) => {
const error = new Error("Error not thrown");
try {
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
pubSubSystem.register(publisher, {
mode: "publish",
schema: {},
topic: "this/is/a/test",
});
throw error;
}
catch (e) {
if (e === error) {
done(e);
}
else {
done();
}
}
});
});
});