2025-05-14 12:26:43 +00:00
|
|
|
import { expect, tap } from '@push.rocks/tapbundle';
|
2025-05-13 12:48:41 +00:00
|
|
|
import {
|
|
|
|
EventSystem,
|
|
|
|
ProxyEvents,
|
|
|
|
ComponentType
|
|
|
|
} from '../../../ts/core/utils/event-system.js';
|
|
|
|
|
2025-05-14 12:26:43 +00:00
|
|
|
// Setup function for creating a new event system
|
|
|
|
function setupEventSystem(): { eventSystem: EventSystem, receivedEvents: any[] } {
|
|
|
|
const eventSystem = new EventSystem(ComponentType.SMART_PROXY, 'test-id');
|
|
|
|
const receivedEvents: any[] = [];
|
|
|
|
return { eventSystem, receivedEvents };
|
|
|
|
}
|
|
|
|
|
|
|
|
tap.test('Event System - certificate events with correct structure', async () => {
|
|
|
|
const { eventSystem, receivedEvents } = setupEventSystem();
|
2025-05-13 12:48:41 +00:00
|
|
|
|
2025-05-14 12:26:43 +00:00
|
|
|
// Set up listeners
|
|
|
|
eventSystem.on(ProxyEvents.CERTIFICATE_ISSUED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'issued',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2025-05-14 12:26:43 +00:00
|
|
|
eventSystem.on(ProxyEvents.CERTIFICATE_RENEWED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'renewed',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2025-05-14 12:26:43 +00:00
|
|
|
// Emit events
|
|
|
|
eventSystem.emitCertificateIssued({
|
|
|
|
domain: 'example.com',
|
|
|
|
certificate: 'cert-content',
|
|
|
|
privateKey: 'key-content',
|
|
|
|
expiryDate: new Date('2025-01-01')
|
|
|
|
});
|
|
|
|
|
|
|
|
eventSystem.emitCertificateRenewed({
|
|
|
|
domain: 'example.com',
|
|
|
|
certificate: 'new-cert-content',
|
|
|
|
privateKey: 'new-key-content',
|
|
|
|
expiryDate: new Date('2026-01-01'),
|
|
|
|
isRenewal: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify events
|
|
|
|
expect(receivedEvents.length).toEqual(2);
|
|
|
|
|
|
|
|
// Check issuance event
|
|
|
|
expect(receivedEvents[0].type).toEqual('issued');
|
|
|
|
expect(receivedEvents[0].data.domain).toEqual('example.com');
|
|
|
|
expect(receivedEvents[0].data.certificate).toEqual('cert-content');
|
|
|
|
expect(receivedEvents[0].data.componentType).toEqual(ComponentType.SMART_PROXY);
|
|
|
|
expect(receivedEvents[0].data.componentId).toEqual('test-id');
|
|
|
|
expect(typeof receivedEvents[0].data.timestamp).toEqual('number');
|
|
|
|
|
|
|
|
// Check renewal event
|
|
|
|
expect(receivedEvents[1].type).toEqual('renewed');
|
|
|
|
expect(receivedEvents[1].data.domain).toEqual('example.com');
|
|
|
|
expect(receivedEvents[1].data.isRenewal).toEqual(true);
|
|
|
|
expect(receivedEvents[1].data.expiryDate).toEqual(new Date('2026-01-01'));
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('Event System - component lifecycle events', async () => {
|
|
|
|
const { eventSystem, receivedEvents } = setupEventSystem();
|
|
|
|
|
|
|
|
// Set up listeners
|
|
|
|
eventSystem.on(ProxyEvents.COMPONENT_STARTED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'started',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
2025-05-14 12:26:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
eventSystem.on(ProxyEvents.COMPONENT_STOPPED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'stopped',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2025-05-14 12:26:43 +00:00
|
|
|
// Emit events
|
|
|
|
eventSystem.emitComponentStarted('TestComponent', '1.0.0');
|
|
|
|
eventSystem.emitComponentStopped('TestComponent');
|
|
|
|
|
|
|
|
// Verify events
|
|
|
|
expect(receivedEvents.length).toEqual(2);
|
|
|
|
|
|
|
|
// Check started event
|
|
|
|
expect(receivedEvents[0].type).toEqual('started');
|
|
|
|
expect(receivedEvents[0].data.name).toEqual('TestComponent');
|
|
|
|
expect(receivedEvents[0].data.version).toEqual('1.0.0');
|
|
|
|
|
|
|
|
// Check stopped event
|
|
|
|
expect(receivedEvents[1].type).toEqual('stopped');
|
|
|
|
expect(receivedEvents[1].data.name).toEqual('TestComponent');
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('Event System - connection events', async () => {
|
|
|
|
const { eventSystem, receivedEvents } = setupEventSystem();
|
|
|
|
|
|
|
|
// Set up listeners
|
|
|
|
eventSystem.on(ProxyEvents.CONNECTION_ESTABLISHED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'established',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
2025-05-14 12:26:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
eventSystem.on(ProxyEvents.CONNECTION_CLOSED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'closed',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
2025-05-14 12:26:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Emit events
|
|
|
|
eventSystem.emitConnectionEstablished({
|
|
|
|
connectionId: 'conn-123',
|
|
|
|
clientIp: '192.168.1.1',
|
|
|
|
port: 443,
|
|
|
|
isTls: true,
|
|
|
|
domain: 'example.com'
|
|
|
|
});
|
|
|
|
|
|
|
|
eventSystem.emitConnectionClosed({
|
|
|
|
connectionId: 'conn-123',
|
|
|
|
clientIp: '192.168.1.1',
|
|
|
|
port: 443
|
|
|
|
});
|
|
|
|
|
|
|
|
// Verify events
|
|
|
|
expect(receivedEvents.length).toEqual(2);
|
|
|
|
|
|
|
|
// Check established event
|
|
|
|
expect(receivedEvents[0].type).toEqual('established');
|
|
|
|
expect(receivedEvents[0].data.connectionId).toEqual('conn-123');
|
|
|
|
expect(receivedEvents[0].data.clientIp).toEqual('192.168.1.1');
|
|
|
|
expect(receivedEvents[0].data.port).toEqual(443);
|
|
|
|
expect(receivedEvents[0].data.isTls).toEqual(true);
|
|
|
|
|
|
|
|
// Check closed event
|
|
|
|
expect(receivedEvents[1].type).toEqual('closed');
|
|
|
|
expect(receivedEvents[1].data.connectionId).toEqual('conn-123');
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('Event System - once and off subscription methods', async () => {
|
|
|
|
const { eventSystem, receivedEvents } = setupEventSystem();
|
|
|
|
|
|
|
|
// Set up a listener that should fire only once
|
|
|
|
eventSystem.once(ProxyEvents.CONNECTION_ESTABLISHED, (data) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'once',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
2025-05-14 12:26:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Set up a persistent listener
|
|
|
|
const persistentHandler = (data: any) => {
|
|
|
|
receivedEvents.push({
|
|
|
|
type: 'persistent',
|
|
|
|
data
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
2025-05-14 12:26:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
eventSystem.on(ProxyEvents.CONNECTION_ESTABLISHED, persistentHandler);
|
|
|
|
|
|
|
|
// First event should trigger both listeners
|
|
|
|
eventSystem.emitConnectionEstablished({
|
|
|
|
connectionId: 'conn-1',
|
|
|
|
clientIp: '192.168.1.1',
|
|
|
|
port: 443
|
|
|
|
});
|
|
|
|
|
|
|
|
// Second event should only trigger the persistent listener
|
|
|
|
eventSystem.emitConnectionEstablished({
|
|
|
|
connectionId: 'conn-2',
|
|
|
|
clientIp: '192.168.1.1',
|
|
|
|
port: 443
|
|
|
|
});
|
|
|
|
|
|
|
|
// Unsubscribe the persistent listener
|
|
|
|
eventSystem.off(ProxyEvents.CONNECTION_ESTABLISHED, persistentHandler);
|
|
|
|
|
|
|
|
// Third event should not trigger any listeners
|
|
|
|
eventSystem.emitConnectionEstablished({
|
|
|
|
connectionId: 'conn-3',
|
|
|
|
clientIp: '192.168.1.1',
|
|
|
|
port: 443
|
2025-05-13 12:48:41 +00:00
|
|
|
});
|
2025-05-14 12:26:43 +00:00
|
|
|
|
|
|
|
// Verify events
|
|
|
|
expect(receivedEvents.length).toEqual(3);
|
|
|
|
expect(receivedEvents[0].type).toEqual('once');
|
|
|
|
expect(receivedEvents[0].data.connectionId).toEqual('conn-1');
|
|
|
|
|
|
|
|
expect(receivedEvents[1].type).toEqual('persistent');
|
|
|
|
expect(receivedEvents[1].data.connectionId).toEqual('conn-1');
|
|
|
|
|
|
|
|
expect(receivedEvents[2].type).toEqual('persistent');
|
|
|
|
expect(receivedEvents[2].data.connectionId).toEqual('conn-2');
|
|
|
|
});
|
|
|
|
|
|
|
|
export default tap.start();
|