|
1 | 1 | import { mockedAddAlert } from '@tests/utils/alerting.utils.mock';
|
2 |
| -import { connectionInfoFromEvent } from '@src/ws/utils'; |
| 2 | +import { connectionInfoFromEvent, sendMessageToClient } from '@src/ws/utils'; |
3 | 3 | import { Severity } from '@src/types';
|
4 | 4 |
|
| 5 | +import { logger } from '@tests/winston.mock'; |
| 6 | +import { RedisClient } from 'redis'; |
| 7 | +import { |
| 8 | + GoneException, |
| 9 | +} from '@aws-sdk/client-apigatewaymanagementapi'; |
| 10 | +import { RedisConfig } from '@src/types'; |
| 11 | + |
| 12 | +const mockedSend = jest.fn(); |
| 13 | + |
| 14 | +jest.mock('@src/redis', () => { |
| 15 | + const originalModule = jest.requireActual('@src/redis'); |
| 16 | + return { |
| 17 | + ...originalModule, |
| 18 | + endWsConnection: jest.fn(), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +jest.mock('@aws-sdk/client-apigatewaymanagementapi', () => { |
| 23 | + const originalModule = jest.requireActual('@aws-sdk/client-apigatewaymanagementapi'); |
| 24 | + return { |
| 25 | + ...originalModule, |
| 26 | + ApiGatewayManagementApiClient: jest.fn().mockImplementation(() => ({ |
| 27 | + send: mockedSend, |
| 28 | + })), |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +jest.mock('redis', () => ({ |
| 33 | + RedisClient: jest.fn().mockImplementation(() => ({ |
| 34 | + endWsConnection: jest.fn(), |
| 35 | + on: jest.fn(), |
| 36 | + set: jest.fn(), |
| 37 | + get: jest.fn(), |
| 38 | + del: jest.fn(), |
| 39 | + quit: jest.fn(), |
| 40 | + })), |
| 41 | +})); |
| 42 | + |
| 43 | +import { endWsConnection } from '@src/redis'; |
| 44 | + |
5 | 45 | test('connectionInfoFromEvent', async () => {
|
6 | 46 | expect.hasAssertions();
|
7 | 47 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
@@ -42,3 +82,66 @@ test('missing WS_DOMAIN should throw', () => {
|
42 | 82 | Severity.MINOR,
|
43 | 83 | );
|
44 | 84 | });
|
| 85 | + |
| 86 | +describe('sendMessageToClient', () => { |
| 87 | + let client: any; |
| 88 | + const redisConfig: RedisConfig = { |
| 89 | + url: 'http://doesntmatter.com', |
| 90 | + password: 'password', |
| 91 | + }; |
| 92 | + const connInfo = { url: 'http://example.com', id: '1234' }; |
| 93 | + const message = 'hello'; |
| 94 | + |
| 95 | + beforeEach(() => { |
| 96 | + jest.clearAllMocks(); |
| 97 | + client = new RedisClient(redisConfig); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should send a message successfully', async () => { |
| 101 | + mockedSend.mockResolvedValue({ |
| 102 | + $metadata: { httpStatusCode: 200 }, |
| 103 | + }); |
| 104 | + |
| 105 | + await sendMessageToClient(client, connInfo, message); |
| 106 | + |
| 107 | + expect(mockedSend).toHaveBeenCalledWith(expect.objectContaining({ |
| 108 | + input: expect.objectContaining({ |
| 109 | + ConnectionId: connInfo.id, |
| 110 | + Data: JSON.stringify(message), |
| 111 | + }) |
| 112 | + })); |
| 113 | + |
| 114 | + expect(logger.error).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should log and throw an error if API Gateway returns non-200 status', async () => { |
| 118 | + mockedSend.mockResolvedValue({ |
| 119 | + $metadata: { httpStatusCode: 400 }, |
| 120 | + }); |
| 121 | + |
| 122 | + await sendMessageToClient(client, connInfo, message); |
| 123 | + |
| 124 | + expect(mockedAddAlert).toHaveBeenCalledWith( |
| 125 | + 'Unhandled error while sending websocket message to client', |
| 126 | + 'The wallet-service was unable to handle an error while attempting to send a message to a websocket client. Please check the logs.', |
| 127 | + Severity.MINOR, |
| 128 | + { |
| 129 | + ConnectionId: connInfo.id, |
| 130 | + Message: JSON.stringify(message), |
| 131 | + }, |
| 132 | + expect.any(Logger), |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should handle GoneException by closing the connection', async () => { |
| 137 | + mockedSend.mockRejectedValue(new GoneException({ |
| 138 | + message: 'Connection is gone.', |
| 139 | + $metadata: { |
| 140 | + httpStatusCode: 410, |
| 141 | + } |
| 142 | + })); |
| 143 | + |
| 144 | + await sendMessageToClient(client, connInfo, message); |
| 145 | + expect(endWsConnection).toHaveBeenCalledWith(client, connInfo.id); |
| 146 | + }); |
| 147 | +}); |
0 commit comments