@eleven-am/pondsocket 0.1.49 → 0.1.51

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.
Files changed (54) hide show
  1. package/abstracts/abstractRequest.js +58 -0
  2. package/abstracts/middleware.js +51 -0
  3. package/channel/channel.js +253 -0
  4. package/{server/channel → channel}/eventRequest.js +4 -1
  5. package/channel/eventResponse.js +150 -0
  6. package/client/channel.js +120 -97
  7. package/client.d.ts +2 -3
  8. package/client.js +34 -20
  9. package/endpoint/endpoint.js +233 -0
  10. package/endpoint/response.js +86 -0
  11. package/enums.js +14 -10
  12. package/errors/pondError.js +27 -0
  13. package/express.d.ts +2 -2
  14. package/express.js +3 -3
  15. package/index.d.ts +1 -2
  16. package/index.js +1 -4
  17. package/lobby/joinRequest.js +39 -0
  18. package/lobby/joinResponse.js +125 -0
  19. package/lobby/lobby.js +174 -0
  20. package/matcher/matcher.js +94 -0
  21. package/node.d.ts +2 -3
  22. package/node.js +3 -4
  23. package/package.json +3 -2
  24. package/presence/presence.js +113 -0
  25. package/server/pondSocket.js +123 -0
  26. package/subjects/subject.js +93 -0
  27. package/types.d.ts +274 -323
  28. package/client/channel.test.js +0 -546
  29. package/server/abstracts/abstractRequest.js +0 -40
  30. package/server/abstracts/abstractRequest.test.js +0 -41
  31. package/server/abstracts/middleware.js +0 -38
  32. package/server/abstracts/middleware.test.js +0 -70
  33. package/server/channel/channelEngine.js +0 -280
  34. package/server/channel/channelEngine.test.js +0 -377
  35. package/server/channel/channelRequest.test.js +0 -29
  36. package/server/channel/channelResponse.test.js +0 -164
  37. package/server/channel/eventResponse.js +0 -153
  38. package/server/endpoint/connectionResponse.js +0 -64
  39. package/server/endpoint/endpoint.js +0 -253
  40. package/server/endpoint/endpoint.test.js +0 -428
  41. package/server/endpoint/endpointResponse.test.js +0 -43
  42. package/server/pondChannel/joinRequest.js +0 -29
  43. package/server/pondChannel/joinResponse.js +0 -103
  44. package/server/pondChannel/pondChannel.js +0 -185
  45. package/server/pondChannel/pondChannelResponse.test.js +0 -109
  46. package/server/presence/presenceEngine.js +0 -107
  47. package/server/presence/presenceEngine.test.js +0 -105
  48. package/server/server/pondSocket.js +0 -121
  49. package/server/server/server.test.js +0 -121
  50. package/server/utils/matchPattern.js +0 -108
  51. package/server/utils/matchPattern.test.js +0 -76
  52. package/server/utils/subjectUtils.js +0 -68
  53. package/server/utils/subjectUtils.test.js +0 -128
  54. /package/{server/abstracts → abstracts}/abstractResponse.js +0 -0
