opinionated-machine
Version:
Very opinionated DI framework for fastify, built on top of awilix
56 lines (55 loc) • 2.11 kB
TypeScript
import type { SSESession } from './AbstractSSEController.ts';
export type SSESessionEvent = {
type: 'connect' | 'disconnect';
connectionId: string;
connection?: SSESession;
};
/**
* Connection spy for testing SSE controllers.
* Tracks connection and disconnection events separately.
*/
export declare class SSESessionSpy {
private events;
private activeConnections;
private claimedConnections;
private connectionWaiters;
private disconnectionWaiters;
/** @internal Called when a connection is established */
addConnection(connection: SSESession): void;
/** @internal Called when a connection is closed */
addDisconnection(connectionId: string): void;
/**
* Wait for a connection to be established.
*
* @param options.timeout - Timeout in milliseconds (default: 5000)
* @param options.predicate - Optional predicate to match a specific connection.
* When provided, waits for an unclaimed connection that matches the predicate.
* Connections are "claimed" when returned by waitForConnection, allowing
* multiple sequential waits for the same URL path.
*
* @example
* ```typescript
* // Wait for any connection
* const conn = await spy.waitForConnection()
*
* // Wait for a connection with specific URL
* const conn = await spy.waitForConnection({
* predicate: (c) => c.request.url.includes('/api/notifications'),
* })
* ```
*/
waitForConnection(options?: {
timeout?: number;
predicate?: (connection: SSESession) => boolean;
}): Promise<SSESession>;
/** Wait for a specific connection to disconnect */
waitForDisconnection(connectionId: string, options?: {
timeout?: number;
}): Promise<void>;
/** Check if a connection is currently active */
isConnected(connectionId: string): boolean;
/** Get all connection events in order, optionally filtered by connectionId */
getEvents(connectionId?: string): SSESessionEvent[];
/** Clear all events and cancel pending waiters */
clear(): void;
}