Skip to content

Commit 8526f29

Browse files
committed
fix: lint fixed and removed unnecessary console logs and try catch blocks
1 parent 4ce1674 commit 8526f29

File tree

2 files changed

+127
-140
lines changed

2 files changed

+127
-140
lines changed

src/nodes/agent/handlers/NodesAuditEventsGet.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import type {
99
} from '../types';
1010
import type Audit from '../../../audit/Audit';
1111
import type { AuditEvent } from '../../../audit/types';
12+
import type { AuditEventId } from '../../../ids';
1213
import { ServerHandler } from '@matrixai/rpc';
1314
import * as auditUtils from '../../../audit/utils';
14-
import { AuditEventId } from '../../../ids';
1515

1616
/**
1717
* Gets audit events from a node
@@ -34,7 +34,7 @@ class NodesAuditEventsGet extends ServerHandler<
3434
let seekEnd_: AuditEventId | number | undefined;
3535

3636
const { seek, seekEnd, limit } = input;
37-
37+
3838
if (typeof seek !== 'number') {
3939
seek_ = auditUtils.decodeAuditEventId(seek);
4040
}
@@ -50,8 +50,8 @@ class NodesAuditEventsGet extends ServerHandler<
5050
for await (const auditEvent of audit.getAuditEvents(
5151
[],
5252
{
53-
seek : seek_,
54-
seekEnd : seekEnd_,
53+
seek: seek_,
54+
seekEnd: seekEnd_,
5555
limit,
5656
},
5757
tran,

tests/nodes/agent/handlers/nodesAuditsGet.test.ts

Lines changed: 123 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -173,41 +173,45 @@ describe('nodesAuditEventsGet', () => {
173173
return audit.generateAuditEventId();
174174
}
175175

176-
/**
177-
* Generates an array of mock audit events of size `numEvents`.
178-
* - Uses `callProtectedGenerateAuditEventId` to get the ID (passed in).
179-
* - Uses fast-check to randomize `remoteNodeId`, `remoteHost`, and `remotePort`.
180-
*/
181-
function generateMockAuditEvents(
176+
/**
177+
* Generates an array of mock audit events of size `numEvents`.
178+
* - Uses `callProtectedGenerateAuditEventId` to get the ID (passed in).
179+
* - Uses fast-check to randomize `remoteNodeId`, `remoteHost`, and `remotePort`.
180+
*/
181+
function generateMockAuditEvents(
182182
numEvents: number,
183-
callProtectedGenerateAuditEventId: () => AuditEventId
183+
callProtectedGenerateAuditEventId: () => AuditEventId,
184184
) {
185185
// Define an arbitrary for the fields we want to randomize:
186186
const randomFieldsArb = fc.record({
187187
remoteHost: fc.ipV4(),
188188
remotePort: fc.nat({ max: 65535 }),
189189
});
190-
190+
191191
// Generate numEvents samples:
192192
const randomValues = fc.sample(randomFieldsArb, numEvents);
193-
193+
194194
// Map each random object to the expected AuditEvent shape:
195-
return randomValues.map(value => ({
195+
return randomValues.map((value) => ({
196196
id: callProtectedGenerateAuditEventId(),
197197
path: ['node', 'connection', 'reverse'],
198198
data: {
199-
remoteNodeId: nodesUtils.encodeNodeId(testNodesUtils.generateRandomNodeId()),
199+
remoteNodeId: nodesUtils.encodeNodeId(
200+
testNodesUtils.generateRandomNodeId(),
201+
),
200202
remoteHost: value.remoteHost,
201203
remotePort: value.remotePort,
202204
type: 'reverse',
203205
},
204206
}));
205207
}
206208

207-
208209
test('should get audit events', async () => {
209210
// Generate valid AuditEventIds
210-
const mockAuditEvents = generateMockAuditEvents(100, callProtectedGenerateAuditEventId);
211+
const mockAuditEvents = generateMockAuditEvents(
212+
100,
213+
callProtectedGenerateAuditEventId,
214+
);
211215

212216
// Add events with correct topicPath and full path in event data
213217
for (const event of mockAuditEvents) {
@@ -225,37 +229,35 @@ function generateMockAuditEvents(
225229
});
226230
}
227231

228-
//Parameters
229-
let seekValue = 0;
230-
let seekEndVal = Date.now();
232+
// Parameters
233+
const seekValue = 0;
234+
const seekEndVal = Date.now();
231235

232-
try {
233-
const response = await rpcClient.methods.nodesAuditEventsGet({
234-
seek: seekValue,
235-
seekEnd: seekEndVal,
236-
});
236+
const response = await rpcClient.methods.nodesAuditEventsGet({
237+
seek: seekValue,
238+
seekEnd: seekEndVal,
239+
});
237240

238-
// Collect results
239-
const auditIds: Array<string> = [];
240-
for await (const result of response) {
241-
auditIds.push(result.id);
241+
// Collect results
242+
const auditIds: Array<string> = [];
243+
for await (const result of response) {
244+
auditIds.push(result.id);
242245
}
243246

244-
const mappedMockedAuditEvents = mockAuditEvents.map((event) => auditUtils.encodeAuditEventId(event.id))
245-
246-
//Check if the audits grabbed from the rpc handler is the same as the generated audits from mockAuditEvents
247-
expect(auditIds).toEqual(
248-
mappedMockedAuditEvents
249-
);
250-
} catch (error) {
247+
const mappedMockedAuditEvents = mockAuditEvents.map((event) =>
248+
auditUtils.encodeAuditEventId(event.id),
249+
);
251250

252-
throw error;
253-
}
251+
// Check if the audits grabbed from the rpc handler is the same as the generated audits from mockAuditEvents
252+
expect(auditIds).toEqual(mappedMockedAuditEvents);
254253
});
255254

256255
test('should get audit events with limit', async () => {
257256
// Generate valid AuditEventIds
258-
const mockAuditEvents = generateMockAuditEvents(100, callProtectedGenerateAuditEventId);
257+
const mockAuditEvents = generateMockAuditEvents(
258+
100,
259+
callProtectedGenerateAuditEventId,
260+
);
259261

260262
// Add events with correct topicPath and full path in event data
261263
for (const event of mockAuditEvents) {
@@ -272,37 +274,33 @@ function generateMockAuditEvents(
272274
path: ['node', 'connection', 'forward'],
273275
});
274276
}
275-
//Parameters
276-
let seekValue = 0;
277-
let seekEndVal = Date.now();
278-
let limitVal = 50;
279-
280-
try {
281-
const response = await rpcClient.methods.nodesAuditEventsGet({
282-
seek: seekValue,
283-
seekEnd: seekEndVal,
284-
limit: limitVal,
285-
});
277+
// Parameters
278+
const seekValue = 0;
279+
const seekEndVal = Date.now();
280+
const limitVal = 50;
281+
282+
const response = await rpcClient.methods.nodesAuditEventsGet({
283+
seek: seekValue,
284+
seekEnd: seekEndVal,
285+
limit: limitVal,
286+
});
286287

287-
// Collect results
288-
const auditIds: Array<string> = [];
289-
for await (const result of response) {
290-
auditIds.push(result.id);
288+
// Collect results
289+
const auditIds: Array<string> = [];
290+
for await (const result of response) {
291+
auditIds.push(result.id);
291292
}
292293

293-
// Verify that the number of events returned is equal to the limit
294-
expect(auditIds).toHaveLength(limitVal);
295-
296-
} catch (error) {
297-
298-
throw error;
299-
}
294+
// Verify that the number of events returned is equal to the limit
295+
expect(auditIds).toHaveLength(limitVal);
300296
});
301297

302-
303298
test('should get audit events with specific seek = 50', async () => {
304299
// Generate valid AuditEventIds
305-
const mockAuditEvents = generateMockAuditEvents(100, callProtectedGenerateAuditEventId);
300+
const mockAuditEvents = generateMockAuditEvents(
301+
100,
302+
callProtectedGenerateAuditEventId,
303+
);
306304

307305
// Add events with correct topicPath and full path in event data
308306
for (const event of mockAuditEvents) {
@@ -320,41 +318,40 @@ function generateMockAuditEvents(
320318
});
321319
}
322320

323-
//Pick some value to seek from the mockAuditEvents selected from the mockAuditEvents
324-
let seekIndex = 50;
325-
let seekValueEncoded = auditUtils.encodeAuditEventId(mockAuditEvents[seekIndex].id);
321+
// Pick some value to seek from the mockAuditEvents selected from the mockAuditEvents
322+
const seekIndex = 50;
323+
const seekValueEncoded = auditUtils.encodeAuditEventId(
324+
mockAuditEvents[seekIndex].id,
325+
);
326326

327-
try {
328-
const response = await rpcClient.methods.nodesAuditEventsGet({
329-
seek: seekValueEncoded,
330-
});
327+
const response = await rpcClient.methods.nodesAuditEventsGet({
328+
seek: seekValueEncoded,
329+
});
331330

332-
// Collect results
333-
const auditIds: Array<string> = [];
334-
for await (const result of response) {
335-
auditIds.push(result.id);
331+
// Collect results
332+
const auditIds: Array<string> = [];
333+
for await (const result of response) {
334+
auditIds.push(result.id);
336335
}
337336

338-
//Verify that the results are the same as the mockAuditEvents from the seek value onwards
337+
// Verify that the results are the same as the mockAuditEvents from the seek value onwards
339338
expect(auditIds).toEqual(
340-
mockAuditEvents.slice(seekIndex + 1).map((event) => auditUtils.encodeAuditEventId(event.id))
339+
mockAuditEvents
340+
.slice(seekIndex + 1)
341+
.map((event) => auditUtils.encodeAuditEventId(event.id)),
341342
);
342343

343-
//Additionally, verify the seek value is exclusive and should be excluded from the results.
344+
// Additionally, verify the seek value is exclusive and should be excluded from the results.
344345
expect(auditIds).not.toContain(seekValueEncoded);
345-
346-
347-
} catch (error) {
348-
console.error(error);
349-
throw error;
350-
}
351-
352346
});
353347

354348
test('should get audit events with specific seek at index 0 (exclude the first event)', async () => {
355349
// Generate valid AuditEventIds
356-
const mockAuditEvents = generateMockAuditEvents(100, callProtectedGenerateAuditEventId);
357-
350+
const mockAuditEvents = generateMockAuditEvents(
351+
100,
352+
callProtectedGenerateAuditEventId,
353+
);
354+
358355
// Insert them all
359356
for (const event of mockAuditEvents) {
360357
// @ts-ignore: protected
@@ -370,43 +367,41 @@ function generateMockAuditEvents(
370367
path: ['node', 'connection', 'forward'],
371368
});
372369
}
373-
370+
374371
// Seek the event at index 0
375372
const seekIndex = 0;
376373
const seekId = mockAuditEvents[seekIndex].id;
377374
const seekIdEncoded = auditUtils.encodeAuditEventId(seekId);
378-
379-
try {
380-
// Make the RPC call
381-
const response = await rpcClient.methods.nodesAuditEventsGet({
382-
seek: seekIdEncoded,
383-
});
384-
385-
// Collect results
386-
const auditIds: Array<string> = [];
387-
for await (const result of response) {
388-
auditIds.push(result.id);
389-
}
390-
391-
// Expect everything from index 1 onward
392-
// (index 0 is excluded because we said exclusive).
393-
const expectedIds = mockAuditEvents
394-
.slice(seekIndex + 1)
395-
.map((event) => auditUtils.encodeAuditEventId(event.id));
396-
397-
expect(auditIds).toEqual(expectedIds);
398-
// And confirm index 0 is NOT in the list
399-
expect(auditIds).not.toContain(seekIdEncoded);
400-
} catch (error) {
401-
console.error(error);
402-
throw error;
375+
376+
// Make the RPC call
377+
const response = await rpcClient.methods.nodesAuditEventsGet({
378+
seek: seekIdEncoded,
379+
});
380+
381+
// Collect results
382+
const auditIds: Array<string> = [];
383+
for await (const result of response) {
384+
auditIds.push(result.id);
403385
}
386+
387+
// Expect everything from index 1 onward
388+
// (index 0 is excluded because we said exclusive).
389+
const expectedIds = mockAuditEvents
390+
.slice(seekIndex + 1)
391+
.map((event) => auditUtils.encodeAuditEventId(event.id));
392+
393+
expect(auditIds).toEqual(expectedIds);
394+
// And confirm index 0 is NOT in the list
395+
expect(auditIds).not.toContain(seekIdEncoded);
404396
});
405-
397+
406398
test('should get audit events with specific seek at index 99 (exclude the last event)', async () => {
407399
// Generate valid AuditEventIds
408-
const mockAuditEvents = generateMockAuditEvents(100, callProtectedGenerateAuditEventId);
409-
400+
const mockAuditEvents = generateMockAuditEvents(
401+
100,
402+
callProtectedGenerateAuditEventId,
403+
);
404+
410405
// Insert them all
411406
for (const event of mockAuditEvents) {
412407
// @ts-ignore: protected
@@ -422,35 +417,27 @@ function generateMockAuditEvents(
422417
path: ['node', 'connection', 'forward'],
423418
});
424419
}
425-
420+
426421
// Seek the event at index 99
427422
const seekIndex = 99;
428423
const seekId = mockAuditEvents[seekIndex].id;
429424
const seekIdEncoded = auditUtils.encodeAuditEventId(seekId);
430-
431-
try {
432-
// Make the RPC call
433-
const response = await rpcClient.methods.nodesAuditEventsGet({
434-
seek: seekIdEncoded,
435-
});
436-
437-
// Collect results
438-
const auditIds: Array<string> = [];
439-
for await (const result of response) {
440-
auditIds.push(result.id);
441-
}
442-
443-
// We expect an EMPTY result, because there's nothing after index 99
444-
// (the last event is excluded).
445-
expect(auditIds).toHaveLength(0);
446-
// Confirm that the last event’s ID is not present
447-
expect(auditIds).not.toContain(seekIdEncoded);
448-
} catch (error) {
449-
console.error(error);
450-
throw error;
451-
}
452-
});
453-
454425

426+
// Make the RPC call
427+
const response = await rpcClient.methods.nodesAuditEventsGet({
428+
seek: seekIdEncoded,
429+
});
455430

431+
// Collect results
432+
const auditIds: Array<string> = [];
433+
for await (const result of response) {
434+
auditIds.push(result.id);
435+
}
436+
437+
// We expect an EMPTY result, because there's nothing after index 99
438+
// (the last event is excluded).
439+
expect(auditIds).toHaveLength(0);
440+
// Confirm that the last event’s ID is not present
441+
expect(auditIds).not.toContain(seekIdEncoded);
442+
});
456443
});

0 commit comments

Comments
 (0)