Files
gridpilot.gg/tests/integration/health/monitor/monitor-health-check.integration.test.ts
Marc Mintel a0f41f242f
Some checks failed
CI / lint-typecheck (pull_request) Failing after 4m51s
CI / tests (pull_request) Has been skipped
CI / contract-tests (pull_request) Has been skipped
CI / e2e-tests (pull_request) Has been skipped
CI / comment-pr (pull_request) Has been skipped
CI / commit-types (pull_request) Has been skipped
integration tests
2026-01-23 00:46:34 +01:00

120 lines
4.1 KiB
TypeScript

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);
});
});
});