-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsqs_plugin.test.js
More file actions
77 lines (67 loc) · 2.36 KB
/
sqs_plugin.test.js
File metadata and controls
77 lines (67 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { bigIntUtils } from '@hathor/wallet-lib';
import { eventHandlerFactory, getSettings } from '../../src/plugins/hathor_sqs';
test('settings', () => {
const oldArgs = process.argv;
process.argv = [
'node', // not used but a value is required at this index
'a_script_file.js', // not used but a value is required at this index
'--plugin_sqs_region', 'test-region',
'--plugin_sqs_queue_url', 'test-url',
'--plugin_sqs_endpoint_url', 'test-endpoint',
];
let settings = getSettings();
expect(settings).toMatchObject({
queueUrl: 'test-url',
sqsConfig: {
apiVersion: '2012-11-05',
region: 'test-region',
endpoint: 'test-endpoint',
},
});
process.argv = [
'node', // not used but a value is required at this index
'a_script_file.js', // not used but a value is required at this index
'--plugin_sqs_queue_url', 'test-url',
];
settings = getSettings();
expect(settings).toMatchObject({
queueUrl: 'test-url',
sqsConfig: { apiVersion: '2012-11-05' },
});
// Restore original argv state
process.argv = oldArgs;
});
test('event handler', () => {
const sqsMock = {
sendMessage: jest.fn(),
};
const mockedSettings = { queueUrl: 'test-queue' };
const evHandler = eventHandlerFactory(sqsMock, mockedSettings);
const data = { test: 'event', bigInt: BigInt(Number.MAX_SAFE_INTEGER) + 1n };
evHandler(data);
expect(sqsMock.sendMessage).toHaveBeenCalledWith({
QueueUrl: mockedSettings.queueUrl,
MessageBody: bigIntUtils.JSONBigInt.stringify(data),
}, expect.anything());
});
test('event handler logs error on SQS failure', () => {
const mockError = jest.fn();
// eslint-disable-next-line global-require
const loggerModule = require('../../src/logger');
jest.spyOn(loggerModule, 'buildAppLogger').mockReturnValue({ error: mockError });
const sqsMock = {
sendMessage: jest.fn((params, cb) => cb(new Error('SQS send failed'))),
};
const mockedSettings = { queueUrl: 'test-queue' };
const evHandler = eventHandlerFactory(sqsMock, mockedSettings);
evHandler({ test: 'event' });
expect(mockError).toHaveBeenCalledWith(
expect.stringContaining('plugin[sqs] error sending to sqs:')
);
});