import CallableInstance from "callable-instance"; /** * Mock implementation of a message queue for testing purposes. * Extends CallableInstance to allow the queue instance to be called as a function * to set up message consumption handlers. * * @extends CallableInstance * * @example * ```typescript * const queue = new Queue({ name: 'test-queue' }); * * // Set up message handler * queue((message, ack) => { * console.log('Received message:', message); * ack(); // Acknowledge the message * }); * * // Send a message to the queue * await queue.incomingMessage('exchange', 'routing.key', { data: 'test' }); * ``` */ declare class Queue extends CallableInstance<[Function], Promise> { /** @private Queue name */ private _name; /** @private Array of exchange bindings */ private _bindings; /** @private Queue arguments/options */ private _arguments; /** @private Maximum number of unacknowledged messages */ private _prefetchCount; /** @private Whether queue is exclusive to one connection */ private _exclusive; /** @private Whether queue auto-deletes when unused */ private _autoDelete; /** @private Whether queue survives broker restarts */ private _durable; /** @private Whether messages require acknowledgment */ private _ack; /** @private Whether to use error queue for failed messages */ private _useErrorQueue; /** Messages waiting to be consumed */ readyMessages: object[]; /** Messages currently being processed */ unackedMessages: object[]; /** Successfully processed messages */ ackedMessages: object[]; /** @private Count of acknowledged messages */ private acked; /** @private Count of negatively acknowledged messages */ private nacked; /** @private Function to handle message consumption */ private consumeFn; /** @private Optional error queue for failed messages */ private errorQueue?; /** * Creates a new Queue instance with the specified configuration * * @param {QueueParams} params - Queue configuration parameters * @param {string} params.name - The name of the queue * @param {Binding[]} [params.bindings] - Array of exchange bindings * @param {object} [params.arguments] - Additional queue arguments * @param {number} [params.prefetchCount=1] - Maximum unacknowledged messages * @param {boolean} [params.exclusive=false] - Whether queue is exclusive * @param {boolean} [params.autoDelete=false] - Whether queue auto-deletes * @param {boolean} [params.durable=true] - Whether queue is durable * @param {boolean} [params.ack=true] - Whether messages require acknowledgment * @param {boolean} [params.useErrorQueue=false] - Whether to use error queue */ constructor(params: QueueParams); /** * Sets the message consumption handler function. * This method is called when the queue instance is invoked as a function. * * @param {Function} fn - The message handler function * @param {object} fn.message - The message to be processed * @param {Function} fn.ack - Callback to acknowledge message processing * @param {Error} [fn.ack.err] - Optional error to indicate processing failure * @returns {Promise} */ instanceMethod(fn: (message: object, ack: (err: any) => void) => Promise): void; /** @private Processes messages from the ready queue according to prefetch settings */ private consume; /** * Simulates an incoming message from an exchange to this queue. * The message will be queued for processing by the registered consumer function. * * @param {string} fromExchange - The name of the exchange sending the message * @param {string} routingKey - The routing key used for the message * @param {object} message - The message payload * @returns {Promise} Promise that resolves when message is acknowledged * * @example * ```typescript * await queue.incomingMessage('user-exchange', 'user.created', { * userId: 123, * email: 'user@example.com' * }); * ``` */ incomingMessage(fromExchange: string, routingKey: string, message: object): Promise; /** * Waits for all messages in the queue to be processed and acknowledged. * Useful for testing to ensure all messages have been handled before assertions. * * @param {number} [timeout=30000] - Maximum time to wait in milliseconds * @returns {Promise} Promise that resolves when queue is empty or rejects on timeout * * @example * ```typescript * // Wait for all messages to be processed * await queue.end(5000); // Wait up to 5 seconds * * // Now safe to make assertions about processed messages * expect(queue.check.acked).toBe(3); * ``` */ end(timeout?: number): Promise; /** * Gets statistics about message processing for testing assertions * * @returns {object} Object containing acked and nacked message counts and reset function * @returns {number} returns.acked - Number of successfully processed messages * @returns {number} returns.nacked - Number of failed message processing attempts * @returns {Function} returns.reset - Function to reset statistics counters * * @example * ```typescript * const stats = queue.check; * expect(stats.acked).toBe(5); * expect(stats.nacked).toBe(0); * * // Reset statistics * stats.reset(); * expect(stats.acked).toBe(0); * ``` */ get check(): { acked: number; nacked: number; /** * Resets the acked and nacked message counters to zero */ reset: () => void; }; /** * Stops the queue (not implemented in mock) * @throws {Error} Always throws as this is not implemented in the mock */ stop(): void; /** * Closes the queue (not implemented in mock) * @throws {Error} Always throws as this is not implemented in the mock */ close(): void; /** * Destroys the queue (not implemented in mock) * @throws {Error} Always throws as this is not implemented in the mock */ destroy(): void; /** * Adds a binding to connect this queue to an exchange * * @param {Binding} binding - The binding configuration to add */ addBinding(binding: Binding): void; get bindings(): Binding[]; /** * Removes a binding from this queue * * @param {Binding} binding - The binding configuration to remove */ removeBinding(binding: Binding): void; resetAll(): void; } export default Queue;