airdcpp-apisocket 3.0.0-beta.1 → 3.0.0-beta.10
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-es/PublicHelpers.js +21 -4
- package/dist-es/PublicHelpers.js.map +1 -1
- package/dist-es/SocketBase.js +13 -11
- package/dist-es/SocketBase.js.map +1 -1
- package/dist-es/SocketRequestHandler.js +3 -5
- package/dist-es/SocketRequestHandler.js.map +1 -1
- package/dist-es/tests/mocks/index.d.ts +3 -0
- package/dist-es/tests/mocks/index.js +4 -0
- package/dist-es/tests/mocks/index.js.map +1 -0
- package/dist-es/tests/mocks/mock-data.d.ts +54 -0
- package/dist-es/tests/mocks/mock-data.js +34 -0
- package/dist-es/tests/mocks/mock-data.js.map +1 -0
- package/dist-es/tests/mocks/mock-server.d.ts +43 -0
- package/dist-es/tests/mocks/mock-server.js +154 -0
- package/dist-es/tests/mocks/mock-server.js.map +1 -0
- package/dist-es/tests/mocks/mock-socket.d.ts +23 -0
- package/dist-es/tests/mocks/mock-socket.js +31 -0
- package/dist-es/tests/mocks/mock-socket.js.map +1 -0
- package/dist-es/tests/test-utils.d.ts +8 -0
- package/dist-es/tests/test-utils.js +28 -0
- package/dist-es/tests/test-utils.js.map +1 -0
- package/dist-es/types/public_helpers.d.ts +12 -3
- package/dist-es/types/public_helpers_internal.d.ts +4 -4
- package/dist-es/types/socket.d.ts +1 -0
- package/package.json +10 -7
- package/src/PublicHelpers.ts +22 -4
- package/src/SocketBase.ts +15 -12
- package/src/SocketRequestHandler.ts +3 -5
- package/src/tests/Socket.test.ts +107 -109
- package/src/tests/mocks/index.ts +3 -0
- package/src/tests/mocks/mock-data.ts +36 -0
- package/src/tests/mocks/mock-server.ts +269 -0
- package/src/tests/mocks/mock-socket.ts +68 -0
- package/src/tests/public_helpers.test.ts +63 -62
- package/src/tests/test-utils.ts +31 -0
- package/src/types/api_internal.ts +0 -1
- package/src/types/public_helpers.ts +11 -11
- package/src/types/public_helpers_internal.ts +4 -4
- package/src/types/socket.ts +1 -0
- package/tsconfig.json +1 -1
- package/src/tests/helpers.ts +0 -195
package/src/tests/helpers.ts
DELETED
@@ -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 };
|