@schukai/monster 3.2.0 → 3.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  import {expect} from "chai"
4
- import {RestAPI} from "../../../../../application/source/data/datasource/restapi.mjs";
5
- import {validateObject} from "../../../../../application/source/types/validate.mjs";
4
+ import {RestAPI} from "../../../../../../application/source/data/datasource/server/restapi.mjs";
5
+ import {validateObject} from "../../../../../../application/source/types/validate.mjs";
6
6
 
7
7
 
8
8
  describe('RestAPI', function () {
@@ -18,16 +18,19 @@ describe('RestAPI', function () {
18
18
 
19
19
  returnStatus = 200;
20
20
  fetchReference = globalThis['fetch'];
21
- globalThis['fetch'] = function (url, options) {
22
-
23
- if (!url) throw new Error('missing url')
21
+ globalThis['fetch'] = function (options) {
24
22
 
25
23
  return new Promise((resolve, reject) => {
26
24
  resolve({
27
25
  text: function () {
28
- return JSON.stringify({
29
- a: "test"
30
- })
26
+ return new Promise((resolve, reject) => {
27
+ resolve(JSON.stringify({
28
+ a: "test"
29
+ }));
30
+ });
31
+
32
+
33
+
31
34
  },
32
35
  status: returnStatus
33
36
  });
@@ -52,11 +55,14 @@ describe('RestAPI', function () {
52
55
  });
53
56
 
54
57
  it('write should ', function (done) {
55
- const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'}, {url: 'https://monsterjs.org/assets/world.json'})
58
+ const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'},
59
+ {
60
+ url: 'https://monsterjs.org/assets/world.json',
61
+ acceptedStatus: [99]
62
+ })
56
63
  ds.write().then(data => {
57
- validateObject(data);
58
- done();
59
- }).catch(e => done(e));
64
+ done("should not be here");
65
+ }).catch(e => done());
60
66
  });
61
67
 
62
68
 
@@ -86,4 +92,4 @@ describe('RestAPI', function () {
86
92
  })
87
93
 
88
94
 
89
- })
95
+ })
@@ -0,0 +1,194 @@
1
+ import {expect} from "chai"
2
+ import {WebConnect} from "../../../../../../application/source/data/datasource/server/webconnect.mjs";
3
+ import {initWebSocket} from "../../../../util/websocket.mjs";
4
+
5
+ const testUrl = "wss://ws.postman-echo.com/raw"
6
+
7
+ // const g = getGlobal();
8
+ // g['WebSocket'] = WS;
9
+
10
+
11
+ describe('Websocket', function () {
12
+
13
+ let ds = undefined
14
+
15
+ before(function (done) {
16
+ initWebSocket().then(() => {
17
+ done()
18
+ }).catch((e) => {
19
+ done(e)
20
+ })
21
+ });
22
+
23
+ afterEach(function (done) {
24
+ if (ds) {
25
+ ds.close()
26
+ }
27
+
28
+ // workaround: without this, the node test will hang
29
+ for (const sym of Object.getOwnPropertySymbols(ds)) {
30
+ if (sym.toString() === 'Symbol(connection)') {
31
+ const connection = ds[sym]
32
+ for (const sym2 of Object.getOwnPropertySymbols(connection)) {
33
+ if (sym2.toString() === 'Symbol(connection)') {
34
+ const socket = connection[sym2]?.socket;
35
+ if (socket) {
36
+ if (typeof socket?.terminate === 'function') {
37
+ socket?.['terminate']()
38
+ }
39
+ }
40
+ }
41
+ }
42
+ }
43
+ }
44
+
45
+ done()
46
+ });
47
+
48
+ it('should get clone', function () {
49
+
50
+ ds = new WebConnect(testUrl)
51
+ const clone = ds.getClone()
52
+
53
+ expect(clone).to.be.an.instanceof(WebConnect)
54
+
55
+
56
+ })
57
+
58
+ it('should transform data', function (done) {
59
+
60
+ let writeCallbackCalled = false
61
+ let readCallbackCalled = false
62
+
63
+ ds = new WebConnect({
64
+ url: testUrl,
65
+ write: {
66
+ mapping: {
67
+ transformer: "call:onWrite",
68
+ callbacks: {
69
+ onWrite: (data) => {
70
+ writeCallbackCalled = true
71
+ return data
72
+ }
73
+ }
74
+ },
75
+ sheathing: {
76
+ object: {
77
+ demo: 1,
78
+ data: {
79
+ xyz: undefined
80
+ }
81
+ },
82
+ path: "data.xyz",
83
+ },
84
+ },
85
+ read: {
86
+ mapping: {
87
+ transformer: "call:onRead",
88
+ callbacks: {
89
+ onRead: (data) => {
90
+ readCallbackCalled = true
91
+ return data
92
+ }
93
+ }
94
+ },
95
+ path: 'data.xyz',
96
+ }
97
+ })
98
+
99
+ ds.connect().then(() => {
100
+ ds.set({
101
+ envelop: {
102
+ message: "Hello World"
103
+ }
104
+ })
105
+
106
+ ds.write().then(() => {
107
+
108
+ ds.set({})
109
+ expect(ds.get()).to.be.deep.equal({});
110
+
111
+ setTimeout(() => {
112
+
113
+
114
+ ds.read().then(() => {
115
+ expect(ds.get()).to.be.deep.equal({envelop:{message: "Hello World"}});
116
+ expect(writeCallbackCalled).to.be.true
117
+ expect(readCallbackCalled).to.be.true
118
+ done()
119
+ }).catch((e) => {
120
+ done(e)
121
+ })
122
+ }, 200)
123
+
124
+ }).catch((err) => {
125
+ done(new Error(err));
126
+ })
127
+
128
+
129
+ }).catch((e) => {
130
+ done(e)
131
+ })
132
+
133
+
134
+ })
135
+
136
+ it('should connect', function (done) {
137
+ ds = new WebConnect({
138
+ url: testUrl,
139
+ reconnect: {
140
+ enabled: false
141
+ }
142
+ });
143
+ ds.connect().then(() => {
144
+ done()
145
+ }).catch((e) => {
146
+ done(e)
147
+ })
148
+
149
+
150
+ })
151
+
152
+ it('should send message', function (done) {
153
+ ds = new WebConnect({
154
+ url: testUrl,
155
+ reconnect: {
156
+ enabled: false
157
+ }
158
+ });
159
+ ds.connect().then(() => {
160
+ ds.set({
161
+ envelop: {
162
+ message: "Hello World"
163
+ }
164
+ })
165
+
166
+ ds.write().then(() => {
167
+
168
+ ds.set({})
169
+ expect(ds.get()).to.be.deep.equal({});
170
+
171
+ setTimeout(() => {
172
+
173
+ ds.read().then(() => {
174
+ expect(ds.get()).to.be.deep.equal({envelop:{message: "Hello World"}});
175
+ done()
176
+ }).catch((e) => {
177
+ done(e)
178
+ })
179
+ },500)
180
+
181
+
182
+ }).catch((err) => {
183
+ done(new Error(err));
184
+ })
185
+
186
+
187
+ }).catch((e) => {
188
+ done(e)
189
+ })
190
+
191
+
192
+ }).timeout(10000);
193
+
194
+ });
@@ -0,0 +1,53 @@
1
+ import {expect} from "chai"
2
+ import {Server} from "../../../../../application/source/data/datasource/server.mjs";
3
+
4
+
5
+ describe('Server', function () {
6
+
7
+ it('should transform data', function () {
8
+
9
+ let writeCallbackCalled = false
10
+ let readCallbackCalled = false
11
+
12
+ const server = new Server({
13
+ write: {
14
+ mapping: {
15
+ transformer: "call:onWrite",
16
+ callbacks: {
17
+ onWrite: (data) => {
18
+ writeCallbackCalled = true
19
+ return data
20
+ }
21
+ }
22
+ },
23
+ sheathing: {
24
+ object: {
25
+ demo: 1,
26
+ data: {
27
+ xyz: undefined
28
+ }
29
+ },
30
+ path: "data.xyz",
31
+ },
32
+ },
33
+ read: {
34
+ mapping: {
35
+ transformer: "call:onRead",
36
+ callbacks: {
37
+ onRead: (data) => {
38
+ readCallbackCalled = true
39
+ return data
40
+ }
41
+ }
42
+ },
43
+ path: 'data.xyz',
44
+ }
45
+ })
46
+
47
+ expect(server.transformServerPayload({demo: 1, data: {xyz: 2}})).to.deep.equal({demo: 1, data: {xyz: 2}})
48
+ expect(server.prepareServerPayload({demo: 1, data: {xyz: 2}})).to.deep.equal({demo: 1, data: {xyz: 2}})
49
+
50
+
51
+ })
52
+
53
+ });
@@ -7,7 +7,7 @@ describe('Monster', function () {
7
7
  let monsterVersion
8
8
 
9
9
  /** don´t touch, replaced by make with package.json version */
10
- monsterVersion = new Version('3.2.0')
10
+ monsterVersion = new Version('3.4.0')
11
11
 
12
12
  let m = getMonsterVersion();
13
13
 
@@ -0,0 +1,50 @@
1
+ import {expect} from "chai"
2
+ import {Message} from "../../../../../application/source/net/webconnect/message.mjs";
3
+
4
+ describe('Message', function () {
5
+
6
+ it('construct withouth parameters should throw', function (done) {
7
+
8
+ try {
9
+ new Message();
10
+ done(new Error('should throw'));
11
+ } catch (e) {
12
+ done();
13
+ return;
14
+ }
15
+
16
+ })
17
+
18
+ it('from json should ' , function (done) {
19
+ const json = {
20
+ "id": "123",
21
+ "type": "test",
22
+ "data": {
23
+ "test": "test"
24
+ }
25
+ }
26
+ const message = Message.fromJSON(JSON.stringify(json));
27
+ const data = message.getData();
28
+ expect(data.id).to.equal(json.id);
29
+ expect(data.type).to.equal(json.type);
30
+ expect(data.data).to.deep.equal(json.data);
31
+ done();
32
+ })
33
+
34
+ it ("to json should", function (done) {
35
+ const obj = {
36
+ "id": "123",
37
+ "type": "test",
38
+ "data": {
39
+ "test": "test"
40
+ }
41
+ }
42
+ const message = new Message(obj);
43
+ const data = JSON.stringify(message);
44
+ expect(data).to.equal('{"id":"123","type":"test","data":{"test":"test"}}');
45
+ done();
46
+ })
47
+
48
+
49
+
50
+ });
@@ -0,0 +1,116 @@
1
+ import {expect} from "chai"
2
+ import {WebConnect} from "../../../../application/source/net/webconnect.mjs";
3
+ import {Message} from "../../../../application/source/net/webconnect/message.mjs";
4
+ import {Observer} from "../../../../application/source/types/observer.mjs";
5
+ import {initWebSocket} from "../../util/websocket.mjs";
6
+
7
+ const testUrl = "wss://ws.postman-echo.com/raw"
8
+
9
+ describe('Websocket', function () {
10
+
11
+ let ds = undefined
12
+
13
+ before(function (done) {
14
+ initWebSocket().then(() => {
15
+ done()
16
+ }).catch((e) => {
17
+ done(e)
18
+ })
19
+ });
20
+
21
+ afterEach(function (done) {
22
+ if (ds) {
23
+ ds.close()
24
+ }
25
+
26
+ // without this, the node test will hang
27
+ for (const sym of Object.getOwnPropertySymbols(ds)) {
28
+ if (sym.toString() === 'Symbol(connection)') {
29
+ if (typeof ds[sym]?.socket?.terminate === 'function') {
30
+ ds[sym]?.socket?.['terminate']()
31
+ }
32
+ }
33
+ }
34
+
35
+ done()
36
+ });
37
+
38
+
39
+ it('should transform data', function (done) {
40
+
41
+ ds = new WebConnect( {
42
+ url: testUrl,
43
+ })
44
+
45
+ ds.connect().then(() => {
46
+
47
+ ds.attachObserver(new Observer(()=> {
48
+ done()
49
+ }))
50
+
51
+ ds.send({
52
+ data: {
53
+ message: "Hello World"
54
+ }
55
+ })
56
+
57
+ }).catch((e) => {
58
+ done(e)
59
+ })
60
+
61
+
62
+ })
63
+
64
+ it('should connect', function (done) {
65
+ ds = new WebConnect({
66
+ url: testUrl,
67
+ reconnect: {
68
+ enabled: false
69
+ }
70
+ });
71
+ ds.connect().then(() => {
72
+ done()
73
+ }).catch((e) => {
74
+ done(e)
75
+ })
76
+
77
+
78
+ })
79
+
80
+ it('should send message', function (done) {
81
+ ds = new WebConnect({
82
+ url: testUrl,
83
+ reconnect: {
84
+ enabled: false
85
+ }
86
+ });
87
+ ds.connect().then(() => {
88
+
89
+ ds.attachObserver(new Observer(()=> {
90
+
91
+ expect(ds.dataReceived()).to.be.true
92
+
93
+ try {
94
+ const msg = ds.poll()
95
+ expect(msg).to.be.instanceOf(Message)
96
+ const data = msg.getData()
97
+ expect(data).to.be.deep.equal({message: "Hello World"})
98
+ } catch (e) {
99
+ done(e)
100
+ return
101
+ }
102
+ done()
103
+ }))
104
+
105
+ ds.send({
106
+ message: "Hello World"
107
+ })
108
+
109
+ }).catch((e) => {
110
+ done(e)
111
+ })
112
+
113
+
114
+ }).timeout(10000);
115
+
116
+ });
@@ -0,0 +1,17 @@
1
+ import {expect} from "chai"
2
+ import {ObservableQueue} from "../../../../application/source/types/observablequeue.mjs";
3
+ import {Observer} from "../../../../application/source/types/observer.mjs";
4
+
5
+ describe('ObservableQueue', function () {
6
+ describe('Observer', function () {
7
+
8
+ it('should notify', function (done) {
9
+ let queue = new ObservableQueue;
10
+ let o = new Observer((q) => {
11
+ done()
12
+ });
13
+ queue.attachObserver(o);
14
+ expect(queue.add('a')).to.be.instanceOf(ObservableQueue);
15
+ });
16
+ });
17
+ })
@@ -30,7 +30,7 @@ describe('Queue', function () {
30
30
  });
31
31
 
32
32
  })
33
-
33
+
34
34
  describe('add and clear', function () {
35
35
 
36
36
  it('should empty', function () {
@@ -42,4 +42,7 @@ describe('Queue', function () {
42
42
  });
43
43
 
44
44
  })
45
+
46
+
47
+
45
48
  })
@@ -30,10 +30,13 @@ import "../cases/i18n/locale.mjs";
30
30
  import "../cases/i18n/formatter.mjs";
31
31
  import "../cases/i18n/providers/fetch.mjs";
32
32
  import "../cases/i18n/provider.mjs";
33
+ import "../cases/net/webconnect/message.mjs";
34
+ import "../cases/net/webconnect.mjs";
33
35
  import "../cases/types/mediatype.mjs";
34
36
  import "../cases/types/typeof.mjs";
35
37
  import "../cases/types/observerlist.mjs";
36
38
  import "../cases/types/randomid.mjs";
39
+ import "../cases/types/observablequeue.mjs";
37
40
  import "../cases/types/uuid.mjs";
38
41
  import "../cases/types/observer.mjs";
39
42
  import "../cases/types/tokenlist.mjs";
@@ -75,8 +78,9 @@ import "../cases/data/buildtree.mjs";
75
78
  import "../cases/data/transformer.mjs";
76
79
  import "../cases/data/pathfinder.mjs";
77
80
  import "../cases/data/diff.mjs";
78
- import "../cases/data/datasource/restapi.mjs";
81
+ import "../cases/data/datasource/server.mjs";
79
82
  import "../cases/data/datasource/storage/sessionstorage.mjs";
80
83
  import "../cases/data/datasource/storage/localstorage.mjs";
81
- import "../cases/data/datasource/webservice.mjs";
84
+ import "../cases/data/datasource/server/restapi.mjs";
85
+ import "../cases/data/datasource/server/websocket.mjs";
82
86
  import "../cases/math/random.mjs";
@@ -5,7 +5,7 @@
5
5
  <title>Mocha Monster</title>
6
6
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
7
7
  <link rel="stylesheet" href="mocha.css"/>
8
- <script id="polyfill" src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Array.isArray,Array.prototype.entries,Array.prototype.fill,Array.prototype.filter,Array.prototype.forEach,Array.prototype.indexOf,Array.prototype.keys,Array.prototype.lastIndexOf,Array.prototype.map,Array.prototype.reduce,Array.prototype.sort,ArrayBuffer,atob,CustomEvent,DataView,document,Document,DocumentFragment,Element,Event,fetch,globalThis,HTMLDocument,HTMLTemplateElement,Intl,JSON,Map,Math.log2,Number.isInteger,Object.assign,Object.defineProperty,Object.entries,Object.freeze,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.keys,Promise,Reflect,Reflect.defineProperty,Reflect.get,Reflect.getOwnPropertyDescriptor,Reflect.setPrototypeOf,Set,String.prototype.endsWith,String.prototype.matchAll,String.prototype.padStart,String.prototype.startsWith,String.prototype.trim,Symbol,Symbol.for,Symbol.hasInstance,Symbol.iterator,Uint16Array,Uint8Array,URL,WeakMap,WeakSet"
8
+ <script id="polyfill" src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Array.isArray,Array.prototype.entries,Array.prototype.fill,Array.prototype.filter,Array.prototype.forEach,Array.prototype.indexOf,Array.prototype.keys,Array.prototype.lastIndexOf,Array.prototype.map,Array.prototype.reduce,Array.prototype.sort,ArrayBuffer,atob,Blob,CustomEvent,DataView,document,Document,DocumentFragment,Element,Event,fetch,globalThis,HTMLDocument,HTMLTemplateElement,Intl,JSON,Map,Math.log2,Number.isInteger,Object.assign,Object.defineProperty,Object.entries,Object.freeze,Object.getOwnPropertyDescriptor,Object.getOwnPropertyNames,Object.getPrototypeOf,Object.keys,Promise,Reflect,Reflect.defineProperty,Reflect.get,Reflect.getOwnPropertyDescriptor,Reflect.setPrototypeOf,Set,String.prototype.endsWith,String.prototype.matchAll,String.prototype.padStart,String.prototype.startsWith,String.prototype.trim,Symbol,Symbol.for,Symbol.hasInstance,Symbol.iterator,Uint16Array,Uint8Array,URL,WeakMap,WeakSet"
9
9
  src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Array.isArray,Array.prototype.entries,Array.prototype.fill,Array.prototype.forEach,Array.prototype.indexOf,Array.prototype.keys,Array.prototype.lastIndexOf,Array.prototype.map,Array.prototype.reduce,Array.prototype.sort,ArrayBuffer,atob,DataView,document,DocumentFragment,Element,Event,globalThis,HTMLDocument,HTMLTemplateElement,JSON,Map,Math.log2,Number.isInteger,Object.assign,Object.defineProperty,Object.entries,Object.getOwnPropertyDescriptor,Object.getPrototypeOf,Object.keys,Promise,Reflect,Reflect.defineProperty,Reflect.get,Reflect.getOwnPropertyDescriptor,Reflect.setPrototypeOf,Set,String.prototype.endsWith,String.prototype.matchAll,String.prototype.padStart,String.prototype.startsWith,String.prototype.trim,Symbol,Symbol.iterator,WeakMap,WeakSet"
10
10
  crossorigin="anonymous"
11
11
  referrerpolicy="no-referrer"></script>
@@ -14,8 +14,8 @@
14
14
  </head>
15
15
  <body>
16
16
  <div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
17
- <h1 style='margin-bottom: 0.1em;'>Monster 3.0.0</h1>
18
- <div id="lastupdate" style='font-size:0.7em'>last update Fr 6. Jan 12:54:47 CET 2023</div>
17
+ <h1 style='margin-bottom: 0.1em;'>Monster 3.3.0</h1>
18
+ <div id="lastupdate" style='font-size:0.7em'>last update So 8. Jan 17:05:05 CET 2023</div>
19
19
  </div>
20
20
  <div id="mocks"></div>
21
21
  <div id="mocha"></div>