UNPKG

@sailboat-computer/event-bus

Version:

Standardized event bus for sailboat computer v3 with resilience features and offline capabilities

201 lines (170 loc) 5.63 kB
/** * Event bus tests */ import { createEventBus, EventPriority, EventCategory, EventBusError, EventBusErrorCode } from '../../src'; describe('EventBus', () => { describe('createEventBus', () => { it('should create an event bus with default configuration', () => { const eventBus = createEventBus({ adapter: { type: 'memory', config: { serviceName: 'test-service' } }, offlineBuffer: { maxSize: 100, priorityRetention: true }, metrics: { enabled: true, detailedTimings: false } }); expect(eventBus).toBeDefined(); expect(typeof eventBus.publish).toBe('function'); expect(typeof eventBus.subscribe).toBe('function'); expect(typeof eventBus.close).toBe('function'); expect(typeof eventBus.getMetrics).toBe('function'); }); }); describe('publish and subscribe', () => { it('should publish and receive events', async () => { const eventBus = createEventBus({ adapter: { type: 'memory', config: { serviceName: 'test-service' } }, offlineBuffer: { maxSize: 100, priorityRetention: true }, metrics: { enabled: true, detailedTimings: false } }); const eventType = 'test.event.v1'; const eventData = { message: 'Hello, world!' }; // Create a mock handler const handler = jest.fn(); // Initialize the event bus await eventBus.initialize({ adapter: { type: 'memory', config: { serviceName: 'test-service' } }, offlineBuffer: { maxSize: 100, priorityRetention: true }, metrics: { enabled: true, detailedTimings: false } }); // Subscribe to the event const subscription = await eventBus.subscribe(eventType, handler); expect(subscription).toBeDefined(); expect(subscription.id).toBeDefined(); expect(subscription.eventType).toBe(eventType); expect(typeof subscription.unsubscribe).toBe('function'); // Publish the event const eventId = await eventBus.publish(eventType, eventData, { priority: EventPriority.HIGH, category: EventCategory.SYSTEM }); expect(eventId).toBeDefined(); // Wait for the event to be processed await new Promise(resolve => setTimeout(resolve, 100)); // Check that the handler was called expect(handler).toHaveBeenCalledTimes(1); expect(handler.mock.calls[0][0].type).toBe(eventType); expect(handler.mock.calls[0][0].data).toEqual(eventData); expect(handler.mock.calls[0][0].metadata.priority).toBe(EventPriority.HIGH); expect(handler.mock.calls[0][0].metadata.category).toBe(EventCategory.SYSTEM); // Unsubscribe await subscription.unsubscribe(); // Publish another event await eventBus.publish(eventType, eventData); // Wait for the event to be processed await new Promise(resolve => setTimeout(resolve, 100)); // Check that the handler was not called again expect(handler).toHaveBeenCalledTimes(1); // Close the event bus await eventBus.close(); }); }); describe('metrics', () => { it('should track metrics', async () => { const eventBus = createEventBus({ adapter: { type: 'memory', config: { serviceName: 'test-service' } }, offlineBuffer: { maxSize: 100, priorityRetention: true }, metrics: { enabled: true, detailedTimings: false } }); const eventType = 'test.event.v1'; const eventData = { message: 'Hello, world!' }; // Initialize the event bus await eventBus.initialize({ adapter: { type: 'memory', config: { serviceName: 'test-service' } }, offlineBuffer: { maxSize: 100, priorityRetention: true }, metrics: { enabled: true, detailedTimings: false } }); // Get initial metrics const initialMetrics = eventBus.getMetrics(); expect(initialMetrics.publishedEvents).toBe(0); expect(initialMetrics.activeSubscriptions).toBe(0); // Subscribe to the event const subscription = await eventBus.subscribe(eventType, () => {}); // Check that the subscription count increased const metricsAfterSubscribe = eventBus.getMetrics(); expect(metricsAfterSubscribe.activeSubscriptions).toBe(1); // Publish the event await eventBus.publish(eventType, eventData); // Wait for the event to be processed await new Promise(resolve => setTimeout(resolve, 100)); // Check that the published events count increased const metricsAfterPublish = eventBus.getMetrics(); expect(metricsAfterPublish.publishedEvents).toBe(1); // Unsubscribe await subscription.unsubscribe(); // Check that the subscription count decreased const metricsAfterUnsubscribe = eventBus.getMetrics(); expect(metricsAfterUnsubscribe.activeSubscriptions).toBe(0); // Close the event bus await eventBus.close(); }); }); });