airdcpp-apisocket 3.0.0-beta.1 → 3.0.0-beta.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.
Files changed (47) hide show
  1. package/dist-es/PublicHelpers.d.ts +1 -1
  2. package/dist-es/PublicHelpers.js +24 -7
  3. package/dist-es/PublicHelpers.js.map +1 -1
  4. package/dist-es/SocketBase.js +13 -11
  5. package/dist-es/SocketBase.js.map +1 -1
  6. package/dist-es/SocketRequestHandler.js +3 -5
  7. package/dist-es/SocketRequestHandler.js.map +1 -1
  8. package/dist-es/SocketSubscriptionHandler.js +9 -5
  9. package/dist-es/SocketSubscriptionHandler.js.map +1 -1
  10. package/dist-es/tests/mocks/index.d.ts +3 -0
  11. package/dist-es/tests/mocks/index.js +4 -0
  12. package/dist-es/tests/mocks/index.js.map +1 -0
  13. package/dist-es/tests/mocks/mock-data.d.ts +54 -0
  14. package/dist-es/tests/mocks/mock-data.js +34 -0
  15. package/dist-es/tests/mocks/mock-data.js.map +1 -0
  16. package/dist-es/tests/mocks/mock-server.d.ts +43 -0
  17. package/dist-es/tests/mocks/mock-server.js +163 -0
  18. package/dist-es/tests/mocks/mock-server.js.map +1 -0
  19. package/dist-es/tests/mocks/mock-socket.d.ts +24 -0
  20. package/dist-es/tests/mocks/mock-socket.js +35 -0
  21. package/dist-es/tests/mocks/mock-socket.js.map +1 -0
  22. package/dist-es/tests/test-utils.d.ts +8 -0
  23. package/dist-es/tests/test-utils.js +28 -0
  24. package/dist-es/tests/test-utils.js.map +1 -0
  25. package/dist-es/types/public_helpers.d.ts +12 -3
  26. package/dist-es/types/public_helpers_internal.d.ts +4 -4
  27. package/dist-es/types/socket.d.ts +1 -0
  28. package/dist-es/types/subscriptions.d.ts +1 -1
  29. package/package.json +10 -7
  30. package/src/PublicHelpers.ts +25 -7
  31. package/src/SocketBase.ts +15 -12
  32. package/src/SocketRequestHandler.ts +3 -5
  33. package/src/SocketSubscriptionHandler.ts +7 -6
  34. package/src/tests/Socket.test.ts +129 -111
  35. package/src/tests/mocks/index.ts +3 -0
  36. package/src/tests/mocks/mock-data.ts +36 -0
  37. package/src/tests/mocks/mock-server.ts +276 -0
  38. package/src/tests/mocks/mock-socket.ts +72 -0
  39. package/src/tests/public_helpers.test.ts +64 -63
  40. package/src/tests/test-utils.ts +31 -0
  41. package/src/types/api_internal.ts +0 -1
  42. package/src/types/public_helpers.ts +11 -11
  43. package/src/types/public_helpers_internal.ts +4 -4
  44. package/src/types/socket.ts +1 -0
  45. package/src/types/subscriptions.ts +1 -1
  46. package/tsconfig.json +1 -1
  47. package/src/tests/helpers.ts +0 -195
@@ -2,7 +2,7 @@ import * as API from './api.js';
2
2
 
3
3
 
4
4
  // SUBSCRIPTIONS
5
- export type SubscriptionRemoveHandler = (sendApi?: boolean) => void;
5
+ export type SubscriptionRemoveHandler = (sendApi?: boolean) => Promise<void>;
6
6
 
7
7
  export type SubscriptionCallback<
8
8
  DataT extends object | void = object,
package/tsconfig.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "esModuleInterop": true
20
20
  },
21
21
  "exclude": [
22
- "src/tests",
22
+ "src/tests/*.test.ts",
23
23
  "jest.config.js",
24
24
  ]
25
25
  }
