integration tests
This commit is contained in:
54
core/health/ports/HealthCheckQuery.ts
Normal file
54
core/health/ports/HealthCheckQuery.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Health Check Query Port
|
||||
*
|
||||
* Defines the interface for querying health status.
|
||||
* This port is implemented by adapters that can perform health checks.
|
||||
*/
|
||||
|
||||
export interface HealthCheckQuery {
|
||||
/**
|
||||
* Perform a health check
|
||||
*/
|
||||
performHealthCheck(): Promise<HealthCheckResult>;
|
||||
|
||||
/**
|
||||
* Get current connection status
|
||||
*/
|
||||
getStatus(): ConnectionStatus;
|
||||
|
||||
/**
|
||||
* Get detailed health information
|
||||
*/
|
||||
getHealth(): ConnectionHealth;
|
||||
|
||||
/**
|
||||
* Get reliability percentage
|
||||
*/
|
||||
getReliability(): number;
|
||||
|
||||
/**
|
||||
* Check if API is currently available
|
||||
*/
|
||||
isAvailable(): boolean;
|
||||
}
|
||||
|
||||
export type ConnectionStatus = 'connected' | 'disconnected' | 'degraded' | 'checking';
|
||||
|
||||
export interface ConnectionHealth {
|
||||
status: ConnectionStatus;
|
||||
lastCheck: Date | null;
|
||||
lastSuccess: Date | null;
|
||||
lastFailure: Date | null;
|
||||
consecutiveFailures: number;
|
||||
totalRequests: number;
|
||||
successfulRequests: number;
|
||||
failedRequests: number;
|
||||
averageResponseTime: number;
|
||||
}
|
||||
|
||||
export interface HealthCheckResult {
|
||||
healthy: boolean;
|
||||
responseTime: number;
|
||||
error?: string;
|
||||
timestamp: Date;
|
||||
}
|
||||
80
core/health/ports/HealthEventPublisher.ts
Normal file
80
core/health/ports/HealthEventPublisher.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Health Event Publisher Port
|
||||
*
|
||||
* Defines the interface for publishing health-related events.
|
||||
* This port is implemented by adapters that can publish events.
|
||||
*/
|
||||
|
||||
export interface HealthEventPublisher {
|
||||
/**
|
||||
* Publish a health check completed event
|
||||
*/
|
||||
publishHealthCheckCompleted(event: HealthCheckCompletedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a health check failed event
|
||||
*/
|
||||
publishHealthCheckFailed(event: HealthCheckFailedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a health check timeout event
|
||||
*/
|
||||
publishHealthCheckTimeout(event: HealthCheckTimeoutEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a connected event
|
||||
*/
|
||||
publishConnected(event: ConnectedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a disconnected event
|
||||
*/
|
||||
publishDisconnected(event: DisconnectedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a degraded event
|
||||
*/
|
||||
publishDegraded(event: DegradedEvent): Promise<void>;
|
||||
|
||||
/**
|
||||
* Publish a checking event
|
||||
*/
|
||||
publishChecking(event: CheckingEvent): Promise<void>;
|
||||
}
|
||||
|
||||
export interface HealthCheckCompletedEvent {
|
||||
healthy: boolean;
|
||||
responseTime: number;
|
||||
timestamp: Date;
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
export interface HealthCheckFailedEvent {
|
||||
error: string;
|
||||
timestamp: Date;
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
export interface HealthCheckTimeoutEvent {
|
||||
timestamp: Date;
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
export interface ConnectedEvent {
|
||||
timestamp: Date;
|
||||
responseTime: number;
|
||||
}
|
||||
|
||||
export interface DisconnectedEvent {
|
||||
timestamp: Date;
|
||||
consecutiveFailures: number;
|
||||
}
|
||||
|
||||
export interface DegradedEvent {
|
||||
timestamp: Date;
|
||||
reliability: number;
|
||||
}
|
||||
|
||||
export interface CheckingEvent {
|
||||
timestamp: Date;
|
||||
}
|
||||
62
core/health/use-cases/CheckApiHealthUseCase.ts
Normal file
62
core/health/use-cases/CheckApiHealthUseCase.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* CheckApiHealthUseCase
|
||||
*
|
||||
* Executes health checks and returns status.
|
||||
* This Use Case orchestrates the health check process and emits events.
|
||||
*/
|
||||
|
||||
import { HealthCheckQuery, HealthCheckResult } from '../ports/HealthCheckQuery';
|
||||
import { HealthEventPublisher } from '../ports/HealthEventPublisher';
|
||||
|
||||
export interface CheckApiHealthUseCasePorts {
|
||||
healthCheckAdapter: HealthCheckQuery;
|
||||
eventPublisher: HealthEventPublisher;
|
||||
}
|
||||
|
||||
export class CheckApiHealthUseCase {
|
||||
constructor(private readonly ports: CheckApiHealthUseCasePorts) {}
|
||||
|
||||
/**
|
||||
* Execute a health check
|
||||
*/
|
||||
async execute(): Promise<HealthCheckResult> {
|
||||
const { healthCheckAdapter, eventPublisher } = this.ports;
|
||||
|
||||
try {
|
||||
// Perform the health check
|
||||
const result = await healthCheckAdapter.performHealthCheck();
|
||||
|
||||
// Emit appropriate event based on result
|
||||
if (result.healthy) {
|
||||
await eventPublisher.publishHealthCheckCompleted({
|
||||
healthy: result.healthy,
|
||||
responseTime: result.responseTime,
|
||||
timestamp: result.timestamp,
|
||||
});
|
||||
} else {
|
||||
await eventPublisher.publishHealthCheckFailed({
|
||||
error: result.error || 'Unknown error',
|
||||
timestamp: result.timestamp,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
const timestamp = new Date();
|
||||
|
||||
// Emit failed event
|
||||
await eventPublisher.publishHealthCheckFailed({
|
||||
error: errorMessage,
|
||||
timestamp,
|
||||
});
|
||||
|
||||
return {
|
||||
healthy: false,
|
||||
responseTime: 0,
|
||||
error: errorMessage,
|
||||
timestamp,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
52
core/health/use-cases/GetConnectionStatusUseCase.ts
Normal file
52
core/health/use-cases/GetConnectionStatusUseCase.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* GetConnectionStatusUseCase
|
||||
*
|
||||
* Retrieves current connection status and metrics.
|
||||
* This Use Case orchestrates the retrieval of connection status information.
|
||||
*/
|
||||
|
||||
import { HealthCheckQuery, ConnectionHealth, ConnectionStatus } from '../ports/HealthCheckQuery';
|
||||
|
||||
export interface GetConnectionStatusUseCasePorts {
|
||||
healthCheckAdapter: HealthCheckQuery;
|
||||
}
|
||||
|
||||
export interface ConnectionStatusResult {
|
||||
status: ConnectionStatus;
|
||||
reliability: number;
|
||||
totalRequests: number;
|
||||
successfulRequests: number;
|
||||
failedRequests: number;
|
||||
consecutiveFailures: number;
|
||||
averageResponseTime: number;
|
||||
lastCheck: Date | null;
|
||||
lastSuccess: Date | null;
|
||||
lastFailure: Date | null;
|
||||
}
|
||||
|
||||
export class GetConnectionStatusUseCase {
|
||||
constructor(private readonly ports: GetConnectionStatusUseCasePorts) {}
|
||||
|
||||
/**
|
||||
* Execute to get current connection status
|
||||
*/
|
||||
async execute(): Promise<ConnectionStatusResult> {
|
||||
const { healthCheckAdapter } = this.ports;
|
||||
|
||||
const health = healthCheckAdapter.getHealth();
|
||||
const reliability = healthCheckAdapter.getReliability();
|
||||
|
||||
return {
|
||||
status: health.status,
|
||||
reliability,
|
||||
totalRequests: health.totalRequests,
|
||||
successfulRequests: health.successfulRequests,
|
||||
failedRequests: health.failedRequests,
|
||||
consecutiveFailures: health.consecutiveFailures,
|
||||
averageResponseTime: health.averageResponseTime,
|
||||
lastCheck: health.lastCheck,
|
||||
lastSuccess: health.lastSuccess,
|
||||
lastFailure: health.lastFailure,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user