import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { HealthTestContext } from '../HealthTestContext'; describe('API Connection Monitor - Health Check Execution', () => { let context: HealthTestContext; beforeEach(() => { context = HealthTestContext.create(); context.reset(); }); afterEach(() => { context.teardown(); }); describe('Success Path', () => { it('should perform successful health check and record metrics', async () => { context.healthCheckAdapter.setResponseTime(50); context.mockFetch.mockImplementation(async () => { await new Promise(resolve => setTimeout(resolve, 50)); return { ok: true, status: 200, } as Response; }); const result = await context.apiConnectionMonitor.performHealthCheck(); expect(result.healthy).toBe(true); expect(result.responseTime).toBeGreaterThanOrEqual(50); expect(result.timestamp).toBeInstanceOf(Date); expect(context.apiConnectionMonitor.getStatus()).toBe('connected'); const health = context.apiConnectionMonitor.getHealth(); expect(health.totalRequests).toBe(1); expect(health.successfulRequests).toBe(1); expect(health.failedRequests).toBe(0); expect(health.consecutiveFailures).toBe(0); }); it('should perform health check with slow response time', async () => { context.healthCheckAdapter.setResponseTime(500); context.mockFetch.mockImplementation(async () => { await new Promise(resolve => setTimeout(resolve, 500)); return { ok: true, status: 200, } as Response; }); const result = await context.apiConnectionMonitor.performHealthCheck(); expect(result.healthy).toBe(true); expect(result.responseTime).toBeGreaterThanOrEqual(500); expect(context.apiConnectionMonitor.getStatus()).toBe('connected'); }); it('should handle multiple successful health checks', async () => { context.healthCheckAdapter.setResponseTime(50); context.mockFetch.mockImplementation(async () => { await new Promise(resolve => setTimeout(resolve, 50)); return { ok: true, status: 200, } as Response; }); await context.apiConnectionMonitor.performHealthCheck(); await context.apiConnectionMonitor.performHealthCheck(); await context.apiConnectionMonitor.performHealthCheck(); const health = context.apiConnectionMonitor.getHealth(); expect(health.totalRequests).toBe(3); expect(health.successfulRequests).toBe(3); expect(health.failedRequests).toBe(0); expect(health.consecutiveFailures).toBe(0); expect(health.averageResponseTime).toBeGreaterThanOrEqual(50); }); }); describe('Failure Path', () => { it('should handle failed health check and record failure', async () => { context.mockFetch.mockImplementation(async () => { throw new Error('ECONNREFUSED'); }); // Perform 3 checks to reach disconnected status await context.apiConnectionMonitor.performHealthCheck(); await context.apiConnectionMonitor.performHealthCheck(); const result = await context.apiConnectionMonitor.performHealthCheck(); expect(result.healthy).toBe(false); expect(result.error).toBeDefined(); expect(context.apiConnectionMonitor.getStatus()).toBe('disconnected'); const health = context.apiConnectionMonitor.getHealth(); expect(health.consecutiveFailures).toBe(3); expect(health.totalRequests).toBe(3); expect(health.failedRequests).toBe(3); }); it('should handle timeout during health check', async () => { context.mockFetch.mockImplementation(() => { return new Promise((_, reject) => { setTimeout(() => reject(new Error('Timeout')), 100); }); }); const result = await context.apiConnectionMonitor.performHealthCheck(); expect(result.healthy).toBe(false); expect(result.error).toContain('Timeout'); expect(context.apiConnectionMonitor.getHealth().consecutiveFailures).toBe(1); }); }); });