@eleven-am/pondsocket 0.1.57 → 0.1.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{dist/channel → channel}/channel.js +5 -3
- package/package.json +3 -3
- package/.eslintrc.json +0 -387
- package/dist/LICENSE +0 -674
- package/dist/README.md +0 -139
- package/dist/package.json +0 -51
- package/jest.config.js +0 -11
- package/src/abstracts/abstractRequest.test.ts +0 -49
- package/src/abstracts/abstractRequest.ts +0 -56
- package/src/abstracts/abstractResponse.ts +0 -26
- package/src/abstracts/middleware.test.ts +0 -75
- package/src/abstracts/middleware.ts +0 -50
- package/src/channel/channel.test.ts +0 -501
- package/src/channel/channel.ts +0 -305
- package/src/channel/eventRequest.test.ts +0 -37
- package/src/channel/eventRequest.ts +0 -27
- package/src/channel/eventResponse.test.ts +0 -249
- package/src/channel/eventResponse.ts +0 -172
- package/src/client/channel.test.ts +0 -799
- package/src/client/channel.ts +0 -342
- package/src/client.ts +0 -124
- package/src/endpoint/endpoint.test.ts +0 -825
- package/src/endpoint/endpoint.ts +0 -304
- package/src/endpoint/response.ts +0 -106
- package/src/enums.ts +0 -52
- package/src/errors/pondError.ts +0 -32
- package/src/express.ts +0 -58
- package/src/index.ts +0 -3
- package/src/lobby/JoinRequest.test.ts +0 -48
- package/src/lobby/JoinResponse.test.ts +0 -162
- package/src/lobby/joinRequest.ts +0 -32
- package/src/lobby/joinResponse.ts +0 -146
- package/src/lobby/lobby.ts +0 -182
- package/src/matcher/matcher.test.ts +0 -103
- package/src/matcher/matcher.ts +0 -105
- package/src/node.ts +0 -33
- package/src/presence/presence.ts +0 -127
- package/src/presence/presenceEngine.test.ts +0 -143
- package/src/server/pondSocket.ts +0 -153
- package/src/subjects/subject.test.ts +0 -163
- package/src/subjects/subject.ts +0 -137
- package/src/typedefs.d.ts +0 -451
- package/src/types.d.ts +0 -89
- package/tsconfig.build.json +0 -7
- package/tsconfig.json +0 -12
- /package/{dist/abstracts → abstracts}/abstractRequest.js +0 -0
- /package/{dist/abstracts → abstracts}/abstractResponse.js +0 -0
- /package/{dist/abstracts → abstracts}/middleware.js +0 -0
- /package/{dist/channel → channel}/eventRequest.js +0 -0
- /package/{dist/channel → channel}/eventResponse.js +0 -0
- /package/{dist/client → client}/channel.js +0 -0
- /package/{dist/client.d.ts → client.d.ts} +0 -0
- /package/{dist/client.js → client.js} +0 -0
- /package/{dist/endpoint → endpoint}/endpoint.js +0 -0
- /package/{dist/endpoint → endpoint}/response.js +0 -0
- /package/{dist/enums.js → enums.js} +0 -0
- /package/{dist/errors → errors}/pondError.js +0 -0
- /package/{dist/express.d.ts → express.d.ts} +0 -0
- /package/{dist/express.js → express.js} +0 -0
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /package/{dist/lobby → lobby}/joinRequest.js +0 -0
- /package/{dist/lobby → lobby}/joinResponse.js +0 -0
- /package/{dist/lobby → lobby}/lobby.js +0 -0
- /package/{dist/matcher → matcher}/matcher.js +0 -0
- /package/{dist/node.d.ts → node.d.ts} +0 -0
- /package/{dist/node.js → node.js} +0 -0
- /package/{dist/presence → presence}/presence.js +0 -0
- /package/{dist/server → server}/pondSocket.js +0 -0
- /package/{dist/subjects → subjects}/subject.js +0 -0
- /package/{dist/types.d.ts → types.d.ts} +0 -0
|
@@ -1,501 +0,0 @@
|
|
|
1
|
-
import { ChannelEngine } from './channel';
|
|
2
|
-
import { EventRequest } from './eventRequest';
|
|
3
|
-
import { EventResponse } from './eventResponse';
|
|
4
|
-
import { ServerActions, PresenceEventTypes, SystemSender, ChannelReceiver, ClientActions, ErrorTypes } from '../enums';
|
|
5
|
-
|
|
6
|
-
export const createParentEngine = () => {
|
|
7
|
-
const parentEngine = {
|
|
8
|
-
destroyChannel: jest.fn(),
|
|
9
|
-
execute: jest.fn(),
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
return parentEngine as any;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
describe('ChannelEngine', () => {
|
|
16
|
-
it('should add user to channel', () => {
|
|
17
|
-
const onMessage = jest.fn();
|
|
18
|
-
const parentEngine = createParentEngine();
|
|
19
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
20
|
-
|
|
21
|
-
expect(channelEngine.size).toEqual(0);
|
|
22
|
-
expect(channelEngine.getUserData('test')).not.toBeDefined();
|
|
23
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
24
|
-
expect(channelEngine.size).toEqual(1);
|
|
25
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
26
|
-
assigns: { test: 1 },
|
|
27
|
-
presence: {},
|
|
28
|
-
id: 'test',
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should throw error if user is already in channel', () => {
|
|
33
|
-
const onMessage = jest.fn();
|
|
34
|
-
const parentEngine = createParentEngine();
|
|
35
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
36
|
-
|
|
37
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
38
|
-
expect(() => {
|
|
39
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
40
|
-
}).toThrow('ChannelEngine: User with id test already exists in channel testChannel');
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should update a users assigns', () => {
|
|
44
|
-
const onMessage = jest.fn();
|
|
45
|
-
const parentEngine = createParentEngine();
|
|
46
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
47
|
-
|
|
48
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
49
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
50
|
-
assigns: { test: 1 },
|
|
51
|
-
presence: {},
|
|
52
|
-
id: 'test',
|
|
53
|
-
});
|
|
54
|
-
channelEngine.updateAssigns('test', { test: 2 });
|
|
55
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
56
|
-
assigns: { test: 2 },
|
|
57
|
-
presence: {},
|
|
58
|
-
id: 'test',
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should throw error if user is not in channel', () => {
|
|
63
|
-
const parentEngine = createParentEngine();
|
|
64
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
65
|
-
|
|
66
|
-
expect(() => {
|
|
67
|
-
channelEngine.updateAssigns('test', { test: 2 });
|
|
68
|
-
}).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should be able to get users assigns in the channel', () => {
|
|
72
|
-
const onMessage = jest.fn();
|
|
73
|
-
const parentEngine = createParentEngine();
|
|
74
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
75
|
-
|
|
76
|
-
expect(channelEngine.getAssigns()).toEqual({});
|
|
77
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
78
|
-
expect(channelEngine.getAssigns()).toEqual({
|
|
79
|
-
test: { test: 1 },
|
|
80
|
-
});
|
|
81
|
-
channelEngine.addUser('test2', { test: 2 }, onMessage);
|
|
82
|
-
expect(channelEngine.getAssigns()).toEqual({
|
|
83
|
-
test: { test: 1 },
|
|
84
|
-
test2: { test: 2 },
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should be able to track presence', () => {
|
|
89
|
-
const onMessage = jest.fn();
|
|
90
|
-
const parentEngine = createParentEngine();
|
|
91
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
92
|
-
|
|
93
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
94
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
95
|
-
assigns: { test: 1 },
|
|
96
|
-
presence: {},
|
|
97
|
-
id: 'test',
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
expect(channelEngine.presenceEngine).not.toBeDefined();
|
|
101
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
102
|
-
expect(channelEngine.presenceEngine).toBeDefined();
|
|
103
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
104
|
-
assigns: { test: 1 },
|
|
105
|
-
presence: { test: 2 },
|
|
106
|
-
id: 'test',
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('should throw error if user is not in channel', () => {
|
|
111
|
-
const parentEngine = createParentEngine();
|
|
112
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
113
|
-
|
|
114
|
-
expect(() => {
|
|
115
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
116
|
-
}).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('should throw error if channel is already tracking the users presence', () => {
|
|
120
|
-
const onMessage = jest.fn();
|
|
121
|
-
const parentEngine = createParentEngine();
|
|
122
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
123
|
-
|
|
124
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
125
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
126
|
-
expect(() => {
|
|
127
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
128
|
-
}).toThrow('PresenceEngine: Presence with key test already exists');
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('should be able to list presence', () => {
|
|
132
|
-
const onMessage = jest.fn();
|
|
133
|
-
const parentEngine = createParentEngine();
|
|
134
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
135
|
-
|
|
136
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
137
|
-
channelEngine.addUser('test2', { test: 2 }, onMessage);
|
|
138
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
139
|
-
expect(onMessage).toHaveBeenCalledWith({
|
|
140
|
-
channelName: 'test',
|
|
141
|
-
action: ServerActions.PRESENCE,
|
|
142
|
-
event: PresenceEventTypes.JOIN,
|
|
143
|
-
payload: {
|
|
144
|
-
presence: [{ test: 2 }],
|
|
145
|
-
changed: {
|
|
146
|
-
test: 2,
|
|
147
|
-
},
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
channelEngine.trackPresence('test2', { test: 3 });
|
|
152
|
-
expect(channelEngine.presenceEngine?.getPresence()).toEqual({
|
|
153
|
-
test: { test: 2 },
|
|
154
|
-
test2: { test: 3 },
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
expect(onMessage).toHaveBeenCalledTimes(3);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('should update a users presence', () => {
|
|
161
|
-
const onMessage = jest.fn();
|
|
162
|
-
const parentEngine = createParentEngine();
|
|
163
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
164
|
-
|
|
165
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
166
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
167
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
168
|
-
assigns: { test: 1 },
|
|
169
|
-
presence: { test: 2 },
|
|
170
|
-
id: 'test',
|
|
171
|
-
});
|
|
172
|
-
channelEngine.presenceEngine?.updatePresence('test', { test: 3 });
|
|
173
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
174
|
-
assigns: { test: 1 },
|
|
175
|
-
presence: { test: 3 },
|
|
176
|
-
id: 'test',
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it('should throw error if user is not in channel', () => {
|
|
181
|
-
const parentEngine = createParentEngine();
|
|
182
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
183
|
-
|
|
184
|
-
expect(() => {
|
|
185
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
186
|
-
}).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
it('should remove user from channel', () => {
|
|
190
|
-
const onMessage = jest.fn();
|
|
191
|
-
const parentEngine = createParentEngine();
|
|
192
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
193
|
-
|
|
194
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
195
|
-
expect(channelEngine.size).toEqual(1);
|
|
196
|
-
expect(channelEngine.getUserData('test')).toBeDefined();
|
|
197
|
-
channelEngine.removeUser('test');
|
|
198
|
-
expect(channelEngine.size).toEqual(0);
|
|
199
|
-
expect(channelEngine.getUserData('test')).not.toBeDefined();
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it('should throw error if user is not in channel', () => {
|
|
203
|
-
const parentEngine = createParentEngine();
|
|
204
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
205
|
-
|
|
206
|
-
expect(() => {
|
|
207
|
-
channelEngine.removeUser('test');
|
|
208
|
-
}).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it('should untrack presence when user is removed', () => {
|
|
212
|
-
const onMessage = jest.fn();
|
|
213
|
-
const parentEngine = createParentEngine();
|
|
214
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
215
|
-
|
|
216
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
217
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
218
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
219
|
-
channelEngine.trackPresence('test2', { test: 2 });
|
|
220
|
-
expect(channelEngine.presenceEngine?.getPresence()).toEqual({
|
|
221
|
-
test: { test: 2 },
|
|
222
|
-
test2: { test: 2 },
|
|
223
|
-
});
|
|
224
|
-
onMessage.mockClear();
|
|
225
|
-
channelEngine.removeUser('test');
|
|
226
|
-
expect(channelEngine.presenceEngine?.getPresence()).toEqual({
|
|
227
|
-
test2: { test: 2 },
|
|
228
|
-
});
|
|
229
|
-
expect(onMessage).toHaveBeenCalledWith({
|
|
230
|
-
channelName: 'test',
|
|
231
|
-
event: PresenceEventTypes.LEAVE,
|
|
232
|
-
action: ServerActions.PRESENCE,
|
|
233
|
-
payload: {
|
|
234
|
-
presence: [{ test: 2 }],
|
|
235
|
-
changed: {
|
|
236
|
-
test: 2,
|
|
237
|
-
},
|
|
238
|
-
},
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it('should throw error if user is not in channel and isPond is false', () => {
|
|
243
|
-
const parentEngine = createParentEngine();
|
|
244
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
245
|
-
|
|
246
|
-
// add a user to the channel
|
|
247
|
-
channelEngine.addUser('test', { test: 1 }, jest.fn());
|
|
248
|
-
|
|
249
|
-
// track the added user's presence
|
|
250
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
251
|
-
|
|
252
|
-
// we do this because we want to initialize the presence engine
|
|
253
|
-
// the engine is not initialized by default when a channel is created
|
|
254
|
-
|
|
255
|
-
expect(() => {
|
|
256
|
-
// now we try to untrack the presence of a user that is not in the channel
|
|
257
|
-
channelEngine.presenceEngine?.removePresence('test1');
|
|
258
|
-
}).toThrow('PresenceEngine: Presence with key test1 does not exist');
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('should not throw error if user is not in channel and isPond is true', () => {
|
|
262
|
-
const parentEngine = createParentEngine();
|
|
263
|
-
const channelEngine = new ChannelEngine('testChannel', parentEngine);
|
|
264
|
-
|
|
265
|
-
// add a user to the channel
|
|
266
|
-
channelEngine.addUser('test', { test: 1 }, jest.fn());
|
|
267
|
-
|
|
268
|
-
// track the added user's presence
|
|
269
|
-
channelEngine.trackPresence('test', { test: 2 });
|
|
270
|
-
|
|
271
|
-
// we do this because we want to initialize the presence engine
|
|
272
|
-
// the engine is not initialized by default when a xhannel is created
|
|
273
|
-
|
|
274
|
-
expect(() => {
|
|
275
|
-
// now we try to untrack the presence of a user that is not in the channel
|
|
276
|
-
channelEngine.presenceEngine?.removePresence('test');
|
|
277
|
-
}).not.toThrow();
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
it('should be able to kick a user from the channel', () => {
|
|
281
|
-
const onMessage = jest.fn();
|
|
282
|
-
const parentEngine = createParentEngine();
|
|
283
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
284
|
-
|
|
285
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
286
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
287
|
-
channelEngine.kickUser('test2', 'test reason');
|
|
288
|
-
expect(channelEngine.size).toEqual(1);
|
|
289
|
-
expect(channelEngine.getUserData('test2')).not.toBeDefined();
|
|
290
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual({
|
|
291
|
-
action: ServerActions.SYSTEM,
|
|
292
|
-
channelName: 'test',
|
|
293
|
-
event: 'kicked_out',
|
|
294
|
-
payload: {
|
|
295
|
-
message: 'test reason',
|
|
296
|
-
},
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
expect(onMessage.mock.calls[1][0]).toStrictEqual({
|
|
300
|
-
action: ServerActions.SYSTEM,
|
|
301
|
-
channelName: 'test',
|
|
302
|
-
event: 'kicked',
|
|
303
|
-
payload: {
|
|
304
|
-
reason: 'test reason',
|
|
305
|
-
userId: 'test2',
|
|
306
|
-
},
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
it('should call destroy on parent engine', () => {
|
|
313
|
-
const parentEngine = createParentEngine();
|
|
314
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
315
|
-
|
|
316
|
-
channelEngine.destroy('test');
|
|
317
|
-
expect(parentEngine.destroyChannel).toHaveBeenCalled();
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
it('should broadcast a message to all users', () => {
|
|
321
|
-
const onMessage = jest.fn();
|
|
322
|
-
const parentEngine = createParentEngine();
|
|
323
|
-
|
|
324
|
-
parentEngine.execute = (_: any, res: any) => {
|
|
325
|
-
res.accept();
|
|
326
|
-
};
|
|
327
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
328
|
-
|
|
329
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
330
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
331
|
-
channelEngine.sendMessage(SystemSender.CHANNEL, ChannelReceiver.ALL_USERS, ServerActions.BROADCAST, 'test', { test: 2 });
|
|
332
|
-
|
|
333
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual({
|
|
334
|
-
action: ServerActions.BROADCAST,
|
|
335
|
-
channelName: 'test',
|
|
336
|
-
event: 'test',
|
|
337
|
-
payload: {
|
|
338
|
-
test: 2,
|
|
339
|
-
},
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
343
|
-
onMessage.mockClear();
|
|
344
|
-
|
|
345
|
-
channelEngine.sendMessage('test2', ChannelReceiver.ALL_EXCEPT_SENDER, ServerActions.BROADCAST, 'test', { test: 3 });
|
|
346
|
-
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
347
|
-
|
|
348
|
-
onMessage.mockClear();
|
|
349
|
-
|
|
350
|
-
// when user is not in channel it throws an error
|
|
351
|
-
expect(() => {
|
|
352
|
-
channelEngine.sendMessage('test3', ChannelReceiver.ALL_EXCEPT_SENDER, ServerActions.BROADCAST, 'test', { test: 3 });
|
|
353
|
-
}).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
it('should broadcast a message to all users except sender', () => {
|
|
357
|
-
const onMessage = jest.fn();
|
|
358
|
-
const parentEngine = createParentEngine();
|
|
359
|
-
|
|
360
|
-
parentEngine.execute = (_: any, res: any) => {
|
|
361
|
-
res.accept();
|
|
362
|
-
};
|
|
363
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
364
|
-
|
|
365
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
366
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
367
|
-
channelEngine.sendMessage('test2', ChannelReceiver.ALL_EXCEPT_SENDER, ServerActions.BROADCAST, 'test', { test: 2 });
|
|
368
|
-
|
|
369
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual({
|
|
370
|
-
action: ServerActions.BROADCAST,
|
|
371
|
-
channelName: 'test',
|
|
372
|
-
event: 'test',
|
|
373
|
-
payload: {
|
|
374
|
-
test: 2,
|
|
375
|
-
},
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
379
|
-
// when user is not in channel it throws an error
|
|
380
|
-
|
|
381
|
-
expect(() => {
|
|
382
|
-
channelEngine.sendMessage('test3', ChannelReceiver.ALL_EXCEPT_SENDER, ServerActions.BROADCAST, 'test', { test: 3 });
|
|
383
|
-
}).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
|
|
384
|
-
|
|
385
|
-
// when sender is channel itself it throws an error
|
|
386
|
-
expect(() => {
|
|
387
|
-
channelEngine.sendMessage(SystemSender.CHANNEL, ChannelReceiver.ALL_EXCEPT_SENDER, ServerActions.BROADCAST, 'test', { test: 3 });
|
|
388
|
-
}).toThrow(`ChannelEngine: Cannot use ${ChannelReceiver.ALL_EXCEPT_SENDER} with ${SystemSender.CHANNEL}`);
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
it('should broadcast a message to specific users', () => {
|
|
392
|
-
const onMessage = jest.fn();
|
|
393
|
-
const parentEngine = createParentEngine();
|
|
394
|
-
|
|
395
|
-
parentEngine.execute = (_: any, res: any) => {
|
|
396
|
-
res.accept();
|
|
397
|
-
};
|
|
398
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
399
|
-
|
|
400
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
401
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
402
|
-
channelEngine.addUser('test3', { test: 1 }, onMessage);
|
|
403
|
-
|
|
404
|
-
channelEngine.sendMessage('test2', ['test', 'test3'], ServerActions.BROADCAST, 'test', { test: 2 });
|
|
405
|
-
|
|
406
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual({
|
|
407
|
-
action: ServerActions.BROADCAST,
|
|
408
|
-
channelName: 'test',
|
|
409
|
-
event: 'test',
|
|
410
|
-
payload: {
|
|
411
|
-
test: 2,
|
|
412
|
-
},
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
expect(onMessage.mock.calls[1][0]).toStrictEqual({
|
|
416
|
-
action: ServerActions.BROADCAST,
|
|
417
|
-
channelName: 'test',
|
|
418
|
-
event: 'test',
|
|
419
|
-
payload: {
|
|
420
|
-
test: 2,
|
|
421
|
-
},
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
425
|
-
|
|
426
|
-
// when recipient is not in channel it throws an error
|
|
427
|
-
expect(() => {
|
|
428
|
-
channelEngine.sendMessage('test3', ['test', 'test3', 'test4'], ServerActions.BROADCAST, 'test', { test: 3 });
|
|
429
|
-
}).toThrow('ChannelEngine: Users test4 are not in channel test');
|
|
430
|
-
|
|
431
|
-
// when sender is not in channel it throws an error
|
|
432
|
-
expect(() => {
|
|
433
|
-
channelEngine.sendMessage('test4', ['test', 'test3'], ServerActions.BROADCAST, 'test', { test: 3 });
|
|
434
|
-
}).toThrow('ChannelEngine: User with id test4 does not exist in channel test');
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
it('should broadcast messages while also triggering the onMessage callback', () => {
|
|
438
|
-
const onMessage = jest.fn();
|
|
439
|
-
const parentEngine = createParentEngine();
|
|
440
|
-
const channelEngine = new ChannelEngine('test', parentEngine);
|
|
441
|
-
|
|
442
|
-
parentEngine.execute = (req: any, res: any) => {
|
|
443
|
-
expect(req).toBeInstanceOf(EventRequest);
|
|
444
|
-
expect(res).toBeInstanceOf(EventResponse);
|
|
445
|
-
res.accept();
|
|
446
|
-
};
|
|
447
|
-
|
|
448
|
-
expect(() => channelEngine.broadcastMessage('test2', {
|
|
449
|
-
action: ClientActions.BROADCAST,
|
|
450
|
-
channelName: 'test',
|
|
451
|
-
event: 'test',
|
|
452
|
-
payload: { test: 1 },
|
|
453
|
-
addresses: ChannelReceiver.ALL_USERS,
|
|
454
|
-
})).toThrow('ChannelEngine: User with id test2 does not exist in channel test');
|
|
455
|
-
|
|
456
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
457
|
-
|
|
458
|
-
channelEngine.broadcastMessage('test2', {
|
|
459
|
-
action: ClientActions.BROADCAST,
|
|
460
|
-
channelName: 'test',
|
|
461
|
-
event: 'test',
|
|
462
|
-
payload: { test: 1 },
|
|
463
|
-
addresses: ChannelReceiver.ALL_USERS,
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
467
|
-
|
|
468
|
-
let count = 0;
|
|
469
|
-
|
|
470
|
-
onMessage.mockClear();
|
|
471
|
-
parentEngine.execute = (req: any, res: any, next: any) => {
|
|
472
|
-
expect(req).toBeInstanceOf(EventRequest);
|
|
473
|
-
expect(res).toBeInstanceOf(EventResponse);
|
|
474
|
-
count++;
|
|
475
|
-
next();
|
|
476
|
-
};
|
|
477
|
-
|
|
478
|
-
channelEngine.broadcastMessage('test2', {
|
|
479
|
-
action: ClientActions.BROADCAST,
|
|
480
|
-
channelName: 'test',
|
|
481
|
-
event: 'test',
|
|
482
|
-
payload: { test: 1 },
|
|
483
|
-
addresses: ChannelReceiver.ALL_USERS,
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
// proves that the message was received by the parent engine
|
|
487
|
-
expect(count).toBe(1);
|
|
488
|
-
|
|
489
|
-
// however the onMessage callback was not called
|
|
490
|
-
// because the message was never handled by the parent engine
|
|
491
|
-
expect(onMessage).toHaveBeenCalledWith({
|
|
492
|
-
action: ServerActions.ERROR,
|
|
493
|
-
channelName: 'test',
|
|
494
|
-
event: ErrorTypes.HANDLER_NOT_FOUND,
|
|
495
|
-
payload: {
|
|
496
|
-
code: 404,
|
|
497
|
-
message: 'A handler did not respond to the event',
|
|
498
|
-
},
|
|
499
|
-
});
|
|
500
|
-
});
|
|
501
|
-
});
|