@omnitronix/rng-client-core 1.0.9 → 1.1.1
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/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/interfaces/client-interfaces.d.ts +6 -2
- package/dist/interfaces/client-interfaces.d.ts.map +1 -1
- package/dist/interfaces/metrics.d.ts +31 -0
- package/dist/interfaces/metrics.d.ts.map +1 -0
- package/dist/interfaces/metrics.js +3 -0
- package/dist/interfaces/metrics.js.map +1 -0
- package/dist/metrics/default-metrics-handlers.d.ts +3 -0
- package/dist/metrics/default-metrics-handlers.d.ts.map +1 -0
- package/dist/metrics/default-metrics-handlers.js +23 -0
- package/dist/metrics/default-metrics-handlers.js.map +1 -0
- package/dist/ring-buffer/interfaces.d.ts +0 -7
- package/dist/ring-buffer/interfaces.d.ts.map +1 -1
- package/dist/ring-buffer/ring-buffer.d.ts +10 -6
- package/dist/ring-buffer/ring-buffer.d.ts.map +1 -1
- package/dist/ring-buffer/ring-buffer.js +91 -10
- package/dist/ring-buffer/ring-buffer.js.map +1 -1
- package/dist/ring-buffer/ring-storage-memory.d.ts +1 -2
- package/dist/ring-buffer/ring-storage-memory.d.ts.map +1 -1
- package/dist/ring-buffer/ring-storage-memory.js +0 -11
- package/dist/ring-buffer/ring-storage-memory.js.map +1 -1
- package/dist/ring-buffer/ring-storage.d.ts +1 -2
- package/dist/ring-buffer/ring-storage.d.ts.map +1 -1
- package/dist/ring-buffer/ring-storage.js +0 -6
- package/dist/ring-buffer/ring-storage.js.map +1 -1
- package/dist/rng-client.d.ts +2 -4
- package/dist/rng-client.d.ts.map +1 -1
- package/dist/rng-client.js +5 -3
- package/dist/rng-client.js.map +1 -1
- package/package.json +3 -2
- package/src/__tests__/grpc-rng-client.test.ts +167 -0
- package/src/__tests__/rng-client-core.integration.test.ts +117 -0
- package/src/__tests__/rng-client-core.test.ts +249 -0
- package/src/__tests__/rng-client-grpc.test.ts +324 -0
- package/src/__tests__/rng-client.test.ts +296 -0
- package/src/core/rng-client-core.ts +80 -0
- package/src/errors/rng-errors.ts +96 -0
- package/src/grpc/grpc-rng-client.ts +162 -0
- package/src/grpc/grpc-rng-interface.ts +36 -0
- package/src/http/http-client.types.ts +48 -0
- package/src/http/http-rng-client.ts +140 -0
- package/src/http/rest-client.ts +151 -0
- package/src/http/rng-client.interface.ts +22 -0
- package/src/index.ts +21 -0
- package/src/interfaces/client-interfaces.ts +15 -0
- package/src/interfaces/metrics.ts +38 -0
- package/src/metrics/default-metrics-handlers.ts +29 -0
- package/src/ring-buffer/interfaces.ts +9 -0
- package/src/ring-buffer/ring-buffer.ts +214 -0
- package/src/ring-buffer/ring-storage-memory.ts +48 -0
- package/src/ring-buffer/ring-storage.ts +59 -0
- package/src/rng-client.ts +190 -0
- package/src/rng.proto +102 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { RngClient } from '../rng-client';
|
|
2
|
+
import { RngClientConfig } from '../interfaces/client-interfaces';
|
|
3
|
+
|
|
4
|
+
// Mock the RngClientCore
|
|
5
|
+
jest.mock('../core/rng-client-core', () => ({
|
|
6
|
+
RngClientCore: jest.fn().mockImplementation(() => ({
|
|
7
|
+
getRandomNumber: jest.fn(),
|
|
8
|
+
getRandomFloat: jest.fn(),
|
|
9
|
+
getSingleNumber: jest.fn(),
|
|
10
|
+
getBatchNumbers: jest.fn(),
|
|
11
|
+
getSingleFloat: jest.fn(),
|
|
12
|
+
getBatchFloats: jest.fn(),
|
|
13
|
+
getBatchNumbersWithSeeds: jest.fn(),
|
|
14
|
+
getBatchFloatWithSeeds: jest.fn(),
|
|
15
|
+
})),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
// Mock the RingBuffer
|
|
19
|
+
jest.mock('../ring-buffer/ring-buffer', () => {
|
|
20
|
+
const mockRngClientCore = {
|
|
21
|
+
getBatchNumbersWithSeeds: jest.fn().mockResolvedValue({
|
|
22
|
+
result: [
|
|
23
|
+
{ value: 1, seed: 1 },
|
|
24
|
+
{ value: 2, seed: 2 },
|
|
25
|
+
],
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
RingBuffer: jest.fn().mockImplementation(({ _clientCore }) => ({
|
|
31
|
+
getNumbersByRange: jest.fn().mockResolvedValue(null),
|
|
32
|
+
getStats: jest.fn().mockResolvedValue(undefined),
|
|
33
|
+
fillUpBuffer: jest.fn().mockResolvedValue(undefined),
|
|
34
|
+
pullNumberByBuffer: jest.fn().mockResolvedValue([]),
|
|
35
|
+
clientCore: mockRngClientCore,
|
|
36
|
+
})),
|
|
37
|
+
FILL_UP_BUFFER_DELAY: 5,
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Mock the RingStorage
|
|
42
|
+
jest.mock('../ring-buffer/ring-storage', () => ({
|
|
43
|
+
RingStorage: jest.fn().mockImplementation(() => ({
|
|
44
|
+
addItemsToBuffer: jest.fn().mockResolvedValue(undefined),
|
|
45
|
+
getFromBuffer: jest.fn().mockResolvedValue(null),
|
|
46
|
+
saveBuffer: jest.fn().mockResolvedValue(undefined),
|
|
47
|
+
getStats: jest.fn().mockResolvedValue(undefined),
|
|
48
|
+
})),
|
|
49
|
+
StorageType: {
|
|
50
|
+
IN_MEMORY: 'in_memory',
|
|
51
|
+
REDIS: 'redis',
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
describe('RngClient grpc', () => {
|
|
56
|
+
let rngClient: RngClient;
|
|
57
|
+
let mockRngClientCore: any;
|
|
58
|
+
|
|
59
|
+
const mockConfig: RngClientConfig = {
|
|
60
|
+
serverUrl: 'http://localhost:3001',
|
|
61
|
+
grpcUrl: 'localhost:50051',
|
|
62
|
+
clientMethod: 'grpc',
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
beforeEach(() => {
|
|
66
|
+
// Mock setTimeout to prevent async calls
|
|
67
|
+
jest.spyOn(global, 'setTimeout').mockImplementation((_fn) => {
|
|
68
|
+
// Don't execute the function
|
|
69
|
+
return 0 as any;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const { RngClientCore } = require('../core/rng-client-core');
|
|
73
|
+
mockRngClientCore = {
|
|
74
|
+
getRandomNumber: jest.fn(),
|
|
75
|
+
getRandomFloat: jest.fn(),
|
|
76
|
+
getSingleNumber: jest.fn(),
|
|
77
|
+
getBatchNumbers: jest.fn(),
|
|
78
|
+
getSingleFloat: jest.fn(),
|
|
79
|
+
getBatchFloats: jest.fn(),
|
|
80
|
+
getBatchNumbersWithSeeds: jest.fn(),
|
|
81
|
+
getBatchFloatWithSeeds: jest.fn(),
|
|
82
|
+
};
|
|
83
|
+
RngClientCore.mockImplementation(() => mockRngClientCore);
|
|
84
|
+
|
|
85
|
+
rngClient = new RngClient(mockConfig);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
afterEach(() => {
|
|
89
|
+
jest.clearAllMocks();
|
|
90
|
+
jest.restoreAllMocks();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('getSingleNumber', () => {
|
|
94
|
+
it('should call clientCore.getSingleNumber with correct parameters', async () => {
|
|
95
|
+
const mockResponse = {
|
|
96
|
+
id: 'test-id',
|
|
97
|
+
seed: 12345,
|
|
98
|
+
result: 42,
|
|
99
|
+
min: 1,
|
|
100
|
+
max: 100,
|
|
101
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
mockRngClientCore.getSingleNumber.mockResolvedValue(mockResponse);
|
|
105
|
+
|
|
106
|
+
const result = await rngClient.getSingleNumber(1, 100, 12345);
|
|
107
|
+
|
|
108
|
+
expect(mockRngClientCore.getSingleNumber).toHaveBeenCalledWith({
|
|
109
|
+
min: 1,
|
|
110
|
+
max: 100,
|
|
111
|
+
seed: 12345,
|
|
112
|
+
});
|
|
113
|
+
expect(result).toEqual({
|
|
114
|
+
value: mockResponse.result,
|
|
115
|
+
seed: mockResponse.seed,
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should call clientCore.getSingleNumber without seed', async () => {
|
|
120
|
+
const mockResponse = {
|
|
121
|
+
id: 'test-id',
|
|
122
|
+
seed: 12345,
|
|
123
|
+
result: 42,
|
|
124
|
+
min: 1,
|
|
125
|
+
max: 100,
|
|
126
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
mockRngClientCore.getSingleNumber.mockResolvedValue(mockResponse);
|
|
130
|
+
|
|
131
|
+
const result = await rngClient.getSingleNumber(1, 100);
|
|
132
|
+
|
|
133
|
+
expect(mockRngClientCore.getSingleNumber).toHaveBeenCalledWith({
|
|
134
|
+
min: 1,
|
|
135
|
+
max: 100,
|
|
136
|
+
seed: undefined,
|
|
137
|
+
});
|
|
138
|
+
expect(result).toEqual({
|
|
139
|
+
value: mockResponse.result,
|
|
140
|
+
seed: mockResponse.seed,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('getBatchNumbers', () => {
|
|
146
|
+
it('should call clientCore.getBatchNumbers with correct parameters', async () => {
|
|
147
|
+
const mockResponse = {
|
|
148
|
+
id: 'test-id',
|
|
149
|
+
seed: 12345,
|
|
150
|
+
result: [1, 2, 3, 4, 5],
|
|
151
|
+
min: 1,
|
|
152
|
+
max: 100,
|
|
153
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
mockRngClientCore.getBatchNumbers.mockResolvedValue(mockResponse);
|
|
157
|
+
|
|
158
|
+
const result = await rngClient.getBatchNumbers(1, 100, 5, 12345);
|
|
159
|
+
|
|
160
|
+
expect(mockRngClientCore.getBatchNumbers).toHaveBeenCalledWith({
|
|
161
|
+
min: 1,
|
|
162
|
+
max: 100,
|
|
163
|
+
count: 5,
|
|
164
|
+
seed: 12345,
|
|
165
|
+
});
|
|
166
|
+
expect(result).toEqual(mockResponse.result);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should call clientCore.getBatchNumbers without seed', async () => {
|
|
170
|
+
const mockResponse = {
|
|
171
|
+
id: 'test-id',
|
|
172
|
+
seed: 12345,
|
|
173
|
+
result: [1, 2, 3, 4, 5],
|
|
174
|
+
min: 1,
|
|
175
|
+
max: 100,
|
|
176
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
mockRngClientCore.getBatchNumbers.mockResolvedValue(mockResponse);
|
|
180
|
+
|
|
181
|
+
const result = await rngClient.getBatchNumbers(1, 100, 5);
|
|
182
|
+
|
|
183
|
+
expect(mockRngClientCore.getBatchNumbers).toHaveBeenCalledWith({
|
|
184
|
+
min: 1,
|
|
185
|
+
max: 100,
|
|
186
|
+
count: 5,
|
|
187
|
+
seed: undefined,
|
|
188
|
+
});
|
|
189
|
+
expect(result).toEqual(mockResponse.result);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('getSingleFloat', () => {
|
|
194
|
+
it('should call clientCore.getSingleFloat with correct parameters', async () => {
|
|
195
|
+
const mockResponse = {
|
|
196
|
+
id: 'test-id',
|
|
197
|
+
seed: 12345,
|
|
198
|
+
result: 0.75,
|
|
199
|
+
min: 0,
|
|
200
|
+
max: 1,
|
|
201
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
mockRngClientCore.getSingleFloat.mockResolvedValue(mockResponse);
|
|
205
|
+
|
|
206
|
+
const result = await rngClient.getSingleFloat(12345);
|
|
207
|
+
|
|
208
|
+
expect(mockRngClientCore.getSingleFloat).toHaveBeenCalledWith({
|
|
209
|
+
seed: 12345,
|
|
210
|
+
});
|
|
211
|
+
expect(result).toEqual(mockResponse);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('should call clientCore.getSingleFloat without seed', async () => {
|
|
215
|
+
const mockResponse = {
|
|
216
|
+
id: 'test-id',
|
|
217
|
+
seed: 12345,
|
|
218
|
+
result: 0.75,
|
|
219
|
+
min: 0,
|
|
220
|
+
max: 1,
|
|
221
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
mockRngClientCore.getSingleFloat.mockResolvedValue(mockResponse);
|
|
225
|
+
|
|
226
|
+
const result = await rngClient.getSingleFloat();
|
|
227
|
+
|
|
228
|
+
expect(mockRngClientCore.getSingleFloat).toHaveBeenCalledWith({
|
|
229
|
+
seed: undefined,
|
|
230
|
+
});
|
|
231
|
+
expect(result).toEqual(mockResponse);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe('getBatchFloats', () => {
|
|
236
|
+
it('should call clientCore.getBatchFloats with correct parameters', async () => {
|
|
237
|
+
const mockResponse = {
|
|
238
|
+
id: 'test-id',
|
|
239
|
+
seed: 12345,
|
|
240
|
+
result: [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
241
|
+
min: 0,
|
|
242
|
+
max: 1,
|
|
243
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
mockRngClientCore.getBatchFloats.mockResolvedValue(mockResponse);
|
|
247
|
+
|
|
248
|
+
const result = await rngClient.getBatchFloats(5, 12345);
|
|
249
|
+
|
|
250
|
+
expect(mockRngClientCore.getBatchFloats).toHaveBeenCalledWith({
|
|
251
|
+
count: 5,
|
|
252
|
+
seed: 12345,
|
|
253
|
+
});
|
|
254
|
+
expect(result).toEqual(mockResponse);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should call clientCore.getBatchFloats without seed', async () => {
|
|
258
|
+
const mockResponse = {
|
|
259
|
+
id: 'test-id',
|
|
260
|
+
seed: 12345,
|
|
261
|
+
result: [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
262
|
+
min: 0,
|
|
263
|
+
max: 1,
|
|
264
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
mockRngClientCore.getBatchFloats.mockResolvedValue(mockResponse);
|
|
268
|
+
|
|
269
|
+
const result = await rngClient.getBatchFloats(5);
|
|
270
|
+
|
|
271
|
+
expect(mockRngClientCore.getBatchFloats).toHaveBeenCalledWith({
|
|
272
|
+
count: 5,
|
|
273
|
+
seed: undefined,
|
|
274
|
+
});
|
|
275
|
+
expect(result).toEqual(mockResponse);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe('getRandomNumber', () => {
|
|
280
|
+
it('should call clientCore.getRandomNumber with correct parameters', async () => {
|
|
281
|
+
const mockResponse = {
|
|
282
|
+
id: 'test-id',
|
|
283
|
+
seed: 12345,
|
|
284
|
+
result: 42,
|
|
285
|
+
min: 1,
|
|
286
|
+
max: 100,
|
|
287
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
mockRngClientCore.getRandomNumber.mockResolvedValue(mockResponse);
|
|
291
|
+
|
|
292
|
+
const result = await rngClient.getRandomNumber(1, 100, 12345);
|
|
293
|
+
|
|
294
|
+
expect(mockRngClientCore.getRandomNumber).toHaveBeenCalledWith({
|
|
295
|
+
min: 1,
|
|
296
|
+
max: 100,
|
|
297
|
+
seed: 12345,
|
|
298
|
+
});
|
|
299
|
+
expect(result).toEqual(mockResponse);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
describe('getRandomFloat', () => {
|
|
304
|
+
it('should call clientCore.getRandomFloat with correct parameters', async () => {
|
|
305
|
+
const mockResponse = {
|
|
306
|
+
id: 'test-id',
|
|
307
|
+
seed: 12345,
|
|
308
|
+
result: 0.75,
|
|
309
|
+
min: 0,
|
|
310
|
+
max: 1,
|
|
311
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
mockRngClientCore.getRandomFloat.mockResolvedValue(mockResponse);
|
|
315
|
+
|
|
316
|
+
const result = await rngClient.getRandomFloat(12345);
|
|
317
|
+
|
|
318
|
+
expect(mockRngClientCore.getRandomFloat).toHaveBeenCalledWith({
|
|
319
|
+
seed: 12345,
|
|
320
|
+
});
|
|
321
|
+
expect(result).toEqual(mockResponse);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
});
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { RngClient } from '../rng-client';
|
|
2
|
+
import { RngClientConfig } from '../interfaces/client-interfaces';
|
|
3
|
+
|
|
4
|
+
// Mock the RngClientCore
|
|
5
|
+
jest.mock('../core/rng-client-core', () => ({
|
|
6
|
+
RngClientCore: jest.fn().mockImplementation(() => ({
|
|
7
|
+
getRandomNumber: jest.fn(),
|
|
8
|
+
getRandomFloat: jest.fn(),
|
|
9
|
+
getSingleNumber: jest.fn(),
|
|
10
|
+
getBatchNumbers: jest.fn(),
|
|
11
|
+
getSingleFloat: jest.fn(),
|
|
12
|
+
getBatchFloats: jest.fn(),
|
|
13
|
+
getBatchNumbersWithSeeds: jest.fn(),
|
|
14
|
+
getBatchFloatWithSeeds: jest.fn(),
|
|
15
|
+
})),
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
// Mock the RingBuffer
|
|
19
|
+
jest.mock('../ring-buffer/ring-buffer', () => ({
|
|
20
|
+
RingBuffer: jest.fn().mockImplementation(() => ({
|
|
21
|
+
getNumbersByRange: jest.fn().mockResolvedValue(null),
|
|
22
|
+
getStats: jest.fn().mockResolvedValue(undefined),
|
|
23
|
+
})),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
// Mock the RingStorage
|
|
27
|
+
jest.mock('../ring-buffer/ring-storage', () => ({
|
|
28
|
+
StorageType: {
|
|
29
|
+
IN_MEMORY: 'in_memory',
|
|
30
|
+
REDIS: 'redis',
|
|
31
|
+
},
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
describe('RngClient', () => {
|
|
35
|
+
let rngClient: RngClient;
|
|
36
|
+
let mockRngClientCore: any;
|
|
37
|
+
|
|
38
|
+
const mockConfig: RngClientConfig = {
|
|
39
|
+
serverUrl: 'http://localhost:3001',
|
|
40
|
+
grpcUrl: 'localhost:50051',
|
|
41
|
+
clientMethod: 'rest',
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
const { RngClientCore } = require('../core/rng-client-core');
|
|
46
|
+
mockRngClientCore = {
|
|
47
|
+
getRandomNumber: jest.fn(),
|
|
48
|
+
getRandomFloat: jest.fn(),
|
|
49
|
+
getSingleNumber: jest.fn(),
|
|
50
|
+
getBatchNumbers: jest.fn(),
|
|
51
|
+
getSingleFloat: jest.fn(),
|
|
52
|
+
getBatchFloats: jest.fn(),
|
|
53
|
+
getBatchNumbersWithSeeds: jest.fn(),
|
|
54
|
+
getBatchFloatWithSeeds: jest.fn(),
|
|
55
|
+
};
|
|
56
|
+
RngClientCore.mockImplementation(() => mockRngClientCore);
|
|
57
|
+
|
|
58
|
+
rngClient = new RngClient(mockConfig);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
afterEach(() => {
|
|
62
|
+
jest.clearAllMocks();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('getSingleNumber', () => {
|
|
66
|
+
it('should call clientCore.getSingleNumber with correct parameters', async () => {
|
|
67
|
+
const mockResponse = {
|
|
68
|
+
id: 'test-id',
|
|
69
|
+
seed: 12345,
|
|
70
|
+
result: 42,
|
|
71
|
+
min: 1,
|
|
72
|
+
max: 100,
|
|
73
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
mockRngClientCore.getSingleNumber.mockResolvedValue(mockResponse);
|
|
77
|
+
|
|
78
|
+
const result = await rngClient.getSingleNumber(1, 100, 12345);
|
|
79
|
+
|
|
80
|
+
expect(mockRngClientCore.getSingleNumber).toHaveBeenCalledWith({
|
|
81
|
+
min: 1,
|
|
82
|
+
max: 100,
|
|
83
|
+
seed: 12345,
|
|
84
|
+
});
|
|
85
|
+
expect(result).toEqual({
|
|
86
|
+
value: mockResponse.result,
|
|
87
|
+
seed: mockResponse.seed,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should call clientCore.getSingleNumber without seed', async () => {
|
|
92
|
+
const mockResponse = {
|
|
93
|
+
id: 'test-id',
|
|
94
|
+
seed: 12345,
|
|
95
|
+
result: 42,
|
|
96
|
+
min: 1,
|
|
97
|
+
max: 100,
|
|
98
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
mockRngClientCore.getSingleNumber.mockResolvedValue(mockResponse);
|
|
102
|
+
|
|
103
|
+
const result = await rngClient.getSingleNumber(1, 100);
|
|
104
|
+
|
|
105
|
+
expect(mockRngClientCore.getSingleNumber).toHaveBeenCalledWith({
|
|
106
|
+
min: 1,
|
|
107
|
+
max: 100,
|
|
108
|
+
seed: undefined,
|
|
109
|
+
});
|
|
110
|
+
expect(result).toEqual({
|
|
111
|
+
value: mockResponse.result,
|
|
112
|
+
seed: mockResponse.seed,
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('getBatchNumbers', () => {
|
|
118
|
+
it('should call clientCore.getBatchNumbers with correct parameters', async () => {
|
|
119
|
+
const mockResponse = {
|
|
120
|
+
id: 'test-id',
|
|
121
|
+
seed: 12345,
|
|
122
|
+
result: [1, 2, 3, 4, 5],
|
|
123
|
+
min: 1,
|
|
124
|
+
max: 100,
|
|
125
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
mockRngClientCore.getBatchNumbers.mockResolvedValue(mockResponse);
|
|
129
|
+
|
|
130
|
+
const result = await rngClient.getBatchNumbers(1, 100, 5, 12345);
|
|
131
|
+
|
|
132
|
+
expect(mockRngClientCore.getBatchNumbers).toHaveBeenCalledWith({
|
|
133
|
+
min: 1,
|
|
134
|
+
max: 100,
|
|
135
|
+
count: 5,
|
|
136
|
+
seed: 12345,
|
|
137
|
+
});
|
|
138
|
+
expect(result).toEqual(mockResponse.result);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should call clientCore.getBatchNumbers without seed', async () => {
|
|
142
|
+
const mockResponse = {
|
|
143
|
+
id: 'test-id',
|
|
144
|
+
seed: 12345,
|
|
145
|
+
result: [1, 2, 3, 4, 5],
|
|
146
|
+
min: 1,
|
|
147
|
+
max: 100,
|
|
148
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
mockRngClientCore.getBatchNumbers.mockResolvedValue(mockResponse);
|
|
152
|
+
|
|
153
|
+
const result = await rngClient.getBatchNumbers(1, 100, 5);
|
|
154
|
+
|
|
155
|
+
expect(mockRngClientCore.getBatchNumbers).toHaveBeenCalledWith({
|
|
156
|
+
min: 1,
|
|
157
|
+
max: 100,
|
|
158
|
+
count: 5,
|
|
159
|
+
seed: undefined,
|
|
160
|
+
});
|
|
161
|
+
expect(result).toEqual(mockResponse.result);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe('getSingleFloat', () => {
|
|
166
|
+
it('should call clientCore.getSingleFloat with correct parameters', async () => {
|
|
167
|
+
const mockResponse = {
|
|
168
|
+
id: 'test-id',
|
|
169
|
+
seed: 12345,
|
|
170
|
+
result: 0.75,
|
|
171
|
+
min: 0,
|
|
172
|
+
max: 1,
|
|
173
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
mockRngClientCore.getSingleFloat.mockResolvedValue(mockResponse);
|
|
177
|
+
|
|
178
|
+
const result = await rngClient.getSingleFloat(12345);
|
|
179
|
+
|
|
180
|
+
expect(mockRngClientCore.getSingleFloat).toHaveBeenCalledWith({
|
|
181
|
+
seed: 12345,
|
|
182
|
+
});
|
|
183
|
+
expect(result).toEqual(mockResponse);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('should call clientCore.getSingleFloat without seed', async () => {
|
|
187
|
+
const mockResponse = {
|
|
188
|
+
id: 'test-id',
|
|
189
|
+
seed: 12345,
|
|
190
|
+
result: 0.75,
|
|
191
|
+
min: 0,
|
|
192
|
+
max: 1,
|
|
193
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
mockRngClientCore.getSingleFloat.mockResolvedValue(mockResponse);
|
|
197
|
+
|
|
198
|
+
const result = await rngClient.getSingleFloat();
|
|
199
|
+
|
|
200
|
+
expect(mockRngClientCore.getSingleFloat).toHaveBeenCalledWith({
|
|
201
|
+
seed: undefined,
|
|
202
|
+
});
|
|
203
|
+
expect(result).toEqual(mockResponse);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
describe('getBatchFloats', () => {
|
|
208
|
+
it('should call clientCore.getBatchFloats with correct parameters', async () => {
|
|
209
|
+
const mockResponse = {
|
|
210
|
+
id: 'test-id',
|
|
211
|
+
seed: 12345,
|
|
212
|
+
result: [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
213
|
+
min: 0,
|
|
214
|
+
max: 1,
|
|
215
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
mockRngClientCore.getBatchFloats.mockResolvedValue(mockResponse);
|
|
219
|
+
|
|
220
|
+
const result = await rngClient.getBatchFloats(5, 12345);
|
|
221
|
+
|
|
222
|
+
expect(mockRngClientCore.getBatchFloats).toHaveBeenCalledWith({
|
|
223
|
+
count: 5,
|
|
224
|
+
seed: 12345,
|
|
225
|
+
});
|
|
226
|
+
expect(result).toEqual(mockResponse);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should call clientCore.getBatchFloats without seed', async () => {
|
|
230
|
+
const mockResponse = {
|
|
231
|
+
id: 'test-id',
|
|
232
|
+
seed: 12345,
|
|
233
|
+
result: [0.1, 0.2, 0.3, 0.4, 0.5],
|
|
234
|
+
min: 0,
|
|
235
|
+
max: 1,
|
|
236
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
mockRngClientCore.getBatchFloats.mockResolvedValue(mockResponse);
|
|
240
|
+
|
|
241
|
+
const result = await rngClient.getBatchFloats(5);
|
|
242
|
+
|
|
243
|
+
expect(mockRngClientCore.getBatchFloats).toHaveBeenCalledWith({
|
|
244
|
+
count: 5,
|
|
245
|
+
seed: undefined,
|
|
246
|
+
});
|
|
247
|
+
expect(result).toEqual(mockResponse);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe('getRandomNumber', () => {
|
|
252
|
+
it('should call clientCore.getRandomNumber with correct parameters', async () => {
|
|
253
|
+
const mockResponse = {
|
|
254
|
+
id: 'test-id',
|
|
255
|
+
seed: 12345,
|
|
256
|
+
result: 42,
|
|
257
|
+
min: 1,
|
|
258
|
+
max: 100,
|
|
259
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
mockRngClientCore.getRandomNumber.mockResolvedValue(mockResponse);
|
|
263
|
+
|
|
264
|
+
const result = await rngClient.getRandomNumber(1, 100, 12345);
|
|
265
|
+
|
|
266
|
+
expect(mockRngClientCore.getRandomNumber).toHaveBeenCalledWith({
|
|
267
|
+
min: 1,
|
|
268
|
+
max: 100,
|
|
269
|
+
seed: 12345,
|
|
270
|
+
});
|
|
271
|
+
expect(result).toEqual(mockResponse);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
describe('getRandomFloat', () => {
|
|
276
|
+
it('should call clientCore.getRandomFloat with correct parameters', async () => {
|
|
277
|
+
const mockResponse = {
|
|
278
|
+
id: 'test-id',
|
|
279
|
+
seed: 12345,
|
|
280
|
+
result: 0.75,
|
|
281
|
+
min: 0,
|
|
282
|
+
max: 1,
|
|
283
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
mockRngClientCore.getRandomFloat.mockResolvedValue(mockResponse);
|
|
287
|
+
|
|
288
|
+
const result = await rngClient.getRandomFloat(12345);
|
|
289
|
+
|
|
290
|
+
expect(mockRngClientCore.getRandomFloat).toHaveBeenCalledWith({
|
|
291
|
+
seed: 12345,
|
|
292
|
+
});
|
|
293
|
+
expect(result).toEqual(mockResponse);
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { RngClientConfig } from "../interfaces/client-interfaces";
|
|
2
|
+
import { RngClientInterface } from "../http/rng-client.interface";
|
|
3
|
+
import { HttpRngClient } from "../http/http-rng-client";
|
|
4
|
+
import { GrpcRngClient } from "../grpc/grpc-rng-client";
|
|
5
|
+
import {
|
|
6
|
+
RngBatchFloatRequest,
|
|
7
|
+
RngBatchRequest,
|
|
8
|
+
RngBatchResponse,
|
|
9
|
+
RngBatchResponseWithSeeds,
|
|
10
|
+
RngSingleFloatRequest,
|
|
11
|
+
RngSingleRequest,
|
|
12
|
+
RngSingleResponse,
|
|
13
|
+
} from "../http/http-client.types";
|
|
14
|
+
|
|
15
|
+
export class RngClientCore {
|
|
16
|
+
private readonly config: RngClientConfig;
|
|
17
|
+
private readonly rngClient: RngClientInterface;
|
|
18
|
+
|
|
19
|
+
constructor(config: RngClientConfig) {
|
|
20
|
+
this.config = config;
|
|
21
|
+
|
|
22
|
+
if (config.clientMethod === "grpc") {
|
|
23
|
+
this.rngClient = new GrpcRngClient(config.grpcUrl);
|
|
24
|
+
} else {
|
|
25
|
+
this.rngClient = new HttpRngClient(config.serverUrl);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public async getRandomNumber(
|
|
30
|
+
request: RngSingleRequest
|
|
31
|
+
): Promise<RngSingleResponse> {
|
|
32
|
+
return this.rngClient.getSingleNumber(request);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public async getSingleNumber(
|
|
36
|
+
request: RngSingleRequest
|
|
37
|
+
): Promise<RngSingleResponse> {
|
|
38
|
+
return this.rngClient.getSingleNumber(request);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async getRandomFloat(
|
|
42
|
+
request: RngSingleFloatRequest
|
|
43
|
+
): Promise<RngSingleResponse> {
|
|
44
|
+
const response = await this.rngClient.getSingleFloat(request);
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async getSingleFloat(
|
|
50
|
+
request: RngSingleFloatRequest
|
|
51
|
+
): Promise<RngSingleResponse> {
|
|
52
|
+
const response = await this.rngClient.getSingleFloat(request);
|
|
53
|
+
|
|
54
|
+
return response;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public async getBatchFloats(
|
|
58
|
+
request: RngBatchFloatRequest
|
|
59
|
+
): Promise<RngBatchResponse> {
|
|
60
|
+
return this.rngClient.getBatchFloats(request);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async getBatchNumbers(
|
|
64
|
+
data: RngBatchRequest
|
|
65
|
+
): Promise<RngBatchResponse> {
|
|
66
|
+
return this.rngClient.getBatchNumbers(data);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async getBatchNumbersWithSeeds(
|
|
70
|
+
request: RngBatchRequest
|
|
71
|
+
): Promise<RngBatchResponseWithSeeds> {
|
|
72
|
+
return this.rngClient.getBatchNumbersWithSeeds(request);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public async getBatchFloatWithSeeds(
|
|
76
|
+
request: RngSingleFloatRequest
|
|
77
|
+
): Promise<RngBatchResponseWithSeeds> {
|
|
78
|
+
return this.rngClient.getBatchFloatWithSeeds(request);
|
|
79
|
+
}
|
|
80
|
+
}
|