@@ -1,377 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createParentEngine = void 0;
4
- const channelEngine_1 = require("./channelEngine");
5
- const enums_1 = require("../../enums");
6
- const createParentEngine = () => {
7
- const parentEngine = {
8
- destroyChannel: jest.fn(),
9
- execute: jest.fn(),
10
- };
11
- return parentEngine;
12
- };
13
- exports.createParentEngine = createParentEngine;
14
- describe('ChannelEngine', () => {
15
- it('should add user to channel', () => {
16
- const onMessage = jest.fn();
17
- const parentEngine = (0, exports.createParentEngine)();
18
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
19
- expect(channelEngine['_users'].size).toEqual(0);
20
- expect(channelEngine.getUserData('test')).not.toBeDefined();
21
- channelEngine.addUser('test', { test: 1 }, onMessage);
22
- expect(channelEngine['_users'].size).toEqual(1);
23
- expect(channelEngine['_users'].get('test')).toEqual({ test: 1 });
24
- expect(channelEngine.getUserData('test')).toEqual({
25
- assigns: { test: 1 },
26
- presence: {},
27
- id: 'test',
28
- });
29
- });
30
- it('should throw error if user is already in channel', () => {
31
- const onMessage = jest.fn();
32
- const parentEngine = (0, exports.createParentEngine)();
33
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
34
- channelEngine.addUser('test', { test: 1 }, onMessage);
35
- expect(() => {
36
- channelEngine.addUser('test', { test: 1 }, onMessage);
37
- }).toThrow('ChannelEngine: User with id test already exists in channel testChannel');
38
- });
39
- it('should update a users assigns', () => {
40
- const onMessage = jest.fn();
41
- const parentEngine = (0, exports.createParentEngine)();
42
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
43
- channelEngine.addUser('test', { test: 1 }, onMessage);
44
- expect(channelEngine.getUserData('test')).toEqual({
45
- assigns: { test: 1 },
46
- presence: {},
47
- id: 'test',
48
- });
49
- channelEngine.updateAssigns('test', { test: 2 });
50
- expect(channelEngine.getUserData('test')).toEqual({
51
- assigns: { test: 2 },
52
- presence: {},
53
- id: 'test',
54
- });
55
- });
56
- it('should throw error if user is not in channel', () => {
57
- const parentEngine = (0, exports.createParentEngine)();
58
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
59
- expect(() => {
60
- channelEngine.updateAssigns('test', { test: 2 });
61
- }).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
62
- });
63
- it('should be able to get users assigns in the channel', () => {
64
- const onMessage = jest.fn();
65
- const parentEngine = (0, exports.createParentEngine)();
66
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
67
- expect(channelEngine.getAssigns()).toEqual({});
68
- channelEngine.addUser('test', { test: 1 }, onMessage);
69
- expect(channelEngine.getAssigns()).toEqual({
70
- test: { test: 1 },
71
- });
72
- channelEngine.addUser('test2', { test: 2 }, onMessage);
73
- expect(channelEngine.getAssigns()).toEqual({
74
- test: { test: 1 },
75
- test2: { test: 2 },
76
- });
77
- });
78
- it('should be able to track presence', () => {
79
- const onMessage = jest.fn();
80
- const parentEngine = (0, exports.createParentEngine)();
81
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
82
- channelEngine.addUser('test', { test: 1 }, onMessage);
83
- expect(channelEngine.getUserData('test')).toEqual({
84
- assigns: { test: 1 },
85
- presence: {},
86
- id: 'test',
87
- });
88
- expect(channelEngine['_presenceEngine']).not.toBeDefined();
89
- channelEngine.trackPresence('test', { test: 2 });
90
- expect(channelEngine['_presenceEngine']).toBeDefined();
91
- expect(channelEngine.getUserData('test')).toEqual({
92
- assigns: { test: 1 },
93
- presence: { test: 2 },
94
- id: 'test',
95
- });
96
- });
97
- it('should throw error if user is not in channel', () => {
98
- const parentEngine = (0, exports.createParentEngine)();
99
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
100
- expect(() => {
101
- channelEngine.trackPresence('test', { test: 2 });
102
- }).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
103
- });
104
- it('should throw error if channel is already tracking the users presence', () => {
105
- const onMessage = jest.fn();
106
- const parentEngine = (0, exports.createParentEngine)();
107
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
108
- channelEngine.addUser('test', { test: 1 }, onMessage);
109
- channelEngine.trackPresence('test', { test: 2 });
110
- expect(() => {
111
- channelEngine.trackPresence('test', { test: 2 });
112
- }).toThrow('ChannelEngine: User with id test already has a presence subscription in channel test');
113
- });
114
- it('should be able to list presence', () => {
115
- const onMessage = jest.fn();
116
- const parentEngine = (0, exports.createParentEngine)();
117
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
118
- expect(channelEngine.getPresence()).toEqual({});
119
- channelEngine.addUser('test', { test: 1 }, onMessage);
120
- channelEngine.addUser('test2', { test: 2 }, onMessage);
121
- channelEngine.trackPresence('test', { test: 2 });
122
- expect(onMessage).toHaveBeenCalledWith({
123
- channelName: 'test',
124
- action: channelEngine_1.ServerActions.PRESENCE,
125
- event: enums_1.PresenceEventTypes.JOIN,
126
- payload: {
127
- presence: [{ test: 2 }],
128
- changed: {
129
- test: 2,
130
- },
131
- },
132
- });
133
- channelEngine.trackPresence('test2', { test: 3 });
134
- expect(channelEngine.getPresence()).toEqual({
135
- test: { test: 2 },
136
- test2: { test: 3 },
137
- });
138
- expect(onMessage).toHaveBeenCalledTimes(3);
139
- });
140
- it('should update a users presence', () => {
141
- const onMessage = jest.fn();
142
- const parentEngine = (0, exports.createParentEngine)();
143
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
144
- channelEngine.addUser('test', { test: 1 }, onMessage);
145
- channelEngine.trackPresence('test', { test: 2 });
146
- expect(channelEngine.getUserData('test')).toEqual({
147
- assigns: { test: 1 },
148
- presence: { test: 2 },
149
- id: 'test',
150
- });
151
- channelEngine.updatePresence('test', { test: 3 });
152
- expect(channelEngine.getUserData('test')).toEqual({
153
- assigns: { test: 1 },
154
- presence: { test: 3 },
155
- id: 'test',
156
- });
157
- });
158
- it('should throw error if user is not in channel', () => {
159
- const parentEngine = (0, exports.createParentEngine)();
160
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
161
- expect(() => {
162
- channelEngine.updatePresence('test', { test: 2 });
163
- }).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
164
- });
165
- it('should throw error if user is not tracking presence', () => {
166
- const onMessage = jest.fn();
167
- const parentEngine = (0, exports.createParentEngine)();
168
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
169
- channelEngine.addUser('test', { test: 1 }, onMessage);
170
- expect(() => {
171
- channelEngine.updatePresence('test', { test: 2 });
172
- }).toThrow('ChannelEngine: Presence engine is not initialized');
173
- });
174
- it('should remove user from channel', () => {
175
- const onMessage = jest.fn();
176
- const parentEngine = (0, exports.createParentEngine)();
177
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
178
- channelEngine.addUser('test', { test: 1 }, onMessage);
179
- expect(channelEngine['_users'].size).toEqual(1);
180
- expect(channelEngine.getUserData('test')).toBeDefined();
181
- channelEngine.removeUser('test');
182
- expect(channelEngine['_users'].size).toEqual(0);
183
- expect(channelEngine.getUserData('test')).not.toBeDefined();
184
- });
185
- it('should throw error if user is not in channel', () => {
186
- const parentEngine = (0, exports.createParentEngine)();
187
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
188
- expect(() => {
189
- channelEngine.removeUser('test');
190
- }).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
191
- });
192
- it('should untrack presence when user is removed', () => {
193
- const onMessage = jest.fn();
194
- const parentEngine = (0, exports.createParentEngine)();
195
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
196
- channelEngine.addUser('test', { test: 1 }, onMessage);
197
- channelEngine.trackPresence('test', { test: 2 });
198
- channelEngine.addUser('test2', { test: 1 }, onMessage);
199
- channelEngine.trackPresence('test2', { test: 2 });
200
- expect(channelEngine.getPresence()).toEqual({
201
- test: { test: 2 },
202
- test2: { test: 2 },
203
- });
204
- onMessage.mockClear();
205
- channelEngine.removeUser('test');
206
- expect(channelEngine.getPresence()).toEqual({
207
- test2: { test: 2 },
208
- });
209
- expect(onMessage).toHaveBeenCalledWith({
210
- channelName: 'test',
211
- event: enums_1.PresenceEventTypes.LEAVE,
212
- action: channelEngine_1.ServerActions.PRESENCE,
213
- payload: {
214
- presence: [{ test: 2 }],
215
- changed: {
216
- test: 2,
217
- },
218
- },
219
- });
220
- });
221
- it('should throw error if user is not in channel and isPond is false', () => {
222
- const parentEngine = (0, exports.createParentEngine)();
223
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
224
- // add a user to the channel
225
- channelEngine.addUser('test', { test: 1 }, jest.fn());
226
- // track the added user's presence
227
- channelEngine.trackPresence('test', { test: 2 });
228
- // we do this because we want to initialize the presence engine
229
- // the engine is not initialized by default when a channel is created
230
- expect(() => {
231
- // now we try to untrack the presence of a user that is not in the channel
232
- channelEngine.unTrackPresence('test1', false);
233
- }).toThrow('PresenceEngine: Presence with key test1 does not exist');
234
- });
235
- it('should not throw error if user is not in channel and isPond is true', () => {
236
- const parentEngine = (0, exports.createParentEngine)();
237
- const channelEngine = new channelEngine_1.ChannelEngine('testChannel', parentEngine);
238
- // add a user to the channel
239
- channelEngine.addUser('test', { test: 1 }, jest.fn());
240
- // track the added user's presence
241
- channelEngine.trackPresence('test', { test: 2 });
242
- // we do this because we want to initialize the presence engine
243
- // the engine is not initialized by default when a xhannel is created
244
- expect(() => {
245
- // now we try to untrack the presence of a user that is not in the channel
246
- channelEngine.unTrackPresence('test', true);
247
- }).not.toThrow();
248
- });
249
- it('should be able to kick a user from the channel', () => {
250
- const onMessage = jest.fn();
251
- const parentEngine = (0, exports.createParentEngine)();
252
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
253
- channelEngine.addUser('test', { test: 1 }, onMessage);
254
- channelEngine.addUser('test2', { test: 1 }, onMessage);
255
- channelEngine.kickUser('test2', 'test reason');
256
- expect(channelEngine['_users'].size).toEqual(1);
257
- expect(channelEngine.getUserData('test2')).not.toBeDefined();
258
- expect(onMessage.mock.calls[0][0]).toStrictEqual({
259
- action: channelEngine_1.ServerActions.SYSTEM,
260
- channelName: 'test',
261
- event: 'kicked_out',
262
- payload: {
263
- reason: 'test reason',
264
- message: 'You have been kicked out of the channel',
265
- },
266
- });
267
- expect(onMessage.mock.calls[1][0]).toStrictEqual({
268
- action: channelEngine_1.ServerActions.SYSTEM,
269
- channelName: 'test',
270
- event: 'kicked',
271
- payload: {
272
- reason: 'test reason',
273
- userId: 'test2',
274
- },
275
- });
276
- expect(onMessage).toHaveBeenCalledTimes(2);
277
- });
278
- it('should call destroy on parent engine', () => {
279
- const parentEngine = (0, exports.createParentEngine)();
280
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
281
- channelEngine.destroy('test');
282
- expect(parentEngine.destroyChannel).toHaveBeenCalled();
283
- });
284
- it('should broadcast a message to all users', () => {
285
- const onMessage = jest.fn();
286
- const parentEngine = (0, exports.createParentEngine)();
287
- parentEngine.execute = (_, res) => {
288
- res.accept();
289
- };
290
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
291
- channelEngine.addUser('test', { test: 1 }, onMessage);
292
- channelEngine.addUser('test2', { test: 1 }, onMessage);
293
- channelEngine.sendMessage(enums_1.SystemSender.CHANNEL, 'all_users', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 2 });
294
- expect(onMessage.mock.calls[0][0]).toStrictEqual({
295
- action: channelEngine_1.ServerActions.BROADCAST,
296
- channelName: 'test',
297
- event: 'test',
298
- payload: {
299
- test: 2,
300
- },
301
- });
302
- expect(onMessage).toHaveBeenCalledTimes(2);
303
- onMessage.mockClear();
304
- channelEngine.sendMessage('test2', 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
305
- expect(onMessage).toHaveBeenCalledTimes(1);
306
- onMessage.mockClear();
307
- // when user is not in channel it throws an error
308
- expect(() => {
309
- channelEngine.sendMessage('test3', 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
310
- }).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
311
- });
312
- it('should broadcast a message to all users except sender', () => {
313
- const onMessage = jest.fn();
314
- const parentEngine = (0, exports.createParentEngine)();
315
- parentEngine.execute = (_, res) => {
316
- res.accept();
317
- };
318
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
319
- channelEngine.addUser('test', { test: 1 }, onMessage);
320
- channelEngine.addUser('test2', { test: 1 }, onMessage);
321
- channelEngine.sendMessage('test2', 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 2 });
322
- expect(onMessage.mock.calls[0][0]).toStrictEqual({
323
- action: channelEngine_1.ServerActions.BROADCAST,
324
- channelName: 'test',
325
- event: 'test',
326
- payload: {
327
- test: 2,
328
- },
329
- });
330
- expect(onMessage).toHaveBeenCalledTimes(1);
331
- // when user is not in channel it throws an error
332
- expect(() => {
333
- channelEngine.sendMessage('test3', 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
334
- }).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
335
- // when sender is channel itself it throws an error
336
- expect(() => {
337
- channelEngine.sendMessage(enums_1.SystemSender.CHANNEL, 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
338
- }).toThrow('ChannelEngine: Cannot send to all users except sender when sender is channel');
339
- });
340
- it('should broadcast a message to specific users', () => {
341
- const onMessage = jest.fn();
342
- const parentEngine = (0, exports.createParentEngine)();
343
- parentEngine.execute = (_, res) => {
344
- res.accept();
345
- };
346
- const channelEngine = new channelEngine_1.ChannelEngine('test', parentEngine);
347
- channelEngine.addUser('test', { test: 1 }, onMessage);
348
- channelEngine.addUser('test2', { test: 1 }, onMessage);
349
- channelEngine.addUser('test3', { test: 1 }, onMessage);
350
- channelEngine.sendMessage('test2', ['test', 'test3'], channelEngine_1.ServerActions.BROADCAST, 'test', { test: 2 });
351
- expect(onMessage.mock.calls[0][0]).toStrictEqual({
352
- action: channelEngine_1.ServerActions.BROADCAST,
353
- channelName: 'test',
354
- event: 'test',
355
- payload: {
356
- test: 2,
357
- },
358
- });
359
- expect(onMessage.mock.calls[1][0]).toStrictEqual({
360
- action: channelEngine_1.ServerActions.BROADCAST,
361
- channelName: 'test',
362
- event: 'test',
363
- payload: {
364
- test: 2,
365
- },
366
- });
367
- expect(onMessage).toHaveBeenCalledTimes(2);
368
- // when recipient is not in channel it throws an error
369
- expect(() => {
370
- channelEngine.sendMessage('test3', ['test', 'test3', 'test4'], channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
371
- }).toThrow('ChannelEngine: Users test4 are not in channel test');
372
- // when sender is not in channel it throws an error
373
- expect(() => {
374
- channelEngine.sendMessage('test4', ['test', 'test3'], channelEngine_1.ServerActions.BROADCAST, 'test', { test: 3 });
375
- }).toThrow('ChannelEngine: User with id test4 does not exist in channel test');
376
- });
377
- });
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const channelResponse_test_1 = require("./channelResponse.test");
4
- const eventRequest_1 = require("./eventRequest");
5
- describe('ChannelRequest', () => {
6
- it('should create a new ChannelRequest', () => {
7
- const channelEngine = (0, channelResponse_test_1.createChannelEngine)();
8
- const event = (0, channelResponse_test_1.createChannelEvent)();
9
- const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
10
- expect(channelRequest).toBeDefined();
11
- });
12
- it('should return the payload', () => {
13
- const channelEngine = (0, channelResponse_test_1.createChannelEngine)();
14
- const event = (0, channelResponse_test_1.createChannelEvent)();
15
- const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
16
- expect(channelRequest.event.payload).toEqual(event.payload);
17
- });
18
- it('should return the user', () => {
19
- const channelEngine = (0, channelResponse_test_1.createChannelEngine)();
20
- const event = (0, channelResponse_test_1.createChannelEvent)();
21
- const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
22
- // because the user in the event does not exist in the channel, this should throw an error
23
- expect(() => channelRequest.user).toThrow();
24
- // add the user to the channel
25
- channelEngine.addUser(event.sender, { assign: 'assign' }, () => { });
26
- // now the user should be returned
27
- expect(channelRequest.user).toEqual(channelEngine.getUserData(event.sender));
28
- });
29
- });
@@ -1,164 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createChannelEvent = exports.createChannelEngine = void 0;
4
- const channelEngine_1 = require("./channelEngine");
5
- const channelEngine_test_1 = require("./channelEngine.test");
6
- const eventResponse_1 = require("./eventResponse");
7
- const enums_1 = require("../../enums");
8
- const createChannelEngine = () => {
9
- const parentEngine = (0, channelEngine_test_1.createParentEngine)();
10
- return new channelEngine_1.ChannelEngine('test', parentEngine);
11
- };
12
- exports.createChannelEngine = createChannelEngine;
13
- const createChannelEvent = () => {
14
- const responseEvent = {
15
- event: 'event',
16
- payload: {
17
- payload: 'payload',
18
- },
19
- sender: 'sender',
20
- action: channelEngine_1.ServerActions.BROADCAST,
21
- recipients: ['recipient'],
22
- };
23
- return responseEvent;
24
- };
25
- exports.createChannelEvent = createChannelEvent;
26
- const createChannelResponse = () => {
27
- const channelEngine = (0, exports.createChannelEngine)();
28
- const event = (0, exports.createChannelEvent)();
29
- channelEngine.addUser(event.sender, { assign: 'assign' }, () => { });
30
- channelEngine.addUser(event.recipients[0], { assign: 'assign' }, () => { });
31
- const response = new eventResponse_1.EventResponse(event, channelEngine);
32
- return {
33
- channelEngine,
34
- event,
35
- response,
36
- };
37
- };
38
- describe('ChannelResponse', () => {
39
- it('should create a new ChannelResponse', () => {
40
- const { response } = createChannelResponse();
41
- expect(response).toBeDefined();
42
- });
43
- it('should return the responseSent', () => {
44
- const { response } = createChannelResponse();
45
- expect(response.responseSent).toEqual(false);
46
- });
47
- it('should accept the request', () => {
48
- const { response } = createChannelResponse();
49
- response.accept();
50
- expect(response.responseSent).toEqual(true);
51
- });
52
- it('should reject the request', () => {
53
- const { response, channelEngine, event } = createChannelResponse();
54
- jest.spyOn(channelEngine, 'sendMessage');
55
- response.reject();
56
- expect(response.responseSent).toEqual(true);
57
- expect(channelEngine.sendMessage).toHaveBeenCalledWith(enums_1.SystemSender.CHANNEL, [event.sender], channelEngine_1.ServerActions.ERROR, enums_1.ErrorTypes.UNAUTHORIZED_BROADCAST, { message: 'Unauthorized request',
58
- code: 403 });
59
- });
60
- it('should send a direct message', () => {
61
- const { response, channelEngine, event } = createChannelResponse();
62
- jest.spyOn(channelEngine, 'sendMessage');
63
- response.send('event', { payload: 'payload' });
64
- expect(response.responseSent).toEqual(true);
65
- expect(channelEngine.sendMessage).toHaveBeenCalledWith(enums_1.SystemSender.CHANNEL, [event.sender], channelEngine_1.ServerActions.SYSTEM, 'event', { payload: 'payload' });
66
- });
67
- it('should broadcast a message', () => {
68
- const { response, channelEngine } = createChannelResponse();
69
- jest.spyOn(channelEngine, 'sendMessage');
70
- response.broadcast('event', { payload: 'payload' });
71
- expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', 'all_users', channelEngine_1.ServerActions.BROADCAST, 'event', { payload: 'payload' });
72
- });
73
- it('should broadcastFromUser a message', () => {
74
- const { response, channelEngine } = createChannelResponse();
75
- jest.spyOn(channelEngine, 'sendMessage');
76
- response.broadcastFromUser('event', { payload: 'payload' });
77
- expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, 'event', { payload: 'payload' });
78
- });
79
- it('should sendToUsers a message', () => {
80
- const { response, channelEngine } = createChannelResponse();
81
- jest.spyOn(channelEngine, 'sendMessage');
82
- response.sendToUsers('event', { payload: 'payload' }, ['recipient']);
83
- expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', ['recipient'], channelEngine_1.ServerActions.BROADCAST, 'event', { payload: 'payload' });
84
- });
85
- it('should fail to send to non existing users', () => {
86
- const { event, response, channelEngine } = createChannelResponse();
87
- jest.spyOn(channelEngine, 'sendMessage');
88
- event.recipients = ['non_existing_user'];
89
- expect(() => response.accept()).toThrowError(new Error('ChannelEngine: Users non_existing_user are not in channel test'));
90
- expect(channelEngine.sendMessage).toHaveBeenCalledWith(event.sender, ['non_existing_user'], channelEngine_1.ServerActions.BROADCAST, event.event, event.payload);
91
- expect(() => response.sendToUsers('event', { payload: 'payload' }, ['non_existing_user']))
92
- .toThrowError(new Error('ChannelEngine: Users non_existing_user are not in channel test'));
93
- });
94
- it('should track a trackPresence', () => {
95
- const { response, channelEngine } = createChannelResponse();
96
- jest.spyOn(channelEngine, 'trackPresence');
97
- response.trackPresence({ status: 'online' });
98
- expect(channelEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
99
- });
100
- it('should untrack a trackPresence', () => {
101
- const { response, channelEngine } = createChannelResponse();
102
- jest.spyOn(channelEngine, 'unTrackPresence');
103
- response.unTrackPresence();
104
- expect(channelEngine.unTrackPresence).toHaveBeenCalledWith('sender');
105
- });
106
- it('should broadcast an error if untrackPresence is called twice', () => {
107
- const { response, channelEngine } = createChannelResponse();
108
- jest.spyOn(channelEngine, 'sendMessage');
109
- // because by default the user is not tracked and the presence engine only exists after a first trackPresence
110
- // we need to call trackPresence first
111
- response.trackPresence({ status: 'online' });
112
- response.unTrackPresence();
113
- response.unTrackPresence();
114
- expect(channelEngine.sendMessage).toHaveBeenCalledWith(enums_1.SystemSender.CHANNEL, ['sender'], channelEngine_1.ServerActions.ERROR, enums_1.ErrorTypes.PRESENCE_LEAVE_FAILED, {
115
- message: 'PresenceEngine: Presence with key sender does not exist',
116
- code: 500,
117
- });
118
- });
119
- it('should updatePresence', () => {
120
- const { response, channelEngine } = createChannelResponse();
121
- jest.spyOn(channelEngine, 'updatePresence');
122
- response.trackPresence({ status: 'online' });
123
- response.updatePresence({ status: 'updated' });
124
- expect(channelEngine.updatePresence).toHaveBeenCalledWith('sender', { status: 'updated' });
125
- });
126
- it('should update a users assign data', () => {
127
- const { response, channelEngine } = createChannelResponse();
128
- jest.spyOn(channelEngine, 'updateAssigns');
129
- response.accept({ assign: 'updated' });
130
- expect(channelEngine.updateAssigns).toHaveBeenCalledWith('sender', { assign: 'updated' });
131
- });
132
- it('should evict a user', () => {
133
- const { response, channelEngine } = createChannelResponse();
134
- jest.spyOn(channelEngine, 'kickUser');
135
- response.evictUser('recipient');
136
- expect(channelEngine.kickUser).toHaveBeenCalledWith('sender', 'recipient');
137
- expect(response.responseSent).toEqual(true);
138
- });
139
- it('should destroy the channel', () => {
140
- const { response, channelEngine } = createChannelResponse();
141
- jest.spyOn(channelEngine, 'destroy');
142
- response.closeChannel('recipient');
143
- expect(channelEngine.destroy).toHaveBeenCalledWith('recipient');
144
- expect(response.responseSent).toEqual(true);
145
- });
146
- it('should set hasResponseSent to true when calling send, accept, reject, end', () => {
147
- const { response } = createChannelResponse();
148
- expect(response.responseSent).toEqual(false);
149
- response.send('event', { payload: 'payload' });
150
- expect(response.responseSent).toEqual(true);
151
- const { response: response2 } = createChannelResponse();
152
- expect(response2.responseSent).toEqual(false);
153
- response2.accept();
154
- expect(response2.responseSent).toEqual(true);
155
- const { response: response3 } = createChannelResponse();
156
- expect(response3.responseSent).toEqual(false);
157
- response3.reject();
158
- expect(response3.responseSent).toEqual(true);
159
- const { response: response4 } = createChannelResponse();
160
- expect(response4.responseSent).toEqual(false);
161
- response4.end();
162
- expect(response4.responseSent).toEqual(true);
163
- });
164
- });