test(core): Move unit tests closer to testable components (no-changelog) (#10287)
This commit is contained in:
44
packages/cli/src/push/__tests__/index.test.ts
Normal file
44
packages/cli/src/push/__tests__/index.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { WebSocket } from 'ws';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
|
||||
import config from '@/config';
|
||||
import type { User } from '@db/entities/User';
|
||||
import { Push } from '@/push';
|
||||
import { SSEPush } from '@/push/sse.push';
|
||||
import { WebSocketPush } from '@/push/websocket.push';
|
||||
import type { WebSocketPushRequest, SSEPushRequest } from '@/push/types';
|
||||
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||
|
||||
import { mockInstance } from '@test/mocking';
|
||||
|
||||
jest.unmock('@/push');
|
||||
|
||||
describe('Push', () => {
|
||||
const user = mock<User>();
|
||||
|
||||
const sseBackend = mockInstance(SSEPush);
|
||||
const wsBackend = mockInstance(WebSocketPush);
|
||||
|
||||
test('should validate pushRef on requests for websocket backend', () => {
|
||||
config.set('push.backend', 'websocket');
|
||||
const push = new Push(mock());
|
||||
const ws = mock<WebSocket>();
|
||||
const request = mock<WebSocketPushRequest>({ user, ws });
|
||||
request.query = { pushRef: '' };
|
||||
push.handleRequest(request, mock());
|
||||
|
||||
expect(ws.send).toHaveBeenCalled();
|
||||
expect(ws.close).toHaveBeenCalledWith(1008);
|
||||
expect(wsBackend.add).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should validate pushRef on requests for SSE backend', () => {
|
||||
config.set('push.backend', 'sse');
|
||||
const push = new Push(mock());
|
||||
const request = mock<SSEPushRequest>({ user, ws: undefined });
|
||||
request.query = { pushRef: '' };
|
||||
expect(() => push.handleRequest(request, mock())).toThrow(BadRequestError);
|
||||
|
||||
expect(sseBackend.add).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
116
packages/cli/src/push/__tests__/websocket.push.test.ts
Normal file
116
packages/cli/src/push/__tests__/websocket.push.test.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { Container } from 'typedi';
|
||||
import { EventEmitter } from 'events';
|
||||
import type WebSocket from 'ws';
|
||||
|
||||
import { WebSocketPush } from '@/push/websocket.push';
|
||||
import { Logger } from '@/Logger';
|
||||
import type { PushDataExecutionRecovered } from '@/Interfaces';
|
||||
|
||||
import { mockInstance } from '@test/mocking';
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
class MockWebSocket extends EventEmitter {
|
||||
public isAlive = true;
|
||||
|
||||
public ping = jest.fn();
|
||||
|
||||
public send = jest.fn();
|
||||
|
||||
public terminate = jest.fn();
|
||||
|
||||
public close = jest.fn();
|
||||
}
|
||||
|
||||
const createMockWebSocket = () => new MockWebSocket() as unknown as jest.Mocked<WebSocket>;
|
||||
|
||||
describe('WebSocketPush', () => {
|
||||
const pushRef1 = 'test-session1';
|
||||
const pushRef2 = 'test-session2';
|
||||
|
||||
mockInstance(Logger);
|
||||
const webSocketPush = Container.get(WebSocketPush);
|
||||
const mockWebSocket1 = createMockWebSocket();
|
||||
const mockWebSocket2 = createMockWebSocket();
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('can add a connection', () => {
|
||||
webSocketPush.add(pushRef1, mockWebSocket1);
|
||||
|
||||
expect(mockWebSocket1.listenerCount('close')).toBe(1);
|
||||
expect(mockWebSocket1.listenerCount('pong')).toBe(1);
|
||||
});
|
||||
|
||||
it('closes a connection', () => {
|
||||
webSocketPush.add(pushRef1, mockWebSocket1);
|
||||
|
||||
mockWebSocket1.emit('close');
|
||||
|
||||
expect(mockWebSocket1.listenerCount('close')).toBe(0);
|
||||
expect(mockWebSocket1.listenerCount('pong')).toBe(0);
|
||||
});
|
||||
|
||||
it('sends data to one connection', () => {
|
||||
webSocketPush.add(pushRef1, mockWebSocket1);
|
||||
webSocketPush.add(pushRef2, mockWebSocket2);
|
||||
const data: PushDataExecutionRecovered = {
|
||||
type: 'executionRecovered',
|
||||
data: {
|
||||
executionId: 'test-execution-id',
|
||||
},
|
||||
};
|
||||
|
||||
webSocketPush.sendToOne('executionRecovered', data, pushRef1);
|
||||
|
||||
expect(mockWebSocket1.send).toHaveBeenCalledWith(
|
||||
JSON.stringify({
|
||||
type: 'executionRecovered',
|
||||
data: {
|
||||
type: 'executionRecovered',
|
||||
data: {
|
||||
executionId: 'test-execution-id',
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(mockWebSocket2.send).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('sends data to all connections', () => {
|
||||
webSocketPush.add(pushRef1, mockWebSocket1);
|
||||
webSocketPush.add(pushRef2, mockWebSocket2);
|
||||
const data: PushDataExecutionRecovered = {
|
||||
type: 'executionRecovered',
|
||||
data: {
|
||||
executionId: 'test-execution-id',
|
||||
},
|
||||
};
|
||||
|
||||
webSocketPush.sendToAll('executionRecovered', data);
|
||||
|
||||
const expectedMsg = JSON.stringify({
|
||||
type: 'executionRecovered',
|
||||
data: {
|
||||
type: 'executionRecovered',
|
||||
data: {
|
||||
executionId: 'test-execution-id',
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(mockWebSocket1.send).toHaveBeenCalledWith(expectedMsg);
|
||||
expect(mockWebSocket2.send).toHaveBeenCalledWith(expectedMsg);
|
||||
});
|
||||
|
||||
it('pings all connections', () => {
|
||||
webSocketPush.add(pushRef1, mockWebSocket1);
|
||||
webSocketPush.add(pushRef2, mockWebSocket2);
|
||||
|
||||
jest.runOnlyPendingTimers();
|
||||
|
||||
expect(mockWebSocket1.ping).toHaveBeenCalled();
|
||||
expect(mockWebSocket2.ping).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user