Skip to content

Commit 5c52778

Browse files
committed
tests: added tests for sendMessageToClient util
1 parent d2fe54f commit 5c52778

File tree

1 file changed

+103
-1
lines changed

1 file changed

+103
-1
lines changed

packages/wallet-service/tests/ws.utils.test.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
11
import { mockedAddAlert } from '@tests/utils/alerting.utils.mock';
2-
import { connectionInfoFromEvent } from '@src/ws/utils';
2+
import { connectionInfoFromEvent, sendMessageToClient } from '@src/ws/utils';
33
import { Severity } from '@src/types';
44

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+
545
test('connectionInfoFromEvent', async () => {
646
expect.hasAssertions();
747
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -42,3 +82,65 @@ test('missing WS_DOMAIN should throw', () => {
4282
Severity.MINOR,
4383
);
4484
});
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+
);
133+
});
134+
135+
it('should handle GoneException by closing the connection', async () => {
136+
mockedSend.mockRejectedValue(new GoneException({
137+
message: 'Connection is gone.',
138+
$metadata: {
139+
httpStatusCode: 410,
140+
}
141+
}));
142+
143+
await sendMessageToClient(client, connInfo, message);
144+
expect(endWsConnection).toHaveBeenCalledWith(client, connInfo.id);
145+
});
146+
});

0 commit comments

Comments
 (0)