import { InMemoryHealthEventPublisher } from './InMemoryHealthEventPublisher'; describe('InMemoryHealthEventPublisher', () => { let publisher: InMemoryHealthEventPublisher; beforeEach(() => { publisher = new InMemoryHealthEventPublisher(); }); describe('Health Check Events', () => { it('should publish and track health check completed events', async () => { // Given const event = { healthy: true, responseTime: 100, timestamp: new Date(), endpoint: 'http://api.test/health', }; // When await publisher.publishHealthCheckCompleted(event); // Then expect(publisher.getEventCount()).toBe(1); expect(publisher.getEventCountByType('HealthCheckCompleted')).toBe(1); const events = publisher.getEventsByType('HealthCheckCompleted'); expect(events[0]).toMatchObject({ type: 'HealthCheckCompleted', ...event, }); }); it('should publish and track health check failed events', async () => { // Given const event = { error: 'Connection refused', timestamp: new Date(), endpoint: 'http://api.test/health', }; // When await publisher.publishHealthCheckFailed(event); // Then expect(publisher.getEventCountByType('HealthCheckFailed')).toBe(1); }); }); describe('Connection Status Events', () => { it('should publish and track connected events', async () => { // Given const event = { timestamp: new Date(), responseTime: 50, }; // When await publisher.publishConnected(event); // Then expect(publisher.getEventCountByType('Connected')).toBe(1); }); it('should publish and track disconnected events', async () => { // Given const event = { timestamp: new Date(), consecutiveFailures: 3, }; // When await publisher.publishDisconnected(event); // Then expect(publisher.getEventCountByType('Disconnected')).toBe(1); }); }); describe('Error Handling', () => { it('should throw error when configured to fail', async () => { // Given publisher.setShouldFail(true); const event = { timestamp: new Date() }; // When & Then await expect(publisher.publishChecking(event)).rejects.toThrow('Event publisher failed'); }); }); describe('Maintenance', () => { it('should clear all events', async () => { // Given await publisher.publishChecking({ timestamp: new Date() }); await publisher.publishConnected({ timestamp: new Date(), responseTime: 10 }); // When publisher.clear(); // Then expect(publisher.getEventCount()).toBe(0); }); }); });