@@ -1,195 +0,0 @@
1
- import { Socket } from '../NodeSocket.js';
2
- import { Client, Server, WebSocket } from 'mock-socket';
3
- import { jest } from '@jest/globals';
4
-
5
- import { OutgoingRequest, RequestSuccessResponse, RequestErrorResponse } from '../types/api_internal.js';
6
- import * as Options from '../types/options.js';
7
- import ApiConstants from '../ApiConstants.js';
8
- import { EventEmitter } from 'events';
9
-
10
- import waitForExpectOriginal from 'wait-for-expect';
11
-
12
- const EXCEPT_TIMEOUT = 1000;
13
- //@ts-ignore
14
- export const waitForExpect = (func: () => void | Promise<void>) => waitForExpectOriginal.default(func, EXCEPT_TIMEOUT);
15
-
16
- const VERBOSE = false;
17
-
18
- const getMockConsole = () => ({
19
- log: jest.fn((a1: any, a2: any, a3: any, a4: any) => {
20
- if (VERBOSE) {
21
- console.log(a1, a2, a3, a4);
22
- }
23
- }),
24
- info: jest.fn((a1: any, a2: any, a3: any, a4: any) => {
25
- if (VERBOSE) {
26
- console.info(a1, a2, a3, a4);
27
- }
28
- }),
29
- warn: jest.fn((a1: any, a2: any, a3: any, a4: any) => {
30
- console.warn(a1, a2, a3, a4);
31
- }),
32
- error: jest.fn((a1: any, a2: any, a3: any, a4: any) => {
33
- console.error(a1, a2, a3, a4);
34
- }),
35
- });
36
-
37
- const CONNECT_PARAMS = {
38
- username: 'test',
39
- password: 'test',
40
- url: 'ws://localhost:7171/api/v1/',
41
- };
42
-
43
- const getDefaultSocketOptions = (mockConsole: Options.LogOutput): Options.APISocketOptions => ({
44
- ...CONNECT_PARAMS,
45
- logOutput: mockConsole,
46
- logLevel: VERBOSE ? 'verbose' : 'warn',
47
- });
48
-
49
- const AUTH_RESPONSE = {
50
- auth_token: 'b823187f-4aab-4b71-9764-e63e88401a26',
51
- refresh_token: '5124faasf-4aab-4b71-9764-e63e88401a26',
52
- user: {
53
- permissions: [ 'admin' ],
54
- username: 'test',
55
- active_sessions: 1,
56
- last_login: 0,
57
- },
58
- system: {
59
- cid: 'AHLUODI2YZ2U7FDWMHFNJU65ERGKUN4MH7GW5LY',
60
- hostname: 'ubuntu-htpc',
61
- network_type: 'private',
62
- path_separator: '/',
63
- platform: 'other',
64
- language: 'fi',
65
- },
66
- wizard_pending: false,
67
- };
68
-
69
- export type MockSocketOptions = Omit<Options.APISocketOptions, 'username' | 'password' | 'url'> & {
70
- username?: string;
71
- password?: string;
72
- url?: string;
73
- };
74
-
75
- const getSocket = (options: MockSocketOptions = {}) => {
76
- const mockConsole = getMockConsole();
77
- const socket = Socket(
78
- {
79
- ...getDefaultSocketOptions(mockConsole),
80
- ...options,
81
- },
82
- WebSocket as any
83
- );
84
-
85
- return { socket, mockConsole };
86
- };
87
-
88
-
89
- type Callback = (requestData: object) => void;
90
-
91
- const getConnectedSocket = async (
92
- server: ReturnType<typeof getMockServer>,
93
- options?: MockSocketOptions,
94
- authCallback?: Callback
95
- ) => {
96
- server.addDataHandler('POST', ApiConstants.LOGIN_URL, AUTH_RESPONSE, authCallback);
97
-
98
- const { socket, mockConsole } = getSocket(options);
99
- await socket.connect();
100
-
101
- return { socket, mockConsole };
102
- };
103
-
104
- const toEmitId = (path: string, method: string) => {
105
- return `${path}_${method}`;
106
- };
107
-
108
- const getMockServer = () => {
109
- const mockServer = new Server(CONNECT_PARAMS.url);
110
- let socket: Client;
111
- const emitter = new EventEmitter();
112
-
113
- const addServerHandler = <DataT extends object | undefined>(
114
- method: string,
115
- path: string,
116
- responseData: Omit<RequestSuccessResponse<DataT>, 'callback_id'> | Omit<RequestErrorResponse, 'callback_id'>,
117
- subscriptionCallback?: Callback
118
- ) => {
119
- emitter.addListener(
120
- toEmitId(path, method),
121
- (request: OutgoingRequest, s: WebSocket) => {
122
- if (subscriptionCallback) {
123
- subscriptionCallback(request);
124
- }
125
-
126
- const response: RequestSuccessResponse | RequestErrorResponse = {
127
- callback_id: request.callback_id,
128
- ...responseData,
129
- };
130
-
131
- s.send(JSON.stringify(response));
132
- }
133
- );
134
- };
135
-
136
- mockServer.on('connection', s => {
137
- socket = s;
138
-
139
- socket.on('message', (messageObj) => {
140
- const request: OutgoingRequest = JSON.parse(messageObj as string);
141
- emitter.emit(toEmitId(request.path, request.method), request, s);
142
- });
143
- });
144
-
145
- mockServer.on('close', () => {
146
- emitter.removeAllListeners();
147
- });
148
-
149
- return {
150
- addDataHandler: <DataT extends object | undefined>(
151
- method: string,
152
- path: string,
153
- data?: DataT,
154
- subscriptionCallback?: Callback
155
- ) => {
156
- addServerHandler<DataT>(
157
- method,
158
- path, {
159
- data,
160
- code: 200,
161
- },
162
- subscriptionCallback
163
- );
164
- },
165
- addErrorHandler: (
166
- method: string,
167
- path: string,
168
- errorStr: string | null,
169
- errorCode: number,
170
- subscriptionCallback?: Callback
171
- ) => {
172
- addServerHandler(
173
- method,
174
- path,
175
- {
176
- error: !errorStr ? null as any : {
177
- message: errorStr,
178
- },
179
- code: errorCode,
180
- },
181
- subscriptionCallback
182
- );
183
- },
184
- stop: () => {
185
- mockServer.stop(() => {
186
- // ...
187
- });
188
- },
189
- send: (data: object) => {
190
- socket.send(JSON.stringify(data));
191
- },
192
- };
193
- };
194
-
195
- export { getMockServer, getSocket, getConnectedSocket, CONNECT_PARAMS, AUTH_RESPONSE };