cmd-control-client-lib 3.0.5 → 3.0.11

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.
@@ -335,6 +335,8 @@ export declare class CMDP_CHANNELSSUMMARY_RESPONSE extends CMDP_CHANNELSSUMMARY
335
335
  pinned: string;
336
336
  /** number of muted channels */
337
337
  muted: string;
338
+ /** number of banned channels */
339
+ banned: string;
338
340
  /** number of vip channels */
339
341
  vip: string;
340
342
  };
@@ -66,9 +66,7 @@ export declare class CMDP_TRACERT_REPLY_RESPONSE extends CMDP_TRACERT_REPLY impl
66
66
  }
67
67
  export declare class CMDP_KICK implements ICOMMAND {
68
68
  action: ACTION;
69
- params: baseParamsType & chatIdType & {
70
- time?: string;
71
- };
69
+ params: baseParamsType & chatIdType;
72
70
  }
73
71
  export declare class CMDP_KICK_RESPONSE extends CMDP_KICK implements IRESPONSE {
74
72
  result: RESULT;
@@ -0,0 +1,4 @@
1
+ export declare class VControlErrorEvent extends Event {
2
+ reConnectTimeout?: number;
3
+ isFatal?: boolean;
4
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cmd-control-client-lib",
3
3
  "description": "Cmd-Client-Library",
4
- "version": "3.0.5",
4
+ "version": "3.0.11",
5
5
  "directories": {
6
6
  "lib": "./dist"
7
7
  },
@@ -64,7 +64,7 @@
64
64
  "build": "webpack --config webpack.prod.js",
65
65
  "build:dev": "webpack --config webpack.dev.js",
66
66
  "watch": "webpack --config webpack.dev.js --watch",
67
- "lint": "eslint src test --ext .ts",
67
+ "lint": "eslint src --ext .ts",
68
68
  "lint:teamcity": "yarn lint --format ./node_modules/eslint-teamcity/index.js",
69
69
  "style": "prettier --write \"{src,test}/**/*.ts\"",
70
70
  "test": "jest",
@@ -0,0 +1,163 @@
1
+ import { CmdConnection, ConnectionConfig } from "../src/cmd-connection";
2
+ import { logger } from "../src/logger";
3
+ import { VControlErrorEvent } from "../src/v-control-error-event";
4
+
5
+ describe("WebSocket connection", () => {
6
+ it("Should open webSocket successfully", () => {
7
+ // @ts-ignore
8
+ global.WebSocket = jest.fn().mockImplementation(() => ({ send: jest.fn() }));
9
+ const connectionConfig = new ConnectionConfig();
10
+ connectionConfig.https = true;
11
+ connectionConfig.host = "hostMock";
12
+ connectionConfig.wssport = "wssPortMock";
13
+ connectionConfig.useWS = true;
14
+
15
+ const CMDConnection = new CmdConnection(connectionConfig);
16
+ CMDConnection.connect();
17
+ expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
18
+ //Socket was successfully opened and is able from the service
19
+ expect(CMDConnection["_socket"]).not.toBeUndefined();
20
+ });
21
+
22
+ it("Should make 5 attempts if webSocket open failed", () => {
23
+ logger.warn = jest.fn();
24
+ jest.useFakeTimers();
25
+ const error = "Connection failed Mock";
26
+ // @ts-ignore
27
+ global.WebSocket = jest.fn().mockImplementation(() => {
28
+ throw new Error(error);
29
+
30
+ return {};
31
+ });
32
+
33
+ const connectionConfig = new ConnectionConfig();
34
+ connectionConfig.https = true;
35
+ connectionConfig.host = "hostMock";
36
+ connectionConfig.wssport = "wssPortMock";
37
+ connectionConfig.useWS = true;
38
+ connectionConfig.jsonp = false;
39
+ connectionConfig.connectionRetryInterval = 15000;
40
+ connectionConfig.onError = jest.fn();
41
+
42
+ const CMDConnection = new CmdConnection(connectionConfig);
43
+ CMDConnection.connect();
44
+ expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
45
+ expect(CMDConnection["_socket"]).toBeUndefined();
46
+ //First attempt
47
+ expect(setTimeout).toHaveBeenCalledTimes(1);
48
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 15000);
49
+
50
+ jest.runOnlyPendingTimers();
51
+ //Second attempt
52
+ const secondError = new VControlErrorEvent("error");
53
+ secondError.reConnectTimeout = 15000;
54
+ expect(connectionConfig.onError).toHaveBeenCalledWith(secondError);
55
+ expect(setTimeout).toHaveBeenCalledTimes(2);
56
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 30000);
57
+
58
+ jest.runOnlyPendingTimers();
59
+ //Third attempt
60
+ const thirdError = new VControlErrorEvent("error");
61
+ thirdError.reConnectTimeout = 30000;
62
+ expect(connectionConfig.onError).toHaveBeenCalledWith(thirdError);
63
+ expect(setTimeout).toHaveBeenCalledTimes(3);
64
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 45000);
65
+
66
+ jest.runOnlyPendingTimers();
67
+ //Fourth attempt
68
+ const fourthError = new VControlErrorEvent("error");
69
+ fourthError.reConnectTimeout = 45000;
70
+ expect(connectionConfig.onError).toHaveBeenCalledWith(fourthError);
71
+ expect(setTimeout).toHaveBeenCalledTimes(4);
72
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 60000);
73
+
74
+ jest.runOnlyPendingTimers();
75
+ //Final attempt
76
+ const finalError = new VControlErrorEvent("error");
77
+ finalError.reConnectTimeout = 60000;
78
+ expect(connectionConfig.onError).toHaveBeenCalledWith(finalError);
79
+ expect(setTimeout).toHaveBeenCalledTimes(5);
80
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 75000);
81
+
82
+ jest.runOnlyPendingTimers();
83
+ const lastTimeout = new VControlErrorEvent("error");
84
+ lastTimeout.reConnectTimeout = 75000;
85
+ expect(connectionConfig.onError).toHaveBeenCalledWith(lastTimeout);
86
+ //No more setTimeout was called
87
+ expect(setTimeout).toHaveBeenCalledTimes(5);
88
+ expect(logger.warn).toHaveBeenCalledWith(
89
+ "CMDP._openSocket SECURITY_ERR, maximum reconnect attempts count reached",
90
+ new Error(error),
91
+ );
92
+ //Calling error function that was provided through settings with fatal:true flag
93
+ const fatalError = new VControlErrorEvent("error");
94
+ fatalError.isFatal = true;
95
+ expect(connectionConfig.onError).toHaveBeenCalledWith(fatalError);
96
+ });
97
+
98
+ it("Should make up to 5 attempts if opened websocket failed", () => {
99
+ logger.log = jest.fn();
100
+ logger.warn = jest.fn();
101
+ jest.useFakeTimers();
102
+ // @ts-ignore
103
+ global.WebSocket = jest.fn().mockImplementationOnce(() => ({ send: jest.fn() }));
104
+ const connectionConfig = new ConnectionConfig();
105
+ connectionConfig.https = true;
106
+ connectionConfig.host = "hostMock";
107
+ connectionConfig.wssport = "wssPortMock";
108
+ connectionConfig.useWS = true;
109
+ connectionConfig.connectionRetryInterval = 15000;
110
+ connectionConfig.onError = jest.fn();
111
+
112
+ const CMDConnection = new CmdConnection(connectionConfig);
113
+ CMDConnection.connect();
114
+ expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
115
+ //Socket is opened
116
+ expect(CMDConnection["_socket"]).not.toBeUndefined();
117
+
118
+ const closeEvent = { reason: "Mocked WebSocket close imitation" } as CloseEvent;
119
+ CMDConnection["_closing"] = false;
120
+ // @ts-ignore
121
+ CMDConnection._onclose(closeEvent);
122
+ expect(logger.log).toHaveBeenCalledWith("CMDP.socket.onclose", closeEvent);
123
+ expect(logger.log).toHaveBeenCalledWith("CMDP.retry connect in 15000 ms");
124
+
125
+ const error = "Connection failed Mock";
126
+ // @ts-ignore
127
+ global.WebSocket = jest.fn().mockImplementation(() => {
128
+ throw new Error(error);
129
+
130
+ return {};
131
+ });
132
+ jest.runOnlyPendingTimers();
133
+ //Socket was killed
134
+ expect(CMDConnection["_socket"]).toBeUndefined();
135
+ expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
136
+ expect(setTimeout).toHaveBeenCalledTimes(2);
137
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 30000);
138
+ jest.runOnlyPendingTimers();
139
+
140
+ const firstError = new VControlErrorEvent("error");
141
+ firstError.reConnectTimeout = 30000;
142
+ expect(connectionConfig.onError).toHaveBeenCalledWith(firstError);
143
+ expect(setTimeout).toHaveBeenCalledTimes(3);
144
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 45000);
145
+ jest.runOnlyPendingTimers();
146
+
147
+ const secondError = new VControlErrorEvent("error");
148
+ secondError.reConnectTimeout = 45000;
149
+ expect(connectionConfig.onError).toHaveBeenCalledWith(secondError);
150
+ expect(setTimeout).toHaveBeenCalledTimes(4);
151
+ expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 60000);
152
+
153
+ //Imitating reconnect success on fourth attempt
154
+ //Socket can be opened successfully
155
+ // @ts-ignore
156
+ global.WebSocket = jest.fn().mockImplementationOnce(() => ({ send: jest.fn() }));
157
+ jest.runOnlyPendingTimers();
158
+ expect(setTimeout).toHaveBeenCalledTimes(4);
159
+ //Socket was successfully reconnected
160
+ expect(global.WebSocket).toHaveBeenCalledWith("wss://hostMock:wssPortMock");
161
+ expect(CMDConnection["_socket"]).not.toBeUndefined();
162
+ });
163
+ });
@@ -1,17 +1,17 @@
1
1
  import { RESULT } from "../src/protocol/command/icommand";
2
- import { CODE } from "../src/protocol/command/resultcode";
2
+ import { ResultCode } from "../src/protocol/command/resultcode";
3
3
 
4
4
  describe("RESULT", () => {
5
5
  it("Should accept ENUM OK value as result codes", () => {
6
6
  const result: RESULT = {
7
- code: CODE.OK,
7
+ code: ResultCode.OK,
8
8
  reason: "All good",
9
9
  };
10
10
  });
11
11
 
12
12
  it("Should accept ENUM ERROR value as result codes", () => {
13
13
  const result: RESULT = {
14
- code: CODE.SESSION_ERROR,
14
+ code: ResultCode.SESSION_ERROR,
15
15
  reason: "Unknown session",
16
16
  };
17
17
  });