@eleven-am/pondsocket 0.1.125 → 0.1.127

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.
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
4
+ const presence_1 = require("./presence");
5
+ const eventResponse_test_1 = require("../channel/eventResponse.test");
6
+ describe('PresenceEngine', () => {
7
+ let presenceEngine;
8
+ let presence;
9
+ let presenceKey;
10
+ let channel;
11
+ beforeEach(() => {
12
+ channel = (0, eventResponse_test_1.createChannelEngine)();
13
+ channel.addUser('presenceKey', { assign: 'assign' }, () => {
14
+ // do nothing
15
+ });
16
+ presenceEngine = new presence_1.PresenceEngine(channel);
17
+ presence = {
18
+ id: 'id',
19
+ name: 'name',
20
+ color: 'color',
21
+ type: 'type',
22
+ location: 'location',
23
+ };
24
+ presenceKey = 'presenceKey';
25
+ });
26
+ describe('trackPresence', () => {
27
+ it('should insert a presence into the presence engine', () => {
28
+ // spy on the channel sendMessage method
29
+ const sendMessage = jest.spyOn(channel, 'sendMessage');
30
+ presenceEngine.trackPresence(presenceKey, presence);
31
+ // get all the params
32
+ const params = sendMessage.mock.calls[0];
33
+ // remove the first element as it contains the request id which is random
34
+ params.shift();
35
+ expect(params).toEqual([
36
+ pondsocket_common_1.SystemSender.CHANNEL,
37
+ ['presenceKey'],
38
+ pondsocket_common_1.ServerActions.PRESENCE,
39
+ pondsocket_common_1.PresenceEventTypes.JOIN,
40
+ {
41
+ changed: presence,
42
+ presence: [presence],
43
+ },
44
+ ]);
45
+ });
46
+ it('should throw an error if the presence already exists', () => {
47
+ presenceEngine.trackPresence(presenceKey, presence);
48
+ expect(() => presenceEngine.trackPresence(presenceKey, presence)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} already exists`);
49
+ });
50
+ });
51
+ describe('updatePresence', () => {
52
+ it('should update a presence', () => {
53
+ presenceEngine.trackPresence(presenceKey, presence);
54
+ const newPresence = {
55
+ id: 'id',
56
+ name: 'name',
57
+ color: 'color',
58
+ type: 'type',
59
+ location: 'location',
60
+ };
61
+ const sendMessage = jest.spyOn(channel, 'sendMessage');
62
+ presenceEngine.updatePresence(presenceKey, newPresence);
63
+ // get all the params
64
+ const params = sendMessage.mock.calls[0];
65
+ // remove the first element as it contains the request id which is random
66
+ params.shift();
67
+ expect(params).toEqual([
68
+ pondsocket_common_1.SystemSender.CHANNEL,
69
+ ['presenceKey'],
70
+ pondsocket_common_1.ServerActions.PRESENCE,
71
+ pondsocket_common_1.PresenceEventTypes.UPDATE,
72
+ {
73
+ changed: Object.assign(Object.assign({}, presence), newPresence),
74
+ presence: [newPresence],
75
+ },
76
+ ]);
77
+ });
78
+ it('should throw an error if the presence does not exist', () => {
79
+ expect(() => presenceEngine.updatePresence(presenceKey, presence)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
80
+ });
81
+ });
82
+ describe('removePresence', () => {
83
+ it('should remove a presence from the presence engine', () => {
84
+ const listener = jest.spyOn(channel, 'sendMessage');
85
+ // before we can track a presence, we need make sure the user is in the channel
86
+ channel.addUser('presenceKey2', { assign: 'assign' }, () => {
87
+ // do nothing
88
+ });
89
+ presenceEngine.trackPresence(presenceKey, presence);
90
+ presenceEngine.trackPresence('presenceKey2', Object.assign(Object.assign({}, presence), { key: 'presence2' }));
91
+ expect(listener).toHaveBeenCalledTimes(2);
92
+ // clear the mock
93
+ listener.mockClear();
94
+ presenceEngine.removePresence(presenceKey);
95
+ expect(listener).toHaveBeenCalledTimes(1);
96
+ // get all the params
97
+ const params = listener.mock.calls[0];
98
+ // remove the first element as it contains the request id which is random
99
+ params.shift();
100
+ expect(params).toEqual([
101
+ pondsocket_common_1.SystemSender.CHANNEL,
102
+ ['presenceKey2'],
103
+ pondsocket_common_1.ServerActions.PRESENCE,
104
+ pondsocket_common_1.PresenceEventTypes.LEAVE,
105
+ {
106
+ changed: presence,
107
+ presence: [
108
+ Object.assign(Object.assign({}, presence), { key: 'presence2' }),
109
+ ],
110
+ },
111
+ ]);
112
+ listener.mockClear();
113
+ presenceEngine.removePresence('presenceKey2');
114
+ expect(listener).toHaveBeenCalledTimes(0);
115
+ });
116
+ it('should throw an error if the presence does not exist', () => {
117
+ expect(() => presenceEngine.removePresence(presenceKey)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
118
+ });
119
+ });
120
+ describe('getPresence', () => {
121
+ it('should return the presence', () => {
122
+ presenceEngine.trackPresence(presenceKey, presence);
123
+ const data = {};
124
+ data[presenceKey] = presence;
125
+ expect(presenceEngine.getPresence()).toEqual(data);
126
+ });
127
+ });
128
+ });
package/schema.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clientMessageSchema = void 0;
4
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
5
+ const zod_1 = require("zod");
6
+ exports.clientMessageSchema = zod_1.z.object({
7
+ event: zod_1.z.string(),
8
+ requestId: zod_1.z.string(),
9
+ channelName: zod_1.z.string(),
10
+ payload: zod_1.z.record(zod_1.z.any()),
11
+ action: zod_1.z.nativeEnum(pondsocket_common_1.ClientActions),
12
+ addresses: zod_1.z.union([zod_1.z.nativeEnum(pondsocket_common_1.ChannelReceiver), zod_1.z.array(zod_1.z.string())]).optional(),
13
+ });
@@ -14,11 +14,11 @@ var _PondSocket_instances, _PondSocket_server, _PondSocket_socketServer, _PondSo
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.PondSocket = void 0;
16
16
  const http_1 = require("http");
17
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
18
  const ws_1 = require("ws");
18
19
  const middleware_1 = require("../abstracts/middleware");
19
20
  const endpoint_1 = require("../endpoint/endpoint");
20
21
  const response_1 = require("../endpoint/response");
21
- const enums_1 = require("../enums");
22
22
  const matcher_1 = require("../matcher/matcher");
23
23
  class PondSocket {
24
24
  constructor(server, socketServer) {
@@ -108,9 +108,10 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
108
108
  __classPrivateFieldGet(this, _PondSocket_socketServer, "f").emit('connection', socket);
109
109
  __classPrivateFieldGet(this, _PondSocket_middleware, "f").run(request, socket, () => {
110
110
  const message = {
111
- action: enums_1.ServerActions.ERROR,
112
- event: enums_1.ErrorTypes.HANDLER_NOT_FOUND,
113
- channelName: enums_1.SystemSender.ENDPOINT,
111
+ action: pondsocket_common_1.ServerActions.ERROR,
112
+ event: pondsocket_common_1.ErrorTypes.HANDLER_NOT_FOUND,
113
+ channelName: pondsocket_common_1.SystemSender.ENDPOINT,
114
+ requestId: (0, pondsocket_common_1.uuid)(),
114
115
  payload: {
115
116
  message: 'No endpoint found',
116
117
  code: 